I am trying to set a FK and I am getting an error
My datatypes are same as reference table.My Artists table contain all the names and I created another table for artist images.
Tables:
ALTER TABLE Artist_Images
ADD CONSTRAINT FK_Artist_Images_Artist
FOREIGN KEY (Artist)
REFERENCES Artists (Artist)
#1215 - Cannot add foreign key constraint
Is it because Artist isnt a PK in Artists table and can't be used as PK? Any other suggestions on how can I link the tables?
One of the most common reason for not being able to create a foreign key when data exists is that the column in the table you are trying to add the key to has values which do not exist in the referenced table.
In this case, you can check for that by running the following query:
SELECT i.Image, i.Artist FROM Artist_Images i
LEFT JOIN Artists a on i.Artist = a.Artist
WHERE a.Artist IS NULL
If you get any rows, you need to resolve that before you can create the foreign key.
Related
I have two tables in Mysql named customer_details and transaction table.
I have one column named account_no( primary key in customer_details) and want to relate with account_no field(foreign key in transaction table).
But it is showing error saying Foreign key relation could not be added.
I tried creating index in both columns but still its not working.
ALTER TABLE transaction
ADD INDEX FK_Transaction_AccountNo_idx (AccountNo ASC);
ALTER TABLE transaction
ADD CONSTRAINT FK_Transaction_AccountNo
FOREIGN KEY (AccountNo)
REFERENCES customer_details (AccountNo)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
collation of both table and column should be equal
I want to add foreign key to an existing table questions which has 1000 above questions of different categories in it. But when I am trying to add foreign key mysql throws error as below.
#1452 - Cannot add or update a child row: a foreign key constraint fails (question_bank.#sql-af8_1d0, CONSTRAINTcat_idFOREIGN KEY (cat_id) REFERENCESdb_category(cat_id) ON DELETE NO ACTION ON UPDATE NO ACTION)
category table db_category structure.
questions table db_questions structure.
Before adding a foreign key you should ensure it fits with the actual data.
The error is simple, you have some rows on questions table with wrongs value on cat_id
Just delete that rows, or create new categories. You can check the problematic rows with:
SELECT * FROM questions WHERE cat_id NOT IN (SELECT cat_id from category)
You cant add a foreign key to that table because you must check first if the primary key table has a record exists value same with foreign key table.
according to primary column value and foreign key column value.
I have a MySQL database with an existing (and not removable) cities table with a new reference to a table province by a field province_id.
When I run:
ALTER TABLE cities ADD province_id INTEGER NOT NULL;
ALTER TABLE cities ADD INDEX (province_id),
ADD FOREIGN KEY (province_id) REFERENCES provinces (id);
I got next error on the second query, adding the index and foreign key:
ALTER TABLE cities ADD INDEX (province_id), ADD FOREIGN KEY (province_id) REFERENCES provinces (id) Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`yanpytest`.`#sql-16d8_325`, CONSTRAINT `#sql-16d8_325_ibfk_3` FOREIGN KEY (`province_id`) REFERENCES `provinces` (`id`))
I donĀ“t understand why, and this is not working in my production environment, in development it worked (both MySQL).
This probably means that in the province_id column in the cities table you have some value(s) e.g. 123 which does not exist in the provinces table in the id column there. So the foreign key (FK) constraint cannot be enforced by MySQL and it fails on you.
You can find the violating records by running:
SELECT * FROM `cities` WHERE `province_id` NOT IN
(SELECT id FROM `provinces`)
I cannot add a foreign key to my exists table. I want to add a foreign key publisher_id from table games references to publishers (publisher_id).
But when running the below sql, it says:
Source and target columns must have the same data type, there must be an index on the target columns and referenced data must exist.
Cannot add or update a child row: a foreign key constraint fails (vngarena.com.#sql-818_1b1, CONSTRAINT #sql-818_1b1_ibfk_2 FOREIGN KEY (publisher_id) REFERENCES publishers (publisher_id) ON DELETE CASCADE ON UPDATE CASCADE
The sql is:
ALTER TABLE games
ADD FOREIGN KEY (publisher_id) REFERENCES publishers (publisher_id) ON DELETE CASCADE ON UPDATE CASCADE;
And here is the diagram of my db:
More info:
- Table games has two records
- Table games has other foreign key which references to table gamecats (gamecat_id)
- Table gamecats has two records
- Table publishers has no record
If there is any existing data that violates the relationship, you will need to clean it up before adding the key.
[Edited]
After your edit: The publisher_id in Table games, is looking for a match in the publishers table. Add a publisher with an id that matches what's in the games table.
You need to delete the games table records before adding foreign key.
If you add foreign key,it should match with existing data. In your condition the publishers table has no records . so it will fails foreign key if you add data with records in games table
or
You should add two records in publishers table with same keys exists in games table.
For more detail, refer below link
Foreign Key
I am using MySQL.
I have an existing table named school with hundreds of rows data be populated. Now I have another table named student, its primary key is "sid".
I would like to alter my school table to have a foreign key reference to a student.
I tried the following sql statement:
ALTER TABLE school ADD FOREIGN KEY (sid) REFERENCES student(sid);
But I get error:
ERROR 1072 (42000): Key column 'sid' doesn't exist in table
What is the correct way to alter table to add a foreign key to another table?
You have to add the column sid on your table first.
ALTER TABLE school ADD COLUMN sid [INT, VARCHAR2(10];
ALTER TABLE school ADD FOREIGN KEY (sid) REFERENCES student(sid);
PS: I put [INT, VARCHAR2(10] because I don't know what type student(sid) is. Just change to the correct one.
where do you want to link your foreign key?
it seems that you missed the key to link against in the school table:
As far as i can imagine you want to link a student to his school.
So what i'd do is to add a column to the student table:
ALTER TABLE STUDENT
ADD COLUMN SCHOOL_ID INT NOT NULL;
then i'd create the foreign key in STUDENT table to point to SCHOOL
ALTER TABLE STUDENT
ADD FOREIGN KEY (F_SCHOOL_ID) REFERENCES SCHOOL(ID);
This is the best way and not the other way round.