What constraint can't be written as a table constraint?



  • I am reading the Postgres 7.3 https://www.postgresql.org/docs/7.3/ddl-alter.html#:~:text=To%20add%20a%20not%2Dnull,before%20it%20can%20be%20added to add a constraint to a PostgreSQL table.

    It says:

    To add a not-null constraint, which cannot be written as a table constraint, use this syntax:

    ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;

    The constraint will be checked immediately, so the table data must satisfy the constraint before it can be added.

    I don't understand what it means by "which cannot be written as a table constraint"?

    What is table constraint? What constraint doesn't check immediately?



  • That's just a question of syntax.

    A column constraint is defined like

    CREATE TABLE ... (
       col type [CONSTRAINT name] constraint_definition,
       ...
    );
    

    while a table constraint look like

    CREATE TABLE ... (
       col type,
       [CONSTRAINT name] constraint_definition,
       ...
    );
    

    The resulting constraint is the same in both cases.

    So the documentation is saying that you need to write NOT NULL in the column definition and you cannot write it anywhere else.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2