Bca 4th Sem Rdbms Well
Bca 4th Sem Rdbms Well
Bca 4th Sem Rdbms Well
RDBMS
UNIT-1
The table name and column names are helpful to interpret the meaning of values in each
row. The data are represented as a set of relations. In the relational model, data are stored
as tables. However, the physical storage of the data is independent of the way the data are
logically organized.
1. Attribute: Each column in a Table. Attributes are the properties which define a
relation. e.g., Student_Rollno, NAME,etc.
2. Tables – In the Relational model the, relations are saved in the table format. It is
stored along with its entities. A table has two properties rows and columns. Rows
represent records and columns represent attributes.
3. Tuple – It is nothing but a single row of a table, which contains a single record.
4. Relation Schema: A relation schema represents the name of the relation with its
attributes.
5. Degree: The total number of attributes which in the relation is called the degree of
the relation.
6. Cardinality: Total number of rows present in the Table.
7. Column: The column represents the set of values for a specific attribute.
8. Relation instance – Relation instance is a finite set of tuples in the RDBMS system.
Relation instances never have duplicate tuples.
9. Relation key - Every row has one, two or multiple attributes, which is called relation
key.
10. Attribute domain – Every attribute has some pre-defined value and scope which is
known as attribute domain
Relational Integrity constraints is referred to conditions which must be present for a valid
relation. These integrity constraints are derived from the rules in the mini-world that the
database represents.
There are many types of integrity constraints. Constraints on the Relational database
management system is mostly divided into three main categories are:
1. Domain constraints
2. Key constraints
3. Referential integrity constraints
Domain Constraints
Domain constraints specify that within each tuple, and the value of each attribute must be
unique. This is specified as data types which include standard data types integers, real
numbers, characters, Booleans, variable length strings, etc.
Key constraints
An attribute that can uniquely identify a tuple in a relation is called the key of the table. The
value of the attribute for different tuples in the relation has to be unique.
Referential integrity constraints is base on the concept of Foreign Keys. A foreign key is an
important attribute of a relation which should be referred to in other relationships.
Referential integrity constraint state happens where relation refers to a key attribute of a
different or same relation. However, that key element must exist in the table.
Tuple for Customer ID =1 is referenced twice in the relation Billing. So we know Customer
Name=Google has billing amount $300
Codd Rules :
Codd rules were proposed by E.F. Codd which should be satisfied by relational model.
1. Foundation Rule: For any system that is advertised as, or claimed to be, a relational
data base management system, that system must be able to manage data bases
entirely through its relational capabilities.
2. Information Rule: Data stored in Relational model must be a value of some cell of a
table.
3. Guaranteed Access Rule: Every data element must be accessible by table name, its
primary key and name of attribute whose value is to be determined.
4. Systematic Treatment of NULL values: NULL value in database must only correspond
to missing, unknown or not applicable values.
5. Active Online Catalog: Structure of database must be stored in an online catalog which
can be queried by authorized users.
6. Comprehensive Data Sub-language Rule: A database should be accessible by a
language supported for definition, manipulation and transaction management
operation.
7. View Updating Rule: Different views created for various purposes should be
automatically updatable by the system.
8. High level insert, update and delete rule: Relational Model should support insert,
delete, update etc. operations at each level of relations. Also, set operations like Union,
Intersection and minus should be supported.
9. Physical data independence: Any modification in the physical location of a table should
not enforce modification at application level.
10. Logical data independence: Any modification in logical or conceptual schema of a table
should not enforce modification at application level. For example, merging of two
tables into one should not affect application accessing it which is difficult to achieve.
11. Integrity Independence: Integrity constraints modified at database level should not
enforce modification at application level.
12. Distribution Independence: Distribution of data over various locations should not be
visible to end-users.
13. Non-Subversion Rule: Low level access to data should not be able to bypass integrity
rule to change data.
Relational Algebra
• Select
• Project
• Union
• Set different
• Cartesian product
• Rename
The result of set difference operation is tuples, which are present in one relation but are
not in the second relation.
Notation − r − s
The results of relational algebra are also relations but without any name. The rename
operation allows us to rename the output relation. 'rename' operation is denoted with
small Greek letter rho ρ.
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Relational Calculus
Notation −
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
{< article, page, subject > | ∈ Tutorials ∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation Tutorials, where subject is
database.
The expression power of Tuple Relation Calculus and Domain Relation Calculus is
equivalent to Relational Algebra.
UNIT-II
Functional Dependency
The functional dependency is a relationship that exists between two attributes. It typically
exists between the primary key and non-key attribute within a table.
X → Y
Here Emp_Id attribute can uniquely identify the Emp_Name attribute of employee table
because if we know the Emp_Id, we can tell that employee name associated with it.
1. Emp_Id → Emp_Name
Types of Functional Dependency
1.In a functional dependency statement ,attributes present at left hand and right hand side
should possess one-to-one relationship.
Transitive dependency
X -> Z is a transitive dependency if the following three functional dependencies hold true:
• X->Y
• Y does not ->X
• Y->Z
Anomalies in DBMS
There are three types of anomalies that occur when the database is not normalized. These
are – Insertion, update and deletion anomaly. Let’s take an example to understand this.
Example: Suppose a manufacturing company stores the employee details in a table named
employee that has four attributes: emp_id for storing employee’s id, emp_name for storing
employee’s name, emp_address for storing employee’s address and emp_dept for storing
the department details in which the employee works. At some point of time the table looks
like this:
Update anomaly: In the above table we have two rows for employee Rick as he belongs to
two departments of the company. If we want to update the address of Rick then we have to
update the same in two rows or the data will become inconsistent. If somehow, the correct
address gets updated in one department but not in other then as per the database, Rick
would be having two different addresses, which is not correct and would lead to
inconsistent data.
Insert anomaly: Suppose a new employee joins the company, who is under training and
currently not assigned to any department then we would not be able to insert the data into
the table if emp_dept field doesn’t allow nulls.
Delete anomaly: Suppose, if at a point of time the company closes the department D890
then deleting the rows that are having emp_dept as D890 would also delete the information
of employee Maggie since she is assigned only to this department.
Normalization
As per the rule of first normal form, an attribute (column) of a table cannot hold multiple
values. It should hold only atomic values.
Example: Suppose a company wants to store the names and contact details of its
employees. It creates a table that looks like this:
8812121212
102 Jon Kanpur
9900012222
This table is not in 1NF as the rule says “each attribute of a table must have atomic (single)
values”, the emp_mobile values for employees Jon & Lester violates that rule.
To make the table complies with 1NF we should have the data like this:
An attribute that is not part of any candidate key is known as non-prime attribute.
Example: Suppose a school wants to store the data of teachers and the subjects they teach.
They create a table that looks like this: Since a teacher can teach more than one subjects,
the table can have multiple rows for a same teacher.
111 Maths 38
111 Physics 38
222 Biology 38
333 Physics 40
333 Chemistry 40
The table is in 1 NF because each attribute has atomic values. However, it is not in 2NF
because non prime attribute teacher_age is dependent on teacher_id alone which is a
proper subset of candidate key. This violates the rule for 2NF as the rule says “no non-prime
attribute is dependent on the proper subset of any candidate key of the table”.
An attribute that is not part of any candidate key is known as non-prime attribute.
Or
It is an advance version of 3NF that’s why it is also referred as 3.5NF. BCNF is stricter than
3NF. A table complies with BCNF if it is in 3NF and for every functional dependency X->Y, X
should be the candidate key of the table.
UNIT-III
What does SQL Mean :
CHAR(Size) It is used to specify a fixed length string that can contain numbers, letters, and special
characters.Its size can be 0 to 255 characters. Default is 1
VARCHAR(Size) It is used to specify a variable length string that can contain numbers, letters, and special c
Its size can be from 0 to 65535 characters.
TEXT(Size) It holds a string that can contain a maximum length of 255 characters.
BLOB(size) It is used for BLOBs (Binary Large Objects). It can hold up to 65,535 bytes.
SQL Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the
accuracy and reliability of the data in the table. If there is any violation between the
constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column,
and table level constraints apply to the whole table.
Schema :
A schema in a SQL database is a collection of logical structures of data. The schema is owned
by a database user and has the same name as the database user. Security permissions can
be applied to schemas hence schemas are an important tool for separating and protecting
database objects on the basis of user access rights. It improves flexibility for security-related
administration of the database.
SQL Commands :
▪ CREATE
▪ DROP
▪ TRUNCATE
▪ ALTER
▪ BACKUP DATABASE
CREATE
This statement is used to create a table or a database.
Syntax
CREATE DATABASE DatabaseName;
Example :
CREATE DATABASE Employee;
Syntax
CREATE TABLE TableName (
Column1 datatype,
Column2 datatype,
Column3 datatype,
....
ColumnN datatype
);
Example :
CREATE TABLE Employee_Info (
EmployeeID int,
EmployeeName varchar(255),
);
DROP
This statement is used to drop an existing table or a database.
Syntax
DROP DATABASE DatabaseName;
Example :
TRUNCATE
This command is used to delete the information present in the table but does not delete the
table. So once you use this command, your information will be lost, but not the table.
Syntax
TRUNCATE TABLE TableName;
Example :
TRUNCATE Table Employee_Info;
ALTER
This command is used to delete, modify or add constraints or columns in an existing table.
Syntax :
Example :
INSERT INTO Customers (CustomerName, ContactName);
VALUES ('Saket', 'Tom', 'Delhi');
DELETE Syntax
DELETE FROM table_name WHERE condition;
Example :
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example :
UPDATE Customers
SET ContactName = 'Shivam', City= 'Delhi'
WHERE CustomerID = 1;
VIEWS :
Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a
real table in the database. We can create a view by selecting fields from one or more tables
present in the database. A View can either have all the rows of a table or specific rows
based on certain condition.
We can create View using CREATE VIEW statement. A View can be created from a single
table or multiple tables.
Syntax :
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
DELETING VIEWS :
Drop VIEW view_name;
UNIT-IV
INTRODUCTION TO PL/SQL :
PL/SQL BLOCK :
8.
Declaration section
A PL/SQL block has a declaration section where you declare variables, allocate
memory for cursors, and define data types.
2) Executable section :
A PL/SQL block has an executable section. An executable section starts with the
keyword BEGIN and ends with the keyword END. The executable section must have a
least one executable statement, even if it is the NULL statement which does nothing.
3) Exception-handling section :
The Oracle engine can process not only single SQL statement but also block of many
statements. The call to Oracle engine needs to be made only once to execute any number
of SQL statements if these SQL statements are bundled inside a PL/SQL block.
The PL/SQL engine resides in the Oracle engine, the Oracle engine can process not only
single SQL statements but also entire PL/SQL blocks.
These blocks are sent to the PL/SQL engine, where procedural statements are executed and
SQL statements are sent to the SQL executor in the Oracle engine. Since the PL/SQL engine
resides in the Oracle engine, this is an efficient and fast operation.
The call to the Oracle engine needs to be made only once to execute any number of SQL
statements, if these SQL statements are bundled inside a PL/SQL block.
Since the oracle engine is called only once for each block, the speed of SQL statement
execution is vastly enhanced, when compared to the Oracle engine being called once for
each SQL sentence.
A condition is any variable or expression that returns a BOOLEAN value (TRUE or FALSE). The
iteration structure executes a sequence of statements repeatedly as long as a condition
holds true. The sequence-structure simply executes a sequence of statements in the order
in which they occur.
Procedural computer programs use the basic control structures.
1.The selection structure tests a condition, then executes one sequence of statements
instead of another, depending on whether the condition is true or false. A condition is
any variable or expression that returns a BOOLEAN value (TRUE or FALSE).
PL/SQL CURSORS : A cursor is a pointer to this context area. PL/SQL controls the context
area through a cursor. A cursor holds the rows (one or more) returned by a SQL statement.
The set of rows the cursor holds is referred to as the active set.
You can name a cursor so that it could be referred to in a program to fetch and process the
rows returned by the SQL statement, one at a time. There are two types of cursors −
• Implicit cursors
• Explicit cursors
Implicit Cursors :
Explicit Cursors :
Explicit cursors are programmer-defined cursors for gaining more control over the context
area. An explicit cursor should be defined in the declaration section of the PL/SQL Block. It
is created on a SELECT Statement which returns more than one row.
an explicit cursor includes the following steps −
PL/SQL TRIGGERS : Triggers are stored programs, which are automatically executed or
fired when some events occur. Triggers are, in fact, written to be executed in response
to any of the following events −
Symbols in PL/SQL :
Symbol Description
% Percent sign: attribute indicator. Symbol used with the LIKE condition
@ At- sign
1 Numeric
Numeric values on which arithmetic operations are performed.
Character
2
Alphanumeric values that represent single characters or strings of characters.
Boolean
3
Logical values on which logical operations are performed.
Datetime
4
Dates and times.
Q5. Explain the concept of relational algebra in detail with various algebraic operations?
Q7. Explain the concept of normalization with different Normal forms with example?
Q12.Write SQL query for selecting columns from a table named as student?
Q13. Write SQL query for outputting Sorted Data using Órder by Clause’’?
Q18.Write SQL query to fetch unique values of DEPARTMENT from Worker table?
Q19. Write SQL query to delete record from customers table where id=6?
Q25.Write SQL query to update table name as customer to change contact name to Riya and
Country is India?
Q26.Write SQL query for outputting sorted data using ‘Örder by’ clause?
ANSWERS
Q20. PL/SQL is an extension of Structured Query Language (SQL) that is used in Oracle.
Unlike SQL, PL/SQL allows the programmer to write code in a procedural format. Full form
of PL/SQL is "Procedural Language extensions to SQL".
It combines the data manipulation power of SQL with the processing power of procedural
language to create super powerful SQL queries.
PL/SQL means instructing the compiler 'what to do' through SQL and 'how to do' through its
procedural way.
Architecture of PL/SQL
1. PL/SQL block
2. PL/SQL Engine
3. Database Server
Q22. PL/SQL is a block-structured language whose code is organized into blocks. A PL/SQL
block consists of three sections: declaration, executable, and exception-handling sections. In
a block, the executable section is mandatory while the declaration and exception-handling
sections are optional. A PL/SQL block has a name.
) Declaration section
A PL/SQL block has a declaration section where you declare variables, allocate memory
for cursors, and define data types.
2) Executable section
A PL/SQL block has an executable section. An executable section starts with the
keyword BEGIN and ends with the keyword END. The executable section must have a least
one executable statement, even if it is the NULL statement which does nothing.
3) Exception-handling section
A PL/SQL block has an exception-handling section that starts with the keyword EXCEPTION.
The exception-handling section is where you catch and handle exceptions raised by the code
in the execution section.
The Oracle engine can process not only single SQL statement but also block of many statements.
The call to Oracle engine needs to be made only once to execute any number of SQL statements if
these SQL statements are bundled inside a PL/SQL block.
The PL/SQL engine resides in the Oracle engine, the Oracle engine can process not only
single SQL statements but also entire PL/SQL blocks.
These blocks are sent to the PL/SQL engine, where procedural statements are executed and
SQL statements are sent to the SQL executor in the Oracle engine. Since the PL/SQL engine
resides in the Oracle engine, this is an efficient and fast operation.
The call to the Oracle engine needs to be made only once to execute any number of SQL
statements, if these SQL statements are bundled inside a PL/SQL block.
Since the oracle engine is called only once for each block, the speed of SQL statement
execution is vastly enhanced, when compared to the Oracle engine being called once for
each SQL sentence.
A condition is any variable or expression that returns a BOOLEAN value (TRUE or FALSE). The
iteration structure executes a sequence of statements repeatedly as long as a condition
holds true. The sequence-structure simply executes a sequence of statements in the order
in which they occur.