IDENTITY
Identity columns are numbered automatically. If you set a column as an
IDENTITY column, SQL Server will generate numbers automatically for that
column as you add new rows to it. The first number in the column is called
the identity seed. To generate subsequent numbers, the identity column
adds a given value to the seed; the value that’s added is called the identity
increment. By default, both the seed and increment have a value of 1, in
which case the generated values are 1, 2, 3, and so on. If the identity seed
were 5 and the identity increment were 10, the generated numbers would be
5, 15, 25, and so on.
IDENTITY is useful for ID columns, such as Department ID, for which you
don’t care what the values are, as long as they’re unique. When you use
IDENTITY, the generated values will always be unique. By default, you can’t
specify values for an IDENTITY column. Note also that the column can never
contain NULL.
Understanding NULL
Be sure not to see NULL as equivalent to 0 (in numerical columns), or an
empty string (in the case of string columns). Both 0 and an empty string are
values; NULL defines the lack of a value.
NULL and Default Values
I’ve often heard people say that when we set a default value for a column,
it doesn’t matter whether or not we set it to accept NULLs. Many people
seem to believe that columns with default values won’t store NULL.
That’s incorrect. You can modify a record after it was created, and change
any field that will allow it to NULL. Your columns’ ability to store NULL is
important for the integrity of your data, and it should reflect the purpose of
that data. A default value does make things easier when we create new rows,
but it’s not as vital as is correctly allowing (or disallowing) NULL in columns.
Primary Keys
Primary keys are the last fundamental concept that you need to understand before
you can create your first data table. In the world of relational databases, each
row in a table must be identified uniquely by a column called a key, on which all
database operations are based.
265
Primary Keys