How to drop unique in MySQL? - 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.

Related

How to remove unique key on particular mysql table field

I have assigned unique key in two field username and email. I have execute this query.
ALTER TABLE goipmonl_users DROP INDEX username;
DROP INDEX username ON goipmonl_users
It's show an error. So how can I remove unique key from selected field.
#1091 - Can't DROP 'username'; check that column/key exists.
I have username, email columns in my table.
Please find the screen shot to delete unique index from table using phpMyAdmin
1- Select the desired database,
2-then select the desired table,
3-click on the Structure tab, select the "Relation View" option at the top of the table,
4- and finally the Indexes option at the bottom of the page.
You can make use of the below command to find out the list of indexes of your table. From that, get the name of your unique index.
SHOW INDEX FROM tbl_name
Then use the below one to drop that index
ALTER TABLE tbl_name DROP INDEX unique_constraint_name;
ALTER TABLE [table name] DROP INDEX [unique key constraint name];
Please double check your unique key constraint name, use this command to check:
select distinct CONSTRAINT_NAME
from information_schema.TABLE_CONSTRAINTS
where table_name = [tablename] and constraint_type = 'UNIQUE';
Well you can simply do something like this:
```
ALTER TABLE goipmonl_users DROP INDEX goipmonl_users_username_unique;
```
That is you prefix the table name with an underscore followed by the table colunm name to which the constraint is on followed again by underscore finally the index/constraint name which is UNIQUE
hope it will helps someone else that might bump into this issue again

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.

Drop Unique mysql for two fields

Hi recently I made mysql two fields unique and now I'm unable to drop them
I've add them like
ALTER TABLE `user_subscription` ADD UNIQUE (
`user_id` ,
`status`
);
But Now i want to undo this, any idea?
Find the index name using:
SHOW INDEXES FROM user_subscription;
Then drop it:
DROP INDEX index_name ON user_subscription;

MySQL need to make column NOT UNIQUE. Error Can't DROP 'ColumnName'; check that column/key exists

ColumnName is Unique (UNIQUE KEY ColumnName).
I just want to make column not unique (must be very simple, but can not understand how).
If in phpMyAdmin check at column name and at bottom click on Unique icon, get #1062 - Duplicate entry '' for key 'RegistrationNumber'. OK, see it is because, clicking on icon it ADD UNIQUE.
There is Unique icon in Structure within row. But the icon is not click-able.
As in phpMyAdmin did not found how to do it, trying with query.
Based on advices tried ALTER TABLE TableName DROP INDEX ColumnName.
Get 1091 Can't DROP 'ColumnName'; check that column/key exists
Here https://stackoverflow.com/a/4414694/2465936 found This error means that you are trying to delete a key which is being used by another table. Possibly the ColumnName is used by another table.
Please advice what need to do to make column not unique.
With SHOW CREATE TABLE get
Array
(
[0] => Array
(
[Table] => 18_6_TransactionPartners
[Create Table] => CREATE TABLE `18_6_TransactionPartners` (
`Number` int(11) NOT NULL AUTO_INCREMENT,
`CompanyName` char(255) COLLATE utf8_unicode_ci NOT NULL,
`RegistrationNumber` char(255) COLLATE utf8_unicode_ci NOT NULL,
.......
PRIMARY KEY (`Number`),
UNIQUE KEY `Number_2` (`Number`),
UNIQUE KEY `CompanyName` (`CompanyName`,`RegistrationNumber`),
KEY `Number` (`Number`)
) ENGINE=InnoDB AUTO_INCREMENT=444 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
)
)
Update
Based on #Bart Friederichs advice tried ALTER TABLE 18_6_TransactionPartners DROP INDEX Number and changed column RegistrationNumber not not unique. Do not understand why (possibly had some mess with unique keys). In any case can change to not unique.
Probably you have a named INDEX. By using SHOW CREATE TABLE tbl you can find out the names of the indices. Then drop them by name (e.g. some test table):
mysql> SHOW CREATE TABLE test;
CREATE TABLE `test` (
`entry_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
UNIQUE KEY `k` (`entry_id`)
)
To drop the index, use this:
ALTER TABLE test DROP INDEX k;
Your key name is RegistrationNumber (as is told by the error message):
ALTER TABLE TableName DROP INDEX RegistrationNumber;
If your column was defined unique using UNIQUE clause, then you can do something like this:
ALTER TABLE mytable DROP INDEX constraint_name
For dropping the index do this:-
ALTER TABLE mytable DROP INDEX index_name;
You have to drop the index using the index name, not the column name.

How to add AUTO_INCREMENT to an existing column?

How do I add auto_increment to an existing column of a MySQL table?
I think you want to MODIFY the column as described for the ALTER TABLE command. It might be something like this:
ALTER TABLE users MODIFY id INTEGER NOT NULL AUTO_INCREMENT;
Before running above ensure that id column has a Primary index.
Method to add AUTO_INCREMENT to a table with data while avoiding “Duplicate entry” error:
Make a copy of the table with the data using INSERT SELECT:
CREATE TABLE backupTable LIKE originalTable;
INSERT backupTable SELECT * FROM originalTable;
Delete data from originalTable (to remove duplicate entries):
TRUNCATE TABLE originalTable;
To add AUTO_INCREMENT and PRIMARY KEY
ALTER TABLE originalTable ADD id INT PRIMARY KEY AUTO_INCREMENT;
Copy data back to originalTable (do not include the newly created column (id), since it will be automatically populated)
INSERT originalTable (col1, col2, col3)
SELECT col1, col2,col3
FROM backupTable;
Delete backupTable:
DROP TABLE backupTable;
More on the duplication of tables using CREATE LIKE:
Duplicating a MySQL table, indices, and data
Alter table table_name modify column_name datatype(length) AUTO_INCREMENT PRIMARY KEY
You should add primary key to auto increment, otherwise you got error in mysql.
Simply just add auto_increment Constraint In column or MODIFY COLUMN :-
ALTER TABLE `emp` MODIFY COLUMN `id` INT NOT NULL UNIQUE AUTO_INCREMENT FIRST;
Or add a column first then change column as -
1. Alter TABLE `emp` ADD COLUMN `id`;
2. ALTER TABLE `emp` CHANGE COLUMN `id` `Emp_id` INT NOT NULL UNIQUE AUTO_INCREMENT FIRST;
This worked for me in case you want to change the AUTO_INCREMENT-attribute for a not-empty-table:
1.)Exported the whole table as .sql file
2.)Deleted the table after export
2.)Did needed change in CREATE_TABLE command
3.)Executed the CREATE_TABLE and INSERT_INTO commands from the .sql-file
...et viola
I managed to do this with the following code:
ALTER TABLE `table_name`
CHANGE COLUMN `colum_name` `colum_name` INT(11) NOT NULL AUTO_INCREMENT FIRST;
This is the only way I could make a column auto increment.
INT(11) shows that the maximum int length is 11, you can skip it if you want.
Alter table table_name modify table_name.column_name data_type AUTO_INCREMENT;
eg:
Alter table avion modify avion.av int AUTO_INCREMENT;
if you have FK constraints and you don't want to remove the constraint from the table. use "index" instead of primary. then you will be able to alter it's type to auto increment
I had existing data in the first column and they were 0's.
First I made the first column nullable.
Then I set the data for the column to null.
Then I set the column as an index.
Then I made it a primary key with auto incrementing turned on. This is where I used another persons answer above:
ALTER TABLE `table_name` CHANGE COLUMN `colum_name` `colum_name` INT(11) NOT NULL AUTO_INCREMENT FIRST;
This Added numbers to all the rows of this table starting at one. If I ran the above code first it wasn't working because all the values were 0's. And making it an index was also required before making it auto incrementing.
Next I made the column a primary key.
This worked in my case , if you want to change the column attribute to auto-increment which is already having some data
1.GO to structure, select the column to want to change.
2.After selecting the column , choose primary key from the options below.
[1]: https://i.stack.imgur.com/r7w8f.png
3.Then change the column attribute to auto-increment using alter method
This is to alter the column adding PRIMARY key:
ALTER TABLE `schema_name`.`table_name`
CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT ,
ADD UNIQUE INDEX `id_UNIQUE` (`id` ASC) VISIBLE,
ADD PRIMARY KEY (`id`);
I copied it from MySQL Workbench... I got curious to see if it was possible to do it all in one command. I'm a little rusty in SQL.
If you are working in an specific schema, you don't need to specify it.
The above statement will create the index, set the column as the PRIMARY KEY as well with just one query.
KEEP IN MIND: There could not be duplicated values in the same column, if there are, the statement will fail to commit.
ALTER TABLE Table name ADD column datatype AUTO_INCREMENT,ADD primary key(column);