Expt No 1
Expt No 1
Problem Description:
The COMPANY database keeps track of a company’s employees, departments, and projects. Suppose that
after the requirements collection and analysis phase, the database designers provide the following
description of the miniworld—the part of the company that will be represented in the database.
● The company is organized into departments. Each department has a unique name, a unique
number, and a particular employee who manages the department. We keep track of the start date
when that employee began managing the department. A department may have several locations.
● A department controls a number of projects, each of which has a unique name, a unique number,
and a single location.
● We store each employee’s name, Social Security number,address, salary, sex (gender), and birth
date. An employee is assigned to one department, but may work on several projects, which are
not necessarily controlled by the same department. We keep track of the current number of hours
per week that an employee works on each project. We also keep track of the direct supervisor of
each employee (who is another employee).
● We want to keep track of the dependents of each employee for insurance purposes. We keep each
dependent’s first name, sex, birth date, and relationship to the employee.
ER Diagram:
Entities:
● An entity type DEPARTMENT with attributes Name, Number, Locations, Manager, and
Manager_start_date. Locations is the only multivalued attribute. We can specify that both Name
and Number are (separate) key attributes because each was specified to be unique.
● An entity type PROJECT with attributes Name, Number, Location, and Controlling_department.
Both Name and Number are (separate) key attributes.
● An entity type EMPLOYEE with attributes Name, Ssn, Sex, Address, Salary, Birth_date,
Department, and Supervisor. Both Name and Address may be composite attributes; however, this
was not specified in the requirements.
● An entity type DEPENDENT with attributes Employee, Dependent_name, Sex, Birth_date, and
Relationship (to the employee).
Relationship types:
● MANAGES, a 1:1 relationship type between EMPLOYEE and DEPARTMENT. EMPLOYEE
participation is partial. DEPARTMENT participation is not clear from the requirements. We
assume that a department must have a manager at all times, which implies total participation.The
attribute Start_date is assigned to this relationship type.
● WORKS_FOR, a 1:N relationship type between DEPARTMENT and EMPLOYEE. Both
participations are total.
● CONTROLS, a 1:N relationship type between DEPARTMENT and PROJECT. The participation
of PROJECT is total, whereas that of DEPARTMENT is determined to be partial, assuming that
some departments may control no projects.
● SUPERVISION, a 1:N relationship type between EMPLOYEE (in the supervisor role) and
EMPLOYEE (in the supervisee role). Both participations are determined to be partial, assuming
that not every employee is a supervisor and not every employee has a supervisor.
● WORKS_ON, determined to be an M:N relationship type with attribute Hours, after the users
indicate that a project can have several employees working on it. Both participations are
determined to be total. DEPENDENTS_OF, a 1:N relationship type between EMPLOYEE and
DEPENDENT, which is also the identifying relationship for the weak entity type DEPENDENT.
The participation of EMPLOYEE is partial, whereas that of DEPENDENT is total
Summary :
Figure above displays the COMPANY ER database schema as an ER diagram. We now review the full
ER diagram notation.
● Entity types such as EMPLOYEE, DEPARTMENT, and PROJECT are shown in rectangular
boxes. Relationship types such as WORKS_FOR, MANAGES, CONTROLS, and WORKS_ON
are shown in diamond-shaped boxes attached to the participating entity types with straight lines.
● Attributes are shown in ovals, and each attribute is attached by a straight line to its entity type or
relationship type.
● Component attributes of a composite attribute are attached to the oval representing the composite
attribute, as illustrated by the Name attribute of EMPLOYEE.
● Multivalued attributes are shown in double ovals, as illustrated by the Locations attribute of
DEPARTMENT.
● Key attributes have their names underlined.
● Derived attributes are shown in dotted ovals, as illustrated by the Number_of_employees attribute
of DEPARTMENT.
● Weak entity types are distinguished by being placed in double rectangles and by having their
identifying relationship placed in double diamonds, as illustrated by the DEPENDENT entity type
and the DEPENDENTS_OF identifying relationship type. The partial key of the weak entity type
is underlined with a dotted line.
● The cardinality ratio of each binary relationship type is specified by attaching a 1, M, or N on
each participating edge. The cardinality ratio of DEPARTMENT:EMPLOYEE in MANAGES is
1:1, whereas it is 1:N for DEPARTMENT: EMPLOYEE in WORKS_FOR, and M:N for
WORKS_ON.
● The participation constraint is specified by a single line for partial participation and by double
lines for total participation (existence dependency).
● In Figure we show the role names for the SUPERVISION relationship type because the same
EMPLOYEE entity type plays two distinct roles in that relationship. Notice that the cardinality
ratio is 1:N from supervisor to supervisee because each employee in the role of supervisee has at
most one direct supervisor, whereas an employee in the role of supervisor can supervise zero or
more employees
Relational Schema:
ER-to-Relational Schema Mapping:
Step 1: Mapping of Regular Entity Types
We create the relations EMPLOYEE, DEPARTMENT, and PROJECT in Figure to correspond to the
regular entity types EMPLOYEE, DEPARTMENT, and PROJECT. We choose Ssn, Dnumber, and
Pnumber as primary keys for the relations EMPLOYEE, DEPARTMENT, and PROJECT, respectively
Step 2: Mapping of Weak Entity Types
We create the relation DEPENDENT in this step to correspond to the weak entity type DEPENDENT. We
include the primary key Ssn of the EMPLOYEE relation—which corresponds to the owner entity
type—as a foreign key attribute of DEPENDENT; We rename it Essn. The primary key of the
DEPENDENT relation is the combination {Essn, Dependent_name}, because Dependent_name is the
partial key of DEPENDENT
Step 3: Mapping of Binary 1:1 Relationship Types.
We map the 1:1 relationship type MANAGES from Figure by choosing the participating entity type
DEPARTMENT to serve in the role of S because its participation in the MANAGES relationship type is
total (every department has a manager). We include the primary key of the EMPLOYEE relation as
foreign key in the DEPARTMENT relation and rename it Mgr_ssn. We also include the simple attribute
Start_date of the MANAGES relationship type in the DEPARTMENT relation and rename it
Mgr_start_date
Step 4: Mapping of Binary 1:N Relationship Types
We now map the 1:N relationship types WORKS_FOR, CONTROLS, and SUPERVISION from Figure.
For WORKS_FOR we include the primary key Dnumber of the DEPARTMENT relation as foreign key in
the EMPLOYEE relation and call it Dno. For SUPERVISION we include the primary key of the
EMPLOYEE relation as foreign key in the EMPLOYEE relation itself—because the relationship is
recursive—and call it Super_ssn. The CONTROLS relationship is mapped to the foreign key attribute
Dnum of PROJECT, which references the primary key Dnumber of the DEPARTMENT relation.
Step 5: Mapping of Binary M:N Relationship Types
We map the M:N relationship type WORKS_ON from the ER diagram by creating the relation
WORKS_ON in relational schema. We include the primary keys of the PROJECT and EMPLOYEE
relations as foreign keys in WORKS_ON and rename them Pno and Essn, respectively. We also include
an attribute Hours in WORKS_ON to represent the Hours attribute of the relationship type. The primary
key of the WORKS_ON relation is the combination of the foreign key attributes {Essn, Pno}.
Step 6: Mapping of Multivalued Attributes
We create a relation DEPT_LOCATIONS. The attribute Dlocation represents the multivalued attribute
LOCATIONS of DEPARTMENT, while Dnumber—as foreign key—represents the primary key of the
DEPARTMENT relation. The primary key of DEPT_LOCATIONS is the combination of {Dnumber,
Dlocation}. A separate tuple will exist in DEPT_LOCATIONS for each location that a department has.
RESULT:
We successfully created an ER diagram from the problem description given. We were also able to
extract the relational schema from the ER diagram.
Expt No 2
APPLICATION OF DDL COMMANDS USING UI AND SQL
AIM : Creation, modification, configuration, and deletion of databases using UI and SQL
Commands
QUERY
use students;
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| employees |
| mysql |
| students |
| test |
+--------------------+
use students;
Create a table students with the fields student id, name, email and phone number.
show tables;
+--------------------+
| Tables_in_students |
+--------------------+
| marks |
| student |
| subject |
+--------------------+
desc student;
+------------+-------------+------+-----+---------+----------------+
+------------+-------------+------+-----+---------+----------------+
+------------+-------------+------+-----+---------+----------------+
desc subject;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
+----------+-------------+------+-----+---------+----------------+
desc marks;
+---------+---------+------+-----+---------+-------+
+---------+---------+------+-----+---------+-------+
+---------+---------+------+-----+---------+-------+
AIM: Creation of database schema - DDL (create tables, set constraints, enforce relationships,
create indices, delete and modify tables). Export ER diagram from the database and verify
relationships (with the ER diagram designed in step 1).
QUERY:
EER diagram: <SCREENSHOT>
RESULT:
We successfully created the Database Schema from the problem description given. We were also
able to export the ER diagram from the database and relationships are verified.
Expt No 4
APPLICATION OF DML COMMANDS USING SQL
AIM :Insertion, updation, deletion, and selection of databases using SQL Commands
Update the marks of subject 200 and the student having student id 102 to 83.
SELECT QUERIES:
+---------+------------+------------+--------------------+------------+
+---------+------------+------------+--------------------+------------+
+---------+------------+------------+--------------------+------------+
+--------+-------------+
| sub_id | sub_name |
+--------+-------------+
| 200 | chemistry |
| 201 | physics |
| 202 | mathematics |
+--------+-------------+
+--------+---------+-------+
| 200 | 100 | 75 |
| 200 | 101 | 94 |
| 200 | 102 | 83 |
| 201 | 100 | 85 |
| 201 | 101 | 98 |
| 201 | 102 | 70 |
| 202 | 100 | 50 |
| 202 | 101 | 96 |
| 202 | 102 | 45 |
+--------+---------+-------+
+---------+------------+------------+--------------------+------------+
+---------+------------+------------+--------------------+------------+
+---------+------------+------------+--------------------+------------+
2.Write a query to display student name, subject and marks of student ordered by marks.
+------------+-------------+-------+
| stud_fname | sub_name | marks |
+------------+-------------+-------+
| riya | mathematics | 45 |
| shanti | mathematics | 50 |
| riya | physics | 70 |
| shanti | chemistry | 75 |
| riya | chemistry | 83 |
| shanti | physics | 85 |
| anjitha | chemistry | 94 |
| anjitha | mathematics | 96 |
| anjitha | physics | 98 |
+------------+-------------+-------+
3.Write a query to display student name, subject and marks of students who have marks
greater than 70 in physics.
='physics';
+------------+----------+-------+
+------------+----------+-------+
| shanti | physics | 85 |
| anjitha | physics | 98 |
+------------+----------+-------+
4.Write a query to display average marks in each subject.
+-------------+------------+
| sub_name | avg(marks) |
+-------------+------------+
| chemistry | 84.0000 |
| mathematics | 63.6667 |
| physics | 84.3333 |
+-------------+------------+
+----------------+
| count(stud_id) |
+----------------+
| 3|
+----------------+
6.Write a query to display the maximum and minimum marks obtained by students in each
subject.
+-------------+------------+------------+
+-------------+------------+------------+
| chemistry | 94 | 75 |
| mathematics | 96 | 45 |
| physics | 98 | 70 |
+-------------+------------+------------+
7.Write a query to display the details of a student whose name begins with S.
+---------+------------+------------+-------------------+------------+
+---------+------------+------------+-------------------+------------+
+---------+------------+------------+-------------------+------------+
8.Write a query to display the details of a student whose first name contains a in the fourth
place.
+---------+------------+------------+--------------------+------------+
+---------+------------+------------+--------------------+------------+
+---------+------------+------------+--------------------+------------+
9.Write a query to display the name,subject and marks of students having marks between
50 and 75.
+------------+-------------+-------+
| shanti | chemistry | 75 |
| riya | physics | 70 |
| shanti | mathematics | 50 |
+------------+-------------+-------+
sub_id;
+------------+------------+-------------+-------+
+------------+------------+-------------+-------+
| anjitha |k | chemistry | 94 |
| anjitha |k | physics | 98 |
| anjitha |k | mathematics | 96 |
+------------+------------+-------------+-------+
A. Create a table store. Fields are order no, code, item, quantity, price, discount, mrp
QUERY
Create table store (order_no int primary key, code int, item char(15), quantity varchar(8), price
int,
discount varchar(7), mrp int);
Insert into store values(‘1’, ‘1’, ‘soap’, ‘5’, ‘75’, ‘2%’, ‘72’,);
1 row created;
Insert into store values(‘2’, ‘2’, ‘chilly powder’, ‘2’, ‘24’, ‘3%’, ‘20’,);
1 row created;
Insert into store values(‘3’, ‘3’, ‘atta’, ‘2’, ‘70’, ‘3%’, ‘78’,);
1 row created;
Insert into store values(‘4’, ‘4’, ‘pepper’, ‘5’, ‘524’, ‘5%’, ‘520’,);
1 row created;
Insert into store values(‘5’, ‘5’, ‘salt’, ‘4’, ‘40’, ‘2%’, ‘39’,);
1 row created;
QUERY
Select * from store;
OUTPUT
c. Write an SQL query to display the reminder, if the amount of an each item in store is
divided by 9.
QUERY
Select item, mod(mrp,9) from store;
OUTPUT
Item mod(mrp)
Soap 0
chilly powder 2
atta 6
pepper 7
salt 3
d. Write SQL query to display the amount in store and its square.
QUERY
Select price, power(price,2) as power from store;
OUTPUT
Price Power
75 5625
24 576
70 49000
524 274576
40 1600
e. Program to divide the amount in stock of each item by 7 in store table and display the resut
round to the nearest integer.
QUERY
Select price, round(price/7,0) as round from store;
OUTPUT
Price Round
75 11
24 3
70 10
524 75
40 6
AGGREGATE FUNCTIONS
1)Find the sum of the salaries of all employees, the maximum salary,the minimum salary,
and the average salary.
FROM EMPLOYEE;
2)Find the sum of the salaries of all employees of the ‘Research’department, as well as the
maximum salary, the minimum salary, and the average salary in this department.
WHERE Dname=‘Research’;
3)Retrieve the total number of employees in the company and the number of employees in
the ‘Research’ department
1)For each department, retrieve the department number, the number of employees in the
department, and their average salary.
SELECT Dno, COUNT (*), AVG (Salary) FROM EMPLOYEE GROUP BY Dno;
2)For each project, retrieve the project number, the project name, and the number of
employees who work on that project.
WHERE Pnumber=Pno
3)For each project on which more than two employees work, retrieve the project number,
the project name, and the number of employees who work on the project.
WHERE Pnumber=Pno
4)For each project, retrieve the project number, the project name, and the number of
employees from department 5 who work on the project.
SELECT Pnumber, Pname, COUNT (*)
5)For each department that has more than five employees, retrieve the department number
and the number of its employees who are making more than $40,000.
( SELECT Dno
FROM EMPLOYEE
GROUP BY Dno
AIM: To perform nested Queries , joining Queries and set operations using DML command
QUERIES
1. Display all employee names and salary whose salary is greater than minimum
salary of the company
2. Issue a query to display information about employees who earn more than any
employee in dept no 5
SQL> select * from Employee where Salary>(select max(Salary) from Employee where
Dno=5);
3. Display the details of those who draw the salary greater than the average salary.
SQL> select distinct * from Employee x where x.Salary >= (select avg(Salary) from
Employee);
4. Write SQL Query which retrieves the name and address of every employee who
works for the Research Department
5 Retrieve the name of each employee who has a dependent with the same first
name and is the same sex as the employee.
SQL>Select E.Fname, E.Lname From Employee as E where E.Ssn in ( Select Essn From
Dependent as D where E.Fname=D.Dependent_Name and E.Sex=D.Sex );
6. Make a list of all project numbers for projects that involve an employee whose
last name is ‘Smith’, either as a worker or as a manager of the department that
controls the project.
7. Write a query to display the name for all employees who work in a department
with any employee whose Fname contains h
Select Fname from Employee where Dno IN (Select Dno from Employee where Fname
LIKE ‘%h%’);
8. Write a SQL query to find those employees who work in the same department
where 'Ramesh' works. Exclude all those records where first name is 'Ramesh'.
Return first name, last name
'Ramesh';
9 Display all the dept numbers available in emp and not in dept tables and vice
versa.
from Employee;
RESULT
QUERY
Create table Student(id int primary key,name varchar(20),mark int);
1 row created;
1 row created;
1 row created;
1 row created;
QUERY
OUTPUT
id name mark
1 Anu 45
2 Manu 43
3 Veena 47
4 Seena 40
C. Implement Savepoint
QUERY
Savepoint a;
//Display table
id name mark
1 Anu 45
2 Manu 43
3 Veena 47
4 Seena 49
Savepoint b;
//Display table
1 Anu 45
2 Neethu 42
3 Veena 47
4 Seena 49
D.Implementation of Rollback
QUERY
Rollback to a;
//Display table
id name mark
1 Anu 45
2 Manu 43
3 Veena 47
4 Seena 49
E. Implementation of Commit
//Display table
id name mark
1 Anu 45
2 Manu 43
3 Veena 47
4 Seena 49
QUERY
Commit;
Rollback to b;
OUTPUT
//commit command saves the data permanently to the database.All the savepoints are
deleted and cannot rollback the same.
RESULT
QUERY
Create Database
mysql> create database db;
Query OK, 1 row affected (0.03 sec)
Use Database
mysql> use db;
Database changed
Revoking Permissions
mysql> revoke insert on student from root@localhost;
Query OK, 0 rows affected (0.00 sec)
RESULT
Views in SQL
AIM: Demonstrate View in SQL
a. Create a table store. Fields are order no, code, item, quantity, price, discount, mrp
QUERY
Create table store (order_no int primary key, code int, item char(15), quantity varchar(8),
price int, discount varchar(7), mrp int);
Insert into store values(‘1’, ‘1’, ‘soap’, ‘5’, ‘75’, ‘2%’, ‘72’,);
1 row created;
Insert into store values(‘2’, ‘2’, ‘chilly powder’, ‘2’, ‘24’, ‘3%’, ‘20’,);
1 row created;
Insert into store values(‘3’, ‘3’, ‘atta’, ‘2’, ‘70’, ‘3%’, ‘78’,);
1 row created;
Insert into store values(‘4’, ‘4’, ‘pepper’, ‘5’, ‘524’, ‘5%’, ‘520’,);
1 row created;
Insert into store values(‘5’, ‘5’, ‘salt’, ‘4’, ‘40’, ‘2%’, ‘39’,);
1 row created;
1 1 soap 5 75 2%
72
2 2 chilly powder 2 24 3%
20
3 3 atta 2 70 3%
78
4 4 pepper 5 524 5%
520
5 5 salt 4 40 2%
39
c. Create a view with item_name and quantity for the above table
SYNTAX
QUERY
View created;
Select * from v;
OUTPUT
Item quantity
Soap 5
Chilli powder 2
Atta 2
Pepper 5
Salt 4
d. Write a query to check whether any changes made to present table is reflected in view.
QUERY
Select * from v;
OUTPUT
Item quantity
Soap 5
Chilli powder 2
Atta 50kg
Pepper 5
Salt 4
QUERY
Drop view v;
View dropped.
RESULT
PROGRAM
1 declare
2 n number:=&n;
3 begin
4 if mod(n,2)=0
5 then
6 dbms_output.put_line('number is even');
7 else
8 dbms_output.put_line('number is odd');
9 end if;
10 end;
11 /
INPUT
old 7: n:=&n;
new 7: n:=6;
OUTPUT
Number is even
b) Write a PL/SQL block to find the maximum number from given three numbers.
PROGRAM:
declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=&c;
else
end if;
end;
/
INPUT
old 7: a:=&a;
new 7: a:=6;
old 8: b:=&b;
new 8: b:=6;
old 9: c:=&c;
new 9: c:=6;
OUTPUT:
B is maximum
C. Write a program to accept a number and find the sum of the digits
PROGRAM
declare
n number(5):=&n;
s number:=0;
r number(2):=0;
begin
while n !=0
loop
r:=mod(n,10);
s:=s+r;
n:=trunc(n/10);
end loop;
end;
INPUT
old 7: n:=&n;
new 7: n:=146;
OUTPUT
d)PL/SQL Program to accept a number from user and print number in reverse order.
PROGRAM
declare
num1 number(5);
num2 number(5);
rev number(5);
begin
num1:=&num1;
rev:=0;
while num1>0
loop
rev:=num2+(rev*10);
num1:=floor(num1/10);
end loop;
end;
INPUT
old 7: num1:=&num1;
new 7: num1:=146;
OUTPUT
PROGRAM
declare
d number:=&num1;
begin
case d
when 1 then
dbms_output.put_line('sunday');
when 2 then
dbms_output.put_line('monday');
when 3 then
dbms_output.put_line('tuesday');
when 4 then
dbms_output.put_line('wednesday');
when 5 then
dbms_output.put_line('thursday');
when 6 then
dbms_output.put_line('friday');
when 7 then
dbms_output.put_line('saturday');
else
dbms_output.put_line('invalid day');
end case;
End;
INPUT
old 7: num1:=&num1;
new 7: num1:=5;
OUTPUT
Thursday
RESULT
The plsql program was executed and output was successfully obtained.
Exp No-13
(parameter [,parameter]) ]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [procedure_name];
1. IN - The parameter can be referenced by the procedure or function. The value of the
parameter can not be overwritten by the procedure or function.
2. OUT - The parameter can not be referenced by the procedure or function, but the value
of the parameter can be overwritten by the procedure or function.
3. IN OUT - The parameter can be referenced by the procedure or function and the value
of the parameter can be overwritten by the procedure or function.
Drop Procedure
Once you have created your procedure in Oracle, you might find that you need to remove it from
the database.
Syntax
QUESTION
PROGRAM
OUTPUT
Largest: 25
2. create table employ(eid number(5),nam varchar(6),sal number(6),dep varchar(8),wef date);
employ values(5,'roshin',5000,'hr','07-04-16');
d date;
begin
end upda;
declare
a2 number:=&a2;
a3 number:=&a3;
begin
upda(a2,a3);
end;
OUTPUT
TRIGGER
QUESTIONS
1)create a trigger which shows the salary difference of a particular employee whenever salary is
getting updated
Worker(id,name,salary)
PROGRAM
create table worker(id number(5),name varchar(10),sal number(10));
insert into worker values(1,'riya',25000);
insert into worker values(2,'febi',30000);
insert into worker values(3,'varsha',20000);
insert into worker values(4,'anjali',50000);
insert into worker values(5,'athira',35000);
OUTPUT
select * from worker;
TRIGGER T compiled
anonymous block completed
Enter the worker id =1
Enter the new salary = 28000
anonymous block completed
Difference in salary is 3000
select * from worker;
PROGRAM
OUTPUT
A standalone function is created using the CREATE FUNCTION statement. The syntax is
given by
RETURN return_datatype
{IS | AS}
<declaration section>
BEGIN
END;
Where,
∙ The optional parameter list contains name, mode and types of the parameters. IN
represents the value that will be passed from outside The function must contain a return
statement.
∙ The RETURN clause specifies the data type you are going to return from the function. ∙
∙ The AS keyword is used instead of the IS keyword for creating a standalone function.
While creating a function, you give a definition of what the function has to do. To use a
function, you will have to call that function to perform the defined task. When a program
calls a function, the program control is transferred to the called function.
To call a function, you simply need to pass the required parameters along with the function
name and if the function returns a value, then you can store the returned value.
QUESTIONS
1)write a PL/SQL function to find factorial of a Number.
2)write a PL/SQL function to find sum of 1st N even Numbers (additional question)
3)sales of different products in one week is recorded
Product (productid,productname,grade)
Sales (prdctname,salesamount,salesday)
Do the following
a) write a function that displays the product name and grade of the given product b)whenever the
product sales is grater than the target value it is given a A grade, if there is no sale for a product
an exception to be raised
PROGRAM
OUTPUT
OUTPUT
sales of 2 is
Creation of Packages
A package is a schema object that groups logically related PL/SQL types, variables, and
subprograms. Packages usually have two parts, a specification (spec) and a body.
The specification is the interface to the package. It declares the types, variables, constants, exceptions,
cursors, and subprograms that can be referenced from outside the package. The body defines the queries
for the cursors and the code for the subprograms.
The package body contains the implementation of every cursor and subprogram declared in the package
spec. Subprograms defined in a package body are accessible outside the package only if their specs also
appear in the package spec. If a subprogram spec is not included in the package spec, that subprogram
can only be called by other subprograms in the same package. A package body must be in the same
schema as the package spec.
6. Variables that you want to remain available between procedure calls in the same session
Modularity
Information Hiding
Added Functionality
Better Performance
Syntax
[ invoker_rights_clause ]
Where
Schema- Specify the schema to contain the package. If you omit schema, then the database creates the
package in your own schema.
invoker_rights_clause : Specifies the AUTHID property of the member functions and procedures of the
object type. The AUTHID clause determines whether all the packaged subprograms execute with the
privileges of their definer (the default) or invoker, and whether their unqualified references to schema
objects are resolved in the schema of the definer or invoker.
[ schema. ] package
To reference the types, items, subprograms, and call specs declared within a package spec, use
dot notation:
package_name.type_name
package_name.item_name
package_name.subprogram_name
package_name.call_spec_name
QUESTION
1. The procedure will accept a supplier id and display the corresponding supplier name,
2. The function will accept the supplier id and calculate the annual commission and
finally return the amount
Algorithm
PROGRAM
values(5,'merina',2500,'banglore');
varchar) is
begin
return number
is
s1 number:=0;
s2 number;
begin
select sucom into s2 from supplier where
suid=a1; s1:=s2*12;
return(s1);
end;
end pack_age;
declare
a1 number:=&a1;
c1 varchar(10);
bb1 number:=0;
begin
pack_age.alpha(a1,c1);
'||c1); end;
declare
b1 number:=&b1;
c1 varchar(10);
bb1 number:=0;
begin
bb1 :=pack_age.beta(b1);
OUTPUT
Creation of Cursors
AIM:To implement programs using Cursors
QUESTIONS
1) Consider the table Customer (accout no, customer name,balance amount,date of join).
Implement a PL/SQL block to insert those customers who have current balance greater than 1
Lakh and date of join before 1 january 2010 into the table premium customer who doesnt meet
above criteria are to be inserted into table nonpremium customer .
PROGRAM
create table customers(acc_no number(16),c_name varchar(15),bal_amt number(10),DOJ date);
create table premium_customers(acc_no number(16),c_name varchar(15),bal_amt
number(10),DOF date);
create table nonpremium_customers(acc_no number(16),c_name varchar(15),bal_amt
number(10),DOF date);
OUTPUT
select * from customers;
PROGRAM
declare
cursor c1 is select * from accdetails;
a accdetails %rowtype;
n number;
i number;
begin
open c1;
loop
fetch c1 into a.accno,a.cname,a.lastdate,a.amount;
exit when c1 %notfound;
n:=months_between(sysdate,a.lastdate);
if n>=1 then
insert into inactive_customer values(a.accno,a.cname);
else
if a.amount<250000 then
i:=a.amount*0.05;
dbms_output.put_line('interest of '||a.cname|| ' is :'||i);
elsif a.amount>250000 and a.amount<500000 then
i:=a.amount*0.1;
dbms_output.put_line('interest of '||a.cname|| ' is :'||i);
elsif a.amount>500000 then
i:=a.amount*0.15;
dbms_output.put_line('interest of '||a.cname|| ' is :'||i);
else
dbms_output.put_line('error');
end if;
end if;
end loop;
end;
OUTPUT
QUESTION
Do the following
a) write a function that displays the product name and grade of the given product
b)whenever the product sales is grater than the target value it is given a A grade, if
there is no sale for a product an exception to be raised
Query
OUTPUT
sales of 2 is
Step 1) Create a JavaScript variable called myStudent to hold the array of documents
Step 2) Add the required documents with the Field Name and values to the variable
Step 3) Use the insert command to insert the array of documents into the collection
The output shows that those 3 documents were added to the collection.
Use the JSON print functionality to see the output in a better format.
2.To create a collection “Course” and to insert fields:
3.To create collection “Section” and to insert fields:
4.To create collection “GradeReport “ and to insert the fields:
5.To insert collection “Prerequisite” and to enter the fields: