DBMS Module 3
DBMS Module 3
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
Domain constraints
• 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)
Syntax:
Example:
.
ALTER
Syntax:
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE - DROP COLUMN
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
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
Example:
INSERT INTO student (sid, sname) VALUES (18101, “Rambabu");
UPDATE / SET / WHERE
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.
For example −
{ T.name | Author(T) AND T.article = 'database' }
Output − Returns tuples with 'name' from Author who has written
article on 'database’.
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.