How can define a combination of rows as unique?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Betty_S
Forum Newbie
Posts: 20
Joined: Tue Dec 05, 2006 3:40 am

How can define a combination of rows as unique?

Post by Betty_S »

Hi

How can define a combination of rows as unique?

I'm using mysql and need to prevent duplicate row inserting. I don't want to define one column as unique but a combination of 3 columns.

How can I do that?

thanks
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Primary keys are always defined as unique. I know in most databases you can define other columns as unique.

Code: Select all

alter table [table name] add unique ([column name]);
should work.

This is taken from www.pantz.org - Handy MySQL Commands
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

CoderGoblin wrote:Primary keys are always defined as unique. I know in most databases you can define other columns as unique.

Code: Select all

alter table [table name] add unique ([column name]);
should work.

This is taken from www.pantz.org - Handy MySQL Commands
For multiple columns, it's

Code: Select all

alter table `table` add unique (`column1`, `column2`);
With phpMyAdmin it's called "Create an index on ... columns"
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

That's a fine resource CoderGoblin -- thanks!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

CoderGoblin - can you post that link in the Useful Resources thread?
Post Reply