MySQL: delete constraint UNIQUE KEY - mysql

I have this table:
CREATE TABLE table1 (
//..
UNIQUE KEY `UNIQ_60349993F97DBD80` (`contrat_parent_id`)
//..
)ENGINE=InnoDB AUTO_INCREMENT=4384 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci.
I try this statement:
alter table table drop index UNIQ_60349993F97DBD80
But it doesn't work. I try many statements, but, they don't work.
Can I help me ?

Just remove ALTER TABLE from the begging of your statement, and also add the table Name on the end.
drop index UNIQ_60349993F97DBD80 ON table1
Reference

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;

Drop unique constraint in MySQL and H2

I have the following constraint in CREATE statement:
UNIQUE (`field_name`)
These commands to remove constraint work in MySQL but not in H2:
DROP INDEX `field_name` ON `table_name`;
ALTER TABLE `table_name` DROP INDEX `field_name`;
I need a command which would work both in MySQL and H2 (MySQL is used in real environment and H2 in Unit tests)
Found the following workaround: removing column removes the constraint, so:
ALTER TABLE `table_name` ADD COLUMN `tmp_code` VARCHAR(50) NOT NULL DEFAULT 'TMP';
UPDATE `table_name`
SET `tmp_code` = `field_name`;
ALTER TABLE `table_name` DROP COLUMN `field_name`;
ALTER TABLE `table_name` ADD COLUMN `field_name` VARCHAR(50) NOT NULL;
UPDATE `table_name`
SET `field_name` = `tmp_code`;
ALTER TABLE `table_name` DROP COLUMN `tmp_code`;
Do SHOW CREATE TABLE to see the name of the UNIQUE KEY. Then you can proceed with the DROP or ALTER. It will probably say
UNIQUE KEY `col` (`col`),
The first col is the key name.
If you need to maintain the INDEX but get rid of the UNIQUEness constraint, then drop the key, then add a non-unique key.

How to remove primary key from a mysql table which is primary key and foreign key in the same table as well.?

I have two different table having 20k entries each and mistakenly I have made summaryId as primary key and foreign key in the same table but now I want to remove the primary key constraint which is auto increament too. When I try drop primary key syntax it returns me an error :
#1025 - Error on rename of '.\tg#sql-a38_7f' to '.\tg\rest_web_availability_summary_pm' (errno: 150)
I tried the below query.
ALTER TABLE 'table_name' DROP PRIMARY KEY
If anybody has any idea please tell me how to remove primary key.
The problem is, that your field is auto_increment. You should remove auto_increment first and then drop the primary key.. so try this:
ALTER TABLE `mytable` CHANGE COLUMN `id` `id` INT(11) NOT NULL, DROP PRIMARY KEY;
Redefining the column without auto_increment removes it
I had the same problem, turned out that as it was referenced by other fields, mysql required the column to be unique, so I first added a unique constraint, and lived happily ever after:
alter table `mytable` add unique key `key` (`fieldname`);
alter table `mytable` drop primary key; -- which is fieldname...
As mentioned, you need remove the FKs before. On MySQL, do like this:
ALTER TABLE `table_name` DROP FOREIGN KEY `id_name_fk`;
ALTER TABLE `table_name` DROP INDEX `id_name_fk`;

How to drop unique in MySQL?

Create Table: CREATE TABLE `fuinfo` (
`fid` int(10) unsigned NOT NULL,
`name` varchar(40) NOT NULL,
`email` varchar(128) NOT NULL,
UNIQUE KEY `email` (`email`),
UNIQUE KEY `fid` (`fid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
I want to drop the unique key on email,how?
Simply you can use the following SQL Script to delete the index in MySQL:
alter table fuinfo drop index email;
There is a better way which don't need you to alter the table:
mysql> DROP INDEX email ON fuinfo;
where email is the name of unique key (index).
You can also bring it back like that:
mysql> CREATE UNIQUE INDEX email ON fuinfo(email);
where email after IDEX is the name of the index and it's not optional. You can use KEY instead of INDEX.
Also it's possible to create (remove) multicolumn unique indecies like that:
mysql> CREATE UNIQUE INDEX email_fid ON fuinfo(email, fid);
mysql> DROP INDEX email_fid ON fuinfo;
If you didn't specify the name of multicolumn index you can remove it like that:
mysql> DROP INDEX email ON fuinfo;
where email is the column name.
mysql> DROP INDEX email ON fuinfo;
where email is the unique key (rather than the column name). You find the name of the unique key by
mysql> SHOW CREATE TABLE fuinfo;
here you see the name of the unique key, which could be email_2, for example. So...
mysql> DROP INDEX email_2 ON fuinfo;
mysql> DESCRIBE fuinfo;
This should show that the index is removed
Use below query :
ALTER TABLE `table_name` DROP INDEX key_name;
If you don't know the key_name then first try below query, you can get key_name.
SHOW CREATE TABLE table_name
OR
SHOW INDEX FROM table_name;
If you want to remove/drop primary key from mysql table, Use below query for that
ALTER TABLE `products` DROP INDEX `PRIMARY`;
Code Taken from: http://chandreshrana.blogspot.in/2015/10/how-to-remove-unique-key-from-mysql.html
DROP INDEX column_name ON table_name
Select the database and query form the sql tab.This removes the index of the particular column. It worked for me in PHP MyADMIN
This may help others
alter table fuinfo drop index fuinfo_email_unique
For MySQL 5.7.11
Step-1: First get the Unique Key
Use this query to get it:
1.1) SHOW CREATE TABLE User;
In the last, it will be like this:
.....
.....
UNIQUE KEY UK_8bv559q1gobqoulqpitq0gvr6 (phoneNum)
.....
....
Step-2: Remove the Unique key by this query.
ALTER TABLE User DROP INDEX UK_8bv559q1gobqoulqpitq0gvr6;
Step-3: Check the table info, by this query:
DESC User;
This should show that the index is removed
Thats All.
ALTER TABLE 0_value_addition_setup DROP INDEX value_code
Try it to remove uique of a column:
ALTER TABLE `0_ms_labdip_details` DROP INDEX column_tcx
Run this code in phpmyadmin and remove unique of column
ALTER TABLE [table name] DROP KEY [key name];
this will work.

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.