mysql - SQL. Unique and Primary Key -


my requirement having table of products cannot have similar vendor, product , version @ same time.

but, want table referenced on table primary key, distinct vendor, product version use unique.

create table products     (     vendor varchar(100),     product varchar(100),     version varchar(30),     unique (vendor, product, version, cve)         ); 

but way cannot reference product_id, want in table:

create table product_cve(      product_id int,     cve varchar(14),     foreign key (product_id) references products(product_id),     foreign key (cve) references vulnerabilitiescve(cve) ); 

, thing can that

   create table products     (     product_id int not null auto_increment,     vendor varchar(100),     product varchar(100),     version varchar(30),     primary key (product_id)      ); 

this way, going end having duplicated products...

the solution simple:

create table products     (     product_id int auto_increment     vendor varchar(100),     product varchar(100),     version varchar(30),     unique(vendor, product, version, cve),     primary key (product_id)         ); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -