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?
Related
Here is the code:
USE new_schema;
DROP TABLE account;
ALTER TABLE transaction
DROP FOREIGN KEY fk_t_account_id;
the error:
09:10:05 DROP TABLE account Error Code: 3730. Cannot drop table 'account' referenced by a foreign key constraint 'fk_t_account_id' on table 'transaction'. 0.000 sec
Looks at the foreign key. That table also needs to be dropped or remove the foreign key constraint
Just switch the order of the SQL
USE new_schema;
ALTER TABLE transaction
DROP FOREIGN KEY fk_t_account_id;
DROP TABLE account;
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 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
I am using phpMyAdmin version 3.3.10.3 to manage my database.
I am using InnoDB and foreign key contraints.
I have attempted to drop several columns from a table. These columns are foreign keys referencing other tables.
ALTER TABLE `product`
DROP `c_status_id`,
DROP `o_certification_id`,
DROP `g_free_certification_id`,
DROP `gm_certification_id`,
DROP `n_certification_id`;
Upon attempt of the query I received the following error message.
#1025 - Error on rename of ' /#sql-ea2_38d9f' to ' /product' (errno: 150
You must first drop the foreign key relationship before you drop the column referenced in the relationship.
ALTER TABLE 'TABLE_NAME' DROP FOREIGN KEY 'NAME_OF_FOREIGN_KEY'
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
How can I drop the "Unique Key Constraint" on a column of a MySQL table using phpMyAdmin?
A unique constraint is also an index.
First use SHOW INDEX FROM tbl_name to find out the name of the index. The name of the index is stored in the column called key_name in the results of that query.
Then you can use DROP INDEX:
DROP INDEX index_name ON tbl_name
or the ALTER TABLE syntax:
ALTER TABLE tbl_name DROP INDEX index_name
You can DROP a unique constraint from a table using phpMyAdmin as requested as shown in the table below. A unique constraint has been placed on the Wingspan field. The name of the constraint is the same as the field name, in this instance.
The indexes capable of placing a unique key constraint on a table are PRIMARY and UNIQUE indexes.
To remove the unique key constraint on a column but keep the index, you could remove and recreate the index with type INDEX.
Note that it is a good idea for all tables to have an index marked PRIMARY.
To add UNIQUE constraint using phpmyadmin, go to the structure of that table and find below and click that,
To remove the UNIQUE constraint, same way, go to the structure and scroll down till Indexes Tab and find below and click drop,
Hope this works.
Enjoy ;)
If you want to remove unique constraints from MySQL database table, use alter table with drop index.
Example:
CREATE TABLE unique_constraints (
unid INT,
activity_name VARCHAR(100),
CONSTRAINT activty_uqniue UNIQUE (activity_name),
PRIMARY KEY (unid)
);
ALTER TABLE unique_constraints
DROP INDEX activty_uqniue;
Where activty_uqniue is UNIQUE constraint for activity_name column.
For WAMP 3.0 :
Click Structure
Below Add 1 Column you will see '- Indexes'
Click -Indexes and drop whichever index you want.
The constraint could be removed with syntax:
ALTER TABLE
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_name DROP CONSTRAINT symbol;
Example:
CREATE TABLE tab(id INT, CONSTRAINT unq_tab_id UNIQUE(id));
-- checking constraint name if autogenerated
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'tab';
-- dropping constraint
ALTER TABLE tab DROP CONSTRAINT unq_tab_id;
db<>fiddle demo
This might help:
Inside your sql terminal
FIRST STEP:
SHOW INDEX FROM {YOUR_TABLE_NAME}
SECOND STEP:
SHOW INDEX FROM {YOUR_TABLE_NAME} WHERE Column_name='ACTUAL_COLUMN_NAME_YOU_GOT_FROM_FIRST_STEP_OUTPUT'
THIRD STEP:
ORIGINAL_KEY_NAME_VALUE = SECOND_STEP_RESPONSE["Key_name"]
FOURTH STEP:
ALTER TABLE {YOUR_TABLE_NAME} DROP INDEX ${ORIGINAL_KEY_NAME_VALUE}
while dropping unique key we use index
ALTER TABLE tbl
DROP INDEX unique_address;
my table name is buyers which has a unique constraint column emp_id now iam going to drop the emp_id
step 1: exec sp_helpindex buyers, see the image file
step 2: copy the index address
step3: alter table buyers drop constraint [UQ__buyers__1299A860D9793F2E]
alter table buyers
drop column emp_id
note:
Blockquote
instead of buyers change it to your table name :)
Blockquote
thats all column name emp_id with constraints is dropped!