![]() ![]() This can be useful for maintaining the integrity of the database, but it should be used carefully to avoid unintended consequences. In summary, the “delete cascade” behavior in Postgres allows for the automatic deletion of child records when their parent is deleted. In many cases, it may be better to simply disallow the deletion of parent records if they have any associated child records, using a “restrict” or “no action” clause instead of a “cascade” clause. To avoid this situation, it is a good idea to use the “delete cascade” behavior only when it is strictly necessary. Use this power carefullyĬascading deletes make it easy to accidentally delete a whole lot of data by mistake when you only intended to delete a single record. Note that this behavior only applies when the parent record is deleted if a child record is deleted directly, the parent record will not be affected. This will ensure that whenever a record in table B is deleted, any corresponding records in table A will also be deleted automatically. Yum.Įxpand your database knowledge with our technical blog.ĪLTER TABLE A ADD FOREIGN KEY ( col_a ) REFERENCES B ( col_b ) ON DELETE CASCADE Learn how to use Beekeeper Studio with bite-sized articles. See a list of everything Beekeeper Studio has to offer Work across multiple devices, or share your connections and queries with others. Quickly iterate on a SQL query, view and visualize results, and share with a colleague.Ī spreadsheet like interface to view, navigate, search, and edit your data.Īn easy to use no-code interface to create and alter tables, indexes, foreign keys, and more. You should be able to work from that to make only the changes that you want in your database.Using the ON DELETE CASCADE Behavior in Postgres to Maintain Database Integrity | Beekeeper Studio Beekeeper Studio menuĮxperience a truly modern SQL editor that really sweats the details. This code will drop and create ALL FK constraints in a database. You can select from that table to get all the information you need.Ī post from John Paul Cook can be found here: This takes a bit of effort, but would result in your constraint being properly set for your case.ĮDIT 2: The information that you need is found in sys.foreign_keys.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |