0

I have the following table:

CREATE TABLE IF NOT EXISTS `customer_list` (
   `id` INT AUTO_INCREMENT,
   `first_name` char(4) NOT NULL,
   `last_name` varchar(80) NOT NULL,
   `phone` varchar(50) NOT NULL,
   `province` varchar(50) NOT NULL,
   `country` varchar(30) NOT NULL,
   `start_date` TIMESTAMP NOT NULL,
   `end_date` TIMESTAMP NOT NULL,
   PRIMARY KEY (id)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

I want to be able to insert into this table with the only restriction being that first_name, last_name and phone CANNOT be the same. If they are the same I want some sort of error returned to warn the end user that the record already exists - No insert/update/replace action is performed.

The key here is the INSERT statement must somehow check 3 fields for duplication. The error must only return if ALL 3 fields are duplicates. IE. 1 or 2 out of the 3 are allowed to be duplicates and still be entered.

Is this possible with one INSERT statement?

1

1 Answer 1

1

Try:

alter table customer_list add unique index(first_name, last_name, phone);
Sign up to request clarification or add additional context in comments.

1 Comment

Thats perfect! Thank you. Unique Indexing is new to me but I fully understand it. There is no checking I need to do in the INSERT. By making those columns unique the general INSERT statement does it all and I just catch the errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.