Professional Documents
Culture Documents
DB Exercise1
DB Exercise1
5. 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.
create table job1(job_id numeric primary key NOT NULL,
job_title varchar(50),
min_salary numeric,
max_salary numeric check(max_salary <= '25000'));
insert into
job1(job_id,job_title,min_salary,max_salary)values('101','Manager','5000','30000');
insert into
job1(job_id,job_title,min_salary,max_salary)values('102','Consultant','50000','3000
');
select * from job1
6. Write a SQL statement for table named countries that no countries except Italy,
India and China will be entered in the table.
create table countries1 (country_id numeric primary key,country_name varchar(50)
NOT NULL check(country_name in ('Italy','India','China')),region_id serial);
insert into countries1(country_id,country_name,region_id)values
('01','India','101');
select * from countries1;
7. Write a SQL statement for table named countries that no duplicate data against
column country_id will be allowed at the time of insertion.
create table countries01 (country_id numeric unique,country_name varchar(50) NOT
NULL check(country_name in ('Italy','India','China')),region_id serial);
//error
insert into countries01(country_id,country_name,region_id)values
('01','India','101'),('01','India','101');
//correct
insert into countries01(country_id,country_name,region_id)values
('01','India','101'),('02','India','101');
select * from countries01;
8. Write a SQL statement to set the default value for jobs table. Set job_title as
blank and min_salary as 8000 and max_salary as NULL will be entered automatically
at the time of insertion if no value assigned for the specified columns.
create table job3( job_title varchar(50) default ' ',min_salary numeric default
'8000',max_salary numeric default NULL);
insert into job3 values(' ');
select * from job3
9. Write a SQL statement for countries tables that the country_id column will be a
key field which will not contain any duplicate data at the time of insertion.
create table countries001 (country_id numeric unique primary key,country_name
varchar(50) NOT NULL check(country_name in ('Italy','India','China')),region_id
serial);
10. Write a SQL statement for table countries that the column country_id will be
unique and store an auto-incremented value.
create table countries001 (country_id numeric unique ,country_name varchar(50) NOT
NULL check(country_name in ('Italy','India','China')),region_id serial);
11. Write a SQL statement for countries that the combination of columns country_id
and region_id will be unique.
create table countries0001 (country_id numeric primary key,country_name varchar(50)
NOT NULL check(country_name in ('Italy','India','China')),region_id
numeric,unique(country_id,region_id));