Postgres constraint ensuring one column of many is present?

前端 未结 6 1504
天命终不由人
天命终不由人 2021-02-01 03:01

What are good ways to add a constraint to PostgreSQL to check that exactly one column (from a set of columns) contains a non-null value?

Update: It is likely th

相关标签:
6条回答
  • 2021-02-01 03:34

    Here's a solution using the built-in array functions:

    ALTER TABLE your_table
    ADD chk_only_one_is_not_null CHECK (array_length(array_remove(ARRAY[col1, col2, col3], NULL), 1) = 1);
    
    0 讨论(0)
  • 2021-02-01 03:39

    A bit clumsy, but should do the trick:

    create table foo
    (
       col1 integer,
       col2 integer,
       col3 integer,
       constraint one_is_not_null check 
            (    (col1 is not null and col2 is null and col3 is null) 
              or (col1 is null and col2 is not null and col3 is null)
              or (col1 is null and col2 is null and col3 is not null)
            )
    )
    
    0 讨论(0)
  • 2021-02-01 03:45

    I think the most clean and generic solution is to create a function to count the null values from some arguments. For that you can use the pseudo-type anyarray and a SQL function like that:

    CREATE FUNCTION count_not_nulls(p_array anyarray)
    RETURNS BIGINT AS
    $$
        SELECT count(x) FROM unnest($1) AS x
    $$ LANGUAGE SQL IMMUTABLE;
    

    With that function, you can create your CHECK CONSTRAINT as:

    ALTER TABLE your_table
    ADD chk_only_one_is_not_null CHECK(count_not_nulls(array[col1, col2, col3]) = 1);
    

    This will only work if the columns are of the same data type. If it's not the case, you can cast them, as text for instance (as you just care for the null case):

    ALTER TABLE your_table
    ADD chk_only_one_is_not_null CHECK(count_not_nulls(array[col1::text, col2::text, col3::text]) = 1);
    

    As well remembered by @muistooshort, you can create the function with variadic arguments, which makes it clear to call:

    CREATE FUNCTION count_not_nulls(variadic p_array anyarray)
    RETURNS BIGINT AS
    $$
        SELECT count(x) FROM unnest($1) AS x
    $$ LANGUAGE SQL IMMUTABLE;
    
    ALTER TABLE your_table
    ADD chk_only_one_is_not_null CHECK(count_not_nulls(col1, col2, col3) = 1);
    
    0 讨论(0)
  • 2021-02-01 03:47

    As hinted by mu is too short:

    alter table t
    add constraint only_one_null check (
        (col1 is not null)::integer + (col2 is not null)::integer = 1
    )
    
    0 讨论(0)
  • 2021-02-01 03:50

    Here is an elegant two column solution according to the "constraint -- one or the other column not null" PostgreSQL message board:

    ALTER TABLE my_table ADD CONSTRAINT my_constraint CHECK (
      (column_1 IS NULL) != (column_2 IS NULL));
    

    (But the above approach is not generalizable to three or more columns.)

    If you have three or more columns, you can use the truth table approach illustrated by a_horse_with_no_name. However, I consider the following to be easier to maintain because you don't have to type out the logical combinations:

    ALTER TABLE my_table
    ADD CONSTRAINT my_constraint CHECK (
      (CASE WHEN column_1 IS NULL THEN 0 ELSE 1 END) +
      (CASE WHEN column_2 IS NULL THEN 0 ELSE 1 END) +
      (CASE WHEN column_3 IS NULL THEN 0 ELSE 1 END) = 1;
    

    To compact this, it would be useful to create a custom function so that the CASE WHEN column_k IS NULL THEN 0 ELSE 1 END boilerplate could be removed, leaving something like:

    (non_null_count(column_1) +
    non_null_count(column_2) +
    non_null_count(column_3)) = 1
    

    That may be as compact as PSQL will allow (?). That said, I'd prefer to get to this kind of syntax if possible:

    non_null_count(column_1, column_2, column_3) = 1
    
    0 讨论(0)
  • 2021-02-01 03:58

    Since PostgreSQL 9.6 you have the num_nonnulls and num_nulls comparison functions that accept any number of VARIADIC arguments.

    For example, this would make sure exactly one of the three columns is not null.

    ALTER TABLE your_table
    ADD CONSTRAINT chk_only_one_is_not_null CHECK (num_nonnulls(col1, col2, col3) = 1);
    
    0 讨论(0)
提交回复
热议问题