🧚 Atenção! Beekeeper Studio é uma GUI de banco de dados rápida, moderna e de código aberto Download
February 4, 2023 Por Matthew Rathbone

A “cascade” in the context of a database refers to the behavior of certain operations when they are performed on a parent record. In particular, when a parent record is deleted, any child records associated with it may also be deleted in a cascading manner. This can be useful in maintaining the integrity of the database, as it ensures that orphaned child records are not left behind when their parent is deleted.

Set with ON DELETE

In the case of Postgres, the “delete cascade” behavior is controlled by the “on delete” clause, which can be specified when a foreign key constraint is defined for a table. For example, if table A has a foreign key constraint that references table B, we can specify the “on delete cascade” behavior for that constraint like this:

ALTER TABLE A
ADD FOREIGN KEY (col_a) REFERENCES B (col_b)
ON DELETE CASCADE;

This will ensure that whenever a record in table B is deleted, any corresponding records in table A will also be deleted automatically. 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.

Use this power carefully

Cascading deletes make it easy to accidentally delete a whole lot of data by mistake when you only intended to delete a single record.

To avoid this situation, it is a good idea to use the “delete cascade” behavior only when it is strictly necessary. 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.

DELETE CASCADE Summary

In summary, the “delete cascade” behavior in Postgres allows for the automatic deletion of child records when their parent is deleted. This can be useful for maintaining the integrity of the database, but it should be used carefully to avoid unintended consequences.

Beekeeper Studio É Uma GUI de Banco de Dados Gratuita e de Código Aberto

A melhor ferramenta de consultas SQL e editor que já usei. Fornece tudo que preciso para gerenciar meu banco de dados. - ⭐⭐⭐⭐⭐ Mit

Beekeeper Studio é rápido, intuitivo e fácil de usar. Beekeeper suporta muitos bancos de dados e funciona muito bem no Windows, Mac e Linux.

A versão Linux do Beekeeper é 100% completa, sem cortes e sem compromissos de recursos.

O Que Os Usuários Dizem Sobre o Beekeeper Studio

★★★★★
"O Beekeeper Studio substituiu completamente meu antigo fluxo de trabalho com SQL. É rápido, intuitivo e torna o trabalho com banco de dados agradável novamente."
— Alex K., Desenvolvedor de Banco de Dados
★★★★★
"Já experimentei muitas GUIs de banco de dados, mas o Beekeeper encontra o equilíbrio perfeito entre recursos e simplicidade. Simplesmente funciona."
— Sarah M., Engenheira Full Stack

Pronto para Melhorar seu Fluxo de Trabalho com SQL?

download Download Gratuito