Clarification about which datatype to use
I'm using SQL Server 2008 and Visual Studio 2008 with .NET Framework 3.5. I'm teaching myself and this my first time posting a question here. And I was wondering if someone could clarify something for me.
I've created a table called Classes
. One of the columns is called Enrolled
and is of data type tinyint
(0 to 255) since the class will never have more than 50 students enrolled.
In my application I created an object called ClassInfo
and declared a private variable ..
private _classAmt as byte
My question is this..
What if someone wants a total of students enrolled for that year? Do I need to convert to a larger data type such as int32 or would it be better to set the data type as Integer in the table to begin?
Thanks for your ad开发者_JAVA百科vice.
Using tinyint as the database column data type is fine. It's where the result of the Sum operation is stored that would matter. If the Sum were stored in a variable in code then that variable needs to be big enough to hold the Sum.
For example if you did an SQL of "select SUM(Enrolled) from Classes" then SQL server converts the result to a size big enough to hold the Sum. then to put that result in a variable you are probably going to need something bigger than can hold 0-255 so you would use an int32, int64,etc. type in your code.
精彩评论