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;
Related
I'm using the following statement ALTER TABLE my_tbl ADD PRIMARY KEY (id ); to add a primary key to an existing MySQL table. In reply I'm getting the error:
Error 156 : Table 'db_name.my_tbl#1' already exists.
I checked and the table has no duplicate id entries, and if I do something like DROP TABLE my_tbl#1 then the original table (my_tbl) is deleted. It's perhaps interesting to note that my_tbl was created by Create Table my_tbl SELECT id, ... FROM tmp_tbl (where tmp_tbl is a temporary table).
Anyone has an idea what's going on here?
Update: there seems to be some kind of an orphaned table situation here. I tried the suggestions in the answers below, but in my case they did not resolve the problem. I finally used a workaround: I created a table with a different name (e.g. my_tbl_new) , copied the information to this table and added to it the primary key. I Then deleted the original table and renamed the new one back to my_tbl.
try something like this:-
ALTER TABLE my_tbl DROP PRIMARY KEY, ADD PRIMARY KEY(id,id);
or try this:-
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
AND TABLE_NAME = '[my_tbl]'
AND TABLE_SCHEMA ='dbo' )
BEGIN
ALTER TABLE [dbo].[my_tbl] ADD CONSTRAINT [PK_ID] PRIMARY KEY CLUSTERED ([ID])
END
or try to flush the table like this:-
DROP TABLE IF EXISTS `my_tbl` ;
FLUSH TABLES `my_tbl` ;
CREATE TABLE `my_tbl` ...
DROP TABLE IF EXISTS `mytable` ;
FLUSH TABLES `mytable` ;
CREATE TABLE `mytable` ...
Also it might be a permission issue.
I had the same problem while trying to alter indexes, through SQLyog, when my database name contained "-" chars. So I renamed the database to not have them and then it worked just fine.
(Since there's no direct way to rename a DB, I had to copy it to a new one, with correct name)
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;
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;
I want to rename "InputOutputConfigurationServerAccountId" to "CompositeKey". How do I do this via SQL?
Part of my table definition:
UNIQUE KEY `InputOutputConfigurationServerAccountId` (`InputOutputConfigurationServerAccountId`,`Identifier`,`TimeStampReceived`)
The table is already in production. I am trying to alter the table.
Yep Femi is right. it would be done like this:
ALTER TABLE `test`.`UniqueKeys`
DROP INDEX `InputOutputConfigurationServerAccountId`,
ADD UNIQUE INDEX `CompositeKey` (`InputOutputConfigurationServerAccountId`,
`Identifier`,`TimeStampReceived`) ;
There is currently no support in MySQL's ALTER syntax to rename a key. You will have to create a new composite key and drop the old one.
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.