I have a complex primary key, I wonder if what I write in the title is possible:
I have a look up table with 4 fields (they should compose the primary key of the table):
1) an INT column;
2) Another INT column;
3) A YEAR column;
4) another INT column which should be AUTO_INCREMENT.
The 1) and 2) fields are also FOREIGN KEYS of another table, and I need them to be so.
How can I handle this?
Best Answer
You can do this:
Adding the ID column to the unique index will guarantee that you can have the same values for C1, C2 and Year in your table. If you don't want that, remove ID from the ALTER TABLE statement.
The syntax of the FOREIGN KEY is for a multiple column key in another table called
othertable
.pkcol1
andpkcol2
would be the corresponding columns in that table.ID would be your fourth column in the question example code you presented.