PHPMyAdmin: How to specify UNIQUE constraint for multiple columns in a table


Say you are creating a table called products. The products table is expected to hold info like productID, name, price, qty, date. Suppose you want to set up your tabe such that name and price of each product is UNIQUE. (Meaning you want the name and the price of each product to be different from the name and price of every other product).  

In PHPMyAdmin, when you are creating the tables, this is how you do it:  

Step 1:  

Go to the indexes tab (or table heading) of each column you want to be one of the combined unique columns, and select the UNIQUE dropdown option:

After clicking Go to confirm on the first column you are setting as unique, the second will promt you a window that asks you to make the UNIQUE index to be composite with other fields. This ensure that the fields that are in composite UNIQUEness are to both be unique for all records.

ez_ad

 

 

Next...

Now click on Create composite index to select it, select the other column you'd also want to make unique, confirm the associated fields of the UNIQUE index...

Now click save to created the table with the changes...

Now you have a products table with name and price combination that make for a unique combination in the table.

Hope this helps!

vli_ad


Comments