20MCA002 (Assignment 2)
20MCA002 (Assignment 2)
Submitted To :-
Er. Rishab Choudhary Sir
Submitted By:-
Name- Avinash Nishad
ID No- 20MCA002
Class- MCA, II Semester
Relational data model
Relational data model is the primary data model, which is used widely around the world
for data storage and processing. This model is simple and it has all the properties and
capabilities required to process data with storage efficiency. Tables − In relational data
model, relations are saved in the format of Tables. This format stores the relation among
entities. A table has rows and columns, where rows represents records and columns
represent the attributes. Tuple − A single row of a table, which contains a single record
for that relation is called a tuple. Relation instance − A finite set of tuples in the relational
database system represents relation instance. Relation instances do not have duplicate
tuples. Relation schema − A relation schema describes the relation name (table name),
attributes, and their names. Relation key − Each row has one or more attributes, known as
relation key, which can identify the row in the relation (table) uniquely. Attribute domain
− Every attribute has some pre-defined value scope, known as attribute domain.
Constraints
Every relation has some conditions that must hold for it to be a valid relation. These
conditions are called Relational Integrity Constraints. There are three main integrity
constraints −
• Key constraints
• Domain constraints
• Referential integrity constraints
• Key Constraints
There must be at least one minimal subset of attributes in the relation, which can identify
a tuple uniquely. This minimal subset of attributes is called key for that relation. If there
are more than one such minimal subsets, these are called candidate keys. Key constraints
force that −
in a relation with a key attribute, no two tuples can have identical values for key
attributes. • a key attribute cannot have NULL values. Key constraints are also referred to
as Entity Constraints.
Domain Constraints
Attributes have specific values in real-world scenario. For example, age can only be a
positive integer. The same constraints have been tried to employ on the attributes of a
relation. Every attribute is bound to have a specific range of values. For example, age
cannot be less than zero and telephone numbers cannot contain a digit outside 0-9.
Referential integrity Constraints
Referential integrity constraints work on the concept of Foreign Keys. A foreign key is a
key attribute of a relation that can be referred in other relation. Referential integrity
constraint states that if a relation refers to a key attribute of a different or same relation,
then that key element must exist.
Entity integrity
Entity integrity is concerned with ensuring that each row of a table has a unique and non-
null primary key value; this is the same as saying that each row in a table represents a
single instance of the entity type modelled by the table For Entity Integrity Rule, each
table has a Primary Key. Primary Key cannot have NULL value. <Student>
Student_ID Student_Awards Student_Awards
Above, you can see our primary key is Student_ID. We cannot consider Student_Awards
as the primary key since not every student would have received the award.
4
example − <Employee>
Employee_ID Employee_Name Employee_Age Employee_Location
In the above table, the Primary Key is Employee_ID Let us now summarize the Entity
Integrity Rule −
• Make sure that each tuple in a table is unique. • Every table mush has a primary key, for
example, Student_ID for a Student table. • Every entity is unique. • The relations Primary
Key must have unique values for each row. • Primary Key cannot have NULL value and
must be unique. • Example can be an Employee_ID cannot be null in an Employee table.
Referential integrity
Referential integrity – It refers to the relationship between tables. Because each table in
a database must have a primary key, this primary key can appear in other tables because
of its relationship to data within those tables. ... Referential integrity is the logical
dependency of a foreign key on a primary key
Referential integrity refers to the relationship between tables. Because each table in a
database must have a primary key, this primary key can appear in other tables because of
its relationship to data within those tables. When a primary key from one table appears in
another table, it is called a foreign key.
Foreign keys join tables and establish dependencies between tables. tables can form a
hierarchy of dependencies in such a way that if you change or delete a row in one table,
you destroy the meaning of rows in other tables. For example, the following figure shows
that the customer_num column of the customer table is a primary key for that table and a
foreign key in the orders and cust_call tables. Customer number 106, George Watson™,
is referenced in both the orders and cust_calls tables. If customer 106 is deleted from the
customer table, the link between the three tables and this particular customer is destroyed.
Relational algebra in dbms is a procedural query language and main foundation is the
relational database and SQL. The goal of a relational algebra query language is to fetch
data from database or to perform various op.erations like delete, insert, update on the
data.
When it is said that relational algebra is a procedural query dbms language, it means that
it performs series of operations to produce the required result and tells the user what data
to be retrieved from database and how to retrieve it.
In relational algebra in dbms takes one relation as input and generate another relation as
output. It takes an instance of relations and performs operations on one or more relations
to describe another relation without changing the original relations.
Relational algebra is a procedural query language, which takes instances of relations as
input and yields instances of relations as output. It uses operators to perform queries. ...
Relational algebra is performed recursively on a relation and intermediate results are also
considered relations.
Relational Algebra Operations in dbms
Different relational algebra operations in dbms are as below:
1. Select
2. Project
3. Union
4. Set different
5. Cartesian product
6. Rename
1. Select Operation (σ)
Selection operation in relational algebra is used to find the tuples in a relation which
satisfy the given condition. It is denoted by sigma (σ).
Notation − σp(r)
Where σ indicates selection predicate and r denotes relation and p is a propositional logic
formula which may use relational operators like and, or, and not.
σaccount_type = “saving”(Account)
Output – It selects tuples from relation Account where the account type is ‘saving.’
2.Project Operation (∏)
Project or projection operation in dbms relational algebra is used to select required
attributes or columns from relation. Symbol ∏ indicates Project operation.
Notation − ∏Attr1, Attr2, —Attrn (r)
Where Attr1, Attr , Attrn are attribute of relation r.
∏Stud_rollno, name, city (Students)
Output – It selects attributes stud_rollno, name and city from relation Student
3.Union Operation (∪)
Union operator in relational algebra is used to select all the tuples from two relations.
Symbol∪indicates union operators.
Notation – r1 U r2
Where r1 and r2 are the relations in the database.
For example, in r1 ∪ r2, the union of two relations r1 and r2 produces an output relation
that contains all the tuples of r1, or r2, or both r1 and r2, duplicate tuples being
eliminated. The condition is that r1 and r2 must have same number of attributes.
To perform the union operation, the following rules must be followed
r1 and r2 must have the same number of attributes. In both relations, attribute domains
must have same scope.
∏ customer_name (Depositor) ∪ ∏ customer_name (Borrower)
Output – It gives the customer name from both relation Depositor and
Relational calculus
It is a non-procedural query language, and instead of algebra, it uses mathematical
predicate calculus. The relational calculus is not the same as that of differential and
integral calculus in mathematics but takes its name from a branch of symbolic logic
termed as predicate calculus. When applied to databases, it is found in two forms. These
are
Tuple relational calculus is used for selecting those tuples that satisfy the given condition.
Table: Student
First_Name Last_Name Age ---------- --------- ---- Ajeet Singh 30
Chaitanya Singh 31 Rajeev Bhatia 27 Carl Pratap 28 Lets
write relational calculus queries.
Query to display the last name of those students where age is greater than 30
{ t.Last_Name | Student(t) AND t.age > 30 } In the above query you can see two parts
separated by | symbol. The second part is where we define the condition and in the first
part we specify the fields which we want to display for the selected tuples.
The result of the above query would be:
Last_Name --------- Singh Query to display all the details of students where Last name is
‘Singh’
{ t | Student(t) AND t.Last_Name = 'Singh' } Output:
First_Name Last_Name Age ---------- --------- ---- Ajeet Singh 30
Chaitanya Singh 31
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 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 This is a mandatory part and contains statements that perform the
designated action.
Exception-handling 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} 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;
13
The above call will display − Hello World
PL/SQL procedure successfully completed. The procedure can also be called from
another PL/SQL block −
BEGIN greetings; END; / The above call will display − Hello World
OUT An OUT parameter returns a value to the calling program. Inside the subprogram,
an OUT parameter acts like a variable. You can change its value and reference the value
after assigning it. The actual parameter must be variable and it is passed by value.
3 IN OUT
A standalone procedure is deleted with the DROP PROCEDURE statement. Syntax for
deleting a procedure is − DROP PROCEDURE procedure-name; You can drop the
greetings procedure by using the following statement − DROP PROCEDURE greetings;
Parameter Modes in PL/SQL Subprograms
The following table lists out the parameter modes in PL/SQL subprograms −
IN & OUT Mode Example 1
This program finds the minimum of two values. Here, the procedure takes two numbers
using the IN mode and returns their minimum using the OUT parameters.
DECLARE a number; b number; c number; PROCEDURE findMin(x IN number,
y IN number, z OUT number) IS BEGIN IF x < y THEN z:= x; ELSE z:=
y; END IF; END; BEGIN a:= 23; b:= 45; findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c); END; /
An IN OUT parameter passes an initial value to a subprogram and returns an updated
value to the caller. It can be assigned a value and the value can be read. The actual
parameter corresponding to an IN OUT formal parameter must be a variable, not a
constant or an expression. Formal parameter must be assigned a value. Actual parameter
is passed by value.
15
When the above code is executed at the SQL prompt, it produces the following result −
Minimum of (23, 45) : 23