postgresql_16:3.2.6.4 Removing a Constraint
PostgreSQL 16 > The SQL Language > Data Definition > Modifying Tables > Removing a Constraint
To remove a constraint you need to know its name. If you gave it a name then that's easy. Otherwise the system assigned a generated name, which you need to find out. The psql command \d *tablename* can be helpful here; other interfaces might also provide a way to inspect table details. Then the command is: ALTER TABLE products DROP CONSTRAINT some_name;
As with dropping a column, you need to add CASCADE if you want to drop a constraint that something else depends on. An example is that a foreign key constraint depends on a unique or primary key constraint on the referenced column(s).
This works the same for all constraint types except not-null constraints. To drop a not null constraint use: ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL; (Recall that not-null constraints do not have names.)