I have 2 tables, User and Employee. Each user is given a User_ID and that is a primary key in the User table and a foreign key in the Employee table. Can that attribute in the Employee table also be a primary key?
I have 2 tables, User and Employee . Each user is given a User_ID
Share
If you have a one-to-one relation between two tables, then the primary key of the details table is a foreign key as well.
If you have a m-to-n relation, the junction table has columns relating to the two primary keys of the m and the n-tables. These columns are primary keys and foreign keys at the same time.
Note that with this construction, a record of one table can only be linked to a specific record of the other table once, since each composite primary key of the junction table must be unique. If you want to allow non-unique pairings, define a separate primary key in the junction table:
In this case, the primary key and foreign key constraints are set on different columns. Alternatively you can also build the primary key with the two foreign keys plus one numerator or another discerning attribute.
In your case, if there is a one-to-one or a one-to-zero-or-one relationship between
UserandEmployee, then yes, theUser_IDin theEmployeetable can be Foreign Key (FK) and Primary Key (PK) at the same time. In words, this would mean: A user can be an employee as well, in which case the employee data would be attached to the user. If he is not an employee (he could be an external expert), no employee record is attached. IfUser_IDis FK and PK inEmployee, each user can have at most one employee record attached. IfUser_IDwas only FK but not PK in tableEmployeethen a user could have several related employee records.