1

Is there any way to add a limitation rule to a table

CREATE TABLE table1
(
    id                        serial not null primary key,
    values1                   smallint[] not null DEFAULT ARRAY [12, 20],
    values2                   smallint[] not null DEFAULT ARRAY [],

    CONSTRAINT constraint_check_error CHECK (values1 NOT ( IS NULL OR values1 = '{}') AND NOT (values2 IS NULL OR values2 = '{}') )
);

to avoid the the tables values1and values2 being empty?

2 Answers 2

2

Don't need NOT ( IS NULL... in constraint, because you already have not null in your table definiton, you can do like this:

CREATE TABLE table1
(
    id                        serial not null primary key,
    values1                   smallint[] not null DEFAULT ARRAY [12, 20],
    values2                   smallint[] not null DEFAULT '{}',
    CONSTRAINT values1_check CHECK ( values1 <> '{}' ),
    CONSTRAINT values2_check CHECK ( values2 <> '{}' )
);
Sign up to request clarification or add additional context in comments.

2 Comments

I am applying this creation and it is not throwing an error!
@chatzich - Show query, from which you expect error throwing
2

You can add a check constraint:

alter table table1 add constraint check_table1_arrays
    check (cardinality(values1) > 0 and cardinality(values2) > 0);

1 Comment

Must I use alter table or can I add the check to the table creation?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.