

Why did these records get deleted? It happened because the distribution section was removed. The syntax to create a database is shown below:Įmp_id | name | STATUS | phone_num | process_fk Let’s create a new database with a unique name to use in our examples. A foreign key in the PostgreSQL child table is a reference to the primary key in the parent table. The parent table refers to the table to which the foreign key is related.


The table containing the foreign key is called the referencing table or child table. If you want to avoid deleting records from the child table, you need to set the foreign key value for the parent table record to NULL. In PostgreSQL, a cascade means that a delete or update of records in a parent table will automatically delete or update matching records in a child table where a foreign key relationship is in place. It’s helpful to have some basic knowledge of database management systems and SQL commands. Use the command psql -V to display the version number for psql, the interactive terminal used with PostgreSQL. You’ll need to have PostgreSQL installed and configured on your machine. Let’s review some key prerequisites that are necessary for this tutorial: In this article, we’ll discuss the PostgreSQL DELETE CASCADE and review some examples of its use. In PostgreSQL, you can use the DELETE CASCADE statement to make sure that all foreign-key references to a record are deleted when that record is deleted. Before you perform a DELETE operation, it’s important to consider any foreign key relationships between the records to be deleted and records in other tables. When you’re managing data in PostgreSQL, there will be times when you need to delete records.
