tags:

views:

77

answers:

5

Suppose I have 2 fields: first_name and last_name.

It is ok to have first name duplicate, or last name duplicate, but, if both first_name and last_name are the same, do not insert! It's like 2 unique keys, linked.

+1  A: 

If the table already exists, then you can alter it to add a composite unique key:

ALTER `table_name` ADD UNIQUE (`first_name`,`last_name`);
Dominic Rodger
+1  A: 

All you need is an index which combines both fields. Here's a sample table definition:

CREATE TABLE IF NOT EXISTS `people` (
  `first` varchar(30) NOT NULL,
  `last` varchar(30) NOT NULL,
  UNIQUE KEY `myIndex` (`first`,`last`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Anax
+2  A: 

Create a unique index on the combined fields:

ALTER `mytable` ADD UNIQUE (`first_name`,`last_name`);
Martin
wat is "user"? sd
TIMEX
+1  A: 

You can do that by adding a unique constraint:

CREATE TABLE People
(
  First_Name VARCHAR(32),
  Last_Name VARCHAR(32),
  UNIQUE (First_name, Last_Name)
);
Andre Miller
A: 

You just have to add a unique index composed with these two fields to your table:

ALTER TABLE `your_table` ADD UNIQUE `uniq_name` ( `First_name` , `Last_name` )
Erlock