Home > Net >  How to set a limit for an array type field in postgresql
How to set a limit for an array type field in postgresql

Time:10-19

I want to define a PostgreSQL table like below:

create table contacts (
    first_name varchar,
    last_name varchar,
    phone_numbers varchar[]
);

However, I want to set a limit for phone_numbers so that the user cannot insert data more than that limit.

CodePudding user response:

Use a check constraint:

create table contacts (
  first_name varchar,
  last_name varchar,
  phone_numbers varchar[],
  constraint limit_phone_numbers
     check (cardinality(phone_numbers) <= 5)
);
  • Related