Re: Help with array constraints

Поиск
Список
Период
Сортировка
От Antti Haapala
Тема Re: Help with array constraints
Дата
Msg-id Pine.GSO.4.44.0304031758060.1729-100000@paju.oulu.fi
обсуждение исходный текст
Ответ на Help with array constraints  (Jason Hihn <jhihn@paytimepayroll.com>)
Ответы Re: Help with array constraints
Список pgsql-general
On Thu, 3 Apr 2003, Jason Hihn wrote:

> Two tables (simplified):
>
> CREATE TABLE _test (
>     id CHAR(1),
>     PRIMARY KEY(id)
> );
>
> INSERT INTO _test VALUES ('a');
> INSERT INTO _test VALUES ('b');
>
> CREATE TABLE test (
>     letter CHAR(1)[3] NOT NULL REFERENCES _test(id)
>     PRIMARY KEY(letter)
> );
>
> CREATE TABLE / PRIMARY KEY will create implicit index 'test_pkey' for table
> 'test'
> NOTICE:  CREATE TABLE will create implicit trigger(s) for FOREIGN KEY
> check(s)
> ERROR:  Unable to identify an operator '=' for types 'character[]' and
> 'character'
>         You will have to retype this query using an explicit cast
>
> Can someone please explain that in English?

There's no way to compare character string (char(1)) and character string
array (char(1)[3]) with '=' operator. PostgreSQL uses triggers to
implement foreign key constraints. Trigger creation fails because the type
of _test.id doesn't match that of test.letter.

> I want ALL the letter field values to be checked against what is in the
> _test table id field when a row is inserted. For example, 'a' and 'b' is
> in the _test table now, if I insert an 'a' or 'b' into test, it will
> suceed. If I insert a 'c' or 'd' it should fail.

Split your fixed sized array to separate table columns and make them
foreign keys and it will work.

And probably you don't want to use char(1), because it uses at least 5
bytes of storage for each single character.

--
Antti Haapala


В списке pgsql-general по дате отправления:

Предыдущее
От: Peter Eisentraut
Дата:
Сообщение: Re: unicode UTF-8 columns
Следующее
От: Richard Huxton
Дата:
Сообщение: Re: Help with array constraints