PostgreSQL Create Table: Create a table to allow one of the columns to contain unique values and another two columns are referenced to the columns of another two tables
16. Write a SQL statement to create a table employees including columns employee_id, first_name, last_name, job_id, salary and make sure that, the employee_id column does not contain any duplicate value at the time of insertion, and the foreign key column job_id, referenced by the column job_id of jobs table, can contain only those values which exist in the jobs table. The specialty of the statement is that the ON UPDATE CASCADE action allows you to perform the cross-table update and ON DELETE RESTRICT action rejects the deletion. The default action is ON DELETE RESTRICT.
Assume that the following is the structure of the table jobs.
Sample Solution:
Code:
Explanation:
- The CREATE TABLE IF NOT EXISTS statement creates a new table only if it does not already exist in the database.
- The table is named 'employees'.
- Each column definition specifies the column name, data type, and optional constraints.
- INTEGER indicates a whole number data type for both 'EMPLOYEE_ID' and 'JOB_ID' columns.
- The NOT NULL constraint ensures that 'EMPLOYEE_ID', 'LAST_NAME', and 'JOB_ID' columns cannot contain NULL values.
- The PRIMARY KEY constraint sets the 'EMPLOYEE_ID' column as the primary key for the table, ensuring uniqueness and providing a unique identifier for each row.
- varchar(20) indicates a variable-length character string with a maximum length of 20 characters for the 'FIRST_NAME'' column.
- varchar(25) indicates a variable-length character string with a maximum length of 25 characters for the 'LAST_NAME' column.
- decimal(8,2) indicates a decimal number with a precision of 8 digits and 2 decimal places for the 'SALARY' column.
- The DEFAULT NULL constraint specifies that if no value is provided during insertion, the 'FIRST_NAME' and 'SALARY' columns default to NULL.
- The FOREIGN KEY constraint establishes a relationship between the 'JOB_ID' column in the 'employees' table and the 'JOB_ID' column in the 'jobs' table.
- ON UPDATE CASCADE specifies that if the referenced key (in the 'jobs' table) is updated, then the corresponding key in the 'employees' table will also be updated.
- ON DELETE RESTRICT specifies that if there are any dependent rows in the 'employees' table referencing the key being deleted, the delete operation will be restricted.
Output:
postgres=# CREATE TABLE IF NOT EXISTS employees ( postgres(# EMPLOYEE_ID INTEGER NOT NULL PRIMARY KEY, postgres(# FIRST_NAME varchar(20) DEFAULT NULL, postgres(# LAST_NAME varchar(25) NOT NULL, postgres(# JOB_ID INTEGER NOT NULL, postgres(# SALARY decimal(8,2) DEFAULT NULL, postgres(# FOREIGN KEY(JOB_ID) postgres(# REFERENCES jobs(JOB_ID) postgres(# ON UPDATE CASCADE ON DELETE RESTRICT postgres(# ); CREATE TABLE
Here is the command to see the structure of the created table :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a sql statement to create a table employees, including columns employee_id, first_name, last_name, email, phone_number hire_date, job_id, salary, commission, manager_id and department_id and make sure that, the employee_id column does not contain any duplicate value at the time of insertion, and the foreign key column department_id, reference by the column department_id of departments table, can contain only those values which exist in the departments table and another foreign key column job_id, referenced by the column job_id of jobs table, can contain only those values which exist in the jobs table.
Next: Write a SQL statement to create a table employees including columns employee_id, first_name, last_name, job_id, salary and make sure that, the employee_id column does not contain any duplicate value at the time of insertion, and the foreign key column job_id, referenced by the column job_id of jobs table, can contain only those values which exist in the jobs table. The specialty of the statement is that the ON DELETE CASCADE that lets you allow to delete records in the employees(child) table that refers to a record in the jobs(parent) table when the record in the parent table is deleted and the ON UPDATE RESTRICT actions reject any updates.
What is the difficulty level of this exercise?