I have noticed a quirk in the primary key creation of tables.
results in the following error message
This seems bizarre, as by definition, a primary key is not null and unique. If however I put the not null constraint in the table whilst creating it, as shown below, the command completes successfully.
I surmise that SQL server is making different entries in the data dictionary, and thereby distinguishing between not null applied when a table is made, and not null added afterwards as a user constraint.
Can anyone explain this peculiar behaviour?
Regards
T
Code:
DROP TABLE PK_TEST
CREATE TABLE PK_TEST
(DUMMY_COLUMN VARCHAR(100))
ALTER TABLE PK_TEST
ADD CONSTRAINT NN_PK_TEST_DUMMY_COLUMN
CHECK (DUMMY_COLUMN IS NOT NULL)
ALTER TABLE PK_TEST
ADD CONSTRAINT PK_PK_TEST
PRIMARY KEY(DUMMY_COLUMN)
results in the following error message
Code:
[red]Msg 8111, Level 16, State 1, Line 8
Cannot define PRIMARY KEY constraint on nullable column in table 'PK_TEST'.
Msg 1750, Level 16, State 0, Line 8
Could not create constraint. See previous errors.[/red]
This seems bizarre, as by definition, a primary key is not null and unique. If however I put the not null constraint in the table whilst creating it, as shown below, the command completes successfully.
Code:
DROP TABLE PK_TEST
CREATE TABLE PK_TEST
(DUMMY_COLUMN VARCHAR(100) NOT NULL)
--ALTER TABLE PK_TEST
--ADD CONSTRAINT NN_PK_TEST_DUMMY_COLUMN
--CHECK (DUMMY_COLUMN IS NOT NULL)
ALTER TABLE PK_TEST
ADD CONSTRAINT PK_PK_TEST
PRIMARY KEY(DUMMY_COLUMN)
I surmise that SQL server is making different entries in the data dictionary, and thereby distinguishing between not null applied when a table is made, and not null added afterwards as a user constraint.
Can anyone explain this peculiar behaviour?
Regards
T