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
Related
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.
As the title says I'm trying to drop constraing and add a new one
What I'm trying to do through phpmyadmin sql query and I get wrong syntax error
ALTER TABLE items
DROP CONSTRAINT items_ibfk_1
And here is the constraint that is on that table
ALTER TABLE `items`
ADD CONSTRAINT items_ibfk_1 FOREIGN KEY (Patch_No) REFERENCES spells (Patch_No) ON DELETE CASCADE;
I think this should do it:
ALTER TABLE items DROP FOREIGN KEY items_ibfk_1;
I am attempting to create a composite foreign key in MySQL however both fields are referencing the same column in another table. I am not sure if this is the accurate approach since the sql is not executing. Under is the SQL statement
SQL
ALTER TABLE tableA ADD CONSTRAINT `comp_fk`
FOREIGN KEY (`a_id` , `b_id` )
REFERENCES `tabelB` (`p_id` , `p_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Error
MySQL Database Error: Can't create table 'sep.#sql-984_8' (errno: 150)
Try this by individually applying constraint
ALTER TABLE `comp_fk`
ADD CONSTRAINT `test` FOREIGN KEY (`a_id`) REFERENCES `tabelB`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
ADD CONSTRAINT `test2` FOREIGN KEY (`b_id`) REFERENCES `tabelB`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
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
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;