Can anyone please tell me whether the instruction IDENTITY NOT NULL
at a table creation is redundant or not? I mean, judging by the message
DEFAULT or NULL are not allowed as explicit identity values.
I would say that any column declared as IDENTITY
is implicitly also declared as NOT NULL
, but I would like to make sure. Can anyone please confirm?
Thank you very much.
The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.
Only one identity column can be created per table.
Additionally an identity column's definitions must not allow null values. One possible drawback of using an identity column is that only one identity column per table can be used.
It is possible to add a NOT NULL constraint to an existing table by using the ALTER TABLE statement. In this case, the column_name must not contain any NULL value before applying the NOT NULL constraint.
SQL Server adds NOT NULL constraint to identity columns automatically eventhough he did not speficy it when creating a table
Consider the following table script
create table test(id int identity(1,1), name varchar(1000))
Now Generate the script of the table from Management Studio. It generates the script as
CREATE TABLE [dbo].[test](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](1000) NULL
) ON [PRIMARY]
Eventhough NOT NULL constraint is not specified in the table script by default it is added. The identity column will never be NULL. So NOT NULL constraint is added default
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