0% found this document useful (0 votes)
24 views2 pages

copy

The document contains SQL statements to create tables, insert and manipulate data in various database tables related to customers, sales, employees, hospitals etc. It includes statements for: 1) Creating tables like customer, sales_man, sales_order etc and inserting sample records. 2) Altering tables to add/drop columns, constraints like primary keys, foreign keys etc. 3) Performing various queries on the tables like selecting, filtering, aggregating data using functions. 4) Creating stored functions to check for prime numbers and generate Fibonacci series.

Uploaded by

Charlie M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

copy

The document contains SQL statements to create tables, insert and manipulate data in various database tables related to customers, sales, employees, hospitals etc. It includes statements for: 1) Creating tables like customer, sales_man, sales_order etc and inserting sample records. 2) Altering tables to add/drop columns, constraints like primary keys, foreign keys etc. 3) Performing various queries on the tables like selecting, filtering, aggregating data using functions. 4) Creating stored functions to check for prime numbers and generate Fibonacci series.

Uploaded by

Charlie M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CUSTOMER ALTER TABLE sales_man add PRODUCT create table and salary>10000; d) select

primary key(salesman_no); e) product(Product_code int e.emp_name from employee1


create table customer(cust_no
ALTER TABLE sales_order add primary key, Product_Name e, works w, company c where
varchar(5),cust_name
constraint varchar(20),Category e.emp_name =w.emp_name
varchar(15),age numeric,phone
sales_order_del_type_check varchar(20), Quantity int,Price and e.city = c.city and
varchar(10)); a) insert into
CHECK (del_type = 'P' OR numeric); insert into product w.company_name =
customer
del_type = 'F'); ALTER TABLE values(1,'colgate','paste',10,100 c.company_name; e) select
values(1,'A',23,94567); select
sales_order ADD CONSTRAINT ); select * from product; a) emp_name from works where
*from customer; b) alter table
FK_salesman_no FOREIGN KEY select * from product order by company_name!='wipro'; f)
customer add d_o_b date; c)
(salesman_no) REFERENCES product_name desc; b) select select company_name from
create table cust_phone as
sales_man(salesman_no); product_code,product_name works group by company_name
select cust_name,phone from
from product where price having count (distinct
customer; select *from HOSPITAL
between 20 and 50; c) select emp_name) >= all (select count
cust_phone; d) alter table
create table hospital(doctor_id product_name,price from (distinct emp_name) from
customer drop age; select *from
text,doctor_name product where category in works group by
customer; e) ALTER TABLE
varchar(10),department ('bath soap','paste','washing company_name);
customer ALTER COLUMN
varchar(25),qualification powder'); d) select * from
cust_name TYPE varchar(25); AREA OF CIRCLE 3-7 do \
varchar(25),experience text); a) product where quantity500; e)
select *from customer; f) 'declare \ r integer; \ area
insert into hospital select product_name from
TRUNCATE customer; select numeric(5,2); \ pi constant float
values('D001','miya','cardiologis product where product_name
*from customer; g) ALTER :=3.14; \begin \ create table
t','mbbs',5); b) select * from like 't%'; f) select product_name
TABLE customer RENAME TO area(r integer ,area
hospital; c) select * from from product where category
cust; select *from cust; h) drop numeric(5,2)); \ r:=3; \ while
hospital where !='paste'; g) insert into product
table cust; r<=7 \ loop
qualification='md'; d) select * values(10,'radhas','washing
\area:=pi*power(r,2); \ insert
SALESMAN AND SALES ORDER from hospital where powder',600,2000); select
into area values(r,area); \
create table experience>'5' and product_name from product
r:=r+1; \ end loop; \ end'; select
sales_man(salesman_no int qualification!='md'; e) select * where product_name like '_a%'
* from area;
primary key,s_name varchar(20) from hospital where and category='washing powder';
not null,place text,phone int department='skin'; f) update FUNCTION PRIME OR NOT
EMPLOYEE DATABASE create
unique); create table hospital set experience=5 where create function primecheck(n
table works(emp_name
sales_order(order_no int doctor_id='D003'; select * from integer) returns varchar(10) as'
varchar(10),company_name
primary key,order_date date hospital; g) delete from hospital declare \ i integer; \ flag
varchar(10),salary int); create
not null,salesman_no int, where doctor_id='D005'; select integer:=0; \ begin \ for i in
table company(company_name
foreign key(salesman_no) * from hospital; 2..n/2 \ loop \ if mod(n,i)=0 \
varchar(10),city varchar(10));
references then \ flag:=1; \ exit; \ end if;
EMPLOYOEE create table create table
sales_man(salesman_no) on \end loop; \ if flag=0 \then \
emp(emp_id int,e_name manages(emp_name
delete cascade,del_type char(1) return ''prime''; \else \ return
char(10),salary int,department varchar(10),manager_name
check(del_type='P' or ''not prime''; \ end if; \ end;' \
char(10),age int); insert into varchar(10)); insert into
del_type='F'),order_status language plpgsql; select
emp employee1
varchar(10) primecheck(20);
values(001,'ram',360000,'hr',22) values('ammu','chennai'); select
check(order_status='inprocess'o
; select*from emp; a) select *from employee1; insert into FIBNOCCI NUMBER UPTO LIMIT
r order_status='fullfilled'or
count(emp_id) from emp; b) works CREATE OR REPLACE FUNCTION
order_status='backorder'or
select e_name,age from emp values('ammu','infosys','11000'); fibonacci1(num integer)
order_status='cancelled')); a)
where age=(select select *from works; insert into RETURNS SETOF numeric AS $$
insert into sales_man DECLARE \ fib1 numeric := 0; \
max(age)from emp); c) select company
values(1,'A','abc',98456); select
avg(age)from emp group by values('infosys','chennai'); select fib2 numeric := 1; \ BEGIN \ IF
* from sales_man; insert into (num <= 0) \ THEN RETURN; \
department; d) select *from company; a) select
sales_order values(1,'01-01- END IF; \ RETURN NEXT fib1; \
department,avg(salary)from emp_name from works where
2000',1,'P','inprocess'); select * LOOP \ EXIT WHEN num <= 1; \
emp group by department; e) company_name='infosys'; b) RETURN NEXT fib2; \ num = num
from sales_order; b) ALTER
select min(salary) from emp; f) select e.emp_name,e.city from - 1; \SELECT fib2, fib1 + fib2
TABLE sales_man DROP
select count(e_name)from emp employee1 e join works w on INTO fib1, fib2; \ END LOOP;
CONSTRAINT sales_man_pkey
where department='purchase';; e.emp_name=w.emp_name \END; \ $$ language plpgsql; \
cascade; c) ALTER TABLE
g) select max(salary) from emp where company_name='wipro'; select fibonacci(12);
sales_man DROP CONSTRAINT
where department='sales'; h) c) select e.emp_name,e.city
sales_order_fkey; ALTER TABLE EMPLOYEE SALARY AVERAGE
select max(salary)-min(salary) from employee1 e join works w
sales_order DROP CONSTRAINT
difference from emp; on e.emp_name=w.emp_name create table emp_sal(empno
sales_order_del_type_check; d)
where company_name='infosys' int,ename varchar(10),edept
varchar(10),salary numeric); insert into phone_book
select* from emp_sal; \create values('ccc',469369369); create
function avgsal(deptt or replace function proc()
varchar(10))returns returns trigger as \ 'begin \
numeric(10,2) as \ 'declare \ insert into del_phonebook
avgsal numeric(10,2); \ begin \ values
avgsal=(select avg(salary) from (old.pname,old.mobno,current_
emp_sal group by edept having date);\ return new;\
edept= deptt); \ return avgsal; \ end;'language plpgsql;\ create
end;'language plpgsql; trigger tr after delete on
phone_book for each row\
EXAM RESULT create table
execute procedure proc ();
examresult(rollno int,avg_score
float,grade char(1)); insert into
examresult values(1,90); do ' \
declare \r record; \ gr char(1); \
begin \ for r in select
rollno,avg_score,grade from
examresult \ loop \ if
r.avg_score>=90 then gr:=''A''; \
elseif(r.avg_score>=75) then
gr:=''B''; \
elseif(r.avg_score>=60) then
gr:=''C''; \
elseif(r.avg_score>=50) then
gr:=''D''; \ else gr:=''E''; \ end if;
\ update examresult set
grade=gr where rollno=r.rollno;
\ end loop; \ end'; \ select *
from examresult; STUDENT
create table student1(regno
varchar(10), sname varchar(20),
sub1 int,sub2 int,sub3 int,sub4
int,sub5 int,total int,avg float);
create or replace function proc()
returns trigger as 'begin \
new.total=new.sub1+new.sub2
+new.sub3+new.sub4+new.sub
5; \ new.avg=new.total/5.0; \
return new; \ end;' \ language
plpgsql; create trigger tr before
insert on student1 for each row
execute procedure proc(); insert
into student1 values('12',
'aa',12,30,40,50,50); select *
from student1;

PHONE BOOK create table


phone_book(pname varchar(10)
primary key,mobno integer);
create table
del_phonebook(pname
varchar(10) primary key,mobno
integer,modify_date date);
insert into phone_book
values('aaa',369369369); \
insert into phone_book
values('bbb',369369469); \

You might also like