sql - How to use two columns in a foreign key constraint -
i have 2 tables:
article
subscription
in article
table have 2 columns make primary key: id
, sl
. in subscription
table have foreign key 'idsl`.
i use constraint :
constraint fk_idsl foreign key (idsl) references css_subscriptiongroup(id, sl)
but when run query, getting error:
number of referencing columns in foreign key differs number of referenced columns, table x
in article table have 2 fields primary key: id,sl. in subscription table have foreign key 'idsl`
this design broken - apparent composite primary key in article(id, sl)
has been mangled single compound foreign key in table subscription
. isn't idea.
instead, need change design of table subscription
include separate columns both id
, sl
, of same type article
table, , create composite foreign key consisting of both columns, referencing article
in same order primary key, e.g:
create table article ( id int not null, sl varchar(50) not null, -- other columns constraint pk_article primary key(id, sl) -- composite primary key ); create table subscription ( -- other columns id int not null, -- same type article.id sl varchar(50) not null, -- same type article.sl constraint fk_subscription_article foreign key(id, sl) references article(id, sl) -- same order article pk );
edit
one thing consider here convention column named table.id
or table.tableid
should unique, , primary key table. however, since table article
requires additional column sl
in primary key, implies id
isn't unique.
Comments
Post a Comment