In this exercise, you will have to update and
delete data. Make sure to select the affected
data upfront to only affect the desired records afterwards.
Copy all of your SQL statements for this exercise to a text editor and save
the file as references_1.sql.
Make screenshots of the outcomes of all select queries.
Add the following data to your database. Why should you start with the departments table?
| departments | |
|---|---|
| department_id | name |
| 1 | engineering |
| 2 | marketing |
| 3 | sales |
Now enter data to your employees table.
| employees | |||
|---|---|---|---|
| employee_id | first_name | last_name | department_id |
| 1 | Amelie | 1 | |
| 2 | Susanna | 1 | |
| 3 | Pat | 2 | |
| 4 | Chris | 3 |
domestic sales and
international sales.sales department. What happens? Why?Chris to be in the new
domestic sales department. Afterwards, try to remove the
former sales department again. This time it should
succeed. Why?marketing department.
Why does this fail?
To fix this, we'll replace the existing foreign key by first removing
it and then adding a new one that will delete the linked data when
a department is deleted.show create table employees;
Remove the foreign key according to the documentation for
alter
table.
restrict. After dropping it, add a new foreign key
between the tables that will cascade the delete operation to affected
employees.
This time, give the foreign key constraint the name
fk_employees_departments to make it easier to work with
it later on.
marketing department
again. Afterwards, use select to show all departments to
check if the deletion was successful. Then show all employees to see
what happend.department called R&D.NULL in their department_id
column.