Usage of Alter command to drop Primary key and Foreign Key - mysql

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

Related

Unable to reuse table name in SQL as constraint still exists

I am trying to rename an empty table with its old name.
I had created constraints in the past (innodb), but that table doesn't exist anymore and if I try to reuse its name (no matter if I create a new table or try to rename another table), I get the following error:
errno: 150 "Foreign key constraint is incorrectly formed"
LATEST FOREIGN KEY ERROR
------------------------
2021-07-16 14:50:24 0x2508 Error in foreign key constraint of table database/publisheremails:
there is no index in the table which would contain
the columns as the first columns, or the data types in the
table do not match the ones in the referenced table
or one of the ON ... SET NULL columns is declared NOT NULL. Constraint:
,
CONSTRAINT `publisheremails_ibfk_1` FOREIGN KEY (`agencyid`) REFERENCES `agencies` (`id`) ON UPDATE CASCADE
I tried to drop the constraint with this code:
ALTER TABLE old_publisheremails
DROP FOREIGN KEY publisheremails_ibfk_1;
I get the following error:
#1091 - Can't DROP FOREIGN KEY publisheremails_ibfk_1; check that it exists
I also tried to drop it with the old table name, but I get this error:
#1146 - Table 'database.publisheremails' doesn't exist
Same goes if I try to drop it from 'agencies'...
Check which foreign keys the table actually has and which name it has with
SELECT
TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_SCHEMA = '<database>' AND
REFERENCED_TABLE_NAME = 'old_publisheremails';
And you can't drop the publisheremails_ibfk_1 because it doesn't exist till now
So check the agencyid column if it has the same data type as agencies (id`) and change it

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;

Cannot create table mysql(mariadb). Table is corrupted

Some how, my database has gotten into a bad state. I previously had a table named live_stream. When I tried to drop a foreign key constraint, I got an error that mariadb could not rename #sql-26_e7a to live_stream. Now when I try to run the following statement, I get this error.
Can't create table live_stream (errno: 150 "Foreign key constraint is incorrectly formed")
CREATE TABLE live_stream
(idbigint(20) NOT NULL PRIMARY KEY);
As you can see I don't have any foreign key constraints in the definition. If I try the exact same definition with a different table name, it works. If I try to drop the table, mariadb complains that live_stream doesn't exist. Its like the table or foreign key are stuck in a transaction or something like that.
I am using galara with maria db 10.3.
UPDATE
I believe the problem was introduced when a foreign key and unique index were given the same name. I recreated the scenario, and when I try to drop the index, mariadb prevents it.
* UPDATE 2 *
Here is the output of SHOW ENGINE INNODB STATUS;
* UPDATE3 *
Here are the steps to reproduce.
create table tb1
(
id bigint null,
constraint tb1_pk
primary key (id)
);
create table tb2
(
id bigint null,
tb1_id bigint null,
constraint tb2_pk
primary key (id),
constraint tb2_tb1_id_fk
foreign key (tb1_id) references tb1 (id)
);
ALTER TABLE tb2 ADD CONSTRAINT tb2_tb1_id_fk UNIQUE (tb1_id, tb1_id);
drop index tb2_tb1_id_fk on tb2;
The problem is that the unique constraint has the same name as the foreign key and references the same column twice.

How to change primary key of table

I want to change primary key of table, initially it was id, now i want to change it to userid
smsusers(id,fname,lname,userid)
Here id is varchar type
adn userid is int type
for this i am trying following query
ALTER TABLE smsusers DROP PRIMARY KEY
which is showing this error
#1025 - Error on rename of '.\xrcwrn_sms\#sql-ae0_6f' to
'.\xrcwrn_sms\smsusers' (errno: 150)
id of smsusers is associated with many tables as foreign key.
How to change the primary key.
Here is an example:
ALTER TABLE `database`.`table`
DROP PRIMARY KEY,
ADD PRIMARY KEY (`userid`);
The message is telling you that you can't drop the primary key yet because it is referenced by one or more foreign keys. You need to identify and drop the foreign keys first, then drop the primary key.
ERROR NO:150 means Foreign key definition problem. I think that some other table has a foreign key constraint depending on this PK, so you need to drop that first and rebuild it later.
I tried this link. This is working properly.
My steps are
CREATE INDEX id_pk_unique ON smsusers (id)
ALTER TABLE parent DROP PRIMARY KEY;
ALTER TABLE parent ADD PRIMARY KEY (userid);

unable to drop the foreign key

I would like to drop the foreign key in my table but been into this error message
mysql> alter table customers drop foreign key customerid;
ERROR 1025 (HY000): Error on rename of '.\products\customers' to '.\products\#sql2-7ec-a3' (errno: 152)
mysql>
The solution described here by Chris White worked for me.
The root problem is that MySQL creates both an index and a foreign key. Both must be removed (the foreign key first contrary to what Chris said).
show create table table_name;
SHOW CREATE TABLE `table_name`:
| table_name | CREATE TABLE `table_name` (
`id` int(20) unsigned NOT NULL auto_increment,
`key_column` smallint(5) unsigned default '1',
KEY `column_tablein_26440ee6` (`key_column`), <--- shows key name
CONSTRAINT `table_name_ibfk_1` FOREIGN KEY (`key_column`) REFERENCES <--- shows foreign key constraint name
`second_table` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
Delete the foreign key constraint:
ALTER TABLE table_name DROP FOREIGN KEY `table_name_ibfk_1`;
Delete the key
ALTER TABLE table_name DROP KEY `column_tablein_26440ee6`;
That did it for me.
It looks like a bug in the error messaging of MySQL. (http://bugs.mysql.com/bug.php?id=10333)
Use SHOW CREATE TABLE table_name to see the actual name of the foreign key. It looks like it might be mysql query browser problem when generating the query with wrong spelling of the foreign key name.
To avoid getting this error while trying to drop a foreign key, use the constraint name rather than the column name of the foreign key.
When I tried
mysql> ALTER TABLE mytable DROP PRIMARY KEY;
I got error as
ERROR 1025 (HY000): Error on rename of '.\database\#sql-454_3' to '.\database\mytable' (errno: 150).
I solved it using:
mysql> ALTER TABLE mytable DROP PRIMARY KEY, ADD PRIMARY KEY (column1,column2,column3);
Some links that will help you.
link 1
link 2 [look for Posted by Alex Blume on November 7 2008 5:09pm & Posted by Hector Delgadillo on January 21 2011 4:57am]
To avoid getting this error while trying to drop a foreign key, use the constraint name rather than the column name of the foreign key
You should try with the foreign key name as Fahim Parkar suggested. Actually that does not work always either.
In my case I used the
FOREIGN KEY `fk`(`col1`) REFERENCES `table2`(`col1`)
code to add the fk by creation.
The problem with this code that it is not valid and should throw some kind of syntax error, but still it added a foreign key with a random name.
When I added the fk with the right syntax:
CONSTRAINT `fk` FOREIGN KEY (`col1`) REFERENCES `table2`(`col1`)
the following code dropped it properly:
ALTER TABLE `table1` DROP FOREIGN KEY `fk`
So this kind of error can happen too if you try to remove a foreign key with an invalid name. It is important to view the table properties with
SHOW CREATE TABLE `table1`
and check the foreign key names if you get this kind of errors.