MYSQL: How to make a combination of 2 columns unique

mysqlSetting up some rules in your MySQL database can save you some programming time. This tip will show you how to make a combination of two columns as unique. Suppose you have a database that records BookTitles and Authors. Just having the Author field unique would not make much sense as every Author can have more than one book. But you also don’t want to have an Author / BookTitle combination entered twice. So here we can make the combination of Author and BookTitle unique.

Execute this MySQL statement to make 2 colums unique:

ALTER TABLE TableName ADD UNIQUE (Column1,Column2);

Replace “TableName” with the name of your database table. “Column1″ and “Column2″ are your column names.

Hope this helps.

Happy Programming!

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge

Get Adobe Flash playerPlugin by wpburn.com wordpress themes