Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql constraint

I cannot seem to get this right, I am trying to modify a field to be a foreign key, with cascading delete... what am i doing wrong?

ALTER TABLE my_table  ADD CONSTRAINT $4  FOREIGN KEY my_field  REFERENCES my_foreign_table  ON DELETE CASCADE; 
like image 221
Ryan Avatar asked Jun 09 '10 11:06

Ryan


People also ask

What is a constraint in PostgreSQL?

Constraints are the rules enforced on data columns on table. These are used to prevent invalid data from being entered into the database. This ensures the accuracy and reliability of the data in the database. Constraints could be column level or table level.

How do I list constraints in PostgreSQL?

To find the name of a constraint in PostgreSQL, use the view pg_constraint in the pg_catalog schema. Join the view pg_catalog. pg_constraint with the view pg_class ( JOIN pg_class t ON t. oid = c.

How do I find unique constraints in PostgreSQL?

SELECT conname FROM pg_constraint WHERE conrelid = (SELECT oid FROM pg_class WHERE relname LIKE 'tableName'); Also you can get it from pgAdmin in objects tree.


1 Answers

It would help if you posted the error message. But I think you are just missing the parenthesis:

ALTER TABLE my_table  ADD CONSTRAINT my_fk  FOREIGN KEY (my_field)  REFERENCES my_foreign_table  ON DELETE CASCADE; 
like image 177
Magnus Hagander Avatar answered Sep 25 '22 08:09

Magnus Hagander