0% found this document useful (0 votes)
14 views24 pages

Dbms Notes Unit 2

Uploaded by

itzshivanand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views24 pages

Dbms Notes Unit 2

Uploaded by

itzshivanand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

UNIT 2

RELATIONAL DATA MODEL AND LANGUAGE


Relational Model Concepts in DBMS
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

Integrity Constraints
Integrity constraints are a set of rules. It is used to maintain the quality of
information.Integrity constraints ensure that the data insertion, updating, and other
processes have to be performed in such a way that data integrity is not affected.
Domain constraints
Domain constraints can be defined as the definition of a valid set of values for an attribute.
The data type of domain includes string, character, integer, time, date, currency, etc. The
value of the attribute must be available in the corresponding domain.
Example:

Entity integrity constraints


The entity integrity constraint states that primary key value can't be null.This is because the
primary key value is used to identify individual rows in relation and if the primary key has a
null value, then we can't identify those rows.A table can contain a null value other than the
primary key field.
Example:

Referential Integrity Constraints


A referential integrity constraint is specified between two tables.In the Referential integrity
constraints, if a foreign key in Table 1 refers to the Primary Key of Table 2, then every value
of the Foreign Key in Table 1 must be null or be available in Table 2.
Example:
Key constraints
Keys are the entity set that is used to identify an entity within its entity set uniquely.An entity
set can have multiple keys, but out of which one key will be the primary key. A primary key
can contain a unique and null value in the relational table.
Example:
Relational Algebra
Relational Algebra is procedural query language, which takes Relation as input and generates
relation as output.
Types of Relational operation

1. Selection Operation
2. Projection Operation
3. Union Operation
4. Intersection Operation
5. Difference Operation
6. Cartisian product Operation
7. Rename Operation

Selection Operation:-
• The select operation selects tuples that satisfy a given condition.
• It is denoted by sigma (σ).
Notation:- σ p(r)
Where
σ is used for selection condition.
r is used for relation.
P is used for propositional logic formula which may use connectors like: AND OR and NOT.
These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
For example: LOAN Relation
BRANCH_NAME LOAN_NO AMOUNT

Downtown L-17 1000

Redwood L-23 2000

Perryride L-15 1500

Downtown L-14 1500

Mianus L-13 500

Roundhill L-11 900

Perryride L-16 1300

Input:
σ BRANCH_NAME="perryride" (LOAN)

Output:

BRANCH_NAME LOAN_NO AMOUNT

Perryride L-15 1500

Perryride L-16 1300

Project Operation:
o This operation shows the list of those attributes that we wish to appear in the result.
Rest of the attributes is eliminated from the table.
o It is denoted by ∏.
Notation: ∏ A1, A2, An (r)
Where
A1, A2, A3 is used as an attribute name of relation r.
Example: CUSTOMER RELATION

NAME STREET CITY

Jones Main Harrison

Smith North Rye

Hays Main Harrison

Curry North Rye

Johnson Alma Brooklyn

Brooks Senator Brooklyn

Input:
∏ NAME, CITY (CUSTOMER)

Output:

NAME CITY

Jones Harrison

Smith Rye

Hays Harrison

Curry Rye

Johnson Brooklyn

Brooks Brooklyn
Union Operation:
o Suppose there are two tuples R and S. The union operation contains all the tuples that
are either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
Notation: R ∪ S
A union operation must hold the following condition:
o R and S must have the attribute of the same number.
o Duplicate tuples are eliminated automatically.
DEPOSITOR RELATION

CUSTOMER_NAME ACCOUNT_NO

Johnson A-101

Smith A-121

Mayes A-321

Turner A-176

Johnson A-273

Jones A-472

Lindsay A-284
BORROW RELATION

CUSTOMER_NAME LOAN_NO

Jones L-17

Smith L-23

Hayes L-15

Jackson L-14

Curry L-93

Smith L-11

Williams L-17

Input:
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)

Output:

CUSTOMER_NAME

Johnson

Smith

Hayes

Turner

Jones
Lindsay

Jackson

Curry

Williams

Mayes

Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation contains all
tuples that are in both R & S.
o It is denoted by intersection ∩.
Notation: R ∩ S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)

Output:

CUSTOMER_NAME

Smith

Jones

Set Difference:
o Suppose there are two tuples R and S. The set difference operation contains all tuples
that are in R but not in S.
o It is denoted by intersection minus (-).
Notation: R - S
Example Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)
Output:

CUSTOMER_NAME

Jackson

Hayes

Willians

Curry

Cartesian product
o The Cartesian product is used to combine each row in one table with each row in the
other table. It is also known as a cross product.
o It is denoted by X.
Notation: E X D
Example:
EMPLOYEE

EMP_ID EMP_NAME EMP_DEPT

1 Smith A

2 Harry C

3 John B
DEPARTMENT

DEPT_NO DEPT_NAME

A Marketing

B Sales

C Legal

Input:
EMPLOYEE X DEPARTMENT
Output:

EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME

1 Smith A A Marketing

1 Smith A B Sales

1 Smith A C Legal

2 Harry C A Marketing

2 Harry C B Sales

2 Harry C C Legal

3 John B A Marketing

3 John B B Sales

3 John B C Lega

Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
ρ (STUDENT1, STUDENT)

Join Operations:
A Join operation combines related tuples from different relations, if and only if a given join
condition is satisfied. It is denoted by ⋈.
Example:
Table 1

ID NAME AGE

101 Alice 23

102 Bob 28

103 Charlee 32

Table 2

ID ADDRESS SALARY

102 New York 50000

103 Boston 30000

104 San Diego 25000

Types of Join operations:


1. Natural Join
2. Equi join
3. Outer Join

1. Natural Join:
A Natural Join is a type of Join that matches columns with the same name in both tables.
Syntax of Natural Join
SELECT table1.column1, table2.column2FROM table1NATURAL JOIN table2;
Example: Let's use the above Table 1 table and Table 2 table:
Input:
SELECT Table1.ID, Table1.Name, Table1.Age, Table2.Address, Table2.SalaryFROM Table1
NATURAL JOIN Table2;

Output:
ID NAME AGE ADDRESS SALARY
102 Bob 28 New York 50000
103 Charlee 32 Boston 30000

2. Equi join:
It is also known as an inner join. It is the most common join. It is based on matched data as
per the equality condition. The equi join uses the comparison operator (=).
Syntax of Equi Join
SELECT table1.column1, table2.column2FROM table1INNER JOIN
table2ONtable1.columnX = table2.columnY;
Example:Let's use the above Table 1 table and Table 2 table:
Input:
SELECT Table1.Name, Table2.AddressFROM Table1INNER JOIN Table2ON Table1.ID =
Table2.ID;
Output:

NAME ADDRESS

Bob New York


Charlee Boston

3. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal with missing
information.
An outer join is basically of three types:
a. Left outer join
b. Right outer join
c. Full outer join
a. Left outer join:

o A Left Outer Join in DBMS returns all the rows from the left table and the matching
rows from the right table. If there is no match, NULL values are returned for the
missing rows.
o It is denoted by ⟕.
Syntax of Left Outer Join
SELECT table1.column1, table2.column2FROM table1LEFT JOIN table2ON
table1.columnX = table2.columnY;
Input:
SELECT Table1.Name, Table2.AddressFROM Table1LEFT JOIN Table2ON Table1.ID =
Table2.ID;

NAME ADDRESS

Alice Null

Bob New York

Charlee Boston

b. Right outer join: A Right Outer Join returns all the rows from the right table and the
matching rows from the left table. If there is no match, NULL values are returned for the
missing rows. It is denoted by ⟖.
Syntax of Right Outer Join
SELECT table1.column1, table2.column2FROM table1RIGHT JOIN table2ON
table1.columnX = table2.columnY;
Query:
SELECT Table1.Name, Table2.AddressFROM Table1RIGHT JOIN Table2ON Table1.ID =
Table2.ID;
Output:

NAME ADDRESS

Bob New York

Charlee Boston

Null San Diego

c. Full outer join:

A Full Outer Join returns all the rows from both tables and NULL values for the missing
rows. It is denoted by ⟗.

Syntax of Full Outer Join


SELECT table1.column1, table2.column2FROM table1FULL OUTER JOIN table2ON
table1.columnX = table2.columnY;
Query:
SELECT Table1.Name, Table2.AddressFROM Table1FULL OUTER JOIN Table2ON
Table1.ID = Table2.ID;
Output:

NAME ADDRESS

Alice Null

Bob New York


Charlee Boston

Null San Diego

Relational Calculus
Relational calculus is a non procedural query language that tells the system what to retrieve
but does not tell how to retrieve.

Types of Relational calculus:

Tuple Relational Calculus (TRC)


Tupe Relational Calculus in DBMS is based on the concept of selecting tuples (rows) from a
relation (table) that satisfies certain conditions.

Notation:

A Query in the tuple relational calculus is expressed as following notation

{T | P (T)} or {T | Condition (T)}


Where

T is the resulting tuples

P(T) is the condition used to fetch T.


Customer Table
Customer_id Name Zip code

1 Rohit 12345

2 Rahul 13245

3 Rohit 56789

4 Amit 12345.

Example 1: Write a TRC query to get all the data of customers whose zip code is 12345.
Query:
{t | Customer(t) ∧ t.Zipcode = 12345 }

Result of the TRC expression above:

Customer_id Name Zip code


1 Rohit 12345
4. Amit 12345

Example 1: Write a TRC query to get Name of customers whose zip code is 56789.
Query:
{t.Name | Customer (t) ∧ t.Zipcode = 56789 }

Result of the TRC expression above:

Name
Rohit

Domain Relational Calculus (DRC)


Domain Relational Calculus in DBMS is based on the concept of selecting values from a
relation (table) that satisfy certain conditions.

Notation:

{ <a1, a2, a3, ..., an> | P (a1, a2, a3, ... ,an)}
Where
Here, <x1,x2,...,xn> represents the domain variables used to get the column values.
P(x1,x2,...,xn) is the predicate which is the condition for results.

Example 1: Write a DRC query to get Customer id,Name of customers whose zip code is
12345.
Query:
{ Customer_id, Name | ∈ Customer (t) ∧ t.Zipcode = 12345 }

Result of the TRC expression above:

Customer_id Name
1 Rohit
4. Amit

Introduction to SQL

SQL

• SQL stands for Structured Query Language. It is used for storing and managing data
in relational database management system (RDMS).
• It is a standard language for Relational Database System. It enables a user to create,
read, update and delete relational databases and tables.
• All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL
as their standard database language.
• SQL allows users to query the database in a number of ways, using English-like
statements.
Why SQL?
SQL is widely popular because it offers the following advantages −
• Allows users to access data in the relational database management systems.
• Allows users to describe the data.
• Allows users to define the data in a database and manipulate that data.
• Allows users to create and drop databases and tables.
• Allows users to create view, stored procedure, functions in a database.
• Allows users to set permissions on tables, procedures and views.

A Brief History of SQL


• 1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the father of relational
databases. He described a relational model for databases.
• 1974 − Structured Query Language appeared.
• 1978 − IBM worked to develop Codd's ideas and released a product named System/R.
• 1986 − IBM developed the first prototype of relational database and standardized by
ANSI. The first relational database was released by Relational Software which later
came to be known as Oracle.
Characteristics of SQL

• SQL is easy to learn.


• SQL is used to access data from relational database management systems.
• SQL can execute queries against the database.
• SQL is used to describe the data.
• SQL is used to define the data in the database and manipulate it when needed.
• SQL is used to create and drop the database and table.
• SQL is used to create a view, stored procedure, function in a database.
• SQL allows users to set permissions on tables, procedures, and views.

Advantages of SQL

1. Simple and easy


2. Faster Query Processing
3. Standard Language
4. Portable
5. Highly Interactive

Simple and easy


One of the things that make SQL a popular choice is that it doesn't require any substantial
knowledge about coding and writing programs. SQL has some basic keywords such as,
SELECT, INSERT INTO, UPDATE, etc that can carry out tasks.
Faster Query Processing
SQL works with an efficient speed. Huge data can be processed in seconds! Querying,
manipulating, and calculations on data with analytical queries in a relational database can be
done in no time.

Standards Language

Long established are used by the SQL databases that are being used by ISO and ANSI.

High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large amount of records
from a database.

SQL Commands
• SQL commands are instructions. It is used to communicate with the database. It is
also used to perform specific tasks, functions, and queries of data.
• SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.

Types of SQL Commands

There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
1. Data Definition Language (DDL)
o DDL changes the structure of the table like creating a table, deleting a table, altering a
table, etc.
o All the command of DDL are auto-committed that means it permanently save all the
changes in the database.

Here are some commands that come under DDL:

o CREATE
o ALTER
o DROP
o TRUNCATE

CREATE: - It is used to create a new table in the database.

Syntax:

CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);


DROP: It is used to delete both the structure and record stored in the table.

Syntax

DROP TABLE table_name;

ALTER: It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.

Syntax:

To add a new column in the table

ALTER TABLE table_name ADD (column_name Datatypes);

To modify existing column in the table:

ALTER TABLE table_name MODIFY (column_name Datatypes);

TRUNCATE: It is used to delete all the rows from the table and free the space containing
the table.

Syntax:

TRUNCATE TABLE table_name;

2. Data Manipulation Language


o DML commands are used to modify the database. It is responsible for all form of
changes in the database.
o The command of DML is not auto-committed that means it can't permanently save all
the changes in the database. They can be rollback.

Here are some commands that come under DML:

o INSERT
o UPDATE
o DELETE

INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a
table.

Syntax:
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2,
value3, .... valueN);

For example:

INSERT INTO BOOK (Author, Subject) VALUES ("Sonoo", "DBMS");

UPDATE: This command is used to update or modify the value of a column in the table.

Syntax:

UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WH


ERE CONDITION]

For example:

UPDATE students SET User_Name = 'Sonoo'WHERE Student_Id = '3'

DELETE: It is used to remove one or more row from a table.

Syntax:

DELETE FROM table_name [WHERE condition];

For example:

DELETE FROM BOOK WHERE Author="Sonoo";

3.Data Control Language

DCL commands are used to grant and take back authority from any database user.

Here are some commands that come under DCL:

o Grant
o Revoke

Grant: It is used to give user access privileges to a database.

Syntax:

GRANT <object-Privilage> ON <Table_Name> TO <User_Name>


Here Privilege name is select, update, delete, insert, alter etc.

For example:Consider a scenario where you are database administrator and a student
table in the database suppose you want a specific user Reeta to only select for retrieve
the data from the student table.

GRANT SELECT ON student TO Reeta;

Revoke: It is used to take back permissions from the user.

Syntax:

REVOKE <object-Privilage> ON <Table_Name> FROM <User_Name>


Example:
REVOKE SELECT ON student FROM Reeta;

4. Transaction Control Language

TCL commands can only use with DML commands like INSERT, DELETE and UPDATE
only.

These operations are automatically committed in the database that's why they cannot be used
while creating tables or dropping them.

Here are some commands that come under TCL:

o COMMIT
o ROLLBACK
o SAVEPOINT

Commit: Commit command is used to save all the transactions to the database.

Syntax:

COMMIT;

Example:

DELETE FROM CUSTOMERS WHERE AGE = 25;


COMMIT;

Rollback: Rollback command is used to undo transactions that have not already been saved
to the database.
Syntax:

ROLLBACK;

Example:

DELETE FROM CUSTOMERS WHERE AGE = 25;


ROLLBACK;

SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back the
entire transaction.

Syntax:

SAVEPOINT SAVEPOINT_NAME;

5. Data Query Language

DQL is used to fetch the data from the database.

It uses only one command:

o SELECT

SELECT: This is the same as the projection operation of relational algebra. It is used to
select the attribute based on the condition described by WHERE clause.

Syntax:

SELECT expressions
FROM TABLES
WHERE conditions;

For example:

SELECT emp_name
FROM employee
WHERE age > 20;

You might also like