2

I have a table containing a video_id field which is set to unique accept NULL values. However it will not accept two null values. Table type InnoDB or MYISAM will not work.

This produces a duplicate key error on video _id (Null value):

INSERT INTO dances (name, video_id, level) VALUES ('abc', '', 'beg');

If I read the Mysql rules correctly it should do so. Can anyone suggest where to go next?

Ian Mackinnon
  • 13,381
  • 13
  • 51
  • 67

3 Answers3

2

Yes, MySQL allows multiple NULLs in a column with a unique constraint. See this Stack Overflow question.

An empty string '' is not the same as NULL.

Use this instead:

INSERT INTO dances (name, video_id, level) VALUES ('abc', NULL, 'beg');
Community
  • 1
  • 1
Praveen Prasannan
  • 7,093
  • 10
  • 50
  • 70
1

You could try BDB (about which I know nothing); see here for more.

Brian Hooper
  • 21,544
  • 24
  • 88
  • 139
0

And why do you need multiple rows of your table to have a null video_id?

From a DB design perspective, it sounds like the object you are modelling with the table is too "big" and needs to be broken up into a main table and detail tables.

Your options are (A) remove the UNIQUE constraint, or (B) create a separate table that contains object_id, video_id pairs (where object_id is whatever the primary key is on your main table).

Paul
  • 26,170
  • 12
  • 85
  • 119
  • The table is a very simple one :- id, dance name, Video-id, dance level. The id refers to youtube video of the dance but some don't have one. In practical terms I could do away with the unique constraint but I don't understand why the Nulls produce a Duplicate Key failure. According to the Mysql info it should be ok. Thanks for the comments. – Terry Wright Jun 19 '13 at 08:13
  • @TerryWright From a data modelling perspective, each row of a table of dances describes the properties of a dance. When a dance has 0 or more of something else, that is not really a property of a dance but a "has a" relationship that often gets a separate table. Perhaps in the system you are developing there is only 0 or 1 videos per dance, and it might be desirable to keep things simple, but a data modeller might say, hey, a dance could have lots of videos, and a long video could also show several dances, possibly necessitating a table of (dance_id,video_id) pairs as described. – Paul Jun 20 '13 at 09:00