Create a MySQL table named jobs and ensure that one of its columns, max_salary, is checked to not exceed the upper limit of 25000
MySQL Create Tables: Exercise-6 with Solution
6. Write a MySQL query 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.
Sample Solution:
-- Creating a table named 'jobs' if it doesn't already exist to store information about job positions
CREATE TABLE IF NOT EXISTS jobs(
-- Column to store job IDs, marked as NOT NULL (required)
JOB_ID varchar(10) NOT NULL,
-- Column to store job titles, marked as NOT NULL (required)
JOB_TITLE varchar(35) NOT NULL,
-- Column to store minimum salary with decimal precision of 6, 0
MIN_SALARY decimal(6,0),
-- Column to store maximum salary with decimal precision of 6, 0
MAX_SALARY decimal(6,0),
-- CHECK constraint to ensure that the maximum salary is less than or equal to 25,000
CHECK(MAX_SALARY<=25000)
);
Let execute the above code in MySQL command prompt
Here is the structure of the table:
mysql> DESC jobs; +------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+-------+ | JOB_ID | varchar(10) | NO | | NULL | | | JOB_TITLE | varchar(35) | NO | | NULL | | | MIN_SALARY | decimal(6,0) | YES | | NULL | | | MAX_SALARY | decimal(6,0) | YES | | NULL | | +------------+--------------+------+-----+---------+-------+ 4 rows in set (0.16 sec)
Explanation:
The above MySQL code creates a table named "jobs" if it doesn't already exist. The table has four columns:
- JOB_ID: Stores job IDs and is marked as NOT NULL to enforce the requirement of having a value in this column.
- JOB_TITLE: Stores job titles and is marked as NOT NULL to enforce the requirement of having a value in this column.
- MIN_SALARY: Stores minimum salary values with decimal precision of 6, 0.
- MAX_SALARY: Stores maximum salary values with decimal precision of 6, 0. Additionally, there is a CHECK constraint to ensure that the maximum salary is less than or equal to 25,000.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a SQL statement to create a table countries set a constraint NULL.
Next: Write a SQL statement to create a table named countries including columns country_id, country_name and region_id and make sure that no countries except Italy, India and China will be entered in the table.
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/mysql-exercises/create-table-exercises/create-table-exercise-6.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics