Take the 2-minute tour ×

I asked this question a couple of weeks ago. Between now and then, I've moved to MySQL, and the solution has sadly not transferred over.

Instead of making the comp_owner_id, comp_name pair unique, those columns have simply both been marked as needing to be unique. What is the correct way of going about this?

I'm using MySQL 5.1 with InnoDB tables.

share|improve this question
Please put the code you are trying to use in MySQL, in this question. So we don't have to travel through links and guess which code combination (question, answers) you are using or what changes you have made. –  ypercube Sep 28 '12 at 5:57
My apologies. I will remember this when asking questions in the future. –  Matthew G Sep 28 '12 at 6:15

2 Answers 2

up vote 1 down vote accepted

If you create a unique index on those columns that should create a constraint that the combination of columns is unique. See http://dev.mysql.com/doc/refman/5.1/en/create-index.html

Something like

CREATE UNIQUE INDEX <index_name> ON <table_name> (COMP_OWNER_ID, COMP_NAME);

where you supply the index_name and table_name.

share|improve this answer

Could you paste your table definition here ? I think you should define primarykey with two columns like so PRIMARYKEY(col1,col2) ?

This is from MySQL doc:

A PRIMARY KEY can be a multiple-column index. However, you cannot create a multiple-column index using the PRIMARY KEY key attribute in a column specification. Doing so only marks that single column as primary. You must use a separate PRIMARY KEY(index_col_name, ...) clause.

share|improve this answer

Your Answer


