Cannot alter a column and make it a foreign key - mysql

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)

Related

How to drop Primary key thats needed in a foreign key constraint in MySQL?

I want to drop the primary key in father. So I set the foreign key in child to delete cascade. Why this doesnt work ? Do I need to delete the foreign key first ? If so, then what the propurse of the delete on cascade statement ?
create database table_test;
use table_test;
create table father(
cod int,
primary key (cod)
);
create table child(
cod int,
cod_father int,
primary key(cod),
constraint fk_cod_father foreign key (cod_father) references father(cod)
on delete cascade
);
alter table father
drop primary key;
ON DELETE CASCADE constraint is used in MySQL to delete the rows from
the child table automatically, when the rows from the parent table are
deleted
You want to remove primary key and in your case first you need to unlink the foreign key reference. So drop foreign key first.

Reference to one foreign key from multiple tables

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);

Mysql Issue Related to foreign key and on delete cascade

I have 4 sql tables.
create table general(regno int NOT NULL primary key);
create table company_information(cregno int NOT NULL primary key);
create table company_jobs (jcode int NOT NULL primary key, cregno int , foreign key(cregno) references company_information(cregno));
create table applied(cregno int ,jcode int, regno int, foreign key(regno) references general(regno), foreign key(jcode) references company_jobs(jcode));
All i need to do is delete from table company_jobs when table applied has some value. Actually, all the tables must have some value for table applied to have some value as you can see from the structure of tables.
I used these commands to add ON DELETE CASCADE Constraint:
alter table company_jobs add constraint fk_cregno13 foreign key(cregno) references company_information (cregno) on delete cascade;
alter table applied add constraint fk_jcode16 foreign key(jcode) references company_jobs(jcode) on delete cascade;
alter table applied add constraint fk_regno14 foreign key(regno) references general(regno) on delete cascade;
But it is not working unfortunately and i am getting this error when i am giving the following command.
mysql> delete from company_jobs;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign
key constrai nt fails (test.applied, CONSTRAINT applied_ibfk_2
FOREIGN KEY (jcode) RE FERENCES company_jobs (jcode))
Help me out please if anyone can. Thank you
When you created the applied table you created 2 foreign keys.
After that, you have added 2 other foreign keys to this table.
As you can see, the error references a foreign key named applied_ibfk_2 that's not the foreign key you added after the creation.
Thus, ad the moment you have 4 foreign key constraint on that table.
So, you have to drop the 2 foreign keys created on table creation (that have a predefined name) and everything will work
The 1st foreign key pointing from table applied to company_job doesn't have any cascade rule, so it simple blocks the delete from company_job;
See mysql dump bellow
ALTER TABLE `applied`
ADD CONSTRAINT `applied_ibfk_1` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`),
ADD CONSTRAINT `applied_ibfk_2` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`),
ADD CONSTRAINT `fk_jcode16` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`) ON DELETE CASCADE,
ADD CONSTRAINT `fk_regno14` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`) ON DELETE CASCADE;
You need to drop the foreign key first, or recreate the table without the 1st foreign key
ALTER TABLE 'applied' DROP CONTRAINT 'applied_ibfk_2';

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.

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`);