SQL identity column with unique code column
-
04-10-2020 - |
Question
This seems a simple question but is it still necessary to include a primary key with identity even if my columns are only a unique code and its description?
CREATE TABLE table1 (
column1 INT NOT NULL IDENTITY,
column2 CHAR(4) NOT NULL,
column3 VARCHAR(10) NULL
CONSTRAINT pk_id PRIMARY KEY (column1),
CONSTRAINT uq_column2 UNIQUE (column2)
)
Sample values:
INSERT INTO table1 VALUES ('A', 'aaa')
INSERT INTO table1 VALUES ('B', 'bbb')
Solution
If your column2
is immutable then it can serve as primary key. Only if it was to hold long values then you could consider to create a separate primary key field to save space in the tables that have a foreign key constraint with this table.
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange