Single attribute used as a foriegn key for multiple tables

Single attribute used as a foriegn key for multiple tables



I want to make Emp_id as a foreign key for Employee table and Record table. Can this be done?



This is my code


CREATE TABLE Empolyee(
EID varchar(8) NOT NULL,
E_name varchar(30) NOT NULL,
NID varchar(30) NOT NULL,
sex varchar(40) NOT NULL,
history varchar(30) NOT NULL,
salary varchar(10) NOT NULL,
cid int NOT NULL,
FOREIGN KEY (cid) REFERENCES Employee_Contact_No(CID),
FOREIGN KEY (cid) REFERENCES Employee_email(Email_ID),
PRIMARY KEY (EID)
);



Receptionist table


CREATE TABLE Receptionist(
r_id varchar(8) NOT NULL,
Emp_ID varchar(8) NOT NULL,
PRIMARY KEY (r_id),
FOREIGN KEY (Emp_ID) REFERENCES Employee(EID),
FOREIGN KEY (Emp_ID) REFERENCES Record(EID)
);



And the receptionist's records


CREATE TABLE Record(
record_no varchar(8) NOT NULL,
EID VARCHAR(8) Not Null,
patient_id varchar(15) NOT NULL,
discription varchar(30) NOT NULL,
appointment varchar(40) NOT NULL,
PRIMARY KEY (record_no)
);






You really should refer to things like "employee ID" with a consistent name like EID or Emp_ID but never both. You have several completely contradictory naming conventions here: All-caps, underscored with caps, underscored without caps. Consistency is important.

– tadman
Sep 14 '18 at 16:49



EID


Emp_ID




1 Answer
1



You can make a single field reference multiple tables, but I cannot think of a scenario where it would ever be a good idea; it would require both tables to have the same id value, and (at least imply) that those id values be meaningfully coordinated.



From the looks of your examples, it is probably more likely that you need tables like Employee_Contact_No and Employee_email to reference the Employee table.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)