表外键问题

来源:百度知道 编辑:UC知道 时间:2024/05/21 17:23:51
两张表
test1
id name
test2
id name
两张表中的内容没有重复的
可以建张表,
test3
id name
id 外键test1,test2.
test1和test2中只要有一个存在就建立这个id
有这个语句吗?怎么写的?
alter table test3 add constraint fk_id foreign key(id)
references test1(id);
alter table test3 add constraint fk_id1 foreign key(id)
references test2(id);
但是这个id是这两张表中都存在的...

外键不能这样用,不过如果你只是为了把两张表的结果放到一个结果集中,那你直接建一张视图,把两张表的结果联立就可以达到你要的效果了

CREATE A VIEW
( SELECT id,name FROM TEST1
UNION
SELECT id,name FROM TEST2)

外键references貌似不可以跟多个表。