Reference to one foreign key from multiple tables - mysql

I'm trying to connect one foreign key from multiple table's primary keys. Like this :
enter image description here
I've tried :
ALTER TABLE Table1 ADD CONSTRAINT entity FOREIGN KEY (entity_id)
REFERENCES MainTable(entitiy_id);
or
ALTER TABLE MainTable
ADD FOREIGN KEY (entity_id) REFERENCES Table1(id),Table2(id),Table3(id);
What should I do?
Thanks.

it is not really clear what you want to achieve here.
The foreign key should go from a non-key column of the "child" table to the key column of the referenced table.
In your diagram, I would expect each one of Table, Table2 and Table3 to have a column "parent_id", and in every one of them you add a foreign key, which goes from the "parent_id" column to the key of the MainTable.
That said, the code is
ALTER TABLE Table1
ADD COLUMN parent_id INT NULL;
ALTER TABLE Table1 ADD CONSTRAINT fk_table1 FOREIGN KEY (parent_id)
REFERENCES MainTable(uniqueId);

Related

Why is Primary Key's position relevant when creating a composite Foreign Key in MySql5.6?

I'm using MySQL 5.6 and I've read the MySql Reference guide regarding this but no where is it mentioned that the PK should be at the end of the list while creating a composite Foreign Key.
The only requirement in the guide that talks about columns is the following -
"In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order."
If so, then why doesn't the following work?
alter table table_1
add constraint "fk_key_1" FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2" ("pk_col", "col_2") ON DELETE NO ACTION;
But this works -
alter table table_1
add constraint "fk_key_1" FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2" ("col_2", "pk_col") ON DELETE NO ACTION;
The foreign key has to match the primary key (or some key, but preferably the primary one) -- both the types and order.
If this works:
FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2 ("col_2", "id_col") ON DELETE NO ACTION;
It is because the primary key on table_2 is defined as (col_2, id_col) rather than (id_col, col_2).

Cannot alter a column and make it a foreign key

I have two tables, employee and tbl_feedback. What I wanted to do is to add id as a foreign key in tbl_feedback. I already have an id column on my tbl_feedback so I tried altering it using this:
ALTER TABLE tbl_feedback
ADD FOREIGN KEY (id) REFERENCES employee(id);
When I hit go, I only get "Cannot add foreign key constraint". Am I doing it wrong?
tbl_feedback
employee
Reference table must have a PK and that PK fields should not be NULL
So, first modify referenced table if required
For example,
ALTER TABLE TblReference
Alter column refid int NOT NULL
ALTER TABLE TblReference
ADD constraint PK_TblReference_RefId primary key (refid)
Then simply add the Foreign Key constraint
ALTER TABLE TblSource
ADD CONSTRAINT FK_TblSource_Id FOREIGN KEY (id)
REFERENCES TblReference (refid)

Putting FOREIGN and UNIQUE at same column

I have a problem with MySQL. I try put a column like a Unique KEY and FOREIGN KEY, then when I have a column like Unique and after I run this line in mysql:
ALTER TABLE table
ADD FOREIGN KEY(id)
REFERENCES main_table(id)
nothing happens, and if I try alter the table to Unique when the column is FOREIGN KEY, then the FOREIGN KEY disappears.

use part of composite key as foreign key

I have a table with a composite key from 3 different tables.
What i want to do is to add a reference as foreignkey from all the 3 tables.
The problem is that 2 of the tables uses composite key. Is it possible to use just a part of the compositekey as a foreign key in another table?
So the table i want to add the foreign key to is ProductCategory and i want to add it to the property id and take that from id of the table Product.
when i do this
ALTER TABLE [AAES_PAM_DEV].[dbo].[ProductCategory]
ADD FOREIGN KEY (id)
REFERENCES [AAES_PAM_DEV].[dbo].[Product](id)
i get this error:
There are no primary or candidate keys in the referenced table 'AAES_PAM_DEV.dbo.Product' that match the referencing column list in the foreign key 'FK__ProductCateg__id__5F492382'.

SQL Add foreign key to existing column

If I am using the following SQL command in SQL Server 2008 to update a table with a foreign key constraint:
ALTER TABLE Employees
ADD FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id)
UserID being my FK column in the Employees table. I'm trying to reference the UserID in my ActiveDirectories table. I receive this error:
Foreign key 'UserID' references invalid column 'UserID' in referencing
table 'Employees'.
Error indicates that there is no UserID column in your Employees table. Try adding the column first and then re-run the statement.
ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id);
Maybe you got your columns backwards??
ALTER TABLE Employees
ADD FOREIGN KEY (UserID) <-- this needs to be a column of the Employees table
REFERENCES ActiveDirectories(id) <-- this needs to be a column of the ActiveDirectories table
Could it be that the column is called ID in the Employees table, and UserID in the ActiveDirectories table?
Then your command should be:
ALTER TABLE Employees
ADD FOREIGN KEY (ID) <-- column in table "Employees"
REFERENCES ActiveDirectories(UserID) <-- column in table "ActiveDirectories"
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
In the future.
ALTER TABLE Employees
ADD UserID int;
ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id);
ALTER TABLE Faculty
WITH CHECK ADD CONSTRAINT FKFacultyBook
FOREIGN KEY FacId
REFERENCES Book Book_Id
ALTER TABLE Faculty
WITH CHECK ADD CONSTRAINT FKFacultyStudent
FOREIGN KEY FacId
REFERENCES Student StuId
way of foreign key creation correct for ActiveDirectories(id), i think the main mistake is you didn't mentioned primary key for id in ActiveDirectories table
If the table has already been created:
First do:
ALTER TABLE `table1_name` ADD UNIQUE( `column_name`);
Then:
ALTER TABLE `table1_name` ADD FOREIGN KEY (`column_name`) REFERENCES `table2_name`(`column_name`);