PostgreSQL Alter Table: Alter a table to drop the existing foreign key referenced by the primary key of another table
11. Write a SQL statement to drop the existing foreign key fk_job_id from job_history table on job_id column, which is referenced to the job_id of jobs table
Here is the structure of the table job_history.
postgres=# \d job_history
Column | Type | Modifiers
---------------+-----------------------+-----------
employee_id | numeric(6,0) |
start_date | date |
end_date | date |
job_id | character varying(10) |
department_id | numeric(4,0) |
Foreign-key constraints:
"fk_job_id" FOREIGN KEY (job_id) REFERENCES jobs(job_id) ON UPDATE RESTRICT ON DELETE CASCADE
Now execute the following statement.
Sample Solution:
Code:
ALTER TABLE job_history
DROP CONSTRAINT fk_job_id;
Output:
Now see the structure of the table job_history after being altered.
postgres=# \d job_history Column | Type | Modifiers ---------------+-----------------------+----------- employee_id | numeric(6,0) | start_date | date | end_date | date | job_id | character varying(10) | department_id | numeric(4,0) |
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a SQL statement to add a foreign key constraint named fk_job_id on job_id column of job_history table referencing to the primary key job_id of jobs table.
Next: Write a SQL statement to add an index named index_job_id on job_id column in the table job_history.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/postgresql-exercises/alter-table/alter-table-exercise-11.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics