0% found this document useful (0 votes)
106 views7 pages

Manish Assignment1

The document provides examples of SQL statements to create tables with various constraints and insert data into tables. It includes statements to create tables with primary keys, unique constraints, default values, check constraints, and insert single or multiple rows of data while enforcing the constraints. The last section provides examples of update statements to modify existing data in tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views7 pages

Manish Assignment1

The document provides examples of SQL statements to create tables with various constraints and insert data into tables. It includes statements to create tables with primary keys, unique constraints, default values, check constraints, and insert single or multiple rows of data while enforcing the constraints. The last section provides examples of update statements to modify existing data in tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

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 serial,
country_name varchar(250),
region_id varchar(255)
);

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


country_id,country_name and region_id which is already exists.

Ans:firstly it will show error because countries table already exist can't be
created.

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


to countries.

Ans:create table dup_countries as select * from countries;

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


structure and data by name dup_countries.

create table if not exist dup_countries as select * from countries;

5. Write a SQL statement to create a table countries set a constraint NULL.

Ans: create table countries(


country_id serial NOT NULL,
country_name varchar(250) NOT NULL,
region_id varchar NOT NULL
);

6. 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 jobs(


job_id serial,
job_title varchar(255),
min_salary decimal(7,0),
max_salary decimal (6,0),
CHECK(max_salary<=25000)
);

7. 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.

create table IF Not Exists countries(


country_id serial,
country_name varchar(255),
check(country_name IN('italy','India','China')),
region_id varchar(255)
)

8. Write a SQL statement to create a table named job_histry including columns


employee_id, start_date, end_date, job_id and department_id and
make sure that the value against column end_date will be entered at the time of
insertion to
the format like '--/--/----'.

create table job_histry(


employee_id serial not null,
start_date date not null,
end_date date not null CHECK (end_date LIKE '- -/ - - / - - - -'),
job_id int not null,
depatment_id int not null
);

9. Write a SQL statement to create a table named countries including columns


country_id,country_name and
region_id and make sure that no duplicate data against column
country_id will be allowed at the time of insertion.

create table IF NOT EXISTS countries (


country_id serial NOT NULL,
country_name varchar(100) NOT NULL,
region_id varchar(255) NOT NULL,
UNIQUE(country_id)
);

10. Write a SQL statement to create a table named jobs including columns job_id,
job_title, min_salary and max_salary, and make sure that,
the default value for job_title is blank and min_salary is 8000 and max_salary
is NULL will be entered automatically at the time of insertion if no value assigned
for the specified columns.

create table IF NOT EXISTS jobs (


job_id serial NOT NULL UNIQUE,
job_title varchar(100) NOT NULL DEFAULT ' ',
min_salary decimal(6,0) DEFAULT 8000,
max_salary decimal(6,0) DEFAULT NULL
);

11. Write a SQL statement to create a table named countries including columns
country_id, country_name
and region_id and make sure that the country_id column will be a key field which
will not contain any duplicate
data at the time of insertion.

create table countries(


country_id serial,
country_name varchar(250) ,
region_id int
CONSTRAINT pk_countries_country_id primary key (country_id));

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


country_id, country_name and region_id and make sure that the column country_id
will be
unique and store an auto incremented value.
create table countries(
country_id serial,
country_name varchar(250) ,
region_id varchar(255) );

-->>Insert Rows into the Table


==============================

1. Write a SQL statement to insert a record with your own value into the table
countries against each columns.

Here in the following is the structure of the table countries.

INSERT INTO countries(country_name,region_id)


VALUES('India','100001');

2. Write a SQL statement to insert one row into the table countries against the
column country_id and country_name.
Here in the following is the structure of the table countries.

country_id is serial its automatically increment


|

INSERT INTO countries(country_name)


VALUES('India');

3. Write a SQL statement to create duplicate of countries table named country_new


with all structure and data.
Here in the following is the structure of the table countries.

+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| COUNTRY_ID | varchar(2) | YES | | NULL | |
| COUNTRY_NAME | varchar(40) | YES | | NULL | |
| REGION_ID | decimal(10,0) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+

create table if not exists country_new AS select * from countries;

4. Write a SQL statement to insert NULL values against region_id column for a row
of countries table.

INSERT INTO countries(country_name)


VALUES('India');

5. Write a SQL statement to insert 3 rows by a single insert statement.

INSERT INTO countries(country_name,region_id)


VALUES('India','netlink'),
('India','netlink'),
('chinaa', 'netlink');
6. Write a SQL statement insert rows from country_new table to countries table.
Here is the rows for country_new table. Assume that, the countries table is empty.

INSERT INTO country_new select * from countries;

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.

INSERT INTO jobs(job_title)


VALUES('intern');

8. 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.

if job_is is primary key /unique+not null then


INSERT INTO jobs(job_id,job_title)
VALUES(1,'intern');
This query will give error because duplicate key value violates unique
constraint "jobs_pkey"

9. Write a SQL statement to insert a record into the table countries to ensure
that,
a country_id and region_id combination will be entered once
in the table.

insert into countries(region_id)


values('tracker');

10. Write a SQL statement to insert rows into the table countries in which the
value of country_id column
will be unique and auto incremented.

country_id column to be unique and auto incremented then it should be declared


as "serial" datatype

Since, I have already declared country_id as serial type when i have create
country table.

so let's insert into it


INSERT INTO Countries(Country_name,region_id)
VALUES ('america',91);

Update Table :
1. Write a SQL statement to change the email column of employees table with 'not
available' for all employees.
+-------------+-------------+-------------+----------+--------------------
+------------+------------+----------+----------------+------------+---------------
+
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER |
HIRE_DATE | JOB_ID | SALARY | COMMISSION_PCT | MANAGER_ID | DEPARTMENT_ID |
+-------------+-------------+-------------+----------+--------------------
+------------+------------+----------+----------------+------------+---------------
+
| 100 | Steven | King | SKING | 515.123.4567 | 1987-
06-17 | AD_PRES | 24000.00 | 0.00 | 0 | 90 |
| 101 | Neena | Kochhar | NKOCHHAR | 515.123.4568 | 1987-
06-18 | AD_VP | 17000.00 | 0.00 | 100 | 90 |
| 102 | Lex | De Haan | LDEHAAN | 515.123.4569 | 1987-
06-19 | AD_VP | 17000.00 | 0.00 | 100 | 40 |
| 103 | Alexander | Hunold | AHUNOLD | 590.423.4567 | 1987-
06-20 | IT_PROG | 9000.00 | 0.00 | 102 | 60 |
| 104 | Bruce | Ernst | BERNST | 590.423.4568 | 1987-
06-21 | IT_PROG | 6000.00 | 0.00 | 103 | 60 |
| 105 | David | Austin | DAUSTIN | 590.423.4569 | 1987-
06-22 | IT_PROG | 4800.00 | 0.00 | 103 | 60 |
+-------------+-------------+-------------+----------+--------------------
+------------+------------+----------+----------------+------------+---------------
+

UPDATE employees SET EMAIL='not available';

2. Write a SQL statement to change the email and commission_pct column of employees
table
with 'not available' and 0.10 for all employees.

UPDATE employees SET EMAIL='not available', commission_pct='0.10';

3. Write a SQL statement to change the email and commission_pct column of employees
table with 'not available'
and 0.10
for those employees whose
department_id is 90.

update employees set email='not available',commission_pct='0.10' where


department_id=90;

4. Write a SQL statement to change the email column of employees table with 'not
available' for those employees
whose department_id is 80 and
gets a commission is less than .20%

UPDATE employees SET EMAIL='not available' WHERE department_id=80 AND


commission_pct<'.20';

5. Write a SQL statement to change the email column of employees table with 'not
available' for
those employees who belongs to the 'Accouning'
department.

UPDATE employees SET EMAIL='not available' WHERE department='Accouning';

6. Write a SQL statement to change salary of employee to 8000 whose ID is 105,


if the existing salary is less than 5000.
update employees set salary=8000 where employee_id=129 and salary<'5000';

7. Write a SQL statement to change job ID of employee which ID is 105,


to SH_CLERK if the employee belongs to department, which ID is 30 and
the existing job ID does not start with SH.

UPDATE employee SET job_id=105 where department='SH_CLERK' OR (job_id=30 AND


department NOT LIKE 'SH%');

Alter Table:

1. Write a SQL statement to rename the table countries to country_new.

ALTER TABLE countries


RENAME TO countries_new;

CREATE A TABLE country_new AND perform below operati


+----------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| LOCATION_ID | decimal(4,0) | YES | | NULL | |
| STREET_ADDRESS | varchar(40) | YES | | NULL | |
| POSTAL_CODE | varchar(12) | YES | | NULL | |
| CITY | varchar(30) | YES | | NULL | |
| STATE_PROVINCE | varchar(25) | YES | | NULL | |
| COUNTRY_ID | varchar(2) | YES | | NULL | |
+----------------+--------------+------+-----+---------+-------+

2. Write a SQL statement to add a column region_id to the table locations.

alter table locations


add column region_id varchar(200);

3. Write a SQL statement change the data type of the column country_id to integer
in the table locations.

ALTER TABLE locations


ALTER Country_ID int ;

4. Write a SQL statement to drop the column city from the table locations.

alter table locations


drop column city;

5. Write a SQL statement to change the name of the column state_province to state,
keeping the data type and size same.

alter table locations


rename column state_province to state;

6. Write a SQL statement to add a primary key for the columns location_id in the
locations table.

alter table country_new


add constraints pk_country_new primary key(country_id);
akshay this alter command not working-
error show-ERROR: syntax error at or near "("

7. Write a SQL statement to add a primary key for a combination of columns


location_id and country_id.

Ans: multiple primary keys for table "locations" are not allowed.

8. Write a SQL statement to drop the existing primary from the table locations
on a combination of columns location_id and country_id.

alter table locations drop primary key;


i have run this command but this error come
ERROR: syntax error at or near "PRIMARY"

9. Write a SQL statement to add a foreign key on job_id column of job_history table

referencing to the primary key job_id of jobs table.

ALTER TABLE job_history


ADD FOREIGN KEY (job_id) REFERENCES jobs(job_id);

10. Write a SQL statement to add a foreign key constraint


named fk_job_id on job_id column of job_history table referencing to the primary
key job_id
of jobs table.

alter table job_history


add constraint fk_job_id
foreign key(job_id) references jobs(job_id)
on update restrict on delete cascade;

11. Write a SQL statement to drop the existing foreign key fk_job_id from
job_history table on job_id column
which is referencing to the job_id of
jobs table.

alter table job_history


drop CONSTRAINTS fk_job_id foreign key fk_job_id
references jobs(job_id);

i have run this command but error show


ERROR: syntax error at or near "fk_job_id"

You might also like