我有三张桌子,顾问,技巧和顾问_技巧。我正在寻找顾问1的所有技能。
我怎样才能在选择和在哪里创建三个关系?
谢谢
SQL - ORACLE DATABASE
CREATE TABLE consultant
(c_id NUMBER(6),
c_last VARCHAR2(20),
c_first VARCHAR2(20),
c_mi CHAR(1),
c_add VARCHAR2(30),
c_city VARCHAR2(20),
c_state CHAR(2),
c_zip VARCHAR2(10),
c_phone VARCHAR2(15),
c_email VARCHAR2(30),
CONSTRAINT consultant_c_id_pk PRIMARY KEY (c_id));
CREATE TABLE skill
(skill_id NUMBER(3),
skill_description VARCHAR2(50),
CONSTRAINT skill_skill_id_pk PRIMARY KEY (skill_id));
CREATE TABLE consultant_skill
(c_id NUMBER(6),
skill_id NUMBER(3),
certification VARCHAR2(8),
CONSTRAINT consultant_skill_pk PRIMARY KEY (c_id, skill_id),
CONSTRAINT consultant_skill_c_id_fk FOREIGN KEY (c_id) REFERENCES consultant(c_id),
CONSTRAINT consultant_skill_skill_id_fk FOREIGN KEY (skill_id) REFERENCES skill(skill_id));
答案 0 :(得分:0)
通常的方法是使用PK来对抗FK,如下所示:
select
*
from
consultant
join
consultant_skill
on consultant.c_id = consultant_skill.c_id
join
skill
on consultant_skill.skill_id = skill.skill_id
根据经验,检查n-1
表是否有n
个连接以避免笛卡尔连接。