The document describes several database schemas for different scenarios including customer-sales, student library, employee payroll, and others. For each schema, it provides the table structures, sample data, and SQL queries to perform various operations like listing records, counts, and creating views. The goals are to create the tables, insert sample data, and write queries to retrieve and summarize information from the tables.
The document describes several database schemas for different scenarios including customer-sales, student library, employee payroll, and others. For each schema, it provides the table structures, sample data, and SQL queries to perform various operations like listing records, counts, and creating views. The goals are to create the tables, insert sample data, and write queries to retrieve and summarize information from the tables.
The document describes several database schemas for different scenarios including customer-sales, student library, employee payroll, and others. For each schema, it provides the table structures, sample data, and SQL queries to perform various operations like listing records, counts, and creating views. The goals are to create the tables, insert sample data, and write queries to retrieve and summarize information from the tables.
The document describes several database schemas for different scenarios including customer-sales, student library, employee payroll, and others. For each schema, it provides the table structures, sample data, and SQL queries to perform various operations like listing records, counts, and creating views. The goals are to create the tables, insert sample data, and write queries to retrieve and summarize information from the tables.
INDEX S.no Program name Date Signature 1 Database Schema for a customer-sale scenario Customer(Cust id : integer, cust_name: string) Item(item_id: integer, item_name: string, price: integer)Sale(bill_no: integer, bill_data: date, cust_id: integer, item_id: integer, qty_sold: integer)
For the above schema, perform the following a) Create the tables with the appropriate integrity constraints b) Insert around 10 records in each of the tables c) List all the bills for the current date with the customer names and item numbers d) List the total Bill details with the quantity sold, price of the item and the final amount e) List the details of the customer who have bought a product which has a price>200 f) Give a count of how many products have been bought by each customer g) Give a list of products bought by a customer having cust_id as 5 h) List the item details which are sold as of today i) Create a view which lists out the bill_no, bill_date, cust_id, item_id, price, qty_sold, amount.
3
Aim: a) Create the tables with the appropriate integrity constraints and Insert around 10 records in each of the tables
g) Give a list of products bought by a customer having cust_id as 5 SQL> select i.itemname from item i, sale s where s.custid=5 and i.itemid- s.itemid;
ITEMNAME -------------- Pens
h) List the item details which are sold as of today SQL> select i.itemid, i.itemname from item I, sale s where i.itemid=s.itemid and s.billdate=to_char(sysdate);
i) Create a view which lists out the bill_no, bill_date, cust_id, item_id, price, qty_sold, amount
SQL>create view cust as (select s.billno, s.billdate, c.custid, i. iitemid, i.price, s.qty from customer c,sale s item I where c.custid=s.custid and i.iemid=s.itemid); view created. SQL>select * from cust;
For the above schema, perform the following a) Create the tables with the appropriate integrity constraints b) Insert around 10 records in each of the tables c) List all the student names with their membership numbers d) List all the issues for the current date with student and Book names e) List the details of students who borrowed book whose author is CJDATE f) Give a count of how many books have been bought by each student g) Give a list of books taken by student with stud_no as 5 h) List the book details which are issued as of today
8
AIM: Create the tables with the appropriate integrity constraints Insert around 10 records in each of the tables
Name Null? Type . MEM_NO NOT NULL NUMBER(5) STUD_NO NUMBER(5)
SQL>insert into membership values(&mem_no,&stud_no); Enter value for mem_no:5440 Enter value for stud_no:510 old 1:insert into membership values(&mem_no,&stud_no) new 1:insert into membership values(5440,510) insert into membership values(5440,510) *
9
Errors Observed:
ERROR at line 1: ORA-02291:integrity constraint(HARISH.SYS_C002724)violated-primary key not found
Name Null? Type .. BOOK_NO NOT NULL NUMBER(5) BOOK_NAME VARCHAR2(20) AUTHOR VARCHAR2(20)
SQL>insert into book values(&book_no,&book_name,&author); SQL>select * from book;
BOOK_NO BOOK_NAME AUTHOR .. 9123 DBMS Rama Krishna 2342 JAVA Robett wilkins 4523 Fearless tales Alfred 8723 my ambition Harish 7821 Harry Potter JK Rowling
d) List all the issues for the current date with student and Book names
SQL> select i.issno, s.studname, b.bookname from iss_rec I, membership m, student s, book b 2 where i.memno=m.memno and m.studno=s.studno and i.issdate=to_char(sysdate);
e) List the details of students who borrowed book whose author is CJDATE
SQL> select * from student where studno in(select studno from membership where memno in 2 (select memno from iss_rec where bookno in(select bookno from book where author=CJDATE)));
f) Give a count of how many books have been bought by each student
SQL> select s.studno, count(i.bookno) from student s.membership m, book b, 2 iss_rec I where s.studno=m.studno and b.bookno=i.bookno group by s.studno;
g) Give a list of books taken by student with stud_no as 5
SQL> select bookname from book where bookno in (select bookno from iss_rec where 2 memno in(select memno from membership where 3 studno in(select studno from student where studno=5)));
BOOKNAME ------------- NT
h) List the book details which are issued as of today
SQL> delete from book where bookno in(select bookno from iss_rec where issdate=to_char(sysdate)); delete from book where bookno in (select bookno from iss_rec where issdate=to_char(sysdate))
For the above schema, perform the following a) Create the tables with the appropriate integrity constraints b) Insert around 10 records in each of the tables c) List the employee details department wise d) List all the employee names who joined after particular date e) List the details of employees whose basic salary is between 10,000 and 20,000 f) Give a count of how many employees are working in each department g) Give a names of the employees whose netsalary>10,000 h) List the details for an employee_id=5
13
AIM: Create the tables with the appropriate integrity constraints Insert around 10 records in each of the tables
For the above schema, perform the following a) Create the tables with the appropriate integrity constraints b) Insert around 10 records in each of the tables c) List all the customer names with their membership numbers d) List all the issues for the current date with the customer names and cassette names
18
AIM: Create the tables with the appropriate integrity constraints Insert around 10 records in each of the tables
d) List all the issues for the current date with the customer names and cassette names
SQL>select i.issno,c.custname,cc.cassettename from customer1 c,membership1 m,cassette cc,issrec1 I where i.issdate=to_char(sysdate) and c.custno=m.custno and i.cassno=cc.cassno and i.memno=m.memno;
For the above schema, perform the following a) Create the tables with the appropriate integrity constraints b) Insert around 10 records in each of the tables c) List all the machine allotments with the student names, lab and machine numbers d) List the total number of lab allotments day wise e) Give a count of how many machines have been allocated to the CSIT class f) Give a machine allotment etails of the stud_no 5 with his personal and class details g) Count for how many machines have been allocatedin Lab_no 1 for the day of the week as Monday h) How many students class wise have allocated machines in the labs
22
AIM: Create the tables with the appropriate integrity constraints Insert around 10 records in each of the tables
d) List the total number of lab allotments day wise
SQL>select l.machno,l.descrip,a.day from lab l,allotment a where a.machno=l.machno;
MACHNO DESCRIP DAY
1 UNIX MONDAY 22 UNIX TUESDAY 3 XP WEDNESDAY 4 WINDOWS THRUSDAY 5 ME FRIDAY
e) Give a count of how many machines have been allocated to the CSIT class
SQL>select count(machno)from allotment where studno in(select studno from student1 where class=CSIT);
COUNT (MACHNO) .. 1 6
25
f) Give a machine allotment etails of the stud_no 5 with his personal and class details
SQL>select a.studno,a.machno,s.studname,s.class from allotment a,student1 s where a.studno=s.studno and a.studno=503;
STUDNO MACHNO STUDNAME CLASS
503 5 ARVIND CSE
g) Count for how many machines have been allocatedin Lab_no 1 for the day of the week as Monday
h) How many students class wise have allocated machines in the labs
SQL>select count(studno) allocated students in the labs,class from student1 where studno in(select studno from allotment) group by class;
allocated students in the lab CLASS
2 CSE 1 ECE 1 EEE 1 IT
26
6) Create a Cursor which update the salaries of an Employee as follows.
1. if sal<1000then update the salary to 1500. 2. if sal>=1000 and <2000 then update the salary to 2500. 3. if sal>=2000 and <=3000 then update the salary to 4000. And also count the no.of records have been updated.*/
Code:
Declare Cursor my_cur is select empno,sal from emp; Xno emp.empno%type; Xsal emp.sal%type; C number; Begin Open my_cur; C:=0; Loop Fetch my_cur into xno,xsal; If(xsal<1000) then Update emp set sal=3000 where empno=xno; C:=c+1; Else if(xsal>=2000) and xsa<3000) then Update emp set sal=4000 where empno=xno; C:=c+1; End if; End if; Exit when my_cur%NOTFOUND ; End loop; Close my_cur; Dbma_output.put_line(c||records have been successfully updated); End; Sql>@a.sql; records have been successfully updated pl/sql procedure successfully completed.
Valid Test Data Before executing the cursor, the records in emp table as follows Sql>select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO -------- ---------- --------- ---------- --------- ---------- ---------------------- 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
.
14 rows selected.
28
7) Create a procedure which updates the salaries of an employees as follows.
1.if sal<1000 then update the salry to 1500. 2.if sal>=1000 and <=2400 then update the salary to 2500.*/
Create or replace procedure myproc as Cursor my_cur is select empno,sal from emp; Xno emp.empno%type; Xsal emp.sal%type; C number; Begin Open my_cur; C:=0; Loop Fetch my_cur into xno,xsal; If(xsal<1000) then Update emp set sal=1500 where empno=xno; C:=c+1; Else Is(xsal>=1000 and xsal<=2400) then Update emp set sal=2500 where empno=xno; C:=c+1; End if; End if; Exit when my_cur%NOTFOUND; End loop; Close my_cur; Dbms_output.put_line(c||records have been successfully updated); End; /
Valid Test Data: Procedure created. Sql>exec myproc;
29
OUTPUT:
Records have been successfully completed.
/* create function which add two given numbers. (Simple programs) */ Create or replace function add_fun(a number,b number) return Number as C number; Begin C:=a+b; Return c; End; / Function created.
/*add_fun specification*/ Declare Result number; Begin Result:=add_fun(10,20); Dbms_output.put_line(the sum of 10 and 20 is||result); End; Sql>/ The sum of 10 and 20 is 30 Pl/sql procedure successfully completed.
/*create a function which count total no.of employees having salary less than 6000.*/ /*function body*/ Create or replace function count_emp(esal number)return number as Cursor vin_cur as Select empno,sal from emp; Xno emp.empno%type; Xsal emp.sal%type; C number; Begin Open vin_cur; C:=0; loop fetch vin_cur into xno,xsal; if(xsal<esal) then c:=c+1; end if; exit when vin_cur%notfound; end loop; close vin_cur; return c; 30
end; / Function created. /*function specification*/ Declare Ne number; Xsal number; Begin Ne:=count_emp(xsal); Dbms_output.put_line(xsal); Dbma_output.put_line(there are ||ne||;employees); End; /