MySQL Create Table - Exercises, Practice, Solution
MySQL Create Table - Exercises, Practice, Solution
MySQL Create Table - Exercises, Practice, Solution
6. Write a SQL statement to create a table named jobs including columns job_id,
job_title, min_salary, max_salary and check whether the max_salary amount
exceeding the upper limit 25000.
10. Write a SQL statement to create a table named jobs including columns
job_id, job_title, min_salary and max_salary, and make sure that, the default
ACCENTURE BATCH 2 – LAB EXERCISES
value for job_title is blank and min_salary is 8000 and max_salary is NULL will
be entered automatically at the time of insertion if no value assigned for the
specified columns.
Page | 2 11. Write a SQL statement to create a table named countries including columns
country_id, country_name and region_id and make sure that the country_id
column will be a key field which will not contain any duplicate data at the time of
insertion.
columns contain only those unique combination values, which combinations are
exists in the departments table.
Page | 3
"A foreign key constraint is not required merely to join two tables. For storage
engines other than InnoDB, it is possible when defining a column to use a
REFERENCES tbl_name(col_name) clause, which has no actual effect, and
ACCENTURE BATCH 2 – LAB EXERCISES
serves only as a memo or comment to you that the column which you are
currently defining is intended to refer to a column in another table." -
Reference dev.mysql.com
Page | 4
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | varchar(10) | NO | PRI | | |
| JOB_TITLE | varchar(35) | NO | | NULL | |
| MIN_SALARY | decimal(6,0) | YES | | NULL | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
17. 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 are exists in the jobs table. The
ACCENTURE BATCH 2 – LAB EXERCISES
InnoDB Engine have been used to create the tables. The specialty of the
statement is that, The ON UPDATE CASCADE action allows you to perform
cross-table update and ON DELETE RESTRICT action reject the deletion. The
default action is ON DELETE RESTRICT.
Page | 5
Assume that the structure of the table jobs and InnoDB Engine have been used
to create the table jobs.
CREATE TABLE IF NOT EXISTS jobs (
JOB_ID integer NOT NULL UNIQUE PRIMARY KEY,
JOB_TITLE varchar(35) NOT NULL DEFAULT ' ',
MIN_SALARY decimal(6,0) DEFAULT 8000,
MAX_SALARY decimal(6,0) DEFAULT NULL
)ENGINE=InnoDB;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
| MIN_SALARY | decimal(6,0) | YES | | 8000 | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
18. 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 are exists in the jobs table. The
InnoDB Engine have been used to create the tables. The specialty of the
statement is that, The ON DELETE CASCADE that lets you allow to delete
records in the employees(child) table that refer 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.
Assume that the structure of the table jobs and InnoDB Engine have been used
to create the table jobs.
CREATE TABLE IF NOT EXISTS jobs (
JOB_ID integer NOT NULL UNIQUE PRIMARY KEY,
JOB_TITLE varchar(35) NOT NULL DEFAULT ' ',
MIN_SALARY decimal(6,0) DEFAULT 8000,
ACCENTURE BATCH 2 – LAB EXERCISES
+------------+--------------+------+-----+---------+-------+
Page | 6 | Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
| MIN_SALARY | decimal(6,0) | YES | | 8000 | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
19. 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 are exists in the jobs table. The
InnoDB Engine have been used to create the tables. The specialty of the
statement is that, The ON DELETE SET NULL action will set the foreign key
column values in the child table(employees) to NULL when the record in the
parent table(jobs) is deleted, with a condition that the foreign key column in the
child table must accept NULL values and the ON UPDATE SET NULL action
resets the values in the rows in the child table(employees) to NULL values when
the rows in the parent table(jobs) are updated.
Assume that the structure of two table jobs and InnoDB Engine have been used
to create the table jobs.
CREATE TABLE IF NOT EXISTS jobs (
JOB_ID integer NOT NULL UNIQUE PRIMARY KEY,
JOB_TITLE varchar(35) NOT NULL DEFAULT ' ',
MIN_SALARY decimal(6,0) DEFAULT 8000,
MAX_SALARY decimal(6,0) DEFAULT NULL
)ENGINE=InnoDB;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
| MIN_SALARY | decimal(6,0) | YES | | 8000 | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
ACCENTURE BATCH 2 – LAB EXERCISES
+------------+--------------+------+-----+---------+-------+
20. 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
Page | 7 insertion, and the foreign key column job_id, referenced by the column job_id of
jobs table, can contain only those values which are exists in the jobs table. The
InnoDB Engine have been used to create the tables. The specialty of the
statement is that, The ON DELETE NO ACTION and the ON UPDATE NO
ACTION actions will reject the deletion and any updates.
Assume that the structure of two table jobs and InnoDB Engine have been used
to create the table jobs.
CREATE TABLE IF NOT EXISTS jobs (
JOB_ID integer NOT NULL UNIQUE PRIMARY KEY,
JOB_TITLE varchar(35) NOT NULL DEFAULT ' ',
MIN_SALARY decimal(6,0) DEFAULT 8000,
MAX_SALARY decimal(6,0) DEFAULT NULL
)ENGINE=InnoDB;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
| MIN_SALARY | decimal(6,0) | YES | | 8000 | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
ACCENTURE BATCH 2 – LAB EXERCISES
Page | 8
SOLUTION
Sample Solution:
CREATE TABLE countries(
COUNTRY_ID varchar(2),
COUNTRY_NAME varchar(40),
REGION_ID decimal(10,0)
);
Copy
Let execute the above code in MySQL 5.6 command prompt
Sample Solution:
Page | 9
CREATE TABLE IF NOT EXISTS countries (
COUNTRY_ID varchar(2),
COUNTRY_NAME varchar(40),
REGION_ID decimal(10,0)
);
Copy
Let execute the above code in MySQL 5.6 command prompt
Sample Solution:
CREATE TABLE IF NOT EXISTS dup_countries
LIKE countries;
Copy
Let execute the above code in MySQL 5.6 command prompt
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| COUNTRY_ID | varchar(2) | YES | | NULL | |
| COUNTRY_NAME | varchar(40) | YES | | NULL | |
Page | 10 | REGION_ID | decimal(10,0) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
3 rows in set (0.03 sec)
Sample Solution:
CREATE TABLE IF NOT EXISTS dup_countries
Copy
Let execute the above code in MySQL 5.6 command prompt
Page | 11
5. Write a SQL statement to create a table countries set a constraint NOT NULL.
Sample Solution:
CREATE TABLE IF NOT EXISTS countries (
);
Copy
Let execute the above code in MySQL 5.6 command prompt
mysql>
ACCENTURE BATCH 2 – LAB EXERCISES
6. Write a SQL statement to create a table named jobs including columns job_id,
job_title, min_salary, max_salary and check whether the max_salary amount
exceeding the upper limit 25000.
MIN_SALARY decimal(6,0),
MAX_SALARY decimal(6,0)
CHECK(MAX_SALARY<=25000)
);
Copy
Let execute the above code in MySQL 5.6 command prompt
Sample Solution:
CREATE TABLE IF NOT EXISTS countries (
COUNTRY_ID varchar(2),
ACCENTURE BATCH 2 – LAB EXERCISES
COUNTRY_NAME varchar(40)
CHECK(COUNTRY_NAME IN('Italy','India','China')) ,
REGION_ID decimal(10,0)
Page | 13
);
Copy
Let execute the above code in MySQL 5.6 command prompt
Sample Solution:
CREATE TABLE IF NOT EXISTS job_history (
);
ACCENTURE BATCH 2 – LAB EXERCISES
Copy
Let execute the above code in MySQL 5.6 command prompt
Sample Solution:
CREATE TABLE IF NOT EXISTS countries (
UNIQUE(COUNTRY_ID)
);
Copy
Let execute the above code in MySQL 5.6 command prompt
+--------------+---------------+------+-----+---------+-------+
| COUNTRY_ID | varchar(2) | YES | | NULL | |
| COUNTRY_NAME | varchar(40) | YES | | NULL | |
| REGION_ID | decimal(10,0) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
Page | 15 3 rows in set (0.01 sec)
10. Write a SQL statement to create a table named jobs including columns
job_id, job_title, min_salary and max_salary, and make sure that, the default
value for job_title is blank and min_salary is 8000 and max_salary is NULL will
be entered automatically at the time of insertion if no value assigned for the
specified columns.
Sample Solution:
CREATE TABLE IF NOT EXISTS jobs (
);
Copy
Let execute the above code in MySQL 5.6 command prompt
11. Write a SQL statement to create a table named countries including columns
country_id, country_name and region_id and make sure that the country_id
column will be a key field which will not contain any duplicate data at the time of
insertion.
Page | 16
Sample Solution:
CREATE TABLE IF NOT EXISTS countries (
);
Copy
Let execute the above code in MySQL 5.6 command prompt
Sample Solution:
CREATE TABLE IF NOT EXISTS countries (
);
DESC countries;
Page | 17
Copy
Let execute the above code in MySQL 5.6 command prompt
Sample Solution:
CREATE TABLE IF NOT EXISTS countries (
Copy
Let execute the above code in MySQL 5.6 command prompt
+--------------+---------------+------+-----+---------+-------+
| COUNTRY_ID | varchar(2) | NO | PRI | | |
| COUNTRY_NAME | varchar(40) | YES | | NULL | |
| REGION_ID | decimal(10,0) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
Page | 18 3 rows in set (0.01 sec)
)ENGINE=InnoDB;
FOREIGN KEY(DEPARTMENT_ID,MANAGER_ID)
REFERENCES departments(DEPARTMENT_ID,MANAGER_ID)
)ENGINE=InnoDB;
Copy
Let execute the above code in MySQL 5.6 command prompt
"A foreign key constraint is not required merely to join two tables. For storage
engines other than InnoDB, it is possible when defining a column to use a
REFERENCES tbl_name(col_name) clause, which has no actual effect, and
serves only as a memo or comment to you that the column which you are
currently defining is intended to refer to a column in another table." -
Reference dev.mysql.com
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
ACCENTURE BATCH 2 – LAB EXERCISES
+------------+--------------+------+-----+---------+-------+
| JOB_ID | varchar(10) | NO | PRI | | |
| JOB_TITLE | varchar(35) | NO | | NULL | |
| MIN_SALARY | decimal(6,0) | YES | | NULL | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
Page | 22 +------------+--------------+------+-----+---------+-------+
Sample Solution:
CREATE TABLE IF NOT EXISTS employees (
FOREIGN KEY(DEPARTMENT_ID)
REFERENCES departments(DEPARTMENT_ID),
FOREIGN KEY(JOB_ID)
REFERENCES jobs(JOB_ID)
)ENGINE=InnoDB;
Copy
Let execute the above code in MySQL 5.6 command prompt
+----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| EMPLOYEE_ID | decimal(6,0) | NO | PRI | NULL | |
| FIRST_NAME | varchar(20) | YES | | NULL | |
Page | 23 | LAST_NAME | varchar(25) | NO | | NULL | |
| EMAIL | varchar(25) | NO | | NULL | |
| PHONE_NUMBER | varchar(20) | YES | | NULL | |
| HIRE_DATE | date | NO | | NULL | |
| JOB_ID | varchar(10) | NO | | NULL | |
| SALARY | decimal(8,2) | YES | | NULL | |
| COMMISSION_PCT | decimal(2,2) | YES | | NULL | |
| MANAGER_ID | decimal(6,0) | YES | | NULL | |
| DEPARTMENT_ID | decimal(4,0) | YES | MUL | NULL | |
+----------------+--------------+------+-----+---------+-------+
11 rows in set (0.01 sec)
17. 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 are exists in the jobs table. The
InnoDB Engine have been used to create the tables. The specialty of the
statement is that, The ON UPDATE CASCADE action allows you to perform
cross-table update and ON DELETE RESTRICT action reject the deletion. The
default action is ON DELETE RESTRICT.
Assume that the structure of the table jobs and InnoDB Engine have been used
to create the table jobs.
CREATE TABLE IF NOT EXISTS jobs (
JOB_ID integer NOT NULL UNIQUE PRIMARY KEY,
JOB_TITLE varchar(35) NOT NULL DEFAULT ' ',
MIN_SALARY decimal(6,0) DEFAULT 8000,
MAX_SALARY decimal(6,0) DEFAULT NULL
)ENGINE=InnoDB;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
| MIN_SALARY | decimal(6,0) | YES | | 8000 | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
ACCENTURE BATCH 2 – LAB EXERCISES
Sample Solution:
CREATE TABLE IF NOT EXISTS employees (
FOREIGN KEY(DEPARTMENT_ID)
REFERENCES departments(DEPARTMENT_ID),
FOREIGN KEY(JOB_ID)
REFERENCES jobs(JOB_ID)
)ENGINE=InnoDB;
Copy
Let execute the above code in MySQL 5.6 command prompt
Assume that the structure of the table jobs and InnoDB Engine have been used
to create the table jobs.
CREATE TABLE IF NOT EXISTS jobs (
JOB_ID integer NOT NULL UNIQUE PRIMARY KEY,
JOB_TITLE varchar(35) NOT NULL DEFAULT ' ',
MIN_SALARY decimal(6,0) DEFAULT 8000,
MAX_SALARY decimal(6,0) DEFAULT NULL
)ENGINE=InnoDB;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
| MIN_SALARY | decimal(6,0) | YES | | 8000 | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
Sample Solution:
CREATE TABLE IF NOT EXISTS employees (
ACCENTURE BATCH 2 – LAB EXERCISES
FOREIGN KEY(JOB_ID)
REFERENCES jobs(JOB_ID)
)ENGINE=InnoDB;
Copy
Let execute the above code in MySQL 5.6 command prompt
jobs table, can contain only those values which are exists in the jobs table. The
InnoDB Engine have been used to create the tables. The specialty of the
statement is that, The ON DELETE SET NULL action will set the foreign key
column values in the child table(employees) to NULL when the record in the
Page | 27
parent table(jobs) is deleted, with a condition that the foreign key column in the
child table must accept NULL values and the ON UPDATE SET NULL action
resets the values in the rows in the child table(employees) to NULL values when
the rows in the parent table(jobs) are updated.
Assume that the structure of two table jobs and InnoDB Engine have been used
to create the table jobs.
CREATE TABLE IF NOT EXISTS jobs (
JOB_ID integer NOT NULL UNIQUE PRIMARY KEY,
JOB_TITLE varchar(35) NOT NULL DEFAULT ' ',
MIN_SALARY decimal(6,0) DEFAULT 8000,
MAX_SALARY decimal(6,0) DEFAULT NULL
)ENGINE=InnoDB;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
| MIN_SALARY | decimal(6,0) | YES | | 8000 | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
Sample Solution:
CREATE TABLE IF NOT EXISTS employees (
JOB_ID INTEGER,
FOREIGN KEY(JOB_ID)
REFERENCES jobs(JOB_ID)
ACCENTURE BATCH 2 – LAB EXERCISES
)ENGINE=InnoDB;
Page | 28
Copy
Let execute the above code in MySQL 5.6 command prompt
Assume that the structure of two table jobs and InnoDB Engine have been used
to create the table jobs.
CREATE TABLE IF NOT EXISTS jobs (
JOB_ID integer NOT NULL UNIQUE PRIMARY KEY,
ACCENTURE BATCH 2 – LAB EXERCISES
Page | 29
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| JOB_ID | int(11) | NO | PRI | NULL | |
| JOB_TITLE | varchar(35) | NO | | | |
| MIN_SALARY | decimal(6,0) | YES | | 8000 | |
| MAX_SALARY | decimal(6,0) | YES | | NULL | |
+------------+--------------+------+-----+---------+-------+
Sample Solution:
CREATE TABLE IF NOT EXISTS employees (
FOREIGN KEY(JOB_ID)
REFERENCES jobs(JOB_ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)ENGINE=InnoDB;
Copy
Let execute the above code in MySQL 5.6 command prompt