views:

117

answers:

5

I have 3 tables:

Employees T

emp_id | name | address

Department T

dep_id | name

Salaries T

emp_id | dep_id | month | year | salary

For each table, what are the primary keys and the foreign keys?

My answer:

Name of the table | PK | FK|

  • Employees: emp_id | dep_id
  • Department: dep_id || emp_id
  • Salaries: emp_id, dep_id | emp_id, dep_id

Is my answer correct?

+6  A: 

The Employees and Department tables have primary keys (which you got correct), but no foreign keys.

In Salaries, both emp_id and dep_id are foreign keys.

There's no single primary key on Salaries, although emp_id, dep_id, month and year could possibly be a composite key (as the combination of these 4 would always be unique, assuming an employee is only salaried once per month by a department :) ).

Ben James
And probably emp_id and dep_id both as primary key, in Salaries that is.
Henk
A primary key can be a composite of 2+ columns, so the primary key for `SALARIES` would be: emp_id, dep_id, month, year
OMG Ponies
It's homework. Any speculation about a composite primary key is probably out of scope.
Robert Harvey
Henk: emp_id and dep_id would not form a primary key together, because it would have to be repeated for each month of the year.
Ben James
Robert, he's l;ooking for the pk for that table 2, I don't think pointing out the only possible valid PK for the salary table is all the fields is out of scope.
HLGEM
A: 

Do you have the create script of the tables? With only this information, you can't do anything but guessing by the names of attributes...

mamendex
It's homework. The create script is probably not part of the question, otherwise the OP would have provided it.
Robert Harvey
+2  A: 

Not really. The foreign key and primary keys have nothing to do with names. You will need to use the DESCRIBE syntax as explained here to find what the keys are.

Vincent Ramdhanie
I'd give you 200 points if I could
HLGEM
It's homework. I doubt he can DESCRIBE these tables.
Robert Harvey
I noticed the homework tag late.
Vincent Ramdhanie
Disagree strongly with Robert, homework is the time to learn how to do things correctly and not make assumptions. This is by far the best answer.
HLGEM
+2  A: 

Based on the names I would make these assumptions:

Employees T

emp_id | name | address

PK: emp_id
FK: none

Department T

dep_id | name

PK: dep_id
FK: none

Salaries T

emp_id | dep_id | month | year | salary

PK: none
FK: emp_id, dep_id

Andrew Hare
That wouldn't work as most primary keys imply unique. As soon as the employee received a second paycheck it would fail to insert.
envalid
@envalid, it's homework; that's probably outside the scope of the question.
Robert Harvey
A primary key can be a composite of 2+ columns, so the primary key for SALARIES would be: emp_id, dep_id, month, year
OMG Ponies
A: 

It looks like two tables (Employees and Department) and linking (junction) Salaries table for implementing many-to-many relationship.

As stated in other answers, you need only primary keys for both Employees (PK emp_id) and Department (PK dep_id) tables, no foreign keys.

Table Salaries mast has foreign keys on emp_id, dep_id columns. For primary key you have two choices. Primary Key enforces uniqueness of the column on which they are defined. So, or no primary key at all, or compound primary key (emp_id, dep_id, month, year); the order of columns depends on you needs (select queries).

Irina C