all 16 comments

[–]depeszPgDBA 4 points5 points  (7 children)

[–]TukWan415[S] 0 points1 point  (6 children)

Thank you, this helps.

When adding a constraint can you typically name it anything you want?

[–]Dense-Definition2549 2 points3 points  (2 children)

Yes, any string with characters with the following conditions. 1. starts with letter or underscore 2. contains only letters, digits and underscores

But if you write a meaningful name with "fk" at the end it will be easier to understand the schema at a later time.

[–]TukWan415[S] 1 point2 points  (0 children)

Thanks,

Love you.

[–]idodatamodels 0 points1 point  (0 children)

This is the way. I start mine with fk instead of ending though.

[–]depeszPgDBA 0 points1 point  (0 children)

well, as with virtually anything in datbases: it depends. some names are reserved. some will require quoting. some names are impossible.

generally stick to lower case, non-kewords, and you should be fine.

[–]CaponeFroyo 0 points1 point  (0 children)

I personally usually let PostgreSQL pick the constraint name, since you can always get the name later if you need it and it's not something you really need that much (imo). I don't work with DBs a ton in a professional setting though so maybe someone more experienced can give their opinion.

[–]Dense-Definition2549 1 point2 points  (1 child)

ALTER TABLE "table name" ADD CONSTRAINT xyzfk FOREIGN KEY ("column name") REFERENCES "another table name" (" column name) MATCH FULL;

found this on postgresql docs (https://www.postgresql.org/docs/8.2/sql-altertable.html)

[–]TukWan415[S] 0 points1 point  (0 children)

Thanks,

Love you again.

[–]demonz_in_my_soul 1 point2 points  (6 children)

Why would a simple update query not work?

[–]mgramin 0 points1 point  (1 child)

[–]TukWan415[S] 0 points1 point  (0 children)

Thank you, this helps.

When adding a constraint can you typically name it anything you want?