Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

1.

Write a SQL statement to create a simple table countries including columns


country_id,country_name and region_id.
create table countries (country_id numeric primary key,country_name varchar(50) NOT
NULL,region_id serial);

2. Write a SQL statement to create a simple table countries including columns


country_id,country_name and region_id which already exist.
create table IF NOT EXISTS countries (country_id numeric primary key,country_name
varchar(50) NOT NULL,region_id serial);

3. Write a SQL statement to create the structure of a table dup_countries similar


to countries.
create table dup_countries1 as select country_id,country_name,region_id from
countries WITH NO DATA;

4. Write a SQL statement to create a duplicate copy of countries table including


structure and data by name dup_countries.
insert into countries(country_id,country_name,region_id)values
('01','America','101'),('');
select * into dup_countries2 from countries;
select * from dup_countries2;

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));

12. Write a SQL statement to create a table job_history including columns


employee_id, start_date, end_date, job_id and department_id 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 contain only those values which exist in the jobs
table.
create table job_his(employee_id numeric primary key unique, start_date
date,end_date date, job_id numeric, department_id numeric,foreign
key(job_id)references job4(job_id));

You might also like