Mysql Insert Records - Exercises, Practice, Solution: Accenture Batch 2 Laboratory
Mysql Insert Records - Exercises, Practice, Solution: Accenture Batch 2 Laboratory
6. Write a SQL statement insert rows from country_new table to countries table.
Page | 2 Here is the rows for country_new table. Assume that, the countries table is
empty.
+------------+--------------+-----------+
| COUNTRY_ID | COUNTRY_NAME | REGION_ID |
+------------+--------------+-----------+
| C0001 | India | 1001 |
| C0002 | USA | 1007 |
| C0003 | UK | 1003 |
+------------+--------------+-----------+
7. Write a SQL statement to insert one row in jobs table to ensure that no
duplicate value will be entered in the job_id column.
8. Write a SQL statement to insert one row in jobs table to ensure that no
duplicate value will be entered in the job_id column.
9. Write a SQL statement to insert a record into the table countries to ensure
that, a country_id and region_id combination will be entered once in the table.
10. Write a SQL statement to insert rows into the table countries in which the
value of country_id column will be unique and auto incremented.
11. Write a SQL statement to insert records into the table countries to ensure that
the country_id column will not contain any duplicate data and this will be
automatically incremented and the column country_name will be filled up by 'N/A'
if no value assigned for that column.
12. Write a SQL statement to insert rows in the job_history table in which one
column job_id is containing those values which are exists in job_id column of
jobs table.
13. Write a SQL statement to insert rows into the table employees in which a set
of columns department_id and manager_id contains a unique value and that
combined values must have exists into the table departments.
ACCENTURE BATCH 2 LABORATORY
14. Write a 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 exists
into the table departments and jobs.
Page | 3
SOLUTIONS
1. Write a SQL statement to insert a record with your own value into the table
countries against each columns.
Page | 4 Here in the following is the structure of the table countries.
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| COUNTRY_ID | varchar(2) | YES | | NULL | |
| COUNTRY_NAME | varchar(40) | YES | | NULL | |
| REGION_ID | decimal(10,0) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
Sample Solution:
INSERT INTO countries VALUES('C1','India',1001);
Copy
Let execute the above code in MySQL 5.6 command prompt.
2. Write a SQL statement to insert one row into the table countries against the
column country_id and country_name.
Copy
Let execute the above code in MySQL 5.6 command prompt.
Page | 5 Here is the structure of the table:
Copy
Let execute the above code in MySQL 5.6 command prompt.
+--------------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
4. Write a SQL statement to insert NULL values against region_id column for a
row of countries table.
Sample Solution:
INSERT INTO countries (country_id,country_name,region_id)
VALUES('C1','India',NULL);
Copy
Let execute the above code in MySQL 5.6 command prompt.
Sample Solution:
INSERT INTO countries VALUES('C0001','India',1001),
('C0002','USA',1007),('C0003','UK',1003);
Copy
Let execute the above code in MySQL 5.6 command prompt.
ACCENTURE BATCH 2 LABORATORY
6. Write a SQL statement insert rows from country_new table to countries table.
Here is the rows for country_new table. Assume that, the countries table is
empty.
+------------+--------------+-----------+
| COUNTRY_ID | COUNTRY_NAME | REGION_ID |
+------------+--------------+-----------+
| C0001 | India | 1001 |
| C0002 | USA | 1007 |
| C0003 | UK | 1003 |
+------------+--------------+-----------+
Sample Solution:
INSERT INTO countries
Copy
Let execute the above code in MySQL 5.6 command prompt.
7. Write a SQL statement to insert one row in jobs table to ensure that no
duplicate value will be entered in the job_id column.
Create the table jobs.
Page | 8 CREATE TABLE IF NOT EXISTS jobs (
JOB_ID integer NOT NULL UNIQUE ,
JOB_TITLE varchar(35) NOT NULL,
MIN_SALARY decimal(6,0)
);
Copy
Let execute the above code in MySQL 5.6 command prompt.
mysql> INSERT INTO jobs VALUES(1001,'OFFICER',8000);
ERROR 1062 (23000): Duplicate entry '1001' for key 'JOB_ID'
8. Write a SQL statement to insert one row in jobs table to ensure that no
duplicate value will be entered in the job_id column.
Create the table jobs.
+--------+-----------+------------+
| 1001 | OFFICER | 8000 |
+--------+-----------+------------+
Sample Solution:
Page | 9 INSERT INTO jobs VALUES(1001,'OFFICER',8000);
Copy
Let execute the above code in MySQL 5.6 command prompt.
mysql> INSERT INTO jobs VALUES(1001,'OFFICER',8000);
ERROR 1062 (23000): Duplicate entry '1001' for key 'PRIMARY'
10. Write a SQL statement to insert rows into the table countries in which the
value of country_id column will be unique and auto incremented.
Create the table countries.
Copy
Let execute the above code in MySQL 5.6 command prompt.
Copy
Let execute the above code in MySQL 5.6 command prompt.
ACCENTURE BATCH 2 LABORATORY
11. Write a SQL statement to insert records into the table countries to ensure that
the country_id column will not contain any duplicate data and this will be
automatically incremented and the column country_name will be filled up by 'N/A'
if no value assigned for that column.
Create the table countries.
Copy
Let execute the above code in MySQL 5.6 command prompt.
Copy
Let execute the above code in MySQL 5.6 command prompt.
ACCENTURE BATCH 2 LABORATORY
Copy
Let execute the above code in MySQL 5.6 command prompt.
12. Write a SQL statement to insert rows in the job_history table in which one
column job_id is containing those values which are exists in job_id column of
jobs table.
Sample table jobs.
+--------+-----------+------------+------------+
ACCENTURE BATCH 2 LABORATORY
Copy
Let execute the above code in MySQL 5.6 command prompt.
Copy
Let execute the above code in MySQL 5.6 command prompt.
mysql> INSERT INTO job_history VALUES(502,1003,80);
ERROR 1452 (23000): Cannot add or update a child row: a foreign
key constraint fails (`hrr`.`job_history`, CONSTRAINT
`job_history_ibfk_1`
(`JOB_ID`) REFERENCES `jobs` (`JOB_ID`))
ACCENTURE BATCH 2 LABORATORY
Here in the above, the value against job_id is 1003 which is not exists in the
job_id column of the jobs(parent table) table and that is why the child table
job_history can not contain the value of job_id as specified. Here the primary key
- foreign key relationship is violating and shows the above message.
Page | 13
13. Write a SQL statement to insert rows into the table employees in which a set
of columns department_id and manager_id contains a unique value and that
combined values must have exists into the table departments.
Sample table departments.
Sample Solution:
INSERT INTO employees VALUES(510,'Alex','Hanes','CLERK',18000,201,60);
Copy
Let execute the above code in MySQL 5.6 command prompt.
Copy
Let execute the above code in MySQL 5.6 command prompt.
mysql> INSERT INTO employees
VALUES(512,'Kim','Leon','CLERK',18000,80,211);
ACCENTURE BATCH 2 LABORATORY
14. Write a SQL statement to insert rows into the table employees in which a set
of columns department_id and manager_id contains a unique value and that
combined values must have exists into the table departments.
Sample table departments.
Sample Solution:
INSERT INTO employees VALUES(510,'Alex','Hanes',60,1001,18000);
Copy
Let execute the above code in MySQL 5.6 command prompt.
+-------------+------------+-----------+---------------+--------
+----------+
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | DEPARTMENT_ID | JOB_ID
| SALARY |
+-------------+------------+-----------+---------------+--------
Page | 17 +----------+
| 510 | Alex | Hanes | 60 | 1001
| 18000.00 |
+-------------+------------+-----------+---------------+--------
+----------+
1 row in set (0.00 sec)
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.
Copy
Let execute the above code in MySQL 5.6 command prompt.
ERROR 1452 (23000): Cannot add or update a child row: a foreign
key constraint fails (`hrr`.`employees`, CONSTRAINT
`employees_ibfk_2` FORE
OB_ID`) REFERENCES `jobs` (`JOB_ID`))
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.
Copy
ERROR 1452 (23000): Cannot add or update a child row: a foreign
key constraint fails (`hrr`.`employees`, CONSTRAINT
`employees_ibfk_2` FOREIGN KEY (`J
OB_ID`) REFERENCES `jobs` (`JOB_ID`))
ACCENTURE BATCH 2 LABORATORY
Here in the above insert statement show that, within child columns department_id
and job_id of child table employees, the job_id are successfully referencing with
the job_id of parent table jobs but department_id column are not successfully
referencing with the department_id of parent table departments, so the problem
Page | 18
have been arisen to the insertion and displayed the error message.