![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
Hello, I was wondering about using the IN operator with check constraints to restrict the values of a column to a certain number of elements (strings). Is there any reasonable maximum for the number of elements in the set? Given: CREATE TABLE Languages ( iso_code CHAR(2) NOT NULL PRIMARY KEY CHECK (iso_code IN ('en', 'de', 'fr', ...)) ) I might extend the list every now and then, e.g. when supporting a new language. But where is the point the check constraint doesn't really serve any more purpose? I could remove the check and allow any combination, but I'm not sure why I should do that. Some people believe that using ENUMs, which are similar, only makes sense for a rather low number of elements, but I'm not sure what should be wrong with larger sets/lists. Any comments on that appreciated. Karsten |
#12
| |||
| |||
|
|
On Mon, 05 Apr 2010 13:22:13 -0700, Karsten Wutzke wrote: From the programming point of view, I believe it would be better to wrap it up in a Perl function. That way, the table definition never changes and check constraints would look something like this CHECK (IS_LEGIT_LANG(LANG) == 1); The function would search through a hash defined like this: my %LANG= ( 'en' => 1, 'fr' => 1, 'pt' => 1); That would probably be faster than using CHECK( LANG IN ('en','fr','pt'...)); |
|
As an added benefit you not only get a better performance but much better manageability, too. |
#13
| |||
| |||
|
|
you could more easily(*) use an SQL function. (* no need to install plperl) |
#14
| |||
| |||
|
|
On Sun, 11 Apr 2010 09:17:03 +0000, Jasen Betts wrote: you could more easily(*) use an SQL function. (* no need to install plperl) I love Perl. It's my favorite tool. I am biased. |
#15
| |||
| |||
|
|
CREATE TABLE Languages ( iso_code CHAR(2) NOT NULL PRIMARY KEY CHECK (iso_code IN ('en', 'de', 'fr', ...)) ) I might extend the list every now and then, e.g. when supporting a new language. But where is the point the check constraint doesn't really serve any more purpose? I could remove the check and allow any combination, but I'm not sure why I should do that. |
#16
| |||
| |||
|
|
Remember I posted my question into a DB/Standard SQL group on purpose. |
#17
| |||
| |||
|
|
On 7 Apr., 18:46, Andreas Kretschmer <akretsch... (AT) spamfence (DOT) net wrote: Thomas Kellerer <OTPXDAJCS... (AT) spammotel (DOT) com> wrote: Andreas Kretschmer, 07.04.2010 08:51: Karsten Wutzke<kwut... (AT) web (DOT) de> wrote: Any comments on that appreciated. Use a lookup-table instead. (FK-constraint) Andreas I was about to answer the same, until I realized his languages table *is* the lookup table and he wants to limit the values in there. That make no sense. Create a table with a CHECK-constraint and alter this constraint every time for a new record? Andreas Yes. I know it sounds contradictory at first, but if I don't use a lookup table, I have to adjust the CHECK for each new entry, too. |
|
Lookup tables are for situations where you want/need to *reuse* a check constraint, right? |
|
Using this kind of "double-check" clearly flags the table as a lookup table and not a regular one (even though the check is redundant). I mean, if it's still somewhat static, I do not change it often - if at all - anyway. It's truly another issue I'm very undecided about. |
![]() |
| Thread Tools | |
| Display Modes | |
| |