RELATION
RELATION
RELATION
UNIT -3
RELATIONAL MODEL
For our Student relation example, the relational schema will be:
STUDENT(ROLL_NUMBER INTEGER, NAME VARCHAR(20), CGPA FLOAT)
Relational Constraints
These are the restrictions or sets of rules imposed on the database
contents. It validates the quality of the database. It validates the various
operations like data insertion, updation, and other processes that have to
be performed without affecting the integrity of the data. It protects us
against threats/damages to the database.
1. Domain Constraints
Every domain must contain atomic values(smallest indivisible units)
which means composite and multi-valued attributes are not allowed.
We perform a datatype check here, which means when we assign a data
type to a column we limit the values that it can contain. Eg. If we assign
the datatype of attribute age as int, we can’t give it values other than int
datatype.
Example:
EID Name Phone
123456789
01 Bikash Dutta
234456678
Example:
01 Bikash 6000000009
02 Paul 9000090009
01 Tuhin 9234567892
Explanation: In the above table, EID is the primary key, and the first
and the last tuple have the same value in EID ie 01, so it is violating the
key constraint.
01 Bikash 9000900099
02 Paul 600000009
Explanation: In the above relation, EID is made the primary key, and
the primary key can’t take NULL values but in the third tuple, the
primary key is null, so it is violating Entity Integrity constraints.
The values of the foreign key in a tuple of relation R1 can either take the
values of the primary key for some tuple in relation R2, or can take NULL
values, but can’t be empty.
Example:
EID Name DNO
01 Divine 12
2 Dino 22
04 Vivian 14
DNO Place
12 Jaipur
13 Mumbai
14 Delhi
Explanation: In the above tables, the DNO of Table 1 is the foreign key,
and DNO in Table 2 is the primary key. DNO = 22 in the foreign key of
Table 1 is not allowed because DNO = 22 is not defined in the primary
key of table 2. Therefore, Referential integrity constraints are violated
here.
Union Operation
Set Different Operation
Cartesian Product Operation
Rename Operation
A∪S
UNION Operation: Notation:
relation that basically includes all the tuples that are present in A or in S,
or in both, eliminating the duplicate tuples.
INTERSECTION Operation:
Notations:
A∩S
where, A and S are the relations,
symbol ‘∩’ is used to denote the Intersection operator.
The result of Intersection operation, which is denoted by A ∩ S, is a
relation that basically includes all the tuples that are present in both A
an S.
PURPOS
FUNCTION EXAMPLE
E
Returns
SELECT
the
MIN(column)
MIN smallest
FROM
value in a
table_name
column.
Returns
SELECT
the
MAX(column)
MAX largest
FROM
value in a
table_name
column
Calculate
s the sum SELECT
of all SUM(column)
SUM
numeric FROM
values in table_name
a column
Returns
SELECT
the
AVG(column)
AVG average
FROM
value for
table_name
a column
Counts
the
SELECT
number
COUNT(colum COUNT(column
of non-
n) ) FROM
null
table_name
values in
a column
PURPOS
FUNCTION EXAMPLE
E
NULLs) in
a column
PL/SQL Introduction
Basics of PL/SQL
•PL/SQL stands for Procedural Language extensions to the Structured
Query Language (SQL).
PL/SQL is a combination of SQL along with the procedural features of
programming languages.
Oracle uses a PL/SQL engine to processes the PL/SQL statements.
PL/SQL includes procedural language elements like conditions and loops.
It allows declaration of constants and variables, procedures and
functions, types and variable of those types and triggers.
Disadvantages of SQL:
SQL doesn’t provide the programmers with a technique of condition
checking, looping and branching.
SQL statements are passed to Oracle engine one at a time which
increases traffic and decreases speed.
SQL has no facility of error checking during manipulation of data.
Features of PL/SQL:
1. PL/SQL is basically a procedural language, which provides the
functionality of decision making, iteration and many more features of
procedural programming languages.
2. PL/SQL can execute a number of queries in one block using single
command.
3. One can create a PL/SQL unit such as procedures, functions, packages,
triggers, and types, which are stored in the database for reuse by
applications.
4. PL/SQL provides a feature to handle the exception which occurs in
PL/SQL block known as exception handling block.
5. Applications written in PL/SQL are portable to computer hardware or
operating system where Oracle is operational.
6. PL/SQL Offers extensive error checking
BEGIN
executable statements
EXCEPTIONS
exception handling statements
END;
Declare section starts with DECLARE keyword in which variables,
constants, records as cursors can be declared which stores data
temporarily. It basically consists definition of PL/SQL identifiers. This part
of the code is optional.
Execution section starts with BEGIN and ends with END keyword.This is
a mandatory section and here the program logic is written to perform
any task like loops and conditional statements. It supports
all DML commands, DDL commands and SQL*PLUS built-in functions as
well.
PL/SQL identifiers
There are several PL/SQL identifiers such as variables, constants,
procedures, cursors, triggers etc.
Variables: Like several other programming languages, variables in
PL/SQL must be declared prior to its use. They should have a valid name
and data type as well. Syntax for declaration of variables:
variable_name datatype [NOT NULL := value ];
Example to show how to declare variables in PL/SQL :
C
SQL> DECLARE
var1 INTEGER;
var2 REAL;
var3 varchar2(20) ;
BEGIN
null;
END;
/
Output:
PL/SQL procedure successfully completed.
***************************************************************************
**********