dbms_js in database management systems with its operations and its functionalities

Upload: suganya-periasamy

Post on 04-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    1/711

    EXPNO: 1 DATABASE LANGUAGES********************

    DATE:

    AIM:To study and execute the data definition language (DDL), data manipulation language (DML), Data

    Control Languages (DCL) and Transaction Control Language (TCL) commands using SQL.

    LIST OF DDL COMMANDS: CREATE TABLE Command. DESCRIBE Command. ALTER TABLE Command. DROP TABLE Command. TRUNCATE TABLE Command.

    LIST OF DML COMMANDS: INSERT Command.

    SELECT Command. UPDATE Command. DELETE Command.

    LIST OF DCL COMMANDS: GRANT Command. REVOKE Command.

    LIST OF TCL COMMANDS: COMMIT Command. ROLLBACK Command.

    PROCEDURE TO CREATE AN USER:1. Create your own user first.2. Create one user name with password.3. If you are in any other system other than sys manager, do the following steps:4. SQL> connect system/manager;

    Itll display as CONNECTED.5. SQL> create user com1 identified by student;

    Where, Com1 - user name.

    Student - password.

    6. CREATE SESSION must be granted by the system manager to the user (com1).7. SQL>connect com1/student;

    Connected.

    DESCRIPTION OF DDL COMMANDS:

    i) CREATE:PURPOSE: This command is used to create a table.SYNTAX :

    Create table (column_name1 datatype size, column_name2 datatype size);CREATE TABLE tablename (column_name data_ type constraints, )

    EXAMPLE:

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    2/712

    SQL> create table empl(empno number(5) primary key, ename char(20), dept (10));

    ii) DESCRIBE:This command display the table structure.

    SYNTAX:sQL>desc tablename;

    EXAMPLE:desc electronic1;

    iii)ALTER:PURPOSE: This command is used to add a new column in or modify a column to the structure ofthe table.SYNTAX:

    SQL>alter table tablename add(columnname datatype,.);SQL>alter table tablename modify(columnname (change the size of the datatype),.);SQL>alter table tablename drop(columnname ,.);SQL>ALTER TABLE EMP ADD CONSTRAINT Pkey1 PRIMARY KEY (EmpNo);

    EXAMPLE:

    alter table electronic1 modify(productsname char(11));SQL> alter table empl add(bp number(8));

    iv) DROP:PURPOSE:This command is used to delete or removing the entire table

    Deletes table structureCannot be recovered because Use with cautionSYNTAX: drop table ;EXAMPLE: drop table electronic;

    v)TRUNCATE:PURPOSE :This command is used to delete or remove the contents of the tableSYNTAX : truncate table ;EXAMPLE: Truncate table electronics1;OUTPUT:

    BEFORE TRUNCATE:SQL> truncate table empl;AFTER TRUNCATE:SQL> select * from empl;

    iv) DELETE TABLE Command:SQL> delete table empl;

    DESCRIPTION OF DML COMMANDS:

    i)INSERT:

    PURPOSE:

    This command is used to insert the values in to the table.

    SYNTAX:

    Insert into values (value1,value2,);

    EXAMPLE:

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    3/713

    SQL> insert into empl values(101,XXX,CSE,12000);

    SQL> insert into empl values(102,YYY,MECH,10000);

    iii) SELECT:

    PURPOSE:

    This command is used to display the entire table.

    SYNTAX:

    Select * from ;EXAMPLE:

    SQL> select * from empl;

    iii)UPDATE:

    PURPOSE:

    This command is used to update the old values in to the new values.

    SYNTAX:

    Update set column_name=value where

    EXAMPLE:SQL> update empl set bp=15000 where eid=101;

    SQL> select * from empl;

    iv)DELETE:

    PURPOSE:

    This command is used to delete the values from the table.

    SYNTAX:

    delete from where ;

    EXAMPLE:SQL> delete from empl where eid=102;

    v)GRANT Command:PURPOSE:This command is used to give some permissions (like select, update) to he user.

    SYNTAX:

    grant on to ;

    where:system privileges is nothing but the DML commands (insert, alter, update, select,..).

    vi)REVOKE Command:PURPOSE: This command is used to release all permissions .

    SYNTAX :

    Revoke on from ;

    vii)COMMIT Command:

    PURPOSE: This command is used to make all transaction changes permanent to the data base. This command erases all save points and release the transaction lock.

    SYNTAX:

    commit;

    SAVEPOINT:

    Savepoint is not a command. It is only a marker. Savepoint are used to divide a lengthy

    transaction into smaller ones.

    SYNTAX:

    savepoint ID;

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    4/714

    viii)ROLLBACKCommand:PURPOSE:

    This command is used to undo all the changes made in the current transaction. We can either rollback the entire transaction or rollback a transaction to a savepoint.

    SYNTAX:

    (a): rollback; (This will rollback(undo) the entire transaction).

    (b): rollback to savepoint ;

    (Thisll undo all changes made after the creation of the savepoint id).

    RESULT:Thus the above program for data manipulation language (DML), Data Control Languages (DCL) and

    Transaction Control Language (TCL)has been executed successfully and the output is verified.

    Exp no: 2

    NESTED QUERIES & AGGREGATE FUNCTIONSDate:

    AIM:To study and execute the queries for nested queries & aggregate functions.

    PROCEDURE:Writing and Practice of Simple Queries.

    1. Get the description of EMP table.

    SQL>desc emp;

    2. Get the description DEPT table.SQL>desc dept;

    3.List all employee details.

    SQL>select * from emp;

    4.List all employee names and their salaries, whose salary lies between 1500/- and 3500/-

    both inclusive.

    SQL>select ename from emp where sal between 1500 and 3500;

    5. List all employee names and their and their manager whose manager is 7902 or 7566 0r

    7789.SQL>select ename from emp where mgr in(7602,7566,7789);

    6. List all employees which starts with either J or T.

    SQL>select ename from emp where ename like J% or ename like T%;

    7. List all employee names and jobs, whose job title includes M or P.

    SQL>select ename,job from emp where job like M% or job like P%;

    8. List all jobs available in employee table.

    SQL>select distinct job from emp;

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    5/715

    9. List all employees who belongs to the department 10 or 20.

    SQL>select ename from emp where deptno in (10,20);

    10. List all employee names , salary and 15% rise in salary.

    SQL>select ename , sal , sal+0.15* sal from emp;

    11. List minimum , maximum , average salaries of employee.SQL>select min(sal),max(sal),avg(sal) from emp;

    12. Find how many job titles are available in employee table.

    SQL>select count (distinct job) from emp;

    13. What is the difference between maximum and minimum salaries of employees in the

    organization?

    SQL>select max(sal)-min(sal) from emp;

    14. Display all employee names and salary whose salary is greater than minimum salary of

    the company and job title starts with M.SQL>select ename,sal from emp where job like M% and sal > (select min (sal) from emp);

    15. Find how much amount the company is spending towards salaries.

    SQL>select sum (sal) from emp;

    16. Display name of the dept. With deptno 20.

    SQL>select ename from emp where deptno = 20;

    17. List ename whose commission is NULL.

    SQL>select ename from emp where comm is null;

    18. Find no.of dept in employee table.

    SQL>select count (distinct ename) from emp;

    19. List ename whose manager is not NULL.

    SQL>select ename from emp where mgr is not null;

    Writing Queries using GROUP BY and other clauses.

    To write queries using clauses such as GROUP BY, ORDER BY, etc. and retrieving information

    by joining tables.

    Source tables: emp, dept, programmer, software, study.Order by: The order by clause is used to display the results in sorted order.

    Group by: The attribute or attributes given in the clauses are used to form groups. Tuples with the

    same value on all attributes in the group by clause are placed in one group.

    Having: SQL applies predicates (conditions) in the having clause after groups have been formed, so

    aggregate function be used.

    1. Display total salary spent for each job category.

    SQL>select job,sum (sal) from emp group by job;

    2. Display lowest paid employee details under each manager.

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    6/716

    SQL>select ename, sal from emp where sal in (select min(sal) from emp group by mgr);

    3. Display number of employees working in each department and their department name.

    SQL> select dname, count (ename) from emp, dept where emp.deptno=dept.deptno group by

    dname;

    4. Display the sales cost of package developed by each programmer.

    SQL>select pname, sum(scost) from software group by pname;

    5. Display the number of packages sold by each programmer.

    SQL>select pname, count(title) from software group by pname;

    6. Display the number of packages in each language for which the development cost is less

    than thousand.

    SQL>select devin, count(title) from software where dcost < 1000 group by devin;

    7. Display each institute name with number of students.

    SQL>select splace, count(pname) from study group by splace;

    8. How many copies of package have the least difference between development and selling

    cost, were sold?

    SQL>select sold from software where scost dcost=(select min(scost dcost) from software);

    9. Which is the costliest package developed in Pascal.

    SQL>select title from software where devin = PASCAL and dcost = ( select max(dcost)from software

    where devin = PASCAL);

    10. Which language was used to develop most no .of packages.

    SQL>select devin, count (*) from software group by devin having count(*) = (select max(count(*) )from software group by devin);

    11.Who are the male programmers earning below the average salary of female

    programmers?

    SQL>select pname from programmer where sal < (select avg(sal) from programmer where sex =

    F) and sex = M;

    12. Display the details of software developed by the male programmers earning more than

    3000/-.

    SQL>select programmer.pname, title, devin from programmer, software where sal > 3000 and sex =

    M and programmer.pname = software.pname;

    13. Display the details of software developed in c language by female programmers of

    pragathi.

    SQL>select software.pname, title, devin, scost, dcost, sold from programmer, software, study where

    devin = c and sex =F and splace = pragathi and programmer.pname = software.pname andsoftware.pname = study.pname;

    14. Which language has been stated by the most of the programmers as proficiency one?

    SQL>select prof1, count(*) from programmer group by prof1 having count (*) = (select max (count

    (*) ) from programmer group by prof1);

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    7/717

    Writing Nested Queries.

    To write queries using Set operations and to write nested queries.

    Set Operations:

    UNION - OR

    INTERSECT - AND

    EXCEPT - - NOTNESTED QUERY:- A nested query makes use of another sub-query to compute or retrieve the

    information.

    1. Find the name of the institute in which the person studied and developed the costliest

    package.

    SQL>select splace, pname from study where pname = (select pname from software where scost =

    (select max (scost) from software);

    2. Find the salary and institute of a person who developed the highest selling package.

    SQL> select study.pname, sal, splace from study, programmer where study.pname =

    programmer.pname and study.pname = (select pname from software where scost = (select max

    (scost) from software));

    3. How many packages were developed by the person who developed the cheapest package.

    SQL>select pname, count (title) from software where dcost = (select min(dcost) from software)

    group by pname;

    4. Calculate the amount to be recovered for those packages whose development cost has not

    yet recovered.

    SQL>select title , (dcost-scost) from software where dcost > scost;

    5. Display the title, scost, dcost, difference of scost and dcost in the descending order of

    difference.SQL> select title, scost, dcost, (scost - dcost) from software descending order by (scost-dcost);

    6. Display the details of those who draw the same salary.

    SQL> select p.pname, p.sal from programmer p, programmer t where p.pname t.pname and p.sal

    = t.sal;(or)

    SQL>select pname,sal from programmer t where pnamet.pname and sal= t.sal;

    WORKING WITH SQL COMMANDS:

    SINGLE ROW FUNCTIONS:

    SQL> select add_months(sysdate,40)from dual;

    SQL> select sysdate from dual;SQL> select last_day(sysdate)from dual;

    SQL> select months_between(sysdate,'4-july-1989')from dual;

    SQL> select next_day(sysdate,'tuesday')from dual;

    SQL> select next_day(sysdate,'friday')from dual;

    SQL> select next_day('17-feb-2010','tuesday')from dual;

    SQL> select round(sysdate,'year')from dual;

    SQL> select round(sysdate,'month')from dual;

    SQL> select greatest('10-jan-2008','tuesday')from dual;

    SQL> select greatest('10-jan-2008','10-jan-2009')from dual;

    SQL> select greatest(sysdate,'10-jan-2009')from dual;

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    8/718

    SQL> select greatest('10-jan-2008',sysdate,'10-jan-2009')from dual;

    SQL> select chr(65)from dual;

    SQL> select concat('bhava','tharani')from dual;

    SQL> select lower('PRAKASH')from dual;

    SQL> select lpad(type,4,'*')from turbo;

    SQL> select rpad(type,3,'*')from turbo;

    SQL> select ltrim('xabcde1jo','x')from dual;

    SQL> select ltrim('xxxxr','x')from duaL;SQL> select rtrim('xxxrxxx','x')from dual;

    SQL> select replace('tamil','t','T')from dual;

    SQL> select replace('main','streat')from dual;

    SQL> select substr('abcd,2)from dual;

    SQL> select upper('type')from dual;

    SQL> select ascii('a')from dual;

    SQL> select ascii('A')from dual;

    SQL> select instr('haifriends','en',3,1) from dual;

    SQL> select instr('this is test','is',3,1)from dual;

    SQL> select instr('this is test','is')from dual;

    SQL> select instr('this is test','is',1,1)from dual;SQL> select instr('haifriends','en',0) from dual;

    SQL> select instr('coso','o',0)from dual;

    SQL> select instr('coso co','o',4)from dual;

    SQL> select instr('cos0 c0','0') from dual;

    SQL> select length('rtq gfdg')from dual;

    SQL> select abs(-65.8)from dual;

    SQL> select abs(65.8)from dual;

    SQL> select ceil(45.04)from dual;

    SQL> select ceil(45.90)from dual;

    SQL> select ceil(45.00)from dual;

    SQL> select cos(45)from dual;SQL> select cos(45*(2217/180))from dual;

    SQL> select cos(0)from dual;

    SQL> select cosh(0)from dual;

    SQL> select exp(0)from dual;

    SQL> select exp(5)from dual;

    SQL> select floor(43.46)from dual;

    SQL> select exp(1)from dual;

    SQL> select ln(1)from dual;

    SQL> select mod(12,5)from dual;

    SQL> select power(2,2)from dual;

    SQL> select power(2,3)from dual;SQL> select round(45.01)from dual;

    SQL> select sign(-45)from dual;

    SQL> select sign(45)from dual;

    SQL> select tan(0)from dual;

    SQL> select tanh(0)from dual;

    Writing Queries using functions.

    AIM: To write queries using single row functions and group functions.

    1. Display the names and dob of all programmers who were born in january.

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    9/71

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    10/7110

    EXP NO: 3 CONSTRAINTS IN SQLDATE:

    AIM:

    To study and execute the constraints using SQL.

    LIST OF CONSTRAINTS:

    It is a declarative way of defining a business rule for a column of a table. An integrityconstraints is a statement about the table, data i.e. always live .The type are

    NULL CONSTRAINTS. NOT NULL CONSTRAINTS. PRIMARY KEY CONSTRAINTS. CHECK CONSTRAINTS. FOREIGN KEY CONSTRAINTS.

    DESCRIPTIONS:

    1) DOMAIN INTEGRITY CONSTRAINTS:

    i)NOTNULL Constraint: When a not null constraint is enforced on a column or a group of columns in a table,

    oracle will not allow null values in the columns.

    Null value is different from zero value. Also a null values in one column is notequivalent to the null value in another column.

    SYNTAX:

    create table ( not null, .);EXAMPLE:

    SQL> create table master(sno number(3) NOT NULL, name CHAR(10) NOT NULL, room_no

    number(2), address varchar2(10));

    After this, oracle will not allow inserting a row with null values in the columns sno, name.

    ii)CHECK Constraint:

    If there is a necessity to enforce an integrity rule based on a logical or a

    Boolean expression; we can use the CHECK constraint.

    EXAMPLE:

    SQL>create table master(roll_no number(3) check (roll_no between 101 and 200), name char(10),

    address varchar2(20));

    Checks the roll number between 101 and 20.OPERATORS USED IN CHECK CONSTRAINT:

    IN NOT IN BETWEEN LIKELIKE:is used to perform pattern matching. There are two patterns % and _ (underscore).

    (a)% represents any number of spaces or characters.(b)_ represents one space or character.

    2) ENTITY INTEGRITY CONSTRAINTS:

    i)UNIQUE CONSTRAINTS:

    Unique key constraint is used to prevent duplication of values within the rowof a specified column or a group of columns in a table.

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    11/7111

    This does not allow a duplicate value in a column or set of columns.SYNTAX:

    SQL> create table tname (column name data type unique..));

    EXAMPLE:

    SQL>create table student(roll_no number(4) unique, name char(10), address varchar2(10));

    At the output oracle will not allow the same entry inside the ROLL_NO column.

    ii)PRIMARY KEY CONSTRAINTS:

    A primary key is one or more columns in a table which identifies each row in the tableuniquely.

    This constraint does not allow NULL values and duplicate values across the columnscomprising the primary key.

    SYNTAX:

    SQL>: create table tname(col name data type primary key));EXAMPLE:

    SQL>create table stud(roll_no number(4) primary key, name char(10), address

    varchar2(20));

    3) REFERENTIAL INTEGRITY CONSTRAINTS:

    Referential integrity constraints are used to establish a parent-child relationshipbetween two tables having a common column.

    To implement this integrity the column should be defined as primary key in theparent table and the same column as a foreign key in the child table.

    SYNTAX:

    SQL> create table tname(col name data type references ON

    CASCADE); (or)

    SQL> create table tname(col1 name data type, constraint constraint_name foreign key(col1 name)

    references other_table_name(col name), col2 name data type,.);

    EXAMPLE:SQL>create table master(id number(4) foreign key references student(roll_no), mark

    number(3)); (or)

    SQL> create table master(id number(4), constraint fkey foreign key(id) references

    student(roll_no),mark number(3));

    Here the foreign key ID references the primary key ROLL_NO from the table student.

    It allows each value in a column or set of columns match a value in related tables unique or primarykey.

    RESULT:

    Thus the above program for studying and executing the integrity constraints has been

    executed successfully and the output is verified.

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    12/7112

    EXPT NO: 4

    JOIN FUNCTION

    DATE:

    AIM:To execute the join function operation using queries

    PROCEDURE:A joinis a query that combines rows from two or more tables, views, or materialized views.

    Oracle performs a join whenever multiple tables appear in the query's FROM clause. The query's

    select list can select any columns from any of these tables. If any two of these tables have a column

    name in common, you must qualify all references to these columns throughout the query with table

    names to avoid ambiguity.

    1)Equi joins:An equijoinis a join with a join condition containing an equality operator ( = ). An equijoin

    combines rows that have equivalent values for the specified columns.

    EXAMPLE:

    select e.empno, e.ename, e.sal, e.deptno, d.dname, d.city from emp e, dept d where

    emp.deptno=dept.deptno;

    (OR)select * from emp,dept where emp.deptno=dept.deptno;

    2)Non Equi Joins:Non equi joins is used to return result from two or more tables where exact join is not

    possible.

    EXAMPLE:

    select e.empno, e.ename, e.sal, s.grade from emp e, salgrade s where e.sal between s.lowsal and

    s.hisal

    a)Self JoinsA self join is a join of a table to itself. This table appears twice in the FROM clause and is

    followed by table aliases that qualify column names in the join condition. To perform a self join,

    Oracle combines and returns rows of the table that satisfy the join condition.

    EXAMPLE:

    Select e.empno, e.ename, m.ename Manager from emp e, emp m where e.mgrid=m.empno

    b)Inner JoinAn inner join(sometimes called a "simple join") is a join of two or more tables that returns

    only those rows that satisfy the join condition.

    SYNTAX:

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    13/7113

    SELECT , FROM INNERJOIN

    ON =

    EXAMPLE:

    SELECT p.last_name, t.title_nameFROM person p INNERJOIN title tON p.title_1 =t.title_abbrev;

    c) Outer Joins:An outer joinextends the result of a simple join. An outer join returns all rows that satisfythe join condition and also returns some or all of those rows from one table for which no rows from

    the other satisfy the join condition.

    i) Left Outer JoinTo write a query that performs an outer join of tables A and B and returns all rows from A (a left

    outer join), use the ANSI LEFT[OUTER] JOINsyntax, or apply the outer join operator (+) to all columns of

    B in the join condition. For all rows in A that have no matching rows in B, Oracle returns null for any

    select list expressions containing columns of B.

    SYNTAX:

    SELECT , FROM LEFT OUTERJOIN

    ON =

    EXAMPLE:

    SELECT p.last_name, t.title_nameFROM person p LEFT OUTERJOIN title tON p.title_1 =

    t.title_abbrev;

    ii) Right Outer JoinTo write a query that performs an outer join of tables A and B and returns all rows from B (a right

    outer join), use the ANSI RIGHT[OUTER] syntax, or apply the outer join operator (+) to all columns of A in

    the join condition. For all rows in B that have no matching rows in A, Oracle returns null for any select list

    expressions containing columns of A.

    SYNTAX:

    SELECT , FROM RIGHT OUTERJOIN

    ON =

    EXAMPLE:

    SELECT p.last_name, t.title_nameFROM person p RIGHT OUTERJOIN title tON p.title_1 =

    t.title_abbrev;

    iii) Full Outer JoinTo write a query that performs an outer join and and returns all rows from A and B, extended with

    nulls if they do not satisfy the join condition (a full outer join), use the ANSI FULL[OUTER] JOINsyntax.

    SYNTAX:

    SELECT , FROM FULL OUTERJOIN

    ON =

    EXAMPLE:

    SELECT p.last_name, t.title_nameFROM person p FULL OUTERJOIN title tON p.title_1 =

    t.title_abbrev;

    select e.empno,e.ename,e.sal,e.deptno,d.dname,d.city from emp e, dept d wheree.deptno(+)=d.deptno;

    http://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/SELECT.htmhttp://psoug.org/definition/ON.htmhttp://psoug.org/definition/JOIN.htmhttp://psoug.org/definition/FROM.htmhttp://psoug.org/definition/SELECT.htm
  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    14/7114

    That is specify the (+) sign to the column which is lacking values.

    RESULT: Thus the above program for studying and executing the joins has been executedsuccessfully and the output is verified.

    EXP NO: 5 VARIOUS SELECT COMMANDS and INDEXES

    ***************************************DATE:

    AIM:To study and execute various select commands and indexes using SQL.

    PROCEDURE:TYPES OF SELECT COMMAND:

    GROUP BY ORDER BY HAVING and BETWEEN DISTINCT

    SETOPERATORS: UNION UNION ALL INTERSECT MINUS

    DESCRIPTION OF COMMANDS:

    SQL> select * from dept;D_ID D_NAME

    ---------- ----------1 CSE2 IT3 MECH4 ECE5 EeE6 Accounts7 CIVIL8 AUTO9 EI

    10 AEROSQL> select * from employee;

    EID NAME AGE SALARY DEPT_ID---------- --------------- ---------- ---------- ----------

    1 Ram 20 5000 22 Sam 25 7000 23 ramesh 54 4000 44 Senthil 27 6700 55 Saranya 25 5700 26 Sathis 26 7400 37 Karthi 24 6200 1

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    15/7115

    8 Jaga 25 12000 7

    i)GROUP BY:This command is used to display the group of values.

    SYNTAX:Select column1, column2 from group by column1 havingcondition;

    EXAMPLE:SQL> select dept_id,avg(salary) from employee group by dept_id;

    DEPT_ID AVG(SALARY)---------- -----------

    1 62002 59004 40005 67003 74007 12000

    SQL> select d_name,count(eid) from employee,dept where employee.dept_id=dept.d_id group byd_name;

    D_NAME COUNT(EID)---------- ----------IT 3EeE 1CIVIL 1CSE 1MECH 1ECE 1

    ii)ORDERBY:This command is used to display the column in an order(ascending order is defaultin all SQL queries).

    SYNTAX:Select * from orderby condition;

    EXAMPLE:SQL> select * from employee order by name;

    EID NAME AGE SALARY DEPT_ID---------- --------------- ---------- ---------- ----------

    8 Jaga 25 12000 77 Karthi 24 6200 11 Ram 20 5000 22 Sam 25 7000 25 Saranya 25 5700 26 Sathis 26 7400 34 Senthil 27 6700 53 ramesh 54 4000 4

    iii)HAVING and BETWEEN:

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    16/71

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    17/7117

    2 AAA 80 70 50

    3 BBB 90 80 50

    Table2: STUD

    ROLLNO NAME M1 M2 M3

    1 XXX 90 60 90

    2 AAA 80 70 50

    4 CCC 80 80 60

    i)UNION:The union operator returns all distinct rows selected by the component queries.

    SQL>select * from student union select * from stud;OUTPUT:

    ROLLNO NAME M1 M2 M3

    1 XXX 90 60 90

    2 AAA 80 70 50

    3 BBB 90 80 50

    4 CCC 80 80 60

    ii)UNION ALL:

    The union all operator returns all rows selected by both queries includingduplicate.SQL>select * from student unionall select * from stud;

    OUTPUT:

    ROLLNO NAME M1 M2 M3

    1 XXX 90 60 90

    2 AAA 80 70 50

    3 BBB 90 80 50

    1 XXX 90 60 90

    2 AAA 80 70 50

    4 CCC 80 80 60

    iii)INTERSECT:This operator returns only rows that are common to both queries.

    SQL>select * from student intersect select * from stud;OUTPUT:

    ROLLNO NAME M1 M2 M3

    1 XXX 90 60 90

    2 AAA 80 70 50

    iv)MINUS:

    This operator returns all distinct rows selected only by the first query and not bythe second query.

    SQL>select * from student minus select * from stud;OUTPUT:

    ROLLNO NAME M1 M2 M3

    3 BBB 90 80 50

    INDEXES:

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    18/7118

    An indexis a performance-tuning method of allowing faster retrieval of records. Anindex creates an entry for each value that appears in the indexed columns. By default, Oraclecreates B-tree indexes.

    Create an Index:SYNTAX:CREATE [UNIQUE] INDEX index_name ON table_name (column1, column2, . column_n);

    UNIQUE indicates that the combination of values in the indexed columns must be unique.

    EXAMPLE:CREATE INDEX supplier_idx ON supplier (supplier_name);In this example, created an index on the supplier table called supplier_idx. It consists of onlyone field - the supplier_name field.We could also create an index with more than one field as in the example below:CREATE INDEX supplier_idx ON supplier (supplier_name, city);

    Rename an Index:

    SYNTAX:ALTER INDEX index_name RENAME TO new_index_name;

    EXAMPLE:ALTER INDEX supplier_idx RENAME TO supplier_index_name;In this example, we're renaming the index called supplier_idxto supplier_index_name.

    Drop an Index:SYNTAX:DROP INDEX index_name;

    EXAMPLE:DROP INDEX supplier_idx;In this example, we're dropping an index called supplier_idx.

    RESULT:

    Thus the above various types of select commands and indexes has been executedsuccessfully and the output is verified.

    EXP NO: 6 VIEWS***************

    DATE:

    AIM:

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    19/7119

    To study and execute various select commands and views using SQL.

    PROCEDURE:

    VIEWS:A view is an object that gives the user a logical view of data from an underlying table or

    tables.Employee

    EID NAME AGE SALARY

    1 Ram 20 5000

    2 Sam 25 7000

    3 ramesh 54 4000

    7 hgh 55 6676

    6 senthil 34 5000

    1. Create a view from single table containing all columns from the base table.SYNTAX:Create view as select * from ;

    EXAMPLE:SQL>create view emp_view as select * from employee;SQL> select * from emp_view;

    2. Create a view from single table with selected columns.SYNTAX:Create view as select from ;EXAMPLE:

    SQL>create view sel_emp_view as select name, salary from employee;SQL> select * from sel_emp_view;NAME SALARY

    Ram 5000

    Sam 7000

    ramesh 4000

    hgh 6676

    senthil 5000

    3. Create a view from two tables with all columns.SYNTAX:

    Create view as select * from full natural join;EXAMPLE:SQL>select * from student;

    SNO SNAME LNAME

    2 sss

    1 senthil kumar

    3 karthi keyan

    4 ravi chandaran

    EID NAME AGE SALARY

    1 Ram 20 5000

    2 Sam 25 7000

    3 ramesh 54 4000

    7 hgh 55 6676

    6 senthil 34 5000

    https://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_database
  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    20/7120

    SQL>create view stud_emp_view as select * from student full natural join employee;

    SNO SNAME LNAME EID NAME AGE SALARY

    2 sss 1 Ram 20 5000

    2 sss 2 Sam 25 7000

    2 sss 3 ramesh 54 4000

    2 sss 6 senthil 34 5000

    1 senthil kumar 1 Ram 20 5000

    1 senthil kumar 2 Sam 25 7000

    1 senthil kumar 3 ramesh 54 4000

    1 senthil kumar 6 senthil 34 5000

    3 karthi keyan 1 Ram 20 5000

    3 karthi keyan 2 Sam 25 7000

    3 karthi keyan 3 ramesh 54 4000

    3 karthi keyan 6 senthil 34 5000

    4 ravi chandaran 1 Ram 20 50004 ravi chandaran 2 Sam 25 7000

    4 ravi chandaran 3 ramesh 54 4000

    4 ravi chandaran 6 senthil 34 5000

    4. Create a view from two tables with selected columns.SYNTAX:Create view as select from, where

    .=.;EXAMPLE:SQL>create view stud_emp_sel_view as select name,lname,age,salary from students,employee e where s.sno=e.eid;SQL> select * from stud_emp_sel_view;

    NAME LNAME AGE SALARY

    Sam 25 7000

    Ram kumar 20 5000

    ramesh keyan 54 4000

    5. Check all DML commands.INSERT:SYNTAX:insert into values(value1,value2,value3,);EXAMPLE:SQL>insert into stud_view values(6,'Muthu','vel');SQL>select * from stud_view;

    UPDATE:SYNTAX:

    SNO SNAME LNAME

    2 sss

    1 senthil kumar

    3 karthi keyan

    4 ravi chandaran

    6 Muthu vel

    https://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_SEL_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_SEL_VIEW%26event%3Dview%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_SEL_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_SEL_VIEW%26event%3Dview%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_SEL_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_SEL_VIEW%26event%3Dview%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_SEL_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_SEL_VIEW%26event%3Dview%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_SEL_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_SEL_VIEW%26event%3Dview%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_SEL_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_SEL_VIEW%26event%3Dview%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_SEL_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_SEL_VIEW%26event%3Dview%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_SEL_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_SEL_VIEW%26event%3Dview%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_SEL_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_SEL_VIEW%26event%3Dview%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUD_EMP_VIEW&event=displayContents&cancelURL=/em/console/database/schema/view%3Fsname%3DSYSTEM%26oname%3DSTUD_EMP_VIEW%26event%3Dedit%26otype%3DVIEW%26target%3Dorcl%26type%3Doracle_database&otype=VIEW&target=orcl&type=oracle_database
  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    21/7121

    update set = where =;EXAMPLE:SQL>update stud_view set lname='nnnn' where sno=2;

    SQL>select * from stud_view;

    Note: update command does not works for all queries on views.DELETE:

    SYNTAX:delete from where =;EXAMPLE:SQL>delete from emp_view where eid=6;SQL>select * from emp_view;

    EID NAME AGE SALARY

    1 Ram 20 5000

    2 Sam 25 7000

    3 ramesh 54 4000

    6. Drop views.SYNTAX:

    drop view ;EXAMPLE:SQL>Drop view stud_view;

    View dropped.SQL>select * from stud_view;Table or view does not exist

    RESULT:

    Thus the above view has been executed successfully and the output is verified.

    EX.NO:7 SIMPLE PL-SQL PROGRAMSDATE:

    AIM:To write and execute simple pl/sql programs.

    PROCEDURE:PL/SQL Control Structures:

    1. Simple IF-THEN StatementSQL> DECLARE

    n number;BEGINn:=&n;IF n > 0 THEN

    SNO SNAME LNAME

    2 sss nnnn

    1 senthil kumar

    3 karthi keyan

    4 ravi chandaran

    6 Muthu vel

    https://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_databasehttps://localhost:1158/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=STUDENT&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DSTUDENT%26event%3Dview%26otype%3DTABLE%26target%3Dorcl%26type%3Doracle_database&otype=TABLE&target=orcl&type=oracle_database
  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    22/7122

    Dbms_output.put_line(Given number is Greater than ZERO);END IF;END;

    /

    2. Simple IF-THEN-ELSE StatementSQL> DECLARE

    n number;BEGINn:=&n;IF n > 0 THENDbms_output.put_line(Given number is Greater than ZERO);ELSEDbms_output.put_line(Given number is Less than ZERO);END IF;END;

    /

    3. Nested IF-THEN-ELSE StatementsSQL> DECLARE

    n number;BEGINn:=&n;IF n > 0 THENDbms_output.put_line(Given number is Greater than ZERO);ELSEIF n = 0 THENDbms_output.put_line(Given number is Equal to ZERO);

    ELSEDbms_output.put_line(Given number is Less than ZERO);END IF;END IF;END;

    /

    4. IF-THEN-ELSIF StatementSQL> DECLARE

    n number;BEGIN

    n:=&n;IF n > 0 THENDbms_output.put_line('Given number is Greater than ZERO');ELSIF n = 0 THENDbms_output.put_line('Given number is Equal to ZERO');ELSEDbms_output.put_line('Given number is Less than ZERO');END IF;END;

    /

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    23/7123

    5. Extended IF-THEN StatementSQL> DECLAREgrade CHAR(1);

    BEGINgrade := 'B';IF grade = 'A' THENDBMS_OUTPUT.PUT_LINE('Excellent');

    ELSIF grade = 'B' THENDBMS_OUTPUT.PUT_LINE('Very Good');ELSIF grade = 'C' THENDBMS_OUTPUT.PUT_LINE('Good');ELSIF grade = 'D' THENDBMS_OUTPUT. PUT_LINE('Fair');ELSIF grade = 'F' THENDBMS_OUTPUT.PUT_LINE('Poor');ELSEDBMS_OUTPUT.PUT_LINE('No such grade');END IF;

    END;/

    6. Simple CASE StatementSQL> DECLARE

    grade CHAR(1);BEGINgrade := 'B';CASE gradeWHEN 'A' THENDBMS_OUTPUT.PUT_LINE('Excellent');

    WHEN 'B' THENDBMS_OUTPUT.PUT_LINE('Very Good');WHEN 'C' THENDBMS_OUTPUT.PUT_LINE('Good');WHEN 'D' THENDBMS_OUTPUT.PUT_LINE('Fair');WHEN 'F' THENDBMS_OUTPUT.PUT_LINE('Poor');ELSEDBMS_OUTPUT.PUT_LINE('No such grade');END CASE;END;

    /7. Searched CASE Statement

    SQL> DECLAREgrade CHAR(1);

    BEGINgrade := 'B';CASEWHEN grade = 'A' THENDBMS_OUTPUT.PUT_LINE('Excellent');WHEN grade = 'B' THENDBMS_OUTPUT.PUT_LINE('Very Good');WHEN grade = 'C'THEN DBMS_OUTPUT.PUT_LINE('Good');WHEN grade = 'D'THEN DBMS_OUTPUT.PUT_LINE('Fair');WHEN grade = 'F'THEN DBMS_OUTPUT.PUT_LINE('Poor');ELSEDBMS_OUTPUT.PUT_LINE('No such grade');END CASE;

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    24/71

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    25/7125

    END LOOP;END;

    /

    12. Simple GOTO StatementSQL> DECLARE

    p VARCHAR2(30);

    n PLS_INTEGER := 37;BEGINFOR j in 2..ROUND(SQRT(n)) LOOPIF n MOD j = 0 THENp := ' is not a prime number';GOTO print_now;END IF;END LOOP;p := ' is a prime number';DBMS_OUTPUT.PUT_LINE(TO_CHAR(n) || p);

    END;/

    13. GOTO Statement to Branch to an Enclosing BlockSQL> DECLARE

    v_last_name VARCHAR2(25);v_emp_id NUMBER(6) := 120;BEGINSELECT last_name INTO v_last_name FROM employees

    WHERE employee_id = v_emp_id;BEGINDBMS_OUTPUT.PUT_LINE (v_last_name);v_emp_id := v_emp_id + 5;IF v_emp_id < 120 THENGOTO get_name;END IF;END;END;

    /14. DoWhile Statement:

    declaren_num number := 1;beginloopdbms_output.put(n_num||', ');n_num := n_num + 1;exit when n_num > 5;end loop;dbms_output.put_line('Final: '||n_num);end;

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    26/7126

    /

    ALGORITHMS:1.Start the program2.Declare the variables.3.Perform the given operation

    4.Display the result.5.Stop the terminate the execution.

    PL-SQL PROGRAMS:1. FACTORIAL VALUE

    PROGRAM:SQL> set serveroutput on;SQL>ed fact.sql;declarei number;

    n number;f number:=1;beginn:=&n;if n=0 thendbms_output.put_line('The factorial value is :'||i);end if;for i in 1..nloopf:=f*i;

    end loop;dbms_output.put_line('The factorial value is :'||f);end;

    /

    Output:Enter value for n: 5old 6: n:=&n;new 6: n:=5;The factorial value is :120

    2. PRIME NUMBER GENERATIONPROGRAM:SQL> set serveroutput on;SQL>ed prime.sql;declaren number;i number;

    j number;k number;

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    27/7127

    p number;beginn:=&n;

    dbms_output.put_line('The prime number are');for i in 2..nloopp:=0;

    for j in 2..(i-1)loopk:=i mod j;if k=0 thenp:=1;end if;end loop;if p=0 thendbms_output.put_line(i);end if;end loop;

    end;/

    Output:SQL>set serveroutput on;SQL>@prime.sql;Enter value for n: 7old 8: n:=&n;new 8: n:=7;The prime number are

    2357

    3. FIBONACCI SERIESPROGRAM:SQL> set serveroutput on;SQL>ed fib.sql;declare

    a number:=-1;b number:=1;c number;i number;n number;beginn:=&n;for i in 1..nloopc:=a+b;

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    28/71

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    29/7129

    INSERT INTO EMPDET VALUES (ENO1, ENAME1, DEPTNO1, BASIC1, HRA1, DA1, PF1,NETPAY1);END;

    OUTPUT:

    SQL> @BASICEnter value for eno1: 104old 11: ENO1:=&ENO1;new 11: ENO1:=104;Enter value for ename1: SRINIVAS REDDYold 12: ENAME1:='&ENAME1';new 12: ENAME1:='SRINIVAS REDDY';Enter value for deptno1: 10old 13: DEPTNO1:=&DEPTNO1;

    new 13: DEPTNO1:=10;Enter value for basic1: 6000old 14: BASIC1:=&BASIC1;

    new 14: BASIC1:=6000;

    SQL>/Enter value for eno1: 105old 11: ENO1:=&ENO1;new 11: ENO1:=105;Enter value for ename1: CIRAJold 12: ENAME1:='&ENAME1';new 12: ENAME1:='CIRAJ';Enter value for deptno1: 10old 13: DEPTNO1:=&DEPTNO1;new 13: DEPTNO1:=10;Enter value for basic1: 6000old 14: BASIC1:=&BASIC1;new 14: BASIC1:=6000;

    SQL> SELECT * FROM EMPDET;

    OUTPUT:

    ENO NAME DEPTNO BASIC HRA DA PF NETPAY--------- ------------------------------ --------- --------- --------- --------- --------- -----------------------

    101 SANTOSH 10 5000 2500 1000 350 8150102 SHANKAR 20 5000 2500 1000 350 8150103 SURESH 20 5500 2750 1100 385 8965104 SRINIVASA REDDY 10 6000 3000 1200 420 9780105 CIRAJ 10 6000 3000 1200 420 9780

    5. checking armstrong numberDECLARE

    num number(5);rem number(5);s number(5):=0;num1 number(5);

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    30/7130

    BEGINnum:=&num;num1:=num;

    while(num>0)loop

    rem:=mod(num,10);s:=s+power(rem,3);

    num:=trunc(num/10);End loop;if (s=num1)then

    dbms_output.put_line(num1 || ' IS ARMSTRONG NUMBER ');else

    dbms_output.put_line(num1 || ' IS NOT ARMSTRONG NUMBER');End if;

    END;/

    OUTPUT:

    SQL>@arm.sqlEnter value for num: 153old 7: num:=&num;new 7: num:=153;153 IS ARMSTRONG NUMBERSQL> /Enter value for num: 123old 7: num:=&num;new 7: num:=123;123 IS NOT ARMSTRONG NUMBER

    6. checking a number even or odd.DECLARE

    num number(5);rem number;

    BEGINnum:=&num;rem:=mod(num,2);if rem=0then

    dbms_output.put_line(' Number '|| num ||' is Even');

    elsedbms_output.put_line(' Number '||num||' is Odd');

    end if;END;

    /

    OUTPUT:

    SQL>start evenEnter value for num: 6old 5: num:=&num;

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    31/7131

    new 5: num:=6;Number 6 is Even

    SQL> /Enter value for num: 3old 5: num:=&num;new 5: num:=3;

    Number 3 is Odd

    7. find sum of digits of a given number.DECLARE

    num number(5);rem number(5);sm number(5):=0;num1 number(5);

    BEGIN

    num:=&num;num1:=num;while(num>0) loop

    rem:=mod(num,10);sm:=sm+rem;num:=trunc(num/10);

    end loop;dbms_output.put_line('SUM OF DIGITS OF '||num1||' IS: '||sm);

    end;/

    OUTPUT:

    SQL> @sumINPUT truncated to 2 charactersEnter value for num: 123old 7: num:=&num;new 7: num:=123;SUM OF DIGITS OF 123 IS: 6

    SQL> @sum

    INPUT truncated to 2 charactersEnter value for num: 456old 7: num:=&num;new 7: num:=456;SUM OF DIGITS OF 456 IS: 15

    8. generating Fibonacci series.DECLARE

    num number(5);

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    32/71

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    33/7133

    DECLAREname1 varchar2(20);name2 varchar2(20);

    l number(5);BEGIN

    name1:='&name1';l:=length(name1);

    while l>0 loopname2:=name2||substr(name1,l,1);l:=l-1;

    end loop;dbms_output.put_line('REVERSE OF STRING IS:'||NAME2);if(name1=name2) then

    dbms_output.put_line(name1||' IS PALINDROME ');else

    dbms_output.put_line(name1||' IS NOT PALINDROME ');end if;

    END;

    /OUTPUT:

    Enter value for name1: LIRILold 6: name1:='&name1';new 6: name1:='LIRIL';REVERSE OF STRING IS:LIRILLIRIL IS PALINDROME

    SQL> /Enter value for name1: MADAMold 6: name1:='&name1';new 6: name1:='MADAM';REVERSE OF STRING IS:MADAMMADAM IS PALINDROME

    RESULT:

    Thus the various programs were implemented and their output was verified.

    EXPNO: 8 PROCEDURES AND FUNCTIONDATE :

    AIM:To write PL/SQL programs that execute the concept of functions and procedures.

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    34/7134

    DEFINITIONA procedure or function is a logically grouped set of SQL and PL/SQL statements that perform

    a specific task. They are essentially sub-programs. Procedures and functions are made up of,

    **These procedures and functions do not show the errors.

    KEYWORDS AND THEIR PURPOSES:

    REPLACE: It recreates the procedure if it already exists.PROCEDURE: It is the name of the procedure to be created.

    ARGUMENT: It is the name of the argument to the procedure. Parenthesis can be omitted if noarguments are present.IN: Specifies that a value for the argument must be specified when calling the procedure ie. Used topass values to a sub-program. This is the default parameter.OUT: Specifies that the procedure passes a value for this argument back to its calling environment

    after execution ie. Used to return values to a caller of the sub-program.INOUT: Specifies that a value for the argument must be specified when calling the procedure andthat procedure passes a value for this argument back to its calling environment after execution.RETURN: It is the data type of the functions return value because every function must return a

    value, this clause is required.

    1) PROCEDURE:A procedure is a sub program that performs a specific action. A procedure can accept parameters.

    SYNTAX:create or replace procedure (argument {in,out,inout} datatype ) {is,as}

    variable declaration;constant declaration;beginPL/SQL subprogram body;exceptionexception PL/SQL block;end;

    Here, the parameter list and exception part are optional.

    Executing a procedure:SQL> exec procedure_name (parameter);

    CREATING THE TABLE ITITEMS AND DISPLAYING THE CONTENTSSQL> create table ititems(itemid number(3), actualprice number(5), ordid number(4), prodidnumber(4));Table created.

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    35/7135

    ITEMID ACTUALPRICE ORDID PRODID--------- ----------- -------- ---------101 2000 500 201102 3000 1600 202103 4000 600 202

    PROGRAM FOR GENERAL PROCEDURESELECTED RECORDS PRICE IS INCREMENTEDBY 500 , EXECUTING THE PROCEDURE CREATED AND DISPLAYING THE UPDATED TABLESQL> create procedure itsum(identity number, total number)isprice number;null_price exception;beginselect actualprice into price from ititems where itemid=identity;if price is null thenraise null_price;elseupdate ititems set actualprice=actualprice+total where itemid=identity;end if;exceptionwhen null_price thendbms_output.put_line('price is null');end;/Procedure created.SQL> exec itsum(101, 500);PL/SQL procedure successfully completed.SQL> select * from ititems;ITEMID ACTUALPRICE ORDID PRODID--------- ----------- --------- ---------101 2500 500 201102 3000 1600 202103 4000 600 202

    PROCEDURE FOR IN PARAMETER CREATION, EXECUTIONSQL> set serveroutput on;SQL> create procedure yyy (a IN number)is price number;beginselect actualprice into price from ititems where itemid=a;dbms_output.put_line('Actual price is ' || price);if price is null thendbms_output.put_line('price is null');end if;end;

    /Procedure created.SQL> exec yyy(103);

    Actual price is 4000

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    36/7136

    PL/SQL procedure successfully completed.

    PROCEDURE FOR OUT PARAMETER CREATION, EXECUTIONSQL> set serveroutput on;SQL> create procedure zzz (a in number, b out number)is identity number;beginselectordid into identity from ititems where itemid=a;if identity declarea number;b number;beginzzz(101,b);dbms_output.put_line('The value of b is '|| b);end;

    /The value of b is 100PL/SQL procedure successfully completed.

    PROCEDURE FOR INOUT PARAMETER CREATION, EXECUTIONSQL> create procedure itit( ainout number) isbegina:=a+1;end;

    /Procedure created.SQL> declarea number:=7;beginitit(a);dbms_output.put_line('The updated value is '||a);end;

    /The updated value is 8PL/SQL procedure successfully completed.

    EXAMPLE AND OUTPUT: Create a table STOCK contains fields like stock code, stock name, available stock and last

    purchase date. Write a procedure to update the current stock value of the stock code. Write a function to display the last purchase date for any stock code.SQL>create table stock(sno number(3), sname char(10), avail_stock number(3), last_purchase date);

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    37/71

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    38/7138

    1 Mouse 40 23-jul-09

    2 Keyboard 30 20-jul-09

    3 Printer 35 18-jul-09

    2) FUNCTION:A function is a sub program that accepts arguments and returns a unique value to the caller.

    SYNTAX:

    create or replace function (argument in datatype,) return{is,as}variable declaration;constant declaration;beginPL/SQL subprogram body;exception

    exception PL/SQL block;end;

    **Here, exception part is optional and mode of operations can be in, out or inout.

    CREATE THE TABLE ITTRAIN TO BE USED FOR FUNCTIONSSQL>create table ittrain( tno number(10), tfare number(10));Table created.SQL>select * from ittrain;TNO TFARE--------- ------------

    1001 5501002 600

    PROGRAM FOR FUNCTION AND ITS EXECUTIONSQL> create function aaa (trainnumber number) return number istrainfunctionittrain.tfare % type;beginselecttfare into trainfunction from ittrain where tno=trainnumber;return(trainfunction);end;

    /

    Function created.SQL> set serveroutput on;SQL> declaretotal number;begintotal:=aaa (1001);dbms_output.put_line('Train fare is Rs. '||total);end;

    /Train fare is Rs.550PL/SQL procedure successfully completed.

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    39/7139

    FACTORIAL OF A NUMBER USING FUNCTIONPROGRAM AND EXECUTIONSQL> create function itfact (a number) return number isfact number:=1;b number;beginb:=a;while b>0loopfact:=fact*b;b:=b-1;end loop;return(fact);end;

    /Function created.SQL> set serveroutput on;SQL> declarea number:=7;f number(10);beginf:=itfact(a);dbms_output.put_line('The factorial of the given number is'||f);end;

    /The factorial of the given number is 5040PL/SQL procedure successfully completed.

    ii)FUNCTION:SQL>ed fun1;

    creatre or replace function fpho(ino number)return date is last_update date;begin;selectlast_purchase into last_update from stock where sno=ino;returnlast_update;end;

    SQL>set serveroutput on;SQL>@ fun1;

    6 /Function created.

    SQL>ed subfun1;declareino number(8);last_update date;beginino:=ino;dbms_output.put_line(Last purchase date of stock number||ino||is||last_update);exceptionwhenno_data_found thendbms_output.put_line(Check the number you have given);end;

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    40/7140

    OUTPUT:SQL>@ subfun1;

    12/Enter value for INO: 1Old : 5 : ino:= &ino;New : 5 : ino:= 1;Last purchase date of stock number 1 is 23-jul-09.

    RESULT:

    Thus the PL/SQL programs werefor procedure and functions has been executed successfully andthe output is verified.

    EXPNO: 9 TRIGGERS

    DATE:

    AIM:To write a PL/SQL program to manipulate record using triggers and update the result in

    the table.

    ALGORITHM:1. Start the program.2. Declare the variables.3. Create the trigger program and update the total.

    4. After trigger creation insert the value in the total.5. Stop the program.

    OVERVIEW TO TRIGGERS:A trigger is a pl/sql block structure which is fired when a DML statements like Insert,

    Delete, Update is executed on a database table. A trigger is triggered automatically when anassociated DML statement is executed.

    Trigger is pl/sql block or procedure that is associated with table,view,schema and

    database.

    Execute immidiately when particular event take place.

    SYNTAX:CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | AFTER | INSTEAD OF }

    {INSERT [OR] | UPDATE [OR] | DELETE} [OF col_name] ON table_name[REFERENCING OLD AS o NEW AS n] [FOR EACH ROW] Where(condition)

    BEGINSQL statements

    END;

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    41/7141

    TYPES OF TRIGGERS:There are two types of triggers based on the which level it is triggered.

    1) Row level trigger - An event is triggered for each row upated, inserted or deleted.2) Statement level trigger - An event is triggered for each sql statement executed.

    i)INSERT:BEFORE INSERT:To write a TRIGGER to ensure that DEPT TABLE does not contain duplicate of nullvalues in DEPTNO column.

    create or replace TRIGGER trig1 before insert on dept for each rowDECLAREa number;BEGINif(:new.d_id is Null) thenraise_application_error(-20001,'error::deptno cannot be null');elseselect count(*) into a from dept where d_id=:new.d_id;if(a=1) thenraise_application_error(-20002,'error:: cannot have duplicate deptno');end if;end if;

    END;

    OUTPUT:SQL> INSERT INTO DEPT VALUES('','SSS');(NULL VALUE)INSERT INTO DEPT VALUES('','SSS')

    *ERROR at line 1:ORA-20001: error::deptno cannot be null

    ORA-06512: at "SYSTEM.TRIG1", line 5ORA-04088: error during execution of trigger 'SYSTEM.TRIG1'

    SQL> INSERT INTO DEPT VALUES(13,'SSS'); (SAME VALUE)INSERT INTO DEPT VALUES(13,'SSS')

    *ERROR at line 1:ORA-20002: error:: cannot have duplicate deptnoORA-06512: at "SYSTEM.TRIG1", line 13ORA-04088: error during execution of trigger 'SYSTEM.TRIG1'

    AFTER INSERT:

    SQL>create or replace trigger trig after insert on empbeginupdate emp set net_sal=(salary*0.15)+salary;end;

    /

    SQL> select * from emp;ENO ENAME SALARY NET_SAL

    ---------- -------------------- ---------- ----------2 Nares 3000 3500

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    42/71

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    43/7143

    when (new.age > old.age)BEGINinsert into t1 values(:old.tno, :old.tname, :new.age);END;

    /

    OUTPUT:SQL> UPDATE T1 SET AGE=20 WHERE TNO=2;

    UPDATE T1 SET AGE=20 WHERE TNO=2*

    ERROR at line 1:ORA-04091: table SYSTEM.T1 is mutating, trigger/function maynot see itORA-06512: at "SYSTEM.TRIGGER1", line 2ORA-04088: error during execution of trigger 'SYSTEM.TRIGGER1'

    SQL> UPDATE T1 SET AGE=25 WHERE TNO=2;1 row updated.

    BEFORE INSERT/UPDATE:CREATE OR REPLACE TRIGGER UPPERCASE BEFORE INSERT OR UPDATE ON STUDENTFOR EACH ROW

    BEGIN:new.sname:=UPPER(:new.sname);:new.lname:=UPPER(:new.lname);

    END UPPERCASE;/

    OUTPUT:SQL> insert into student values(5,'daya','nandhan');

    1 row created.

    SQL> select * from student;

    SNO SNAME LNAME---------- ---------- --------------------

    2 sss nnnn1 senthil kumar3 karthi keyan4 ravi chandaran6 Muthu vel5 DAYA NANDHAN

    iii)DELETE:BEFORE DELETE:

    CREATE OR REPLACE TRIGGER TRIG2 BEFORE DELETE ON EMP FOR EACH ROWBEGINraise_application_error(-20010,'You cannot do manipulation');END;/

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    44/71

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    45/7145

    SQL> insert into emp values(3,'Saru',5400,6667);

    SQL> update emp set ename='Nares' where eno=2;

    SQL>delete from emp where eno=5;

    SQL> select * from log_emp;

    CHANGE_TYPE CHANGED_BY TIME ENO

    I SYSTEM 01-FEB-11 09.07.45.000000000 PM 3

    U SYSTEM 01-FEB-11 09.10.38.000000000 PM 2

    D SYSTEM 01-FEB-11 09.12.28.000000000 PM 5

    RESULT:

    Thus the above experiment for Trigger has been executed successfully and the output isverified.

    CURSORSExp. No: 10DATE:

    AIMTo study and execute the manipulation of cursor management

    DEFINITION OF CURSOR:A cursor is a pointer to the context area. A PL/SQL program can control the context area

    through the cursor.

    There are of 2 types of cursors: STATIC CURSCOR REF CURSOR

    1. STATIC CURSOR:There are two types:

    Attributes:a)%notfound- No more records can be fetched from the cursor.b)%found- A record can be fetched from the cursor.c)%rowcount- The number of rows fetched from the cursor so far.d)%isopen- The cursor has been opened.

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    46/7146

    a)OPEN:SYNTAX: open ;b)FETCH:

    SYNTAX: fetch into ;c)CLOSE:SYNTAX: close ;

    2) REF CURSOR:i) Declaring cursor variable:SYNTAX: type is refcursor [return return_type];

    ii) Opening a cursor variable:SYNTAX: open for select_stmt;

    iii) Closing a cursor variable:SYNTAX: close ;

    EXAMPLE:

    1. TO WRITE A PL/SQL BLOCK TO CALCULATE AND DISPLAY THE EMPOYEE SALARYUSING CURSOR FOR LOOP

    Create a table named EMPLOYEE. Declare the cursor that contains empno, name, netsalary. Update the salary of employee by using the cursor. Update the bonus of the employee using the cursor.

    SQL>create table employee(eid number(3),ename char(10), basic number(5), da number(5),hranumber(5), netsal number(10), bonus number(10),dno number(3));SQL>insert into employee values(&eid,&ename,&basic,&da,&hra,&netsal,&bonus,&dno);SQL>insert into employee values(1,syed,5000,10,1000,0,0,11);

    SQL>ed cur1;declares1 employee.basic%type;e1 employee.eid%type;d1 employee.da%type;f1 employee.hra%type;n1 employee.netsal%type;b1 employee.bonus%type;cursor cur11 is select eid, basic, da, hra, netsal,bonus from employee;begin

    open cur11;loopfetch cur11 into e1,s1,d1,f1,n1,b1;update employee set netsal=s1+(s1*d1)/100+f1 where eid=e1;update employee set bonus=netsal+500 where eid=e1;exit when cur11%notfound;end loop;close cur11;end;

    /

  • 8/13/2019 DBMS_JS in database management systems with its operations and its functionalities

    47/7147

    SQL>@ cur1;PL/SQL procedure successfully completed.OUTPUT:

    SQL>select * from employee;

    EID ENAME BASIC DA HRA NETSAL BONUS DNO

    1 s ed 5000 10 1000 6500 7000 11

    2 Ram 6300 20 1200 8760 9260 12

    3 Kumar 7500 50 1500 12750 13250 11

    2. TO WRITE A PL/SQL BLOCK TO DISPLAY THE EMPOYEE ID AND EMPLOYEE NAMEUSING CURSOR FOR LOOP

    SQL> set serveroutput on;SQL>ed cur12declarebeginfor emy in (select eid,ename from employee)loop

    dbms_output.put_line('Employee id and employee name are '|| emy.eid ||' and '|| emy.ename);end loop;end;

    /OUTPUT:SQL>@cur12;Employee id and employee name are 1 and syedEmployee id and employee name are 2 and Ram

    Employee id and employee name are 3 and KumarPL/SQL procedure successfully completed.

    3. TO WRITE A PL/SQL BLOCK TO UPDATE THE SALARY OF ALL EMPLOYEES WHEREDEPARTMENT NO IS 11 BY 5000 USING CURSOR FOR LOOP AND TO DISPLAY THEUPDATED TABLE

    SQL> set serveroutput on;SQL> declarecursor cem is select eid,ename,netsal,dno from employee where dno=11;begin--open cem;for rem in cemloopupdate employee set netsal=rem.netsal+5000 where eid=rem.eid;end loop;--close cem;end;

    /SQL>@curr12PL/SQL procedure successfully completed.SQL> select * from employee;

    EID ENAME BASIC DA HRA NETSAL BONUS DNO

    1 s ed 5000 10 1000 11500 7000 11

    2 Ram 6300 20 1200 8760 9260 12

    https://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%3DEMPLOYEE%26event%3Dview%26otype%3DTABLE%26target%3DJsenthil%26type%3Doracle_database&otype=TABLE&target=Jsenthil&type=oracle_databasehttps://localhost:5500/em/console/database/schema/displayContents?sname=SYSTEM&pageName=/database/schema/displayContentsPage&oname=EMPLOYEE&event=displayContents&cancelURL=/em/console/database/schema/table%3Fsname%3DSYSTEM%26oname%