Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a table have two foreign keys?

I have the following tables (Primary key in bold. Foreign key in Italic)

Customer table

  • ID---Name---Balance---Account_Name---Account_Type

Account Category table

  • Account_Type----Balance

Customer Detail table

  • Account_Name---First_Name----Last_Name---Address

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

  • Asset_ID-----Asset Name----Balance----Account Type

Receivable

  • Receivable_ID-----Receivable Name-------Address--------Tel-----Asset_ID----Account Type

Receivable Account

  • Transaction_ID----Description----Amount--- Balance----Receivable_ID----Asset_ID---Account Type

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?

like image 609
Rav Avatar asked Mar 14 '12 05:03

Rav


People also ask

Can I have 2 foreign keys to the same table?

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

How many foreign keys can be there in a table?

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.

Can a PRIMARY KEY have 2 foreign keys?

Yes, it is okay to have two fk to the same pk in one table.

How do you create a table with two 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

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) ) 
like image 115
user2915443 Avatar answered Oct 02 '22 16:10

user2915443