Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

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.

like image 259
user2465936 Avatar asked Aug 09 '13 07:08

user2465936


People also ask

How do I make a column not unique in MySQL?

Drop Unique Constraint The syntax for dropping a unique constraint in MySQL is: ALTER TABLE table_name DROP INDEX constraint_name; table_name. The name of the table to modify.

How do I remove a foreign key from a column in MySQL?

Dropping Foreign Key Constraints You can drop a foreign key constraint using the following ALTER TABLE syntax: ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol; If the FOREIGN KEY clause defined a CONSTRAINT name when you created the constraint, you can refer to that name to drop the foreign key constraint.

How do I drop a column in MySQL?

The syntax to drop a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name DROP COLUMN column_name; table_name. The name of the table to modify.


2 Answers

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;
like image 119
Bart Friederichs Avatar answered Sep 19 '22 01:09

Bart Friederichs


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;
like image 41
Rahul Tripathi Avatar answered Sep 18 '22 01:09

Rahul Tripathi