Dbms Lab Manual RGPV

download Dbms Lab Manual RGPV

of 38

description

description of dbms mannual

Transcript of Dbms Lab Manual RGPV

dbms.docx

Sagar Institute of Science, Technology & Research, SISTec-R

LAB MANUAL

Department Computer science and engineeringSession2013-14

Teachers name Prof. Anidra Katiyar & Prof. Sujeet singh www.sistec.ac.inSemesterFifth

SubjectRDBMSSub. codeME-506

LAB MANUALPrepared by: Prof. Anidra Katiyar & Prof. Sujeet singh SQL DML and DDLSQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL).The query and update commands form the DML part of SQL: SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a databaseThe DDL part of SQL permits database tables to be created or deleted. It also defines indexes (keys), specifies links between tables, and imposes constraints between tables. The most important DDL statements in SQL are: CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an indexSQL is a standard language for accessing and manipulating databases.

What is SQL?1-SQL stands for Structured Query Language2-SQL lets you access and manipulate databases 3-SQL is an ANSI (American National Standards Institute) standard

What Can SQL do?1-SQL can execute queries against a database2-SQL can retrieve data from a database3-SQL can insert records in a database 4-SQL can update records in a database5-SQL can delete records from a database6-SQL can create new databases7-SQL can create new tables in a database8-SQL can create stored procedures in a database9-SQL can create views in a database 10-SQL can set permissions on tables, procedures, and views

SQL is a Standard - BUT....Although SQL is an ANSI (American National Standards Institute) standard, there are many different versions of the SQL language.However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.Note: Most of the SQL database programs also have their own proprietary extensions in addition to the SQL standard!

Using SQL in Your Web SiteTo build a web site that shows some data from a database, you will need the following: An RDBMS database program (i.e. MS Access, SQL Server, MySQL) A server-side scripting language, like PHP or ASP SQL HTML / CSSSQL is a standard language for accessing databases.Our SQL tutorial will teach you how to use SQL to access and manipulate data in:MySQL, SQL Server, Access, Oracle, Sybase, DB2, and other database systems..CONSTRAINTS

PRIMARY CONSTRAINTS : PRIMARY, UNIQUE, CHECK, REFERENCESSECONDARY CONSTRAINTS: NOT NULL, DEFAULTCONSTRAINTS BASED ON 2 LEVELS COLUMN LEVEL and TABLE LEVELExcept Not null all others can be defined as Both table and column level.But Not null as only column level.E.g. For SECONDARY CONSTRAINTS1. NOT NULL CONSTRAINT CREATE TABLE EMP( ENO NUMBER(3) NOT NULL, ENAME VARCHAR2(10));

2. DEFAULT CONSTRAINTCREATE TABLE EMP(ENO NUMBER(3) NOT NULL, ENAME VARCHAR2(10),DOJ DATE DEFAULT SYSDATE);

3. PRIMARY CONSTRAINT (COLUMN LEVEL)UNIQUE CREATE TABLE EMP(ENO NUMBER(3) NOT NULL CONSTRAINT UNIEMP UNIQUE, ENAME VARCHAR2(10));

PRIMARY KEYCREATE TABLE EMP( ENO NUMBER(3) CONSTRAINT PKEMP PRIMARY KEY, ENAME VARCHAR2(10));

4. CHECK CONSTRAINTCREATE TABLE BANK( ACNO NUMBER(2) CONSTRAINT PKBANK PRIMARY KEY,ACTYPE VARCHAR2(2) CONSTRAINT CKBANK CHECK ( ACTYPE IN ('SB','CA','RD')),ACNAME VARCHAR2(10), AMOUNT NUMBER(4));

5. REFERENCESCREATE TABLE EMP( ENO NUMBER(3) CONSTRAINT PKE9 PRIMARY KEY,JOB VARCHAR2(10),ENAME VARCHAR2(10),MGR NUMBER(4) REFERENCES EMP(ENO));

6. REFERENCES (REFERING TO DIFFERENT TABLE)CREATE TABLE DEPT(DEPTNO NUMBER(2) CONSTRAINT PKDNO PRIMARY KEY, DNAME VARCHAR2(10), LOC VARCHAR2(10)); CREATE TABLE EMP( EMPNO NUMBER(4),ENAME VARCHAR2(10), SAL NUMBER(7,2),DEPTNO NUMBER(2) CONSTRAINT FKDNO REFERENCES DEPT(DEPTNO));

7. TABLE LEVEL CONSTRAINTSUNIQUE TABLE LEVELCREATE TABLE BANK( ACNO NUMBER(3),ACTYPE VARCHAR2(10), BAL NUMBER(7,2),PLACE VARCHAR2(10),CONSTRAINT UNIBANK UNIQUE(ACNO,ACTYPE)); PRIMARY KEY(TABLE LEVEL)CREATE TABLE BANK( ACNO NUMBER(2), ACTYPE VARCHAR2(2) CONSTRAINT CKBANK CHECK (ACTYPE IN ('SB','CA','RD')),AMOUNT NUMBER)

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-1

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub. codeCS-503

Program 1Delete duplicate row from the table.DELETE FROM DEPT WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT GROUP BY DEPTNO HAVING COUNT(DEPTNO)>1); DELETE FROM emp A WHERE ROWID NOT IN(SELECT MIN(ROWID) FROM emp WHERE A.DEPTNO=B.DEPTNO);

ORDELETE FROM DEPT A WHERE ROWID NOT IN (SELECT MIN(ROWID) FROM DEPT B WHERE A.DEPTNO=B.DEPTNO);

Ques1:-Delete the row containing name Ram?Ques2:-Delete all the rows having same name more than once?Ques3:- Delete the row of employee whose name start with M?

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-2

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub. codeCS-503

Program 2Display the alternate row from table.SELECT * FROM EMP WHERE ROWID IN(SELECT DECODE(MOD(ROWNUM,2),0,ROWID) FROM EMP);

ORSELECT * FROM GDEPT WHERE ROWID IN(SELECT DECODE(MOD(ROWNUM,2),0,ROWID) FROM GDEPT);

Ques1:-Show the name of those employees who earn commission?Ques2:-Show all employees who has no commission but have a10% hike in their salary?

Ques3:-Show the last name of all employees together with the number of years & the number of complete months that they have been employed?

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-3

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub.codeCS-503

Program 3Delete alternate row from table.DELETE FROM GDEPT WHERE ROWID IN(DELETE DECODE(MOD(ROWNUM,2),0,ROWID) FROM GDEPT);

Ques1:-Delete the row of employee who works in location Bombay?Ques2:- Delete the row of employee whose name end with N?Ques3:- Delete the row of employee whose salary is more than 25000?

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-4

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub. codeCS-503

Program 4

Update multiple rows in using single update statementDISPLAY ALL THE DETAILS WHERE DEPT IS EITHER SALES OR RESEARCH Select * from emp where dname = any(select dname from emp where dname = sales or dname = research);Select * from emp where dname = any(select dname from emp where Dname like(sales,research));

Ques1:-Find the name of those entire employee who work in Delhi and update their location to Bombay?Ques2:-Find the name of those dept which are in same city?Ques3:- Write a query to raise the salary by 50% of those employees who do not have a commission?

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-5

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub. codeCS-503

Program 5Find the third highest paid and third lowest paid salary.SOL: SELECT MAX(SAL) FROM EMP WHERE SAL(SELECT MIN(SAL) FROM EMP WHERE SAL>(SELECT MIN(SAL) FROM EMP WHERE SAL>(SELECT MIN(SAL) FROM EMP))));

Ques1:-Write a query to find all those employee who are in the dept which has the max salary of all dept?Ques2:- Write a query to find those entire employees who earn maximum salary?Ques3:- Write a query to find those employees who work in that dept in which the higher salary taker works?

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-6

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub.codeCS-503

DISPLAY from NTH ROWSELECT * FROM DEPT WHERE ROWID NOT IN(SELECT ROWID FROM DEPT WHERE ROWNUM SAVEPOINT B;Savepoint created. SQL> INSERT INTO DEPT VALUES(70,'IT','GOA');1 row created. SQL> SELECT * FROM DEPT; DEPTNO DNAME LOC--------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON 50 CS MYSORE 60 IP BANGALORE 70 IT GOA7 rows selected. SQL> ROLLBACK TO SAVEPOINT B;Rollback complete.SQL> SELECT * FROM DEPT; DEPTNO DNAME LOC--------- -------------- ------------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON 50 CS MYSORE 60 IP BANGALORE6 rows selected. temp~~~~prodname comment date1create table temp( prodname varchar2(10), comm varchar2(16), date1 date);declareqty NUMBER(5);pname VARCHAR2(10);beginselect quantity, prodname into qty,pname from inv whereprodname='&productname';if qty>5 thenDBMS_OUTPUT.PUT_LINE('THANK U FOR THE PURCHASES MADE VISIT AGAIN');update inv set quantity=quantity-1 where prodname=pname;commit;elseDBMS_OUTPUT.PUT_LINE('STOCK LEVEL IS BELOW ORDER LEVEL');insert into temp values(pname,'out of stock',sysdate);commit;end if;end;

Ques1:-Draw a sequence diagram for roll back and save point activity in ATM transaction?Ques2:-Write syntax for rollback SQL query using suitable example?

PL/SQLPL/SQL stands for Procedural Language/SQL.PL/SQL extends SQL by adding constructs found in procedural languages, resulting in a structural language that is more powerful than SQL.The basic unit in PL/SQL is a block, All PL/SQL programs are made up of blocks, which can be nested within each other. Typically, each block performs a logical action in the program.Block has the following structure:DECLARE/* Declarative section: variables, types, and local subprograms.*/BEGIN/* Executable section: procedural and SQL statements gohere. *//* This is the only section of the block that is required. */EXCEPTION/* Exception handling section: error handling statements gohere. */END;Let us see an example of the aboveDECLARETEMP_COST NUMBER(10, 2);BEGINSELECT COST FROM JD11.BOOK INTO TEMP_COSTWHERE ISBN = 21;IF TEMP_COST > 0 THENUPDATE JD11.BOOK SET COST =(TEMP_COST*1.175) WHERE ISBN = 21;ELSEUPDATE JD11.BOOK SET COST = 21.32 WHEREISBN = 21;END IF;COMMIT;EXCEPTIONWHEN NO_DATA_FOUND THENINSERT INTO JD11.ERRORS (CODE, MESSAGE)VALUES(99, ISBN 21 NOT FOUND);END;

Only the executable section is required. The other sections are optional. The only SQL statements allowed in a PL/SQL program are SELECT, INSERT, UPDATE, DELETE and several other data manipulation statements plus some transaction control. Data definition statements like CREATE, DROP, or ALTER are not allowed. The executable section also contains constructs such as assignments, branches, loops, procedure calls, and triggers, which are all described below (except triggers). PL/SQL is not case sensitive. C style comments (/* ...*/) may be used. To execute a PL/SQL program, we must follow the program text itself by A line with a single dot (.), and then A line with run; As with Oracle SQL programs, we can invoke a PL/SQLprogram either by typing it in sqlplus or by putting the code in afile and invoking the file in the various ways we learned inGetting Started With Oracle.What are the Variables?Information is transmitted between a PL/SQL program and the database through variables. Every variable has a specific type associated with it. That type can be1-One of the types used by SQL for database columns2-A generic type used in PL/SQL such as NUMBER3- Declared to be the same as the type of some database ColumnThe most commonly used generic type is NUMBER. Variables of type NUMBER can hold either an integer or a real number.The most commonly used character string type is VARCHAR (n), where n is the maximum length of the string in bytes. This length is required, and there is no default. For example, we might declare:DECLAREprice NUMBER;MyBeer VARCHAR (20);You know that PL/SQL allows BOOLEAN variables, eventhough Oracle does not support BOOLEAN as a type fordatabase columns.Types in PL/SQLTypes in PL/SQL can be tricky. In many cases, a PL/SQL variable will be used to manipulate data stored in a existing relation. In this case, it is essential that the variable have the same type as the relation column. If there is any type mismatch, variable assignments and comparisons may not work the way you expect. To be safe, instead of hard coding the type of a variable, you should use the %TYPE operator. For example:DECLAREgives PL/SQL variable myBeer whatever type was declared forthe name column in relation Beers.

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-11

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub.codeCS-503

Program 11Write a pl/sql for select, insert, update and delete statements.CREATE TABLE TEMP( ENAME VARCHAR2(10),DESIG VARCHAR2(10),SAL NUMBER(7,2));DECLARENAME VARCHAR2(10);DESIG VARCHAR2(10);SALARY NUMBER(7,2);ENO NUMBER(4):=&EMPNO;BEGINSELECT ENAME,JOB,SAL INTO NAME,DESIG,SALARY FROM EMP WHERE EMPNO=ENO;DBMS_OUTPUT.PUT_LINE(ENO||' '||NAME||' '||SALARY||' '||DESIG);IF DESIG='CLERK' THENDELETE FROM EMP WHERE EMPNO=ENO;INSERT INTO TEMP VALUES(NAME,DESIG,SALARY);DBMS_OUTPUT.PUT_LINE('DELETED FROM EMP AND INSERTED TO TEMP');COMMIT;ELSIF DESIG='MANAGER' THENUPDATE EMP SET SAL=SALARY+200 WHERE EMPNO=ENO;DBMS_OUTPUT.PUT_LINE('INCREMENTED SALARY IS '||TO_CHAR(SALARY+200));END IF;END;

Ques1:- Write a pl/sql for merge statement using suitable example?Ques2:-Write a query to create a view for DEPT table (Full view, View of fragmented table) ?

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-12

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub. codeCS-503

Program 12Write a pl/sql block to delete a record. If delete operation is successful return 1 else return 0.create or replace function fun3(n emp.empno%type) return number isa number;begindelete from emp where empno=n;if sql%found thenreturn 1;elsereturn 0;end if;--exception--when no_data_found then--return 100;end;declaren number;beginn:=fun3(&empno);dbms_output.put_Line(n);if n=0 thendbms_output.put_line('deletion unsuccessfully');elsif n=1 thendbms_output.put_line('deletion successfull');end if;end;

Ques1:-Write a query to run grant and revoke command?Ques2:-What is sequence?

CursorsWhat are Cursors?A cursor is a variable that runs through the tuples of some relation. This relation can be a stored table, or it can be the answer to some query. By fetching into the cursor each tuple of the relation, we can write a program to read and process the value of each such tuple. If the relation is stored, we can also update or delete the tuple at the current cursor position. The example below illustrates a cursor loop. It uses our example relation T1(e,f) whose tuples are pairs of integers. The program will delete every tuple whose first component is less than the second, and insert the reverse tuple into T1.

DECLARE/* Output variables to hold the result of the query: */a T1.e%TYPE;b T1.f%TYPE;/* Cursor declaration: */CURSOR T1Cursor ISSELECT e, fFROM T1WHERE e < fFOR UPDATE;BEGINOPEN T1Cursor;LOOP/* Retrieve each row of the result of the above queryinto PL/SQL variables: */FETCH T1Cursor INTO a, b;/* If there are no more rows to fetch, exit the loop: */EXIT WHEN T1Cursor%NOTFOUND;/* Delete the current tuple: */DELETE FROM T1 WHERE CURRENT OF T1Cursor;/* Insert the reverse tuple: */INSERT INTO T1 VALUES(b, a);END LOOP;/* Free cursor used by the query. */CLOSE T1Cursor;END;

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-13

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub. codeCS-503

Program 13Display name, hire date of all employees using cursors.DECLARE cursor c1 is select ename,hiredate from emp; name varchar(20); hdate date;begin open c1; loop fetch c1 into name, hdate; exit when c1%NOTFOUND; dbms_output.put_line(name||' '||hdate); end loop; close c1;end;

Ques1:-Display maximum salary using cursor?Ques2:-Display salary of all employees in descending order using cursor?

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-14

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub. codeCS-503

Program 14Display details of first 5 highly paid employees using cursors

DECLARE cursor c1 is select * from emp order by sal desc; a c1%rowtype;begin open c1; loop fetch c1 into a; exit when c1%rowcount>6; dbms_output.put_line(a.ename||' '||a.sal||' '||a.job||' '||C1%ROWCOUNT); end loop; close c1;end;

Ques1:-Write a query to find the details of those employees who have same job using cursor?Ques2:-Write a query to show dept where no sales representative works using cursor?

TriggersA trigger (essentially, a stored SQL statement associated with a table) is a database object that defines events that happen when some other event, called a triggering event, occurs. Create a trigger by using the CREATE TRIGGER statement. Triggers execute when an INSERT, UPDATE, or DELETE modifies a specified column or columns in the subject table. Typically, the stored SQL statements perform an UPDATE, INSERT, or DELETE on a table different from the subject table.Sometimes a statement fires a trigger, which in turn, fires another trigger. Thus the outcome of one triggering event can itself become another trigger. The Teradata RDBMS processes and optimizes the triggered and triggering statements in parallel to maximize system performance.

Trigger FunctionsUse triggers to perform various functions:1- Define a trigger on the parent table to ensure that UPDATEs and Deletes performed to the parent table are propagated to the child table.2- Use triggers for auditing. For example, you can define a trigger which causes INSERTs in a log record when an employee receives a raise higher than 10%.3- Use a trigger to disallow massive UPDATEs, INSERTs, or DELETEs duringbusiness hours.

For example: you can use triggers to set thresholds for inventory of each item by store, to create a purchase order when the inventory drops below threshold, or to change a price if the daily volume does not meet expectations.

Restrictions on Using TriggersTeradata triggers do not support FastLoad and MultiLoad utilities and, andyou must disable triggers before you run load utilities. In addition, a positioned(updatable cursor) UPDATE or DELETE is not allowed to fire a trigger andgenerates an error.Note: You cannot define a join index on a table with a trigger.CREATE TRIGGER AFTER UPDATE/INSERT/DELETE OF ON FOR EACH ROW BEGIN ----- ----- executable statements; ----- -----END;

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-15

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub. codeCS-503

Program 15

15.Write a database trigger which fires if you try to insert, update, or delete after 7o clock

CREATE OR REPLACE TRIGGER GEETIME BEFORE INSERT OR UPDATE OR DELETE ON EMP for each row DECLARE A VARCHAR2(10); BEGIN SELECT TO_CHAR(SYSDATE,'HH:MI') INTO A FROM DUAL; IF A > '06:59' then RAISE_APPLICATION_ERROR(-20500,'YOU CANT DO THIS OPERATION NOW'); END IF; END;

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-16

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub. codeCS-503

Program 16

16. Write a data base trigger, which acts just like primary key and does not allow duplicateCREATE OR REPLACE TRIGGER PRIKEY BEFORE INSERT ON EMP FOR EACH ROW DECLARE A NUMBER; BEGIN SELECT COUNT(*) INTO A FROM EMP WHERE EMPNO=:NEW.EMPNO; IF A >=1 THEN RAISE_APPLICATION_eRROR(-20500,'THE PRI KEY RULE IS VOILATED'); ELSIF A=0 THEN PRINT('RECORD IS INSERTED'); END IF; END;SQL> INSERT INTO EMP(EMPNO,DEPTNO) VALUES(7788,20);INSERT INTO EMP(EMPNO,DEPTNO) VALUES(7788,20)ERROR at line 1:*ORA-20500: THE PRI KEY RULE IS VOILATEDORA-06512: at "GEETHA.PRIKEY", line 6ORA-04088: error during execution of trigger 'GEETHA.PRIKEY'SQL> INSERT INTO EMP(EMPNO,DEPTNO) VALUES(77,20);

1 row created.

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-17

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub. codeCS-503

Program 1717. Create a data base trigger, which performs the action of the on delete cascade CREATE OR REPLACE TRIGGER DELDEPT AFTER DELETE ON DEPT FOR EACH ROW BEGIN DELETE FROM EMP WHERE DEPTNO=:OLD.DEPTNO; PRINT('RECORDS IN EMP ARE ALSO DELETED'); END;

Sagar Institute of Science, Technology & Research, SISTec-R

EXPERIMENT NO-18

Department Computer science and engineeringSession2013-14

Teachers name Anidra KatiyarSemesterFifth

SubjectDatabase management SystemSub. codeCS-503

Program 18

18. Write a data base trigger, which should not delete from emp table if the day is Sunday.CREATE OR REPLACE TRIGGER EMPNO_CHECK BEFORE DELETE ON empBEGIN if to_char(sysdate,'dAy')='SUNDAY' then raise_application_error(-20001,'TO DAY IS SUNDAY '); end if;END;

Ques 1:- What are triggers? How many triggers you can have on a table? How to invoke a trigger on demand?Ques2:- Write a data base trigger, for the employee whose day of joining is Sunday?1. Why is it important to write to the log before changing the database values?