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;
Related
I haven't practised SQL in a while and I forgot how to add a foreign key to my table
mysql> alter table students
add foreign key fk_unit(unitid)
references unit(unitid)
on delete no action
on update cascade;
ERROR 1072 (42000): Key column 'unitid' doesn't exist in table
I'm wondering why this is the case? My unit table has a primary key called unitid, why does this keep happening?
Try this one it should work....
ALTER TABLE students
ADD CONSTRAINT FK_UnitId FOREIGN KEY (unitid)
REFERENCES unit(unitid);
Try this
ALTER TABLE Students
ADD FOREIGN KEY (unitid)
REFERENCES unit(unitid)
Your query is correct. Looks like field 'unitid' is missing from 'students' table or it has a different name.
On PHPMyAdmin, I would like to DROP a FOREIGN KEY with
ALTER TABLE information DROP FOREIGN KEY IDX_29791883B30676A7
Because when I do the next query it's not working:
ALTER TABLE information DROP INDEX IDX_29791883B30676A7
Cannot drop index 'IDX_29791883B30676A7': needed in a foreign key constraint
However, the second query as the error that the index is use as foreigner key.
Fine, but when I do the first query I get this error:
Can't DROP 'IDX_29791883B30676A7'; check that column/key exists
So the questions are:
How do I acually check that,
How can I finally drop that key.
Foreign keys by default are prefixed with 'FK' not 'IDX'. You are trying to remove an index instead of a foreign key. You mentioned that the foreign key is: FK_29791883B30676A7 so the correct way to remove it will be:
ALTER TABLE information DROP FOREIGN KEY FK_29791883B30676A7
Once a foreign key has been created, you may find that you wish to drop the foreign key from the table. You can do this with the ALTER TABLE statement in SQL Server
I have the same problem with foreign keys, well I found a solution to removing constraint. For first take a look at:
SHOW CREATE TABLE information;
You will found the name of the rule, and then just drop it:
ALTER TABLE information DROP CONSTRAINT `name_of_rule`;
Try this
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE information DROP FOREIGN KEY IDX_29791883B30676A7;
ALTER TABLE information DROP INDEX IDX_29791883B30676A7;
SET FOREIGN_KEY_CHECKS=1;
I am trying to drop a foriegn key in php admin (mysql) so I am performing this code below:
`ALTER TABLE Image_Question DROP INDEX FK_QuestionSession`
Problem is though that I am receiving this error:
#1553 - Cannot drop index 'FK_QuestionSession': needed in a foreign key constraint
The foreign key for QuestionId is linked from the Image_Question Table to the QuestionId in the Question Table.
Thanks
Remove foreign key constrain first and then drop index. Otherwise you will always get error.
alter table Image_Question drop foreign key key_name_here
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
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