Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> UNIQUE dla niepustych rekordów, Unikalny indeks dla opcjonalnych danych
Pilsener
post
Post #1





Grupa: Zarejestrowani
Postów: 1 590
Pomógł: 185
Dołączył: 19.04.2006
Skąd: Gdańsk

Ostrzeżenie: (0%)
-----


Witajcie, mam (nie wiem czy nie głupi) problem - mam w bazie pole "xyz" i chcę, aby dane w nim zawarte (powiedzmy numer aneksu umowy) były unikalne - dodałem indeks UNIQUE i niby jest ok, ale co zrobić, gdy pole "xyz" nie jest wymagane i może być puste? W momemncie dodawania drugiego pustego rekordu pojawia się "duplicate entry...". Da się jakoś prosto temu zaradzić? Ustawić unikalność tylko dla niepustych?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 5)
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




puste pola ustawiaj jako NULL a nie jako pusty string
Go to the top of the page
+Quote Post
dr_bonzo
post
Post #3





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


nospor - e-e, pozwoli wrzucic tylko JEDNEGO null'a, Pilsner - BEFORE INSERT TRIGGERA dodaj ktory sprawdzi i odrzuci rekord ze zduplikowana wartoscia

edit:
eh (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) a jednak UNIQUE zadziala w mysql

Cytat
UNIQUE index creates a constraint such that all values in the index must be distinct. An error occurs if you try to add a new row with a key value that matches an existing row. This constraint does not apply to NULL values except for the BDB storage engine. For other engines, a UNIQUE index allows multiple NULL values for columns that can contain NULL.


http://dev.mysql.com/doc/refman/5.0/en/create-index.html

Ten post edytował dr_bonzo 15.09.2008, 11:18:06
Go to the top of the page
+Quote Post
nospor
post
Post #4





Grupa: Moderatorzy
Postów: 36 559
Pomógł: 6315
Dołączył: 27.12.2004




Cytat
nospor - e-e, pozwoli wrzucic tylko JEDNEGO null'a
no popatrz, tyle lat tego uzywam i jakos dziala (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post
Pilsener
post
Post #5





Grupa: Zarejestrowani
Postów: 1 590
Pomógł: 185
Dołączył: 19.04.2006
Skąd: Gdańsk

Ostrzeżenie: (0%)
-----


Cytat(nospor @ 15.09.2008, 10:07:58 ) *
...jako NULL a nie jako pusty string
- no tak, value=NULL a value='' to nie to samo przecież (IMG:http://forum.php.pl/style_emoticons/default/sciana.gif) Wszystko przez to, że musi być value='$zmienna' a nie value=$zmienna - trochę mi to nasieje zamętu w kostruktorze zapytań, ale wolę już to niż trigger (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Dzięki Nospor za pomoc!
Go to the top of the page
+Quote Post
dr_bonzo
post
Post #6





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


Z PG:

Cytat
When an index is declared unique, multiple table rows with equal indexed values will not be allowed. Null values are not considered equal

I juz latwiej zapamietac czemu tak jest (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif)
$moja_wiedza++
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 6.10.2025 - 10:46