0% found this document useful (0 votes)
32 views5 pages

Ex No 2

The document outlines a series of SQL commands executed in a PostgreSQL database named 'employeedb', including the creation of tables, insertion of employee data, and various queries to manipulate and retrieve information. It demonstrates the addition of columns, updating salaries, and filtering results based on specific conditions. The document also includes error handling for data types and constraints during table operations.

Uploaded by

takash2412
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)
32 views5 pages

Ex No 2

The document outlines a series of SQL commands executed in a PostgreSQL database named 'employeedb', including the creation of tables, insertion of employee data, and various queries to manipulate and retrieve information. It demonstrates the addition of columns, updating salaries, and filtering results based on specific conditions. The document also includes error handling for data types and constraints during table operations.

Uploaded by

takash2412
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

EX.

NO:1
postgres=# create database employeedb;
CREATE DATABASE
postgres=# \connect employeedb
You are now connected to database "employeedb" as user "postgres".
employeedb=# create table employees(ename varchar(18),city varchar (18));
CREATE TABLE
employeedb=# \d+ employees
Table "public.employees"
Column | Type | Collation | Nullable | Default | Storage |
Compression | Stats target | Description
--------+-----------------------+-----------+----------+---------+----------+-
------------+--------------+-------------
ename | character varying(18) | | | | extended |
| |
city | character varying(18) | | | | extended |
| |
Access method: heap

employeedb=# insert into employees(ename,city) values('anil','chennai');


INSERT 0 1
employeedb=# insert into employees(ename,city) values('shankar','mumbai');
INSERT 0 1
employeedb=# insert into employees(ename,city) values('jaya','chennai');
INSERT 0 1
employeedb=# insert into employees(ename,city) values('vijay','delhi');
INSERT 0 1
employeedb=# insert into employees(ename,city) values('ajay','chennai');
INSERT 0 1
employeedb=# select * from employees;
ename | city
---------+---------
anil | chennai
shankar | mumbai
jaya | chennai
vijay | delhi
ajay | chennai
(5 rows)

employeedb=# insert into employees(ename,city) values('sunil','mumbai');


INSERT 0 1
employeedb=# alter table employees add column contact varchar(18);
ALTER TABLE
employeedb=# select * from employees;
ename | city | contact
---------+---------+---------
anil | chennai |
shankar | mumbai |
jaya | chennai |
vijay | delhi |
ajay | chennai |
sunil | mumbai |
(6 rows)

employeedb=# create table dummy(id text);


CREATE TABLE
employeedb=# insert into dummy(id) values('1');
INSERT 0 1
employeedb=# insert into dummy(id) values('2');
INSERT 0 1
employeedb=# insert into dummy(id) values('3');
INSERT 0 1
employeedb=# insert into dummy(id) values('4');
INSERT 0 1
employeedb=# select * from dummy;
id
----
1
2
3
4
(4 rows)

employeedb=# truncate table dummy;


TRUNCATE TABLE
employeedb=# select * from dummy;
id
----
(0 rows)

employeedb=# drop table dummy;


DROP TABLE
employeedb=# select * from dummy;
ERROR: relation "dummy" does not exist
LINE 1: select * from dummy;
^
employeedb=# create table emp_company(ename varchar(18),cname varchar (18),
salary numeric(5,2),jdate date);
CREATE TABLE
employeedb=# insert into emp_company(ename,cname,salary,jdate) values('anil',
'tcs',1500,'1989-05-01');
ERROR: numeric field overflow
DETAIL: A field with precision 5, scale 2 must round to an absolute value less
than 10^3.
employeedb=# alter table emp_company alter column salary type numeric(8,2);
ALTER TABLE
employeedb=# insert into emp_company(ename,cname,salary,jdate) values('anil',
'tcs',1500,'1989-05-01');
INSERT 0 1
employeedb=# insert into emp_company(ename,cname,salary,jdate) values('shankar',
'mrf',3000,'1990-07-01');
INSERT 0 1
employeedb=# insert into emp_company(ename,cname,salary,jdate) values('jaya','tcs',
1800,'1990-06-17');
INSERT 0 1
employeedb=# insert into emp_company(ename,cname,salary,jdate)
values('vijay','tcs',
5000,'1988-01-03');
INSERT 0 1
employeedb=# insert into emp_company(ename,cname,salary,jdate) values('ajay','cts',
8000,'1985-04-30');
INSERT 0 1
employeedb=# insert into emp_company(ename,cname,salary,jdate)
values('sunil','mrf',
1700,'1989-01-01');
INSERT 0 1
employeedb=# select * from emp_company;
ename | cname | salary | jdate
---------+-------+---------+------------
anil | tcs | 1500.00 | 1989-05-01
shankar | mrf | 3000.00 | 1990-07-01
jaya | tcs | 1800.00 | 1990-06-17
vijay | tcs | 5000.00 | 1988-01-03
ajay | cts | 8000.00 | 1985-04-30
sunil | mrf | 1700.00 | 1989-01-01
(6 rows)

employeedb=# update emp_company set salary = salary+salary*0.1 where cname='tcs';


UPDATE 3
employeedb=# select * from emp_company;
ename | cname | salary | jdate
---------+-------+---------+------------
shankar | mrf | 3000.00 | 1990-07-01
ajay | cts | 8000.00 | 1985-04-30
sunil | mrf | 1700.00 | 1989-01-01
anil | tcs | 1650.00 | 1989-05-01
jaya | tcs | 1980.00 | 1990-06-17
vijay | tcs | 5500.00 | 1988-01-03
(6 rows)

employeedb=# insert into emp_company(ename,cname,salary,jdate) values('dummy',


'dummy',0,'2022-09-09');
INSERT 0 1
employeedb=# select * from emp_company;
ename | cname | salary | jdate
---------+-------+---------+------------
shankar | mrf | 3000.00 | 1990-07-01
ajay | cts | 8000.00 | 1985-04-30
sunil | mrf | 1700.00 | 1989-01-01
anil | tcs | 1650.00 | 1989-05-01
jaya | tcs | 1980.00 | 1990-06-17
vijay | tcs | 5500.00 | 1988-01-03
dummy | dummy | 0.00 | 2022-09-09
(7 rows)

employeedb=# delete from emp_company where cname='dummy';


DELETE 1
employeedb=# select * from emp_company;
ename | cname | salary | jdate
---------+-------+---------+------------
shankar | mrf | 3000.00 | 1990-07-01
ajay | cts | 8000.00 | 1985-04-30
sunil | mrf | 1700.00 | 1989-01-01
anil | tcs | 1650.00 | 1989-05-01
jaya | tcs | 1980.00 | 1990-06-17
vijay | tcs | 5500.00 | 1988-01-03
(6 rows)
employeedb=# select ename from emp_company where salary <= 3000;
ename
---------
shankar
sunil
anil
jaya
(4 rows)

employeedb=# select ename from emp_company where jdate >='1989-01-01' and


jdate <='1989-12-31';
ename
-------
sunil
anil
(2 rows)

employeedb=# select ename from emp_company where cname='tcs' and salary < 2000;
ename
-------
anil
jaya
(2 rows)

employeedb=# select ename from emp_company where ename='ajay';


ename
-------
ajay
(1 row)

employeedb=# select ename from emp_company where salary between 4000 and 5000;
ename
-------
(0 rows)

employeedb=# update emp_company set salary = salary+salary*45 where jdate<=


'1990-01-01';
UPDATE 4
employeedb=# select * from emp_company;
ename | cname | salary | jdate
---------+-------+-----------+------------
shankar | mrf | 3000.00 | 1990-07-01
jaya | tcs | 1980.00 | 1990-06-17
ajay | cts | 368000.00 | 1985-04-30
sunil | mrf | 78200.00 | 1989-01-01
anil | tcs | 75900.00 | 1989-05-01
vijay | tcs | 253000.00 | 1988-01-03
(6 rows)

employeedb=# select ename from emp_company where ename like 'a%';


ename
-------
ajay
anil
(2 rows)

employeedb=# select ename,salary from emp_company where ename like '_a%';


ename | salary
-------+---------
jaya | 1980.00

You might also like