Removing unique key in mysql and retain the field - mysql

Am using a mysql database and i would like to remove the unique key that has been set on a column but it always returns an error at second line
The table tblprstatus has a field called PrStatus(unique key). I would like the Unique key removed but the field retained
I have tried
ALTER TABLE tblprstatus
DROP UNIQUE PrStatus
But this always returns an error

First use
SHOW INDEX FROM tbl_name
to find out the name of the index. The name of the index is stored in the column called key_name in the results of that query.
Then you can use DROP INDEX:
DROP INDEX index_name ON tbl_name
replace index_name with your index and tbl_name with your table
or you can see this image
http://i.stack.imgur.com/enM7Y.png

Try this:
Alter table tblprstatus DROP index PrStatus;

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

Making a unique attribute in an already existing table NOT unique - MYSQL

A non-unique attribute of a table can be made unique by the query:
ALTER TABLE mytbl ADD UNIQUE (columnName);
I need to set a already unique attribute of a table NON-unique. Can anyone help me with the query?
alter table mytbl drop index columnName;
Use the above command for the same.
Drop it, like so:
ALTER TABLE yourTable DROP INDEX ItsName;
SQL Fiddle Demo
you can drop the unique index with the following statement:
ALTER TABLE mytbl DROP INDEX columnName
First of all you want to get name of index, you can do that by SHOW INDEX IN mytbl, then you can just ALTER TABLE:
ALTER TABLE mytbl DROP INDEX auto_index_name;

How to remove unique key from mysql table

I need to remove a unique key from my mysql table. How can remove that using mysql query.
I tried this but it is not working
alter table tbl_quiz_attempt_master drop unique key;
Please help me
Thanks
All keys are named, you should use something like this -
ALTER TABLE tbl_quiz_attempt_master
DROP INDEX index_name;
To drop primary key use this one -
ALTER TABLE tbl_quiz_attempt_master
DROP INDEX `PRIMARY`;
ALTER TABLE Syntax.
First you need to know the exact name of the INDEX (Unique key in this case) to delete or update it.
INDEX names are usually same as column names. In case of more than one INDEX applied on a column, MySQL automatically suffixes numbering to the column names to create unique INDEX names. For example if 2 indexes are applied on a column named customer_id
The first index will be named as customer_id itself.
The second index will be names as customer_id_2 and so on.
To know the name of the index you want to delete or update
SHOW INDEX FROM <table_name>
as suggested by #Amr
To delete an index
ALTER TABLE <table_name> DROP INDEX <index_name>;
ALTER TABLE mytable DROP INDEX key_Name;
Here is how to get index_name which is mentioned in Devart's answer or key_name which is mentioned in Uday Sawant's answer:
SHOW INDEX FROM table_name;
This will show all the indexes for the given table_name. And you can pick the name of the index or the unique key you want to remove.
There are two method two remove index in mysql.
First method is GUI. In this method you have to open GUI interface of MYSQL and then go to that database and then go to that particular table in which you want to remove index.
After that click on the structure option, Then you can see table structure and below you can see table indexes. You can remove indexes by clicking on drop option
Second method by
ALTER TABLE student_login_credentials DROP INDEX created_at;
here student_login_credentials is table name and created_at is column name
Unique key is actually an index.
http://codeghar.wordpress.com/2008/03/28/drop-unique-constraint-in-mysql/
To remove a unique key from a column, you have to run the below query:
ALTER TABLE your_table_name
DROP INDEX tableName_columnName_keyName;
Where tableName should be your name of the table followed by an underscore then columnName should be the name of the column which you want to remove from the unique key constraint followed by an underscore and at last keyName should be the name of the key i.e unique in your case.
To add a unique key use :
alter table your_table add UNIQUE(target_column_name);
To remove a unique key use:
alter table your_table drop INDEX target_column_name;

Dropping Unique constraint from MySQL table

How can I drop the "Unique Key Constraint" on a column of a MySQL table using phpMyAdmin?
A unique constraint is also an index.
First use SHOW INDEX FROM tbl_name to find out the name of the index. The name of the index is stored in the column called key_name in the results of that query.
Then you can use DROP INDEX:
DROP INDEX index_name ON tbl_name
or the ALTER TABLE syntax:
ALTER TABLE tbl_name DROP INDEX index_name
You can DROP a unique constraint from a table using phpMyAdmin as requested as shown in the table below. A unique constraint has been placed on the Wingspan field. The name of the constraint is the same as the field name, in this instance.
The indexes capable of placing a unique key constraint on a table are PRIMARY and UNIQUE indexes.
To remove the unique key constraint on a column but keep the index, you could remove and recreate the index with type INDEX.
Note that it is a good idea for all tables to have an index marked PRIMARY.
To add UNIQUE constraint using phpmyadmin, go to the structure of that table and find below and click that,
To remove the UNIQUE constraint, same way, go to the structure and scroll down till Indexes Tab and find below and click drop,
Hope this works.
Enjoy ;)
If you want to remove unique constraints from MySQL database table, use alter table with drop index.
Example:
CREATE TABLE unique_constraints (
unid INT,
activity_name VARCHAR(100),
CONSTRAINT activty_uqniue UNIQUE (activity_name),
PRIMARY KEY (unid)
);
ALTER TABLE unique_constraints
DROP INDEX activty_uqniue;
Where activty_uqniue is UNIQUE constraint for activity_name column.
For WAMP 3.0 :
Click Structure
Below Add 1 Column you will see '- Indexes'
Click -Indexes and drop whichever index you want.
The constraint could be removed with syntax:
ALTER TABLE
As of MySQL 8.0.19, ALTER TABLE permits more general (and SQL standard) syntax for dropping and altering existing constraints of any type, where the constraint type is determined from the constraint name: ALTER TABLE tbl_name DROP CONSTRAINT symbol;
Example:
CREATE TABLE tab(id INT, CONSTRAINT unq_tab_id UNIQUE(id));
-- checking constraint name if autogenerated
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'tab';
-- dropping constraint
ALTER TABLE tab DROP CONSTRAINT unq_tab_id;
db<>fiddle demo
This might help:
Inside your sql terminal
FIRST STEP:
SHOW INDEX FROM {YOUR_TABLE_NAME}
SECOND STEP:
SHOW INDEX FROM {YOUR_TABLE_NAME} WHERE Column_name='ACTUAL_COLUMN_NAME_YOU_GOT_FROM_FIRST_STEP_OUTPUT'
THIRD STEP:
ORIGINAL_KEY_NAME_VALUE = SECOND_STEP_RESPONSE["Key_name"]
FOURTH STEP:
ALTER TABLE {YOUR_TABLE_NAME} DROP INDEX ${ORIGINAL_KEY_NAME_VALUE}
while dropping unique key we use index
ALTER TABLE tbl
DROP INDEX unique_address;
my table name is buyers which has a unique constraint column emp_id now iam going to drop the emp_id
step 1: exec sp_helpindex buyers, see the image file
step 2: copy the index address
step3: alter table buyers drop constraint [UQ__buyers__1299A860D9793F2E]
alter table buyers
drop column emp_id
note:
Blockquote
instead of buyers change it to your table name :)
Blockquote
thats all column name emp_id with constraints is dropped!

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.