Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple foreign keys in same table

Tags:

mysql

Can I reference 3 foreign keys in MYSQL? because in INNODB, only one foreign key constraint appears in the relational view.

like image 465
James Avatar asked Mar 29 '13 07:03

James


People also ask

Can you have multiple foreign keys to the same table?

A table can have multiple foreign keys based on the requirement.

How many foreign keys can one table have?

A table with a foreign key reference to itself is still limited to 253 foreign key references. Greater than 253 foreign key references are not currently available for columnstore indexes, memory-optimized tables, Stretch Database, or partitioned foreign key tables. Stretch Database is deprecated in SQL Server 2022 (16.

Can database have 2 foreign keys?

It is possible to have more than one foreign key in a table, and they can accept a null value. Foreign key values do not need to be unique; duplicate values can be stored in foreign key columns. Foreign keys do have to link back to columns with unique values. Those columns are frequently primary keys.

How do I insert multiple foreign keys?

To insert records into tables with multiple foreign keys, you should first create corresponding records in the tables that are referenced by foreign keys in the original tables. In practice, to insert records into the Employee table, we must first create corresponding records in the Department and Insurance tables.


1 Answers

Yes you can do,

EXAMPLE:

CREATE TABLE table
(
user_id int,
track_id int,
primary key (user_id, track_id),
foreign key (user_id) references table1(table1Column),
foreign key(track_id) references table2(table2Column)
)

Composite Primary Key;

FOREIGN KEY ('column1','column2','column3') 
REFERENCES table1('column1','column2','column3') ;
like image 65
PSR Avatar answered Oct 13 '22 10:10

PSR