I am trying to drop a number of foreign keys using:
ALTER TABLE `table` DROP FOREIGN KEY `fk_table_users1` , DROP FOREIGN KEY `fk_table_accounts1` , DROP FOREIGN KEY `fk_table_data1` ;
but it returns the error:
Error on rename of './db/table' to './db/#sql2-179c-288289' (errno: 152)
I have run SHOW ENGINE INNODB STATUS which says:
120725 12:38:37 Error in dropping of a foreign key constraint of table db/table,
in SQL command
ALTER TABLE `table` DROP FOREIGN KEY `fk_table_users1` , DROP FOREIGN KEY `fk_table_accounts1` , DROP FOREIGN KEY `fk_table_data1`
Cannot find a constraint with the given id fk_table_users1.
SHOW CREATE TABLE 'table' output:
CREATE TABLE `table` (
`id` int(11) NOT NULL auto_increment,
`data_id` int(11) NOT NULL,
`account_id` int(11) NOT NULL,
`status` enum('pending','complete') NOT NULL default 'pending',
`created_at` datetime NOT NULL,
`created_by` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_orders_users1` (`created_by`),
KEY `fk_orders_data1` (`data_id`),
KEY `fk_orders_accounts1` (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
However when I look at the structure via phpmyadmin it lists the foreign key with the same name. Do I need to do something else before I can drop the foreign keys?
There are no foreign keys. Refer MySQL documentation which says
KEY is normally a synonym for INDEX.
So basically in table you have created indexes, not foreign keys. For Foreign Key info, Click here
You need to temporarily drop the constraint so that you can remove it.
SET FOREIGN_KEY_CHECKS=0;
and then turn them on again after you drop the foreign key:
SET FOREIGN_KEY_CHECKS=1;
first drop foreign key then delete column
alter table 'table name' drop foreign key 'constraint id ;
if you don't know constraint id create database dump in that constraint id is available in dump file ..
then delete column..
The index name and constraint name may not be same. You should delete constraint first using code: ALTER TABLE tablename DROP FOREIGN KEY constraintname
Related
I am trying to drop a FOREIGN KEY but it is throwing an error.
**mysql> alter table traveltime drop foreign key travelid;
ERROR 1091 (42000): Can't DROP 'travelid'; check that column/key exists
mysql>**
The column travelid is the foreign key referencing to another table. Here is the output of SHOW CREATE TABLE traveltime;
CREATE TABLE `traveltime` (
`timeid` int(11) DEFAULT NULL,
`travelid` int(11) DEFAULT NULL,
`hour` int(11) DEFAULT NULL,
`minute` int(11) DEFAULT NULL,
KEY `travelid` (`travelid`),
CONSTRAINT `traveltime_ibfk_1` FOREIGN KEY (`travelid`) REFERENCES `travel` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
The column on which the FOREIGN KEY was defined was travelid, but you probably did not specify an identifier for the constraint itself when it was created, and MySQL created one on your behalf: traveltime_ibfk_1.. That's the identifier you need to target in your ALTER statement.
ALTER TABLE traveltime DROP FOREIGN KEY traveltime_ibfk_1
Though it's difficult to visually parse, MySQL's ALTER TABLE docs specify
ALTER [IGNORE] TABLE tbl_name
[alter_specification [, alter_specification] ...]
[partition_options]
alter_specification:
table_options
...
...
DROP FOREIGN KEY fk_symbol
... where fk_symbol is the constraint's identifier name, rather than the column on which it was defined (because it is possible to define multi-column FK's).
I tried to create a table in MySQL using the CREATE TABLE statement below:
CREATE TABLE `visit` (
`visit_id` int(11) NOT NULL,
`site_id` int(11) DEFAULT NULL,
PRIMARY KEY (`visit_id`),
CONSTRAINT `FK_visit_site` FOREIGN KEY (`site_id`) REFERENCES `site` (`site_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I received this error:
ERROR 1005 (HY000): Can't create table 'fooschema.visit' (errno: 121)
I used SHOW ENGINE INNODB STATUS command. This is the error message:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
140222 7:03:17 Error in foreign key constraint creation for table `fooschema`.`visit`.
A foreign key constraint of name `fooschema/FK_visit_site`
already exists. (Note that internally InnoDB adds 'databasename/'
in front of the user-defined constraint name).
Note that InnoDB's FOREIGN KEY system tables store
constraint names as case-insensitive, with the
MySQL standard latin1_swedish_ci collation. If you
create tables or databases whose names differ only in
the character case, then collisions in constraint
names can occur. Workaround: name your constraints
explicitly with unique names.
Then, I used the query below to list all available constraints:
select *
from information_schema.table_constraints
where constraint_schema = 'fooschema'
I didn't see any constraint with name 'FK_visit_site' in the result.
The FK_visit_site constraint was a foreign key constraint of table visit. I dropped the visit table and attempted to recreate it.
Is there a way I can drop this foreign key constraint even when the table it was associated to doesn't exist?
your foreign key already exist , so either drop existed foreign key or rename your second key.
ALTER TABLE `site` DROP FOREIGN KEY `FK_visit_site`;
or rename to other new one.
CREATE TABLE `visit` (
`visit_id` int(11) NOT NULL PRIMARY KEY,
`site_id` int(11) NOT NULL,
CONSTRAINT `FK_visit_site` FOREIGN KEY (`site_id`) REFERENCES `site` (`site_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Added PRIMARY KEY to the visit_id line.
Note:
make sure that site_id in the site table have exact same datatype of site_id in visit table.
like that
`site_id` int(11) DEFAULT NULL --//in the `site` table
The two keys you're coupling must have the exact same datatype ( INT NOT NULL), even signedness
AFAIK, you will get this error when you're trying to add a constraint with a name that's already used somewhere else. Means, in your case FK FK_visit_site had already been used before.
If the table you're trying to create includes a foreign key constraint, and you've provided your own name for that constraint, remember that it must be unique within the database.
You can run the below query to find out the same
SELECT
constraint_name,
table_name
FROM
information_schema.table_constraints
WHERE
constraint_type = 'FOREIGN KEY'
AND table_schema = DATABASE()
ORDER BY
constraint_name;
Taken from the post here
http://www.thenoyes.com/littlenoise/?p=81
Try using a different name for your FK like
CREATE TABLE `visit` (
`visit_id` int(11) NOT NULL,
`site_id` int(11) DEFAULT NULL,
PRIMARY KEY (`visit_id`),
CONSTRAINT `FK_visit_site_New` FOREIGN KEY (`site_id`)
REFERENCES `site` (`site_id`),
)
I have the following 2 tables:
CREATE TABLE `personal_info` (
`p_id` int(11) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`initials` text NOT NULL,
`surname` text NOT NULL,
`home_lang` int(11) NOT NULL,
PRIMARY KEY (`p_id`),
KEY `home_lang` (`home_lang`),
CONSTRAINT `personal_info_ibfk_1` FOREIGN KEY (`home_lang`) REFERENCES `language_list` (`ll_id`)
) ENGINE=InnoDB AUTO_INCREMENT=44 DEFAULT CHARSET=latin1
CREATE TABLE `language_list` (
`ll_id` int(11) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
PRIMARY KEY (`ll_id`)
) ENGINE=InnoDB AUTO_INCREMENT=73 DEFAULT CHARSET=latin1
I am trying to remove a column from a table with the following:
ALTER TABLE `personal_info` DROP `home_lang`
But cannot do it since I recieve this error:
#1025 - Error on rename of '.\MyDB\#sql-112c_82' to '.\MyDB\personal_info' (errno: 150)
I have tried to first remove the index and then remove the column with this:
ALTER TABLE personal_info DROP INDEX home_lang
But then I get the following error:
#1553 - Cannot drop index 'home_lang': needed in a foreign key constraint
So I tried to drop the foreign key:
ALTER TABLE personal_info DROP FOREIGN KEY home_lang
But received this error:
#1025 - Error on rename of '.\MyDB\personal_info' to '.\MyDB\#sql2-112c-8d' (errno: 152)
I have also tried to first set all the values to null:
update personal_info set home_lang = null
But then received this error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`MyDB`.`personal_info`, CONSTRAINT `personal_info_ibfk_1` FOREIGN KEY (`home_lang`) REFERENCES `language_list` (`ll_id`))
And now I am stuck. I have tried a few things but just cannot get the column removed. I am not allowed to alter the DB in any way other than removing the column.
Your DROP FOREIGN KEY syntax is using the wrong key name. It's trying to drop your "plain" index on the home_lang field. It's NOT the foreign key itself.
CONSTRAINT `personal_info_ibfk_1` FOREIGN KEY (`home_lang`) REFERENCES `language_list` (`ll_id`)
^^^^^^^^^^^^^^^^^^^^^--- THIS is the name of the foreign key
Try:
ALTER TABLE personal_info DROP FOREIGN KEY `personal_info_ibfk_1`
Use this given below query to find the name of the foreign key.
SHOW CREATE TABLE forms_main;
Then once u got the key, execute drop foreign key command
alter TABLE `forms_main`
drop FOREIGN key `forms_main_ibfk_1`;
Then execute the drop column command
ALTER TABLE `forms_main` DROP `company_id`;
ALTER TABLE db_name.table_name
DROP FOREIGN KEY foreign_key;
ALTER TABLE test.exam
DROP INDEX id ;
I am trying to delete a multi-column unique key from a table that also has a foreign key. I keep getting 'errno 150', unless I delete the foreign key first.
For example, if I create the table:
CREATE TABLE `testtable` (
`testtable_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`testtable_value` char(255) DEFAULT NULL,
`othertable_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`testtable_id`),
UNIQUE KEY `tt_unique_key` (`othertable_id`,`testtable_value`),
CONSTRAINT `tt_foreign_key` FOREIGN KEY (`othertable_id`) REFERENCES `othertable` (`othertable_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
and I try to remove the unique key like this:
ALTER TABLE `testtable` DROP KEY `tt_unique_key`;
It generates the error:
Error Code: 1025
Error on rename of './testdb/#sql-374_27' to './testdb/testtable' (errno: 150)
I tried setting FOREIGN_KEY_CHECKS = 0, but I get the same error:
SET FOREIGN_KEY_CHECKS = 0;
ALTER TABLE `testtable` DROP KEY `tt_unique_key`;
SET FOREIGN_KEY_CHECKS = 1;
This generates the same error message as above.
However, if I first delete the foreign key, then delete the unique key, then recreate the foreign key, everything works:
ALTER TABLE `testtable` DROP FOREIGN KEY `tt_foreign_key`;
ALTER TABLE `testtable` DROP KEY `tt_unique_key`;
ALTER TABLE `testtable` ADD CONSTRAINT `tt_foreign_key` FOREIGN KEY (`othertable_id`) REFERENCES `othertable` (`othertable_id`);
This seems really inefficient. Can anyone explain what is going on? Is there a way to drop the unique key without dropping the foreign key first?
a FOREIGN KEY REFERENCES, require a key,
the only key that can be used is t_unique_key,
thats why you can't remove it.
so add another matching key first, and then remove the old key, in your case the othertable_id field
ALTER TABLE `testtable`
ADD KEY (othertable_id),
DROP KEY `tt_unique_key`;
Here's my table:
CREATE TABLE `alums_alumphoto` (
`id` int(11) NOT NULL auto_increment,
`alum_id` int(11) NOT NULL,
`photo_id` int(11) default NULL,
`media_id` int(11) default NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `alums_alumphoto_alum_id` (`alum_id`),
KEY `alums_alumphoto_photo_id` (`photo_id`),
KEY `alums_alumphoto_media_id` (`media_id`),
CONSTRAINT `alums_alumphoto_ibfk_1` FOREIGN KEY (`media_id`) REFERENCES `media_mediaitem` (`id`),
CONSTRAINT `alum_id_refs_id_706915ea` FOREIGN KEY (`alum_id`) REFERENCES `alums_alum` (`id`),
CONSTRAINT `photo_id_refs_id_63282119` FOREIGN KEY (`photo_id`) REFERENCES `media_mediaitem` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=63 DEFAULT CHARSET=utf8
I want to delete the column photo_id, which presumably will also require deleting the foreign key constraint and the index.
The problem is that I get errors when I try to drop the column:
ERROR 1025 (HY000): Error on rename of '.\dbname\#sql-670_c5c' to '.\dbname\alums_alumphoto' (errno: 150)
... when I try to drop the index (same as above), and when I try to drop the foreign key constraint:
ERROR 1091 (42000): Can't DROP 'photo_id_refs_id_63282119'; check that column/key exists)
What order should I be doing all of this in? What precise commands should I be using?
Precisely, try this :
First drop the Foreign Key or Constraint :
ALTER TABLE `alums_alumphoto` DROP FOREIGN KEY `photo_id_refs_id_63282119`;
The previous command removes the Foreign Key Constraint on the column. Now you can drop the column photo_id (the index is removed by MySQL on dropping the column) :
ALTER TABLE `alums_alumphoto` DROP COLUMN `photo_id`;
Aternatively, you could combine these 2 operations into one :
ALTER TABLE `alums_alumphoto`
DROP FOREIGN KEY `photo_id_refs_id_63282119` ,
DROP COLUMN `photo_id`;
The sure thing is to make a duplicate table.
> CREATE TABLE alums_alumphoto_new LIKE alums_alumphoto;
> ALTER TABLE .... // Drop constraint
> ALTER TABLE .... // Drop KEY
> ALTER TABLE .... // Drop the column
> INSERT INTO alums_alumphoto_new (SELECT id, alum_id, photo_id, media_id, updated FROM alums_alumphoto);
> RENAME TABLE alums_alumphoto TO alums_alumphoto_old, alums_alumphoto_new TO alums_alumphoto;
If there's an error executing RENAME TABLE, some other tables might have foreign key constraints referencing this table, in which case this whole approach is stupid. :)
Try combining the DROP KEY and DROP FOREIGN KEY statements.
ALTER TABLE `alums_alumphoto`
DROP KEY KEY `alums_alumphoto_photo_id`,
DROP FOREIGN KEY `photo_id_refs_id_63282119`;
ALTER TABLE `alums_alumphoto`
DROP COLUMN `photo_id`;