Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

MySQL insert into Statement Exercises: Insert one row in jobs table to ensure that no duplicate value will be entered in the job_id column

MySQL insert into Statement: Exercise-7 with Solution

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.

CREATE TABLE IF NOT EXISTS jobs ( 
JOB_ID integer NOT NULL UNIQUE , 
JOB_TITLE varchar(35) NOT NULL, 
MIN_SALARY decimal(6,0)
);

INSERT INTO jobs VALUES(1001,'OFFICER',8000);

mysql> SELECT * FROM jobs;
+--------+-----------+------------+
| JOB_ID | JOB_TITLE | MIN_SALARY |
+--------+-----------+------------+
|   1001 | OFFICER   |       8000 |
+--------+-----------+------------+

Sample Solution:

INSERT INTO jobs VALUES(1001,'OFFICER',8000);

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'

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous:Write a SQL statement insert rows from country_new table to countries table.
Next: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.

What is the difficulty level of this exercise?