Write a postgre SQL statement to insert rows into the table employees in which a set of columns department_id and job_id contains the values which must have existed into the table departments and jobs
Write a postgre SQL statement to insert rows into the table employees in which a set of columns department_id and job_id contains the values which must have existed into the table departments and jobs.
INSERT INTO employees VALUES(510,'Alex','Hanes',60,1001,18000);
Here is the command to see the list of inserted rows :
postgres=# SELECT * FROM employees;
employee_id | first_name | last_name | department_id | job_id | salary
-------------+------------+-----------+---------------+--------+----------
510 | Alex | Hanes | 60 | 1001 | 18000.00
(1 row)
Here in the above insert statement the child column department_id and job_id of child table employees are successfully referencing with the department_id and job_id column of parent tables departments and jobs respectively, so no problem have been arisen to the insertion.
Now insert another row in the employees table.
INSERT INTO employees VALUES(511,'Tom','Elan',60,1003,22000);
Now see the output :
postgres=# INSERT INTO employees VALUES(511,'Tom','Elan',60,1003,22000);
ERROR: insert or update on table "employees" violates foreign key constraint "employees_job_id_fkey"
DETAIL: Key (job_id)=(1003) is not present in table "jobs".
Here in the above insert statement show that, within child columns department_id and job_id of child table employees, the department_id are successfully referencing with the department_id of parent table departments but job_id column are not successfully referencing with the job_id of parent table jobs, so the problem have been arisen to the insertion displayed an error message.
Now insert another row in the employees table.
INSERT INTO employees VALUES(511,'Tom','Elan',80,1001,22000);
Now see the output :
postgres=# INSERT INTO employees VALUES(511,'Tom','Elan',80,1001,22000);
ERROR: insert or update on table "employees" violates foreign key constraint "employees_department_id_fkey"
DETAIL: Key (department_id)=(80) is not present in table "departments".
Now input two rows into the table departments:
Here is the command to see the list of inserted rows :
postgres=# select * from departments; department_id | department_name | manager_id | location_id ---------------+-----------------+------------+------------- 60 | SALES | 201 | 89 61 | ACCOUNTS | 201 | 89 (2 rows)Here is another table jobs:
Now input two rows into the table departments:
Here is the command to see the list of inserted rows :
Here is another table employees :
Now insert the rows into the table employees.
Here is the command to see the list of inserted rows :
postgres=# SELECT * FROM employees; employee_id | first_name | last_name | department_id | job_id | salary -------------+------------+-----------+---------------+--------+---------- 510 | Alex | Hanes | 60 | 1001 | 18000.00 (1 row)Here in the above insert statement the child column department_id and job_id of child table employees are successfully referencing with the department_id and job_id column of parent tables departments and jobs respectively, so no problem have been arisen to the insertion.
Now insert another row in the employees table.
Now see the output :
Here in the above insert statement show that, within child columns department_id and job_id of child table employees, the department_id are successfully referencing with the department_id of parent table departments but job_id column are not successfully referencing with the job_id of parent table jobs, so the problem have been arisen to the insertion displayed an error message.
Now insert another row in the employees table.
Now see the output :
need an explanation for this answer? contact us directly to get an explanation for this answer