I am a beginner of SQL Server 2008. there a colunm in table like StudentID
.
StudentID
will be the pk of table, it only could be integer and it will be a huge number.
My question is what type for column StudentID in SQL Server 2008 is best?
bigint
?
numeric(18,0)
? or others?
thanks a lot.
An identity column is a numeric column in a table that is automatically populated with an integer value each time a row is inserted. Identity columns are often defined as integer columns, but they can also be declared as a bigint, smallint, tinyint, or numeric or decimal as long as the scale is 0.
The type of data that is used to model student ID numbers is ordinal data.
Assigns a unique identifying number to any database object. The GUID data type is a 16 byte binary data type. This data type is used for the global identification of objects, programs, records, and so on.
you can use varchar as your data type for email column as emails are usually composed of letters, numbers and special characters.
You define a column of type INT
(or SMALLINT, TINYINT, BIGINT
) with the IDENTITY
attribute:
CREATE TABLE dbo.YourTable( ID INT IDENTITY(1,1) ......
With this setup, SQL Server will automatically generate consecutive ID's for your table when rows are being inserted into your table.
With a type INT
, starting at 1, you get over 2 billion possible rows - that should be more than sufficient for the vast majority of cases. With BIGINT
, you get roughly 922 quadrillion (922 with 15 zeros - 922'000 billions) - enough for you??
If you use an INT IDENTITY
starting at 1, and you insert a row every second, you need 66.5 years before you hit the 2 billion limit ... (so in my opinion, this is more than enough for the vast majority of cases!)
If you use a BIGINT IDENTITY
starting at 1, and you insert one thousand rows every second, you need a mind-boggling 292 million years before you hit the 922 quadrillion limit ....
INT
uses 4 bytes of storage, while BIGINT
uses 8 bytes. Especially if you deal with a large number of rows, and a number of non-clustered indexes, you want to keep this as small as possible - yet another reason why I typically pick INT
as my "ID" type (unless I have a very strong indication that INT
won't be enough...)
Read more about it (with all the options there are) in the MSDN Books Online.
Int
is the Best type for Id column in a table in SQL Server
If the table eventually has too many records we can change to BigInt
We can also create the Id as the Identity and/or PK of the table.
CREATE TABLE [dbo].[TableName](
[TableNameId] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]
Example of Identity & Primary Key
CREATE TABLE [dbo].[TableName3](
[TableName3Id] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_TableName3] PRIMARY KEY CLUSTERED
(
[TableName3Id] ASC
)
)
Hope this helps your question helped me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With