Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter a live table to make a key non-unique

I saw some other questions related to this, but they were not MySQL.

The database is a live database, so I don't want to delete and recreate the table. I simply want to make a column no longer unique, which is less permissive in nature so it shouldn't cause any problems.

like image 942
Alexander Bird Avatar asked Nov 10 '10 01:11

Alexander Bird


People also ask

How do I change the unique key on a table?

Sometimes we want to add a unique key to the column of an existing table; then, this statement is used to add the unique key for that column. Following are the syntax of the ALTER TABLE statement to add a unique key: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE(column_list);

How remove unique key from table in SQL?

The DROP CONSTRAINT command is used to delete a UNIQUE, PRIMARY KEY, FOREIGN KEY, or CHECK constraint.

How do I make a foreign key in a table already created?

First, make sure the column student_id is already added in the table students . Otherwise, create a new column student_id , because you can't add a FOREIGN KEY unless the column exists. ALTER TABLE students ADD FOREIGN KEY (student_id) REFERENCES points(id);


2 Answers

If your column was defined unique using UNIQUE clause, then use:

ALTER TABLE mytable DROP INDEX constraint_name

, or, if your constraint was implicitly named,

ALTER TABLE mytable DROP INDEX column_name

If it was defined unique using PRIMARY KEY clause, use:

ALTER TABLE mytable DROP PRIMARY KEY

Note, however, that if your table is InnoDB, dropping PRIMARY KEY will result in implicit recreation of your table and rebuilding all indexes, which will lock the table and may make it inaccessible for quite a long time.

like image 78
Quassnoi Avatar answered Oct 02 '22 07:10

Quassnoi


These are instructions for phpmyadmin app (if you are using phpMyAdmin) ::

In a some cases, the developer (you) may not want to drop it but rather just modify the "uniqueness" to "not-unique".

Steps :

  1. Go to the table in context, where you want to make the modification

  2. Click on the "Structure" tab (mostly next to Browse)

  3. Look for the "+Indexes" link, just under the columns. Yeah... now click it
  4. Now you can see all the "Indexes" and you can now click on the "DROP" button or link to modify.

Answer was found here : Source : https://forums.phpfreaks.com/topic/164827-phpmyadmin-how-to-make-not-unique/

like image 20
AppEmmanuel Avatar answered Oct 02 '22 07:10

AppEmmanuel