Академический Документы
Профессиональный Документы
Культура Документы
Departmental Vision
To evolve as a national level center of excellence in academics and research with the aim of
imparting contemporary knowledge in the field of Computer Science and Engineering.
2. Departmental Mission
1. Have state of art infrastructure and resources for teaching and research.
2. Impart relevant technical knowledge,skills and attributes along with values and ethics.
3. Enhance research quality and creativity through innovative teaching learning
methodologies.
4. Mold Computer Science Engineering Professionals in synchronization with the dynamic
industry requirements, worldwide.
5. Inculcate essential leadership qualities coupled with commitment to the society.
Continuously strive for higher achievements in life keeping moral and ethical
PEO2
values such as honesty, loyalty, good relationship and best performance, aloft.
Prepared By Approved By
Exp.
Experiment Name Page No.
No.
10 Creation of Packages 34
Expected Outcome
Studied the relationship between databases and has been implemented using join operation.
Viva Questions:
1. Define the “integrity Rules”
2. Discuss About the following
• Primary Key
• Candidate Key
• Foreign Key
• Secondary Key
3. What is meant by Constraints?
4. Define UID?
5. Explain about the following integrity with example
• Entity Integrity
• Domain Integrity
• Referential Integrity
• Column Integrity
6. Write the syntax for Check Constraints and Null constraints.
7. What is meant by Alternate key?
8. What are four functions that an ALTER statement can perform on constraints?
9. Why might you want to disable and then re-enable a constraint?
10. What are the restrictions on defining a CHECK constraint?
11. What are the limitations of constraints that may be applied at the column level and at the
table level?
12. Why is it important to give meaningful names to constraints?
DATE FUNCTION
1. Add_month
This function returns a date after adding a specified date with specified number of months.
Syntax: Add_months(d,n); where d-date n-number of months
Example: Select add_months(sysdate,2) from dual;
2. last_day
It displays the last date of that month.
Syntax: last_day (d); where d-date
Example: Select last_day (‘1-jun-2009’) from dual;
3. Months_between
It gives the difference in number of months between d1 & d2.
Syntax: month_between (d1,d2); where d1 & d2 -dates
Example: Select month_between (‘1-jun-2009’,’1-aug-2009’) from dual;
4. next_day
It returns a day followed the specified date.
Syntax: next_day (d,day);
Example: Select next_day (sysdate,’wednesday’) from dual
5. round
This function returns the date, which is rounded to the unit specified by the format model.
Syntax : round (d,[fmt]);
where d- date, [fmt] – optional. By default date will be rounded to the nearest day
Example: Select round (to_date(‘1-jun-2009’,’dd-mm-yy’),’year’) from dual;
Select round (‘1-jun-2009’,’year’) from dual;
NUMERICAL FUNCTIONS
CHARACTER FUNCTIONS
replace (char,search string, replace select replace(‘jack and jue’,‘j’,‘bl’) from black and
string); dual; blue
CONVERSION FUNCTION
1. to_char()
Syntax: to_char(d,[format]);
This function converts date to a value of varchar type in a form specified by date format.
If format is negelected then it converts date to varchar2 in the default date format.
Example: select to_char (sysdate, ’dd-mm-yy’) from dual;
2. to_date()
Syntax: to_date(d,[format]);
This function converts character to date data format specified in the form character.
Example: select to_date(‘aug 15 2009’,’mm-dd-yy’) from dual;
MISCELLANEOUS FUNCTIONS
GROUP FUNCTIONS
COUNT FUNCTION
1. Define function?
Function is a group of code that accepts zero or more arguments and both return one
or more results. Both are used to manipulate individual data items.
2. Write the two types of functions
i. Single row functions
ii. Group functions
3. What are single row functions?
A single row function or scalar function returns only one value for every row
queries in table. Single row function can appear in a select command and can also be
included in a where clause. The single row function can be broadly classified as,
o Date Function o Numeric Function
o Character Function o Conversion Function
o Miscellaneous Function
4. List some character funcitons
initcap(char);
lower (char);
Algorithm:
Step 1: Start.
Step 2: Go to SQL.
Step 3: Enter the user name and password.
Step 4: Type the commands for creating table and perform Build in functions on
the tables.
Step 5: The output is displayed.
Step 6: Stop.
Expected Outcome
Studied and Implemented of Built in functions in RDBMS
Aggregative operators: In addition to simply retrieving data, we often want to perform some
computation or summarization. SQL allows the use of arithmetic expressions. We now consider a
powerful class of constructs for computing aggregate values such as MIN and SUM.
1. Count: COUNT following by a column name returns the count of tuple in that column. If
DISTINCT keyword is used then it will return only the count of unique tuple in the column.
Otherwise, it will return count of all the tuples (including duplicates) count (*) indicates all the
tuples of the column.
Syntax: COUNT (Column name)
Example: SELECT COUNT (Sal) FROM emp;
2. SUM: SUM followed by a column name returns the sum of all the values in that column.
Syntax: SUM (Column name)
Example: SELECT SUM (Sal) From emp;
3. AVG: AVG followed by a column name returns the average value of that column values.
Syntax: AVG (n1,n2..)
Example: Select AVG(10, 15, 30) FROM DUAL;
4. MAX: MAX followed by a column name returns the maximum value of that column.
Syntax: MAX (Column name)
Example: SELECT MAX (Sal) FROM emp;
SQL> select deptno,max(sal) from emp group by deptno;
DEPTNO MAX(SAL)
------ --------
10 5000
ALGORITHM:
Step 1: Start.
Step 2: Go to SQL.
Step 3: Enter the user name and password.
STEP 2: Create a table with its essential attributes.
Step 4: Type the commands for implementing Group by, Having Clause and Order by
Clause
Step 5: The output is displayed.
Step 6: Stop.
Theory
GROUP BY:
This query is used to group to all the records in a relation together for each
and every value of a specific key(s) and then display them for a selected set of fields the
relation.
GROUP BY-HAVING :
The HAVING clause was added to SQL because the WHERE
keyword could not be used with aggregate functions. The HAVING clause must follow the
GROUP BY clause in a query and must also precede the ORDER BY clause if used.
Example:
SQL> SELECT empno,SUM(SALARY) FROM emp,dept
WHERE emp.deptno =20 GROUP BY empno;
ORDER BY:
Example: SQL> SELECT empno, ename, job FROM emp ORDER BY job;
Syntax: SELECT <set of fields (from both relations)> FROM relation_1, relation_2
WHERE relation_1.field_x = relation_2.field_y ORDER BY field_z;
Aim : Study and implement set operators, nested queries and Join queries
Find the queries for the following :
Q1: Display all employee names and salary whose salary is greater than minimum salary of the
company and job title starts with 'A'„.
Q2: Issue a query to find all the employees who work in the same job as Arjun.
Q3: Display all the dept numbers available with the dept and emp tables avoiding duplicates.
Q4: Display all the dept numbers available with the dept and emp tables.
Q5: Display all the dept numbers available in emp and not in dept tables and vice versa.
Theory
set operators and nested queries
A subQuery is a form of an SQL statement that appears inside another SQL statement. It also
termed as nested Query. The statement containing a subQuery called a parent statement. The rows
returned by the subQuery are used by the following statement.
It can be used by the following commands:
1. To insert records in the target table.
2. To create tables and insert records in this table.
3. To update records in the target table.
4. To create view.
5. To provide values for the condition in the WHERE , HAVING IN ,
SELECT,UPDATE, and DELETE statements.
Exam:-
Creating clientmaster table from oldclient_master, table
Create table client_master
AS SELECT * FROM oldclient_master;
Using the Union, Intersect and Minus Clause:
Union Clause:
The user can put together multiple Queries and combine their output using the union
clause . The union clause merges the output of two or more Queries into a single set of
rows and column. The final output of union clause will be
Output: = Records only in Query one + records only in Query two + A single set of
records with is common in the both Queries.
Syntax:
SELECT columnname, columname
FROM tablename 1 UNION SELECT columnname, columnname From tablename2;
Intersect Clause:
The user can put together multiple Queries and their output using the
interest clause. The final output of the interest clause will be :
Output =A single set of records which are common in both Queries
Syntax:
SELECT columnname, columnname FROM tablename 1
INTERSECT
SELECT columnname, columnname FROM tablename 2;
Minus Clause:- The user can put together multiple Queries and combine their output
= records only in Query one Syntax:
SELECT columnname, columnname FROM tablename ;
Expected Output
Studied and implemented set operators, nested queries and Join queries.
Theory
PL/SQL has a variety of control structures that allow you to control the behaviour of the
block as it runs. These structures include conditional statements and loops.
If-then-
else Case
o Case with no else
o Labeled case
o Searched
case Simple loop
While loop
For loop
Goto and Labels
Conditional control in PL/SQL-
Syntax:
IF <condition> THEN
<Action>
ELSEIF<condition>
<Action>
ELSE
<Action>
ENDIF;
The WHILE LOOP:
Syntax:
WHILE <condition>
LOOP
<Action>
END LOOP;
The FOR LOOP statement:
Syntax:
FOR variable IN [REVERSE] start—end
LOOP
<Action>
END LOOP;
The GOTO statement: The goto statement allows you to change the flow of control
within a PL/SQL Block.
Expected Output
studied and implemented of various control structures using PL/SQL
Theory
A subprogram is a program unit/module that performs a particular task. These subprograms are
combined to form larger programs. This is basically called the 'Modular design'. A subprogram
can be invoked by another subprogram or program which is called the calling program.
A subprogram can be created −
• At the schema level
• Inside a package
• Inside a PL/SQL block
At the schema level, subprogram is a standalone subprogram. It is created with the CREATE
PROCEDURE or the CREATE FUNCTION statement. It is stored in the database and can be
deleted with the DROP PROCEDURE or DROP FUNCTION statement.
A subprogram created inside a package is a packaged subprogram. It is stored in the database
and can be deleted only when the package is deleted with the DROP PACKAGE statement. We
will discuss packages in the chapter 'PL/SQL - Packages'.
PL/SQL subprograms are named PL/SQL blocks that can be invoked with a set of parameters.
PL/SQL provides two kinds of subprograms −
• Functions − These subprograms return a single value; mainly used to compute and return
a value.
• Procedures − These subprograms do not return a value directly; mainly used to perform
an action.
This chapter is going to cover important aspects of a PL/SQL procedure. We will discuss
PL/SQL function in the next chapter.
Parts of a PL/SQL Subprogram
Each PL/SQL subprogram has a name, and may also have a parameter list. Like anonymous
PL/SQL blocks, the named blocks will also have the following three parts −
S.No Parts & Description
Declarative Part
It is an optional part. However, the declarative part for a subprogram does not start with
1 the DECLARE keyword. It contains declarations of types, cursors, constants, variables,
exceptions, and nested subprograms. These items are local to the subprogram and cease to
exist when the subprogram completes execution.
Executable Part
2
This is a mandatory part and contains statements that perform the designated action.
Exception-handling
3
This is again an optional part. It contains the code that handles run-time errors.
Creating a Procedure
A procedure is created with the CREATE OR REPLACE PROCEDURE statement. The
simplified syntax for the CREATE OR REPLACE PROCEDURE statement is as follows −
CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter_name [IN | OUT | IN OUT] type [, ...])]
{IS | AS}
Sahrdaya College of Engineering and Technology 28
BEGIN
< procedure_body >
END procedure_name;
Where,
• procedure-name specifies the name of the procedure.
• [OR REPLACE] option allows the modification of an existing procedure.
• The optional parameter list contains name, mode and types of the parameters. IN
represents the value that will be passed from outside and OUT represents the parameter
that will be used to return a value outside of the procedure.
• procedure-body contains the executable part.
• The AS keyword is used instead of the IS keyword for creating a standalone procedure.
Example
The following example creates a simple procedure that displays the string 'Hello World!' on the
screen when executed.
CREATE OR REPLACE PROCEDURE greetings
AS
BEGIN
dbms_output.put_line('Hello World!');
END;
/
When the above code is executed using the SQL prompt, it will produce the following result −
Procedure created.
Executing a Standalone Procedure
A standalone procedure can be called in two ways −
• Using the EXECUTE keyword
• Calling the name of the procedure from a PL/SQL block
The above procedure named 'greetings' can be called with the EXECUTE keyword as −
EXECUTE greetings;
The above call will display −
Hello World
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
CREATE OR REPLACE FUNCTION totalCustomers
RETURN number IS
total number(2) := 0;
BEGIN
SELECT count(*) into total
FROM customers;
RETURN total;
END;
/
When the above code is executed using the SQL prompt, it will produce the following result −
Function created.
Calling a 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.
A called function performs the defined task and when its return statement is executed or when the
last end statement is reached, it returns the program control back to the main program.
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. Following program calls
the function totalCustomers from an anonymous block −
DECLARE
c number(2);
Expected Output
Created procedures and functions specified above.
Theory
The package might include a set of procedures that forms an API, or a pool of type
definitions and variable declarations. The package is compiled and stored in the database, where
its contents can be shared by many applications.
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; sometimes the
body is unnecessary. 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.
You can think of the spec as an interface and of the body as a black box. You can debug,
enhance, or replace a package body without changing the package spec.
To create package specs, use the SQL statement CREATE PACKAGE. A CREATE PACKAGE
BODY statement defines the package body.
The body holds implementation details and private declarations, which are hidden from code
outside the package.
Following the declarative part of the package body is the optional initialization part,
which holds statements that initialize package variables and do any other one-time setup steps.
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.
A call spec lets you map a package subprogram to a Java method or external C function.
The call spec maps the Java or C name, parameter types, and return type to their SQL
counterparts.
You can reference package contents from database triggers, stored subprograms, 3GL
application programs, and various Oracle tools. For example, you can call package procedures as
shown in Example 9-3.
The following example calls the hire_employee procedure from an anonymous block in a Pro*C
program. The actual parameters emp_id, emp_lname, and emp_fname are host variables.
EXEC SQL EXECUTE
BEGIN
emp_actions.hire_employee(:emp_id,:emp_lname,:emp_fname, ...);
Restrictions
You cannot reference remote packaged variables, either directly or indirectly. For
example, you cannot call the a procedure through a database link if the procedure refers to a
packaged variable.
Inside a package, you cannot reference host variables.
Expected Output
Aim : Mini project (Application Development using Oracle/ MySQL using Database
Connectivity)
Expected Output