I have had a 1 to many relationship between course and instructor, which I wanted to drop. When I tried to drop the instructorID in course table it told me that. I couldn't drop it as it was a foreign key. Then I decided to drop it like this:
ALTER TABLE course DROP FOREIGN KEY instructorID
But i get this error :
#1091 - Can't DROP 'InstructorID'; check that column/key exists
I don't get what this error means. what am i doing wrong?
Please run an SHOW CREATE TABLE course; to make sure instructorID is the name of foreign key constraint.
Additional:
The error means MySQL searches for a foreign key constraint named "InstructorID" but there is no constraint with such name, maybe this is your column name, but you have to use the constraint name to delete foreign keys.
After you run SHOW CREATE table course;
you should find the fk symbol which is commonly like the one bellow:
(course_ibfk_1)
it may differ according to your mysql version you are using then drop the foreign key using the fk symbol as follow :
alter table course drop foreign key course_ibfk_1;
You need to delete the 'foreign key constraint' and the 'key'.
Alter Table <table name> drop foreign key <constraint_name>
Alter table <table name> drop key <column name>
If any of you still not able to DROP the table. Try this. You can able to see all the details by running this
SELECT
TABLE_NAME,
COLUMN_NAME,
CONSTRAINT_NAME,
REFERENCED_TABLE_NAME,
REFERENCED_COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME = 'ReferenceTableName'; <-- change only this
If you want to see just the constrains
SELECT
CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME = 'ReferenceTableName';
To drop a FOREIGN KEY constraint:
MySQL:
ALTER TABLE Orders DROP FOREIGN KEY {Constraint/Key_name};
For SQL Server / Oracle / MS Access:
ALTER TABLE Orders DROP CONSTRAINT {Constraint/Key_name};
the reason why you cannot drop InstructorID is because you need to use the name of the constraint of the Foreign key . KevDev specified that you must run 'SHOW CREATE TABLE course' to find the constraint name. after doing so , you can delete the foreign key. BUT wait theres more, the 'key' still stays behind which must get deleted. You can run 'SHOW CREATE TABLE' course to check that the key is still behind. once checking that it is still there then perform what Bobby advised. 'Alter table drop key' in doing so you have fully removed the the foreign key
Can't DROP 'string'; check that column/key exists: ALTER TABLE accreditor_architectures DROP string error show in terminal when remove column from data base in ruby on rails
Related
I've tried to find a solution myself but haven't had any luck. I'm using MySQL version 8.0.14 and my problem is:
When trying to drop a foreign key...
alter table Employee drop foreign key fk_Employee_Contact1;
It fails...
Error Code: 1091. Can't DROP 'fk_Employee_Contact1'; check that column/key exists
I search for the constraint...
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME='Employee';
Which brings up (partial table)...
CONSTRAINT_NAME, TABLE_SCHEMA, TABLE_NAME, CONSTRAINT_TYPE
'fk_Employee_Contact1', 'RRAS Test Database', 'Employee', 'FOREIGN KEY'
But if I search...
SHOW CREATE TABLE Employee;
It shows it as a key...
KEY `fk_Employee_Contact1_idx` (`idContactDets`),
But not as a CONSTRAINT.
I have also tried...
alter table Employee drop foreign key fk_Employee_Contact1_idx;
Which fails with the same error code. And...
alter table Employee drop column idContactDets;
Which gives...
Error Code: 1828. Cannot drop column 'idContactDets': needed in a foreign key constraint 'fk_Employee_Contact1'
Is there any way to fix this? I'm very new to databases so please explain in simple terms if possible. Thank you. :)
I added a column in a table (query below)
ALTER TABLE `acct_doc_item_data`
ADD fk_job_id INT(11),
ADD FOREIGN KEY (fk_job_id) REFERENCES `job`(`job_id`);
Now I want to drop this column (fk_job_id). I tried these queries but they are giving error.
ALTER TABLE `acct_doc_item_data` DROP FOREIGN KEY `fk_job_id`;
ALTER TABLE `acct_doc_item_data` DROP COLUMN `fk_job_id`;
1st Alter statement gives error as - Can't DROP 'fk_job_id'; check that column/key exists (But the column exists).
2nd Alter statement gives error as - Cannot drop index 'fk_job_id': needed in a foreign key constraint
first drop the foreign key constraint then drop the column
eg:
alter table table_name drop constraint constraint_name
alter table table_name drop column column_name
to get constraint name use this
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE TABLE_NAME='YourTableName';
In my case(phpmyadmin and Mysql InnoDB) You cannot just do:
alter table table_name drop constraint constraint_name
becouse it gives "Acces denied" for root "to database 'information_schema' "
But I could go to Structure tab -> Relation view and from there you can drop desired fk
Related topic: How to remove constraints from my MySQL table?
I want to remove constraints from my table. My query is:
ALTER TABLE `tbl_magazine_issue`
DROP CONSTRAINT `FK_tbl_magazine_issue_mst_users`
But I got an error:
#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 FK_tbl_magazine_issue_mst_users' at line 1
Mysql has a special syntax for dropping foreign key constraints:
ALTER TABLE tbl_magazine_issue
DROP FOREIGN KEY FK_tbl_magazine_issue_mst_users
I had the same problem and I got to solve with this code:
ALTER TABLE `table_name` DROP FOREIGN KEY `id_name_fk`;
ALTER TABLE `table_name` DROP INDEX `id_name_fk`;
There is no such thing as DROP CONSTRAINT in MySQL. In your case you could use DROP FOREIGN KEY instead.
If the constraint is not a foreign key, eg. one added using 'UNIQUE CONSTRAINT (colA, colB)' then it is an index that can be dropped using ALTER TABLE ... DROP INDEX ...
To add a little to Robert Knight's answer, since the title of the post itself doesn't mention foreign keys (and since his doesn't have complete code samples and since SO's comment code blocks don't show as well as the answers' code blocks), I'll add this for unique constraints. Either of these work to drop the constraint:
ALTER TABLE `table_name` DROP KEY `uc_name`;
or
ALTER TABLE `table_name` DROP INDEX `uc_name`;
Also nice, you can temporarily disable all foreign key checks from a mysql database:
SET FOREIGN_KEY_CHECKS=0;
And to enable it again:
SET FOREIGN_KEY_CHECKS=1;
Some ORM's or frameworks use a different naming convention for foreign keys than the default FK_[parent table]_[referenced table]_[referencing field], because they can be altered.
Laravel for example uses [parent table]_[referencing field]_foreign as naming convention. You can show the names of the foreign keys by using this query, as shown here:
SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_SCHEMA = '<database>' AND REFERENCED_TABLE_NAME = '<table>';
Then remove the foreign key by running the before mentioned DROP FOREIGN KEY query and its proper name.
The simplest way to remove constraint is to use syntax ALTER TABLE tbl_name DROP CONSTRAINT symbol; introduced in MySQL 8.0.19:
As of MySQL 8.0.19, ALTER TABLE permits more general (and SQL standard) syntax for dropping and altering existing constraints of any type, where the constraint type is determined from the constraint name
ALTER TABLE tbl_magazine_issue DROP CONSTRAINT FK_tbl_magazine_issue_mst_users;
db<>fiddle demo
For those that come here using MariaDB:
Note that MariaDB allows DROP CONSTRAINT statements in general, for example for dropping check constraints:
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;
https://mariadb.com/kb/en/library/alter-table/
Go to structure view of the table
You will see 2 option at top a.Table structure b.Relation view.
Now click on Relation view , here you can drop your foreign key constraint. You will get all relation here.
In MySQL you have to specify what kind of constraint you want to remove:
Example:
CONSTRAINT `shop_ibfk_1` FOREIGN KEY (`fb_user_id`) REFERENCES `fb_user` (`id`),
CONSTRAINT `shop_chk_1` CHECK ((`import_lock` in (0,1)))
the first one you would remove with:
alter mytable shop drop FOREIGN KEY `shop_ibfk_1`;
the second one with
alter mytable drop CHECK `shop_chk_1`;
this will works on MySQL to drop constraints
alter table tablename drop primary key;
alter table tablename drop foreign key;
We're trying to rename a column in MySQL (5.1.31, InnoDB) that is a foreign key to another table.
At first, we tried to use Django-South, but came up against a known issue:
http://south.aeracode.org/ticket/243
OperationalError: (1025, "Error on rename of './xxx/#sql-bf_4d' to './xxx/cave_event' (errno: 150)")
AND
Error on rename of './xxx/#sql-bf_4b' to './xxx/cave_event' (errno: 150)
This error 150 definitely pertains to foreign key constraints. See e.g.
What does mysql error 1025 (HY000): Error on rename of './foo' (errorno: 150) mean?
http://www.xaprb.com/blog/2006/08/22/mysqls-error-1025-explained/
So, now we're trying to do the renaming in raw SQL. It looks like we're going to have to drop the foreign key first, then do the rename, and then add the foreign key back again. Does that sound right? Is there a better way, since this seems pretty confusing and cumbersome?
Any help would be much appreciated!
AFAIK, dropping the constraint, then rename, then add the constraint back is the only way. Backup first!
In case anyone is looking for the syntax it goes something like this:
alter table customer_account drop foreign key `FK3FEDF2CC1CD51BAF`;
alter table customer_account add constraint `FK3FEDF2CCD115CB1A` foreign key (campaign_id) REFERENCES campaign(id);
here is the SQL syntax for regular keys
ALTER TABLE `thetable`
DROP KEY `oldkey`,
ADD KEY `newkey` (`tablefield`);
Expanding on #Dewey's answer, here's a little script to rename FKs generated by Hibernate in a useful manner ("FK__" + table name + "__" + referenced table name).
SELECT CONCAT(
"alter table ", TABLE_NAME, " drop foreign key ", CONSTRAINT_NAME,";\n",
"alter table ", TABLE_NAME, " drop key ", CONSTRAINT_NAME, ";\n",
"alter table ", TABLE_NAME, " add key FK__", table_name, "__",
referenced_table_name, " (", column_name, ");\n",
"alter table ", TABLE_NAME, " add constraint FK__", table_name, "__",
referenced_table_name , " foreign key (", column_name, ") ",
"references ", referenced_table_name,
"(", referenced_column_name, ");"
) AS runMe
FROM
information_schema.key_column_usage
WHERE
TABLE_SCHEMA='myschemaname'
AND
constraint_name like 'FK_%';
A bit of output:
alter table visitor_browsers drop foreign key FK_4ygermmic4fujggq1kp96dx47;
alter table visitor_browsers drop key FK_4ygermmic4fujggq1kp96dx47;
alter table visitor_browsers add key FK__visitor_browsers__websites (website);
alter table visitor_browsers add constraint FK__visitor_browsers__websites foreign key (website) references websites(id);
The following query will build the correct syntax automatically.
Just execute each line returned and all your FKEYs will be gone.
I leave the reverse (adding them back) as an exercise for you.
SELECT CONCAT("alter table ", TABLE_NAME," drop foreign key `", CONSTRAINT_NAME,"`; ") AS runMe
FROM information_schema.key_column_usage
WHERE TABLE_SCHEMA='MY_SCHEMA_NAME';
03 2022
I know this question is old, but if anyone is looking for an answer with an explanation then here it is. The code below is tested.
Drop the old constrain first OldColumnConstrain
Drop the old Foreign key column OldColumnName
Add the new foreign key column NewColumnName with the datatype INTEGER UNSIGNED NULL
(optional) I want this new foreign key after a specific column - AFTER previousColumn
Constrain the new column newColumnConstrain with the reference table t1
ALTER TABLE t1
DROP FOREIGN KEY OldColumnConstrain,
DROP COLUMN OldColumnName,
ADD NewColumnName INTEGER UNSIGNED NULL AFTER previousColumn,
ADD CONSTRAINT newColumnConstrain FOREIGN KEY (NewColumnName) REFERENCES t2(id);
Important
Running the above query will rename your Old column to a New column (not exactly renaming but deleting and adding a column), but WATCHOUT that column data will be lost and it will be replaced with NULL.
Can you ACTUALLY rename your fk?
I guess not. If I find a solution I will update this answer.
See this answer https://stackoverflow.com/a/2014519/5413283
This task becomes simpler if you use GUI tools. I tried to rename ID column using IntelliJ IDEA Database tool and it worked like a charm! I don't have to bother about foreign keys when renaming a table or column.
See more details in IntelliJ IDEA Help | Renaming items.
I want to remove constraints from my table. My query is:
ALTER TABLE `tbl_magazine_issue`
DROP CONSTRAINT `FK_tbl_magazine_issue_mst_users`
But I got an error:
#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 FK_tbl_magazine_issue_mst_users' at line 1
Mysql has a special syntax for dropping foreign key constraints:
ALTER TABLE tbl_magazine_issue
DROP FOREIGN KEY FK_tbl_magazine_issue_mst_users
I had the same problem and I got to solve with this code:
ALTER TABLE `table_name` DROP FOREIGN KEY `id_name_fk`;
ALTER TABLE `table_name` DROP INDEX `id_name_fk`;
There is no such thing as DROP CONSTRAINT in MySQL. In your case you could use DROP FOREIGN KEY instead.
If the constraint is not a foreign key, eg. one added using 'UNIQUE CONSTRAINT (colA, colB)' then it is an index that can be dropped using ALTER TABLE ... DROP INDEX ...
To add a little to Robert Knight's answer, since the title of the post itself doesn't mention foreign keys (and since his doesn't have complete code samples and since SO's comment code blocks don't show as well as the answers' code blocks), I'll add this for unique constraints. Either of these work to drop the constraint:
ALTER TABLE `table_name` DROP KEY `uc_name`;
or
ALTER TABLE `table_name` DROP INDEX `uc_name`;
Also nice, you can temporarily disable all foreign key checks from a mysql database:
SET FOREIGN_KEY_CHECKS=0;
And to enable it again:
SET FOREIGN_KEY_CHECKS=1;
Some ORM's or frameworks use a different naming convention for foreign keys than the default FK_[parent table]_[referenced table]_[referencing field], because they can be altered.
Laravel for example uses [parent table]_[referencing field]_foreign as naming convention. You can show the names of the foreign keys by using this query, as shown here:
SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_SCHEMA = '<database>' AND REFERENCED_TABLE_NAME = '<table>';
Then remove the foreign key by running the before mentioned DROP FOREIGN KEY query and its proper name.
The simplest way to remove constraint is to use syntax ALTER TABLE tbl_name DROP CONSTRAINT symbol; introduced in MySQL 8.0.19:
As of MySQL 8.0.19, ALTER TABLE permits more general (and SQL standard) syntax for dropping and altering existing constraints of any type, where the constraint type is determined from the constraint name
ALTER TABLE tbl_magazine_issue DROP CONSTRAINT FK_tbl_magazine_issue_mst_users;
db<>fiddle demo
For those that come here using MariaDB:
Note that MariaDB allows DROP CONSTRAINT statements in general, for example for dropping check constraints:
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;
https://mariadb.com/kb/en/library/alter-table/
Go to structure view of the table
You will see 2 option at top a.Table structure b.Relation view.
Now click on Relation view , here you can drop your foreign key constraint. You will get all relation here.
In MySQL you have to specify what kind of constraint you want to remove:
Example:
CONSTRAINT `shop_ibfk_1` FOREIGN KEY (`fb_user_id`) REFERENCES `fb_user` (`id`),
CONSTRAINT `shop_chk_1` CHECK ((`import_lock` in (0,1)))
the first one you would remove with:
alter mytable shop drop FOREIGN KEY `shop_ibfk_1`;
the second one with
alter mytable drop CHECK `shop_chk_1`;
this will works on MySQL to drop constraints
alter table tablename drop primary key;
alter table tablename drop foreign key;