MySQL InnoDB foreign key between different databases - mysql

I would like to know if it's possible in InnoDB in MySQL to have a table with foreign key that references another table in a different database ?
And if so, how this can be done ?

I do not see any limitation on https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html.
So just use otherdb.othertable and you will be good.

It's possible : Link to do it
Example (Table1 is in database1 and HelloTable is in database2) :
ALTER TABLE Table1
ADD foreign key FK_table1(ColumnNameFromTable1)
REFERENCES db2.HelloTable(ColumnNameFromHelloTable)

Below is how to add a foreign key on table t2, reference from table db1.historial(codh):
alter table t2
add foreign key FK_t2(micod2)
references db1.historial(codh)
on delete cascade
on update cascade;

ALTER TABLE `tablename1`
ADD CONSTRAINT `tablename1_student_id_foreign`
FOREIGN KEY (`tablename1`.`id`)
REFERENCES `db2`.`tablename2`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE;
if we have table calling answers in db1 and student in db2 calling ramiyusu_offline
we must type as below
ALTER TABLE `answers`
ADD CONSTRAINT `answers_student_id_foreign`
FOREIGN KEY (`id`)
REFERENCES `ramiyusu_offline`.`student`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE;

Related

How to fix error in MySQL : 1452 - Cannot add or update a child row: a foreign key constraint fails

I run sql query in Navicat, so got error;
Query:
ALTER TABLE `customer_eav_attribute`
ADD CONSTRAINT `CUSTOMER_EAV_ATTRIBUTE_ATTRIBUTE_ID_EAV_ATTRIBUTE_ATTRIBUTE_ID`
FOREIGN KEY (`attribute_id`)
REFERENCES `eav_attribute` (`attribute_id`)
ON DELETE CASCADE;
Error:
1452 - Cannot add or update a child row: a foreign key constraint
fails (`caterin1_test`.`#sql-dd4_13`, CONSTRAINT
`CUSTOMER_EAV_ATTRIBUTE_ATTRIBUTE_ID_EAV_ATTRIBUTE_ATTRIBUTE_ID`
FOREIGN KEY (`attribute_id`)
REFERENCES `eav_attribute` (`attribute_id`)
ON DELETE CA)
How can I fix it?
I think there is no linked id in eav_attribute table and customer_eav_attribute table.
You must check eav_attribute table and customer_eav_attribute table (best way: plz delete eav_attribute and customer_eav_attribute and then insert data again).
You can find solution.
This is usually happening when you try to source file into existing database. Drop two tables(customer_eav_attribute, eav_attribute). Please create table again.
CREATE TABLE t1
(id INTEGER);
CREATE TABLE t2
(t1_id INTEGER,
CONSTRAINT FOREIGN KEY (t1_id) REFERENCES t1 (id));
And then set like this.
ALTER TABLE `customer_eav_attribute`
ADD CONSTRAINT `CUSTOMER_EAV_ATTRIBUTE_ATTRIBUTE_ID_EAV_ATTRIBUTE_ATTRIBUTE_ID`
FOREIGN KEY (`attribute_id`)
REFERENCES `eav_attribute` (`attribute_id`)
ON DELETE CASCADE;
The parent table must exist before you define a foreign key to reference it. you must define the tables in the right order: Parent table first, then the child table. If both tables references each other, you must create one table without FK constraints, then create the second table, then add the FK constraint to the first table with ALTER TABLE.

Can I alter a foreign key constraint in mysql?

I have a foreign key in 3 tables and they are on cascade update . so I would like to add another cascade delete ,. Is that possible without dropping the constraint ? if yes please give me any example with alter .
To change your foreign key, you first have to drop it (using the name) and then create a new foreign key with the correct definition and your done!
ALTER TABLE `pets` DROP FOREIGN KEY `your_fk_name_here`;
ALTER TABLE `pets` ADD FOREIGN KEY (`owner_id`) REFERENCES `owners`(`id`) ON UPDATE CASCADE ON DELETE CASCADE;
or
ALTER TABLE `pets` ADD CONSTRAINT fk_owner_pet FOREIGN KEY (`owner_id`) REFERENCES `owners`(`id`) ON UPDATE CASCADE ON DELETE CASCADE;

How to use foreign keys in SQL Buddy?

I need to use foreign keys for update and cascade, etc.
ALTER TABLE topics
ADD FOREIGN KEY(topic_by) REFERENCES users(user_id)
ON DELETE RESTRICT ON UPDATE CASCADE;
but I am not able to make foreign keys in SQL Buddy.
Any way to do that?
did you try this :
ALTER TABLE topics
ADD CONSTRAINT topic_by FOREIGN KEY(user)
REFERENCES users(user_id) ON DELETE RESTRICT ON UPDATE CASCADE
Try this query:
ALTER TABLE topics
ADD CONSTRAINT topic_by
FOREIGN KEY (user_id) REFERENCES users(user_id);

Change ondelete behavior on SQL table

I have previously done:
ALTER TABLE `cms__model__file_taggable_tag` ADD CONSTRAINT
cms__model__file_taggable_tag_id_cms__model__file_id FOREIGN KEY (id) REFERENCES
cms__model__file (id), ADD CONSTRAINT
cms__model__file_taggable_tag_tag_id_taggable_tag_id FOREIGN KEY (tag_id)
REFERENCES taggable_tag (id) ON DELETE CASCADE ON UPDATE CASCADE;
but I meant to execute (forgot to add on update cascade constraint):
ALTER TABLE `cms__model__file_taggable_tag` ADD CONSTRAINT
cms__model__file_taggable_tag_id_cms__model__file_id FOREIGN KEY (id) REFERENCES
cms__model__file (id) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT
cms__model__file_taggable_tag_tag_id_taggable_tag_id FOREIGN KEY (tag_id)
REFERENCES taggable_tag (id) ON DELETE CASCADE ON UPDATE CASCADE;
How can I remove the previous constraints and apply the correct one?
Tried:
ALTER TABLE `cms__model__file_taggable_tag` DROP CONSTRAINT
cms__model__file_taggable_tag_id_cms__model__file_id
got:
#1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'CONSTRAINT
cms__model__file_taggable_tag_id_cms__model__file_id' at line 1
Use SHOW CREATE TABLE tableName to check if MySQL has actually created the constraint and if it has changed (truncated) its name.
Then use DROP FOREIGN KEY, not DROP CONSTRAINT:
ALTER TABLE cms__model__file_taggable_tag
DROP FOREIGN KEY cms__model__file_taggable_tag_id_cms__model__file_id
Check the MySQL docs: ALTER TABLE and FOREIGN KEY Constraints
First,
ALTER TABLE tablename DROP CONSTRAINT constraintname
Then, put your corrected constraints back in.
Edit: Didn't notice you were using MySQL. I believe MySQL's syntax is
ALTER TABLE tablename DROP INDEX indexname
Replace the word ADD with DROP in your ALTER clause

how can i modify foreign key?

I'm wondering if it's possible to modify a Foreign Key?
FOREIGN KEY (member) REFERENCES scores (level) ON DELETE CASCADE,
And I would like to change it to:
FOREIGN KEY (member, subject) REFERENCES scores (level, subject) ON DELETE set null,
Is it possible?
You cannot modify the key in a single statement, see the ALTER TABLE syntax, in which there is no ALTER CONSTRAINT available.
You must use 2 ALTER TABLE statements to accomplish what you want.
Delete the key in the first one using an ALTER TABLE DROP FOREIGN KEY.
Re-create it with the new columns in the second, using an ALTER TABLE ADD CONSTRAINT FOREIGN KEY.
You can encapsulate both within a single transaction to make an atomic modification.
In MySql, you can accomplish that by following Tom Tresansky response, which will give us this syntax:
ALTER TABLE `table_name` DROP FOREIGN KEY `key_name`;
ALTER TABLE `table_name` ADD CONSTRAINT `constraint_name` FOREIGN KEY (`new_key_name`)
REFERENCES `other_table_name` ('other_table_id') ON UPDATE CASCADE ON DELETE CASCADE;
have you tried the alter table command?
http://www.w3schools.com/sql/sql_foreignkey.asp