|
|
|
UNIQUE constraint with multipule nulls
|
|||
|---|---|---|---|
|
#18+
пришлось сегодня столкнуться: "I created a unique constraint on a column called Col1 in table Test1 to enforce uniqueness with the exception of NULL. The entry for "Unique Constraint" in SQL Server Books Online (BOL) specifically says that you can create a unique constraint on columns that allow NULL. However, as soon as I add data and try to enter a second record with NULL in Col1, I receive a message that says I've violated the unique constraint. As I understand unique constraints (as opposed to unique indexes), they allow duplicate NULLS. Am I correct? BOL can be misleading on this topic. The correct answer is no: The unique constraint doesn't let you add multiple records that contain a value of NULL. Here's why I believe the reference in BOL is misleading. In the "Unique Constraint" entry, BOL says, "UNIQUE constraints can be defined on columns that allow null values, whereas PRIMARY KEY constraints can be defined only on columns that do not allow null values." In addition, the ANSI SQL-92 standard says that NULL isn't equal to NULL—in other words, the expression (1=1) evaluates to true, whereas (NULL = NULL) evaluates to false—so it seems reasonable to assume that the unique constraint would allow multiple values of NULL. However, keep in mind that you use a standard unique index to physically enforce a unique constraint. The "Create Index" section of BOL says, "Multiple NULL values are considered duplicates when a UNIQUE index is created." In general, a value of NULL isn't equal to NULL, but two different NULL values are treated as equal for the purpose of enforcing a unique index. Alternatively, you could easily use a set of UPDATE and INSERT triggers to create an index that would allow just one NULL value in the set of composite keys for the index." ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 13.11.2002, 16:12:09 |
|
||
|
UNIQUE constraint with multipule nulls
|
|||
|---|---|---|---|
|
#18+
действительно неувязочка получается видимо в БОЛ и всех книгах написано правильно, а в SQL сервере - баг! и не надо говорить что это фича бай дизайн! ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 13.11.2002, 18:06:14 |
|
||
|
UNIQUE constraint with multipule nulls
|
|||
|---|---|---|---|
|
#18+
Вот что говорит BOL по этому поводу : Note You cannot create a unique index on a single column if that column contains NULL in more than one row. Similarly, you cannot create a unique index on multiple columns if the combination of columns contains NULL in more than one row. These are treated as duplicate values for indexing purposes. С точки зрения логики это действительно странно - ведь NULL <> NULL. Тем не менее, в BOL все правильно написано. ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 13.11.2002, 18:20:58 |
|
||
|
UNIQUE constraint with multipule nulls
|
|||
|---|---|---|---|
|
#18+
Неувязочка получается в BOL. Вот выдержка из раздела Creating and Modifying UNIQUE Constraints: When a UNIQUE constraint is added to an existing column or columns in the table, Microsoft® SQL Server™ 2000 by default checks the existing data in the columns to ensure all values, except NULL, are unique. ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 13.11.2002, 19:32:32 |
|
||
|
UNIQUE constraint with multipule nulls
|
|||
|---|---|---|---|
|
#18+
какая теперь разница, что пишет BOL я просто наткнулся на это в базе, когда понадобилось создать UNIQUE constraint SQL не дался..а таблице было много NULLs завтра жду ответ от разработчиков..... ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 13.11.2002, 21:27:30 |
|
||
|
UNIQUE constraint with multipule nulls
|
|||
|---|---|---|---|
|
#18+
и , как это не прискорбно, не даст ..... если так уж нужно то вешайте триггер или обновляйте через sp... ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 13.11.2002, 21:42:12 |
|
||
|
UNIQUE constraint with multipule nulls
|
|||
|---|---|---|---|
|
#18+
триггер проблематично во всей базе нет ни одного триггира, так он разработана... т.е идеалогия такая - без триггеров мы можем внести лишь небольшие изменения скорее всего будем через sp_ ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 13.11.2002, 21:45:19 |
|
||
|
UNIQUE constraint with multipule nulls
|
|||
|---|---|---|---|
|
#18+
Где-то здесь видел я идейку, но найти сейчас не смог. Можно сделать в таблице вычисляемый столбец типа Код: plaintext 1. 2. и потом уже на этот столбец вешать unique constraint. ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 14.11.2002, 11:42:37 |
|
||
|
UNIQUE constraint with multipule nulls
|
|||
|---|---|---|---|
|
#18+
2Yossarian >ведь NULL <> NULL это сравнение тоже ложь, как и NULL = NULL. Да и в BOL всё правильно написано... ... |
|||
|
:
Нравится:
Не нравится:
|
|||
| 14.11.2002, 12:13:02 |
|
||
|
|

start [/forum/topic.php?fid=46&msg=32067721&tid=1818822]: |
0ms |
get settings: |
8ms |
get forum list: |
13ms |
check forum access: |
4ms |
check topic access: |
4ms |
track hit: |
111ms |
get topic data: |
11ms |
get forum data: |
3ms |
get page messages: |
52ms |
get tp. blocked users: |
1ms |
| others: | 228ms |
| total: | 435ms |

| 0 / 0 |
