Reputation: 329
Suppose a field is in the table of int
data type and is declared a unique key and not null.
Can I use it as primary key?
Upvotes: 0
Views: 820
Reputation: 94859
Yes. This is all a primary key needs to be: unique and not nullable.
After all, "primary key" is just a name for such a column (or set of columns). Often there is just one such key. If there are two, you could make one primary and the other secondary, but beside that name there is nothing more to it. There is no actual difference between the two.
I doesn't make a difference whether you create a product table with a unique global trade item number thus:
create table product
(
gtin int not null,
id int not null,
name varchar(50) not null,
price decimal(10,2) not null,
constraint product_gtin primary key (gtin),
constraint product_id unique (id)
);
or thus:
create table product
(
gtin int not null,
id int not null,
name varchar(50) not null,
price decimal(10,2) not null,
constraint product_gtin unique (gtin),
constraint product_id primary key (id)
);
or thus:
create table product
(
gtin int not null,
id int not null,
name varchar(50) not null,
price decimal(10,2) not null,
constraint product_gtin unique (gtin),
constraint product_id unique (id)
);
Upvotes: 0
Reputation: 1269503
There are three criteria for a primary key:
Declaring a column as a primary key entails all three of these -- you don't need to separate declare a column unique
or not null
. You can use such a column as a primary key if no other column(s) are already declared as the primary key for the table.
Upvotes: 4