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;
Related
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
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
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);
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.
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.