Why can't I drop a table on MYsql? - mysql

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;

Related

constraint does not exist

I am new to SQL and I want to learn it.
I have created a table to practice 'adding' and 'removing' primary key using constraints in MySQL.
syntax: create table test1 (id_no int,name varchar(25));
now, i wanted to add a primarykey to the existing table, and i had used a constraint so that i could remove primary key from column in future.
syntax: alter table test1 add constraint pk_id primary key(id_no);
when i tried to drop constraint
syntax: alter table test1 drop constraint pk_id;
it says:
ERROR 3940 (HY000): Constraint 'pk_id' does not exist.
how to add constraint to my id_no column.
i had tried different methods it still says constraint does not exist
please help me
You can use this in MySQL to drop the primary key.
ALTER TABLE test1 DROP PRIMARY KEY;
Try this instead:
alter table 'test1' drop constraint Primary key pk_id;

Unable to drop a foreign key column in MySql

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?

How to drop my Foreign key in database?

I am using Hibernate to generate my schema. Hibernate create the following sql instruction
alter table Person add index FKA126572FF5D5DSE (job_id), add constraint FKA126572FF5D5DSE foreign key (job_id) references Job(id)
This sql was executed in my database and the index exists.I would like just to remove the index and its associated constraint.
Is it the following sufficient ?
alter table Person drop foreign key FKA126572FF5D5DSE ;
Thanks.
To drop the foreign key and its index you need to execute:
alter table t1 drop FOREIGN KEY FKA126572FF5D5DSE;
ALTER TABLE t1 DROP INDEX FKA126572FF5D5DSE;
SQLFiddle

Usage of Alter command to drop Primary key and Foreign Key

I have created three tables
create table employee1 (eid int, ename varchar(25),email varchar(15));
create table accounts1 (eid int, accno int,atype varchar(2),bal int);
create table trans1(cid int, accno int, atype varchar(2), accounts int, bal int);
using alter command I have added primary key and foreign keys
alter table employee1 add primary key(eid);
alter table accounts1 add foreign key(eid) references employee1(eid);
alter table accounts1 add primary key(accno,atype);
alter table trans1 add foreign key(accno,atype) references accounts1(accno,atype);
Now my problem is I am trying to delete all the primary key and foreign keys of all the tables its giving me errors.
Initially I tried deleteing the primary key. then on refering to the below links showed me that foreign key must be deleted first and I tried doing that but still i am getting the error.
MYSQL 5.5 Drop Primary Key
errors:
mysql> alter table employee1 drop constraint eid;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corres
ponds to your MySQL server version for the right syntax to use near 'constraint eid' a
t line 1
mysql> alter table accounts1 drop primary key;
ERROR 1025 (HY000): Error on rename of '.\jlcindia#sql-b20_1' to '.\jlcindia\accounts
1' (errno: 150)
mysql> alter table employee1 drop primary key;
ERROR 1025 (HY000): Error on rename of '.\jlcindia#sql-b20_1' to '.\jlcindia\employee
1' (errno: 150)
mysql> alter table trans1 drop foreign key;
ERROR 1005 (HY000): Can't create table 'jlcindia.#sql-b20_1' (errno: 150)
mysql> show engine innodb status
Note: I have no auto increment or any such thing in any of my tables.
I checked the following links but none was of any help.
Remove Primary Key in MySQL
mysql, alter column remove primary key and auto incremement
Error altering primary key column in mysql table
How can I alter a primary key constraint using SQL syntax?
You need to drop the constraints in the following order(Which is the reverse of the order in which you created the keys):
Foreign keys in the table trans1
Primary keys in the table accounts1
Foreign key in the table accounts1
Primary key in the table employee1
fiddle
I think,You need to first remove the foreign key of table trans1.Then remove foreign key of table accounts1.Then try to remove primary key of table accounts1.then remove primary key of table *employee1 *.
If you remove foreign key and primary key in this order then you can remove all constraints as you want.
First of all you need to find the constraint name related to the Primary and Foreign keys. Then you need to use these constraint names while dropping the keys.
select *
from
information_schema.key_column_usage
where
table_name = 'my_table'
From the above query you will be able to find the constraint names.
Now use the below syntax to drop the keys.
ALTER TABLE tablename DROP CONSTRAINT constraint name

MySQL Removing Some Foreign keys

I have a table whose primary key is used in several other tables and has several foreign keys to other tables.
CREATE TABLE location (
locationID INT NOT NULL AUTO_INCREMENT PRIMARY KEY
...
) ENGINE = InnoDB;
CREATE TABLE assignment (
assignmentID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
locationID INT NOT NULL,
FOREIGN KEY locationIDX (locationID) REFERENCES location (locationID)
...
) ENGINE = InnoDB;
CREATE TABLE assignmentStuff (
...
assignmentID INT NOT NULL,
FOREIGN KEY assignmentIDX (assignmentID) REFERENCES assignment (assignmentID)
) ENGINE = InnoDB;
The problem is that when I'm trying to drop one of the foreign key columns (ie locationIDX) it gives me an error.
"ERROR 1025 (HY000): Error on rename"
How can I drop the column in the assignment table above without getting this error?
As explained here, seems the foreign key constraint has to be dropped by constraint name and not the index name.
The syntax is:
ALTER TABLE footable DROP FOREIGN KEY fooconstraint;
The foreign keys are there to ensure data integrity, so you can't drop a column as long as it's part of a foreign key. You need to drop the key first.
I would think the following query would do it:
ALTER TABLE assignmentStuff DROP FOREIGN KEY assignmentIDX;
As everyone said above, you can easily delete a FK. However, I just noticed that it can be necessary to drop the KEY itself at some point. If you have any error message to create another index like the last one, I mean with the same name, it would be useful dropping everything related to that index.
ALTER TABLE your_table_with_fk
drop FOREIGN KEY name_of_your_fk_from_show_create_table_command_result,
drop KEY the_same_name_as_above
Check what's the CONSTRAINT name and the FOREIGN KEY name:
SHOW CREATE TABLE table_name;
Remove both the CONSTRAINT name and the FOREIGN KEY name:
ALTER TABLE table_name
DROP FOREIGN KEY the_name_after_CONSTRAINT,
DROP KEY the_name_after_FOREIGN_KEY;
Hope this helps!
Hey I followed some sequence above,
and found some solution.
SHOW CREATE TABLE footable;
You will get FK Constrain Name like
ProjectsInfo_ibfk_1
Now you need to remove this constraints. by alter table commantd
alter table ProjectsInfo drop foreign key ProjectsInfo_ibfk_1;
Then drop the table column,
alter table ProjectsInfo drop column clientId;
Here's a way to drop foreign key constraint, it will work.
ALTER TABLE location.location_id
DROP FOREIGN KEY location_ibfk_1;
You usually get this error if your tables use the InnoDB engine. In that case you would have to drop the foreign key, and then do the alter table and drop the column.
But the tricky part is that you can't drop the foreign key using the column name, but instead you would have to find the name used to index it. To find that, issue the following select:
SHOW CREATE TABLE region;
This should show you a row ,at left upper corner click the +option ,the click the full text raio button then click the go .there you will get the name of the index, something like this:
CONSTRAINT region_ibfk_1 FOREIGN KEY (country_id) REFERENCES country (id) ON DELETE NO ACTION ON UPDATE NO ACTION
Now simply issue an:
alter table region drop foreign key region_ibfk_1;
or
more simply just type:-
alter table TableName drop foreign key TableName_ibfk_1;
remember the only thing is to add _ibfk_1 after your tablename to make like this:- TableName_ibfk_1
first need to get actual constrain name by this query
SHOW CREATE TABLE TABLE_NAME
This query will result constrain name of the foreign key, now below query will drop it.
ALTER TABLE TABLE_NAME DROP FOREIGN KEY COLUMN_NAME_ibfk_1
last number in above constrain name depends how many foreign keys you have in table
You can not drop the foreign key column because it is being referenced from the table assignmentStuff. So you should first drop the foreign key constraint assignmentStuff.assignmentIDX.
A similar question has already been asked here. Check also here for more info.
Try this:
alter table Documents drop
FK__Documents__Custo__2A4B4B5E
step1: show create table vendor_locations;
step2: ALTER TABLE vendor_locations drop foreign key vendor_locations_ibfk_1;
it worked for me.