postgresql delete cascade 1

postgresql delete cascade

CREATE TABLE employee(emp_id SERIAL PRIMARY  KEY, name VARCHAR(30),
	sector_fk INT NOT NULL);

CREATE TABLE process(emp_id SERIAL PRIMARY KEY, sector VARCHAR(20));

ALTER TABLE employee ADD FOREIGN KEY (sector_fk)
REFERENCES company(emp_id) ON DELETE CASCADE;

-- Deletes cascade employees referencing deleted companies
DELETE FROM company WHERE sector='industry';

Here is what the above code is Doing:
1. Create a table called company with a primary key called emp_id.
2. Create a table called employee with a primary key called emp_id.
3. Create a table called process with a primary key called emp_id.
4. Add a foreign key to the employee table that references the company table.
5. Delete all rows from the company table where the sector is industry.

Similar Posts