You are on page 1of 2

MySQL - Correct way to use the UNIQUE KEY?

- Stack Overflow

Welcome to Q&A for professional and enthusiast programmers check out the FAQ!
log in | careers | dev days | chat | meta | about | faq
search

Questions

Tags

Users

Badges

Unanswered

Ask Question

MySQL - Correct way to use the UNIQUE KEY?


Hello World!
This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required. about faq tagged
mysql

Is the proper way to use the UNIQUE KEY in my MySQL table?

MySQL data. id 1 2 3 pid 2 3 3 aid 3 2 4

60230

MySQL table. CREATE TABLE ab ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, pid INT UNSIGNED NOT NULL DEFAULT 0, aid INT UNSIGNED NOT NULL DEFAULT 0, PRIMARY KEY (id), UNIQUE KEY (pid, aid) );
mysql

asked 10 months ago viewed 67 times active 10 months ago

link | improve this question

asked Oct 30 '10 at 19:11 slang 18 2

That depends on what you need it to do, which you haven't provided any detail about. OMG Ponies Oct 30 '10 at 19:13 @OMG Ponies what do you mean? slang Oct 30 '10 at 19:14 Elaborate please? Unique key would ensure that the values inserted are always unique (no duplicates are allowed). Pavanred Oct 30 '10 at 19:16 So my UNIQUE Key is wrong for my data? slang Oct 30 '10 at 19:17 Presentation Layer (HTML5/JQUERY) Architect for Innovative CREATETHE GROUP New York, NY; United States Sr. Software Engineer (Ruby on Rails) Sharethrough San Francisco, CA view more jobs

feedback

1 Answer

active

oldest

votes

Related

The unique index you have created is a valid index. It creates an index which allows duplicate values in pid and also in aid but does not allow duplicates of any pair (pid, aid) . For example, this insert would fail if your table already contains the data from your example because it conflicts with the second row: INSERT INTO ab (pid, aid) VALUES (3, 2)
link | improve this answer answered Oct 30 '10 at 19:17 Mark Byers

MySQL ON DUPLICATE KEY UPDATE with nullable column in unique key Can't create mysql table with foreign key Mysql foreign key by non unique key how is that possible? mysql unique key and index

http://stackoverflow.com/questions/4060251/mysql-correct-way-to-use-the-unique-key[8/28/2011 4:41:31 AM]

MySQL - Correct way to use the UNIQUE KEY? - Stack Overflow

134k 15 203 378


if i have the number 3 for both pid and aid is that valid? slang Oct 30 '10 at 19:19 @sland: Yes that is valid. Mark Byers Oct 30 '10 at 19:20 can I have both aid and pid with the same values only once on the same row to be clear. slang Oct 30 '10 at 19:22 @slang: Yes, you can try INSERT INTO ab (pid, aid) VALUES (3, 3) . This will work the first time - but if you try it twice it will fail the second time. Mark Byers Oct 30 '10 at 19:24 Thanks for the help Mark Byers slang Oct 30 '10 at 19:25

Best way to have unique key over 500M varchar(255) records in mysql/innodb? MySQL 'UPDATE ON DUPLICATE KEY' without a unique column? MySQL - Cannot insert NULL value in column, but I have a default value specified? MySQL query problem MySQL query design question MySQL table structure question MySQL table structure question MySQL syntax Error; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 4 MYSQL - how to a second unique string key?

feedback

Your Answer

MySQL UPDATE conflicts with unique key MySQL unique behavior on NULL columns mysql error 1451 mysql unique key MySQL Enum correct use? what datatype to use for MySQL unique key/index What is the correct way to print out multiple data using MYSQL? Is There a Unique Record Key for Mysql? mySQL Using on Duplicate Key on index that is not unique why Mysql is giving me error 1280 "Wrong Index"

Name Email
or

Duplicate unique key error after rollback in mysql how to drop unique key on foreign key in mysql?

log in

Home Page

Post Your Answer

Not the answer you're looking for? Browse other questions tagged mysql or ask your own question.

question feed

about | faq | blog | chat | data | podcast | shop | legal | advertising info | mobile | contact us | feedback

stackoverflow.com api/apps careers serverfault.com superuser.com meta area 51 webapps gaming ubuntu webmasters cooking game development math photography stats tex english theoretical cs programmers unix apple wordpress physics home improvement gis electronics android security bicycles dba
site design / logo 2011 stack exchange inc; user contributions licensed under cc-wiki with attribution required

rev 2011.8.27.1

http://stackoverflow.com/questions/4060251/mysql-correct-way-to-use-the-unique-key[8/28/2011 4:41:31 AM]