0

How can I do the composite key in the database?

I want to creat table that include just two culmon and both are foreign key.. which that two will be primary key.. how can I do that in mysql?

25th Apr 2020, 11:49 AM
šŸ‘©šŸ»__šŸ‡“šŸ‡²
šŸ‘©šŸ»__šŸ‡“šŸ‡² - avatar
4 Answers
+ 1
Just set both of them as primary key. It automatically become a composite key. So if you have folkowing values column1 | column2 1 | 6 1 | 3 There will be no error
25th Apr 2020, 12:05 PM
Ore
Ore - avatar
+ 1
Thank u a lot
25th Apr 2020, 4:48 PM
šŸ‘©šŸ»__šŸ‡“šŸ‡²
šŸ‘©šŸ»__šŸ‡“šŸ‡² - avatar
0
So if I use this query ALTERĀ TABLEĀ Orders ADDĀ FOREIGNĀ KEYĀ (PersonID)Ā REFERENCESĀ Persons(PersonID); For both column it will be correct yes?
25th Apr 2020, 2:00 PM
šŸ‘©šŸ»__šŸ‡“šŸ‡²
šŸ‘©šŸ»__šŸ‡“šŸ‡² - avatar
0
šŸ‘©šŸ»__šŸ‡“šŸ‡² You have to set the ON UPDATE constraint to CASCADE ALTER TABLE Orders ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID) ON UPDATE CASCADE
25th Apr 2020, 4:37 PM
Ore
Ore - avatar