0% found this document useful (0 votes)
17 views46 pages

DBMS Module 3

Uploaded by

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

DBMS Module 3

Uploaded by

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

DBMS

DATA BASE MANAGEMENT SYSTYEMS

Module -- 3
The Relational Data Model

Contents to be covered:
Relational Database Constraints
Relational Algebra
SQL
SQL Data Definition
Data Types
Specifying Constraints in SQL
Simple Queries in SQL
Aggregate functions in SQL
The Relational Data Model
• Relational data model is the primary data model, which is
used widely around the world for data storage and
processing.
• The relational Model of Data is based on the concept of a
Relation
• A Relation is a mathematical concept based on the ideas of sets
• The model was first proposed by Dr. E.F. Codd of IBM Research in
1970
• This model is simple and it has all the properties and
capabilities required to process data with storage efficiency.
Example of a Relation
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.
– rows represents records
– 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 key
• Each row has one or more attributes, known as
relation key, which can identify the row in the
relation (table) uniquely.
Relation Schema
• The Schema (or description) of a Relation:
– Denoted by R(A1, A2, .....An)
– R is the name of the relation
– The attributes of the relation are A1, A2, ..., An

• Example:
CUSTOMER (Cust-id, Cust-name, Address, Phone#)
– CUSTOMER is the relation name
– Defined over the four attributes: Cust-id, Cust-name, Address, Phone#
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 can not 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.
Relational Database Schema
• Relational Database Schema:
– A set S of relation schemas that belong to the
same database.
– S is the name of the whole database schema
– S = {R1, R2, ..., Rn}
– R1, R2, …, Rn are the names of the individual
relation schemas within the database S
• Following slide shows a COMPANY database
schema with 6 relation schemas
COMPANY Database Schema
Referential Integrity Constraints
for COMPANY database
SQL - Structured Query Language
• SQL is a programming language for Relational Databases.

• It is designed over relational algebra and tuple relational calculus.

• SQL comes as a package with all major distributions of RDBMS.

• SQL comprises both data definition and data manipulation languages.

• Using the data definition properties of SQL, one can design and
modify database schema.
• Data manipulation properties allows SQL to store and retrieve data
from database.
SQL - Languages
• Data Definition Language ( DDL)

• Data Manipulation Language (DML)

• Data Control Language (DCL)


Data Definition Language ( DDL)
• CREATE
• DROP
• TRUNCATE
• ALTER
CREATE
• Creates new databases, tables and views from RDBMS.

CREATE TABLE table_name


(
column1 datatype,
column2 datatype,
column3 datatype,
....
);
MySQL Data Types (Version 8.0)

In MySQL there are three main data types:


1. string
CHAR(size)
VARCHAR(size)
2. numeric
INT(size)
FLOAT(size, d)
DOUBLE(size, d)
3. date and time.
DATE
TIME
YEAR
CREATE - Examples
CREATE TABLE Persons
(
PersonID int,
LastName varchar(20),
FirstName varchar(20),
Address varchar(20),
City varchar(20)
);
DROP
1. Statement deletes a table and its rows
permanently.
2. Drops tables and databases from RDBMS.
Syntax is

SQL> DROP TABLE table_name;

Example1: DROP TABLE STUDENT;


Example2:
DROP TABLE IF EXISTS table1, table2, table3;
TRUNCATE
The statement is used to delete the data inside a table, but not the table itself

Syntax:

TRUNCATE TABLE table_name;

Example:

TRUNCATE TABLE Student1;

.
ALTER

Modifies database schema.

Syntax:
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE - DROP COLUMN
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;

ALTER TABLE Persons


ALTER COLUMN DateOfBirth year;
ALTER: Examples
• ALTER TABLE table_name ADD column_name datatype;

• ALTER TABLE table_name DROP COLUMN column_name;

• ALTER TABLE table_name MODIFY COLUMN column_name


datatype;

• ALTER TABLE table_name MODIFY column_name datatype


NOT NULL;
Data Manipulation Language (DML)

• DML modifies the database instance by


inserting, updating and deleting its data.

• DML is responsible for all forms of the data


modification in a database.
DML Commands
SQL contains the following set of commands in its
DML section −

SELECT / FROM / WHERE

INSERT INTO / VALUES

UPDATE / SET / WHERE

DELETE FROM / WHERE
These basic constructs allow database programmers and users to enter data and
information into the database and retrieve efficiently using a number of filter options.
SELECT/FROM/WHERE


SELECT − This is one of the fundamental query command of
SQL. It selects the attributes based on the condition described
by WHERE clause.


FROM − This clause takes a relation name as an argument
from which attributes are to be selected/projected.


WHERE − This clause defines predicate or conditions, which
must match in order to qualify the attributes to be projected.
Example
. Select Attribute_Name
From Relation_Name
Where Condition;

Select author_name
From book_author
Where age > 50;

This command will yield the names of authors from the relation book_author whose
age is greater than 50.
INSERT INTO / VALUES

• This command is used for inserting values into the


rows of a table (relation).
Syntax:
INSERT INTO table (column1 [, column2, column3 ... ])
VALUES (value1 [, value2, value3 ... ])
Or
INSERT INTO table VALUES (value1, [value2, ... ])

Example:
INSERT INTO student (sid, sname) VALUES (18101, “Rambabu");
UPDATE / SET / WHERE

• This command is used for updating or modifying the values of


columns in a table(relation).

Syntax:
UPDATE table_name
SET column_name = value [, column_name = value ...]
[WHERE condition]

Example:
UPDATE student
SET sid=50001
WHERE sid=18101;
DELETE / FROM / WHERE
This command is used for removing one or more rows
from a table (relation).

Syntax:
DELETE
FROM table_name
[WHERE condition];

Example:
DELETE
FROM student
WHERE sid=50001;
Data Control Language (DCL)
DCL includes commands mainly deal with the rights,
permissions and other controls of the database
system.

i)GRANT: gives users access privileges to the


database.

ii) REVOKE: withdraw user’s access privileges


given by using the GRANT
command.
TCL(transaction Control Language):
TCL commands deal with the
transaction within the database.

Examples of TCL commands:

•COMMIT– commits a Transaction.


•ROLLBACK– rollbacks a transaction in case of any
error occurs.
•SAVEPOINT–sets a savepoint within a transaction.
•SET TRANSACTION–specify characteristics for the
transaction.
Relational Algebra
• Relational algebra is a procedural query language
• It uses operators to perform queries
• An operator can be either unary or binary.
• Relational algebra is performed recursively on a relation
and intermediate results are also considered relations.
– The fundamental operations of relational algebra are as follows

Select

Project

Union

Set different

Cartesian product

Rename
Examples
Examples
Relational Calculus
• Relational Calculus is a non-procedural query language.
• It tells what to do but never explains how to do it.

Relational calculus exists in two forms


• Tuple Relational Calculus (TRC)
• Domain Relational Calculus (DRC)

• Filtering variable ranges over tuples


• Notation − {T | Condition}
• Returns all tuples T that satisfies a condition.
• For example −
• { T.name | Author(T) AND T.article = 'database' }
• Output − Returns tuples with 'name' from Author who has written article on 'database'.
• TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).

{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}


• For example −

Tuple Relational Calculus (TRC)
Filtering variable ranges over tuples
Notation − {T | Condition}
Returns all tuples T that satisfies a condition.

For example −
{ T.name | Author(T) AND T.article = 'database' }
Output − Returns tuples with 'name' from Author who has written
article on 'database’.

TRC can be quantified.


We can use Existential (∃) and Universal Quantifiers (∀).

{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}


For example −
Domain Relational Calculus (DRC)

In DRC, the filtering variable uses the domain of attributes instead of


entire tuple values (as done in TRC, mentioned above).

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 > | ∈ TutorialsPoint ∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation
TutorialsPoint, where subject is database.
SQL Aggregate Functions
•AVG – calculates the average of a set of
values.
•COUNT – counts rows in a specified table or
view.
•MIN – gets the minimum value in a set of
values.
•MAX – gets the maximum value in a set of
values.
•SUM – calculates the sum of values.

You might also like