I have the following tables (Primary key in bold. Foreign key in Italic)
Can I have two foreign keys in the Customer table and how can I implement this in MySQL?
Updated
I am developing a web based accounting system for a final project.
Account Category
Account Type--------------Balance
Assets
Liabilities
Equity
Expenses
Income
Asset
Receivable
Receivable Account
I drew the ER(Entity relationship) diagram using a software and when I specify the relationship it automatically added the multiple foreign keys as shown above. Is the design not sound enough?
A table can have multiple foreign keys based on the requirement.
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.
Yes, it is okay to have two fk to the same pk in one table.
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.
create table Table1 ( id varchar(2), name varchar(2), PRIMARY KEY (id) ) Create table Table1_Addr ( addid varchar(2), Address varchar(2), PRIMARY KEY (addid) ) Create table Table1_sal ( salid varchar(2),`enter code here` addid varchar(2), id varchar(2), PRIMARY KEY (salid), index(addid), index(id), FOREIGN KEY (addid) REFERENCES Table1_Addr(addid), FOREIGN KEY (id) REFERENCES Table1(id) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With