Q:

Write a SQL statement to insert one row in the jobs table to ensure that no duplicate values will be entered into the job_id column

0

Write a  postgre SQL statement to insert one row in the jobs table to ensure that no duplicate values will be entered into the job_id column

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Here is the code to create a sample table jobs:

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

Now insert a row into the table jobs :

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

Here is the command to see the list of inserting rows :

postgres=# SELECT * FROM jobs;
 job_id | job_title | min_salary
--------+-----------+------------
   1001 | OFFICER   |       8000
(1 row)

Now, try to insert the duplicate value in the key column and see what happen :

postgres=# INSERT INTO jobs VALUES(1001,'OFFICER',8000);
ERROR:  duplicate key value violates unique constraint "jobs_job_id_key"
DETAIL:  Key (job_id)=(1001) already exists.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now