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.
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.
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`);
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;
Create a unique index on the combined fields:
ALTER `mytable` ADD UNIQUE (`first_name`,`last_name`);
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)
);
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` )