You are on page 1of 84

The Relational Model

Why Is This Important?

How does a relational database conceptually represent data?


How can we access specific values in a database?
How do we map an ER diagram to an actual database?
Why Study the Relational Model?
Most widely used model.
Big vendors: Oracle, IBM, Microsoft, MySQL
Legacy systems in older models,
e.g., IBMs IMS
Other competitors Tuple stores: Hadoop HBase, Google
BigTable, Amazon SimpleDB and Dynamo
Document stores: CouchDB, MongoDB
Graph databases: Sones, AllegroGraph
Object-oriented databases:
ObjectStore, Versant, Objectivity A synthesis: object-
relational model by all major relational vendors
XML databases: Oracle Berkeley DB XML, Tamino,
MarkLogic, eXist
Relational databases
First introduced in 1970 by E.F. Codd at IBM
Two early research projects on relational model
IBM System R (prototype RDBMS)
UC Berkeleys Ingres (academic RDBMS)
Todays dominant technology for DBM
Hundreds of RDBMS products from PC to mainframe
Relational databases
To fellow pilots and aircrew in the Royal Air Force during
World War II and the dons at Oxford:
These people were the source of my determination to
fight for what I believed was right during the ten or
more years in which government, industry, and
commerce were strongly opposed to the relational
approach to database management.
Introduction to Relational model: Definitions
Relational database: a set of relations.

Relation is made up of 2 parts:


Relation Schema : specifies name of relation, name of each
field(column or attribute) and domain of each field.
Domain name has set of associated values.
Relation scheme for university database is:

Ex: Students(sid: string, name: string, login: string,


age: integer, gpa: real).
Can think of a relation as a set of rows or tuples (i.e., all rows
are distinct).
Relation Instance :
Is a table, with rows and columns.
Each row is called as tuple.
No of Rows = cardinality,
No of fields/columns = degree / arity.

Fig: Instance of student relation


Example Instance of Students
Relation

sid name login age gpa


53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8

Cardinality = 3, degree = 5, all rows


distinct
Do all columns in a relation instance have to
be distinct?
What is sql
SQL (pronounced "ess-que-el") stands for Structured Query
Language. SQL is used to communicate with a database.
According to ANSI (American National Standards Institute), it is
the standard language for relational database management
systems.
Different sql commands are divided into following types:
Data Definition Language (DDL) statements are used to
define the database structure or schema. Some examples:
CREATE - to create objects in the database
ALTER - alters the structure of the database
DROP - delete objects from the database
TRUNCATE - remove all records from a table, including all

spaces allocated for the records


COMMENT - add comments to the data dictionary
RENAME - rename an object
Data Manipulation Language(DML)
statements are used for managing data within schema
objects. Some examples:
SELECT - retrieve data from the a database
INSERT - insert data into a table
UPDATE - updates existing data within a table
DELETE - deletes all records from a table, the space
for the records remain
MERGE - UPSERT operation (insert or update)
CALL - call a PL/SQL or Java subprogram
EXPLAIN PLAN - explain access path to data
LOCK TABLE - control concurrency
Data Control Language(DCL) statements. Some examples:
GRANT - gives user's access privileges to database
REVOKE - withdraw access privileges given with the GRANT command
TCL

Transaction Control(TCL) statements are used to manage the


changes made by DML statements. It allows statements to be grouped
together into logical transactions.
COMMIT - save work done
SAVEPOINT - identify a point in a transaction to which you can later roll
back
ROLLBACK - restore database to original since the last COMMIT
SET TRANSACTION - Change transaction options like isolation level and
what rollback segment to use
Data Definition Language (DDL) -CREATE

The commands are CREATE, ALTER, DROP, RENAME, and TRUNCATE.

The CREATE DATABASE Statement is used to create a database.


After creating a database, we can create several other database
objects (tables, views, procedures etc.) into it.

Syntax:
---------------------

mysql> create database studentinfo;

mysql> show databases;

+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| pets |
| sakila |
| studentinfo |
| sys |
| world |
+--------------------+
SQL USE Statement
The USE Statement is used to select a database and perform SQL
operations into that database.
The database remains default until end of session or execution of
another USE statement with some other database.

Syntax:
----------------

mysql> USE studentinfo;


Database changed
SQL CREATE TABLE Statement

The CREATE TABLE Statement is used to create tables to store


data.
Integrity Constraints like primary key, unique key, foreign key can
be defined for the columns while creating the table.

mysql> CREATE TABLE Studentdata (sid char(20), name


char(30),login char(20),age
integer,gpa real);
Query OK, 0 rows affected (0.40 sec)
SQL ALTER TABLE Statement

The SQL ALTER TABLE command is used to modify the definition


(structure) of a table by modifying the definition of its columns. The
ALTER command is used to perform the following functions.

1) Add, drop, modify table columns


2) Add and drop constraints
3) Enable and Disable constraints
ADD Column
mysql> show columns from studentdata;

+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| sid | char(20) | YES | | NULL | |
| name | char(25) | YES | | NULL | |
| login | char(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| gpa | double | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table studentdata add branch char(5);


Query OK, 0 rows affected (0.83 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> show columns from studentdata;


Add a column
Drop a column
Modifying a column
SQL RENAME Command
The SQL RENAME command is used to change the name of the
table or a database object.

If you change the object's name any reference to the old name
will be affected.
SQL Truncate

The SQL TRUNCATE command is used to delete all the rows from
the table and free the space containing the table.
SQL Drop DataBase
The DROP DATABASE Statement is used to drop or delete a
database.

Dropping of the database will drop all database objects (tables,


views, procedures etc.) inside it.

The user should have admin privileges for deleting a database.


SQL DROP Statement:
The SQL DROP command is used to remove an object from the
database. If you drop a table, all the rows in the table is deleted
and the table structure is removed from the database. Once a
table is dropped we cannot get it back, so be careful while using
DROP command. When a table is dropped all the references to the
table will not be valid.
Difference between DROP and TRUNCATE Statement:

If a table is dropped, all the relationships with other tables will no


longer be valid,

the integrity constraints will be dropped, grant or access privileges


on the table will also be dropped,

if you want use the table again it has to be recreated with the
integrity constraints, access privileges and the relationships with
other tables should be established again.

But, if a table is truncated, the table structure remains the same,


therefore any of the above problems will not exist.
Data Manipulation Language (DML)

SQL SELECT Statement

Syntax of SQL SELECT Statement:

SELECT column_list FROM table-name


[WHERE Clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];

table-name is the name of the table from which the information is


retrieved.
column_list includes one or more columns from which data is
retrieved.
The code within the brackets is optional.
SQL WHERE Clause

The WHERE Clause is used when you want to retrieve specific


information from a table excluding other irrelevant data.

For example, when you want to see the information about students
in class 10th only then you do need the information about the
students in other class.

Retrieving information about all the students would increase the


processing time for the query.

So SQL offers a feature called WHERE clause, which we can use to


restrict the data that is retrieved.

The condition you provide in the WHERE clause filters the rows
retrieved from the table and gives you only those rows which you
expected to see.

WHERE clause can be used along with SELECT, DELETE, UPDATE


statements.
Syntax of SQL WHERE Clause

WHERE {column or expression} comparison-operator value

Syntax for a WHERE clause with Select statement is:

SELECT column_list FROM table-name


WHERE condition;

column or expression - Is the column of a table or a


expression
comparison-operator - operators like = < > etc.
value - Any user value or a column name for comparison
SQL HAVING Clause

Having clause is used to filter data based on the group functions.

This is similar to WHERE condition but is used with group


functions.
Group functions cannot be used in WHERE Clause but can be
used in HAVING clause.

SQL GROUP BY Clause


The SQL GROUP BY Clause is used along with the group functions
to retrieve data grouped according to one or more columns.

NOTE: The group by clause should contain all the columns in the
select list expect those used along with the group functions.
SQL ORDER BY

The ORDER BY clause is used in a SELECT statement to sort results


either in ascending or descending order.

Oracle sorts query results in ascending order by default.

Syntax for using SQL ORDER BY clause to sort data is:

SELECT column-list
FROM table_name [WHERE condition]
[ORDER BY column1 [, column2, .. columnN] [DESC]];
SQL GROUP Functions

Group functions are built-in SQL functions that operate on groups


of rows and return one value for the entire group. These functions
are: COUNT, MAX, MIN, AVG, SUM, DISTINCT

SQL COUNT ( ): This function returns the number of rows in the


table that satisfies the condition specified in the WHERE condition.
If the WHERE condition is not specified, then the query returns the
total number of rows in the table.
SQL DISTINCT( ): This function is used to select the distinct rows.
SQL MAX( ): This function is used to get the maximum value from a
column.

SQL MIN( ): This function is used to get the minimum value from a
column.

SQL AVG( ): This function is used to get the average value of a


numeric column.

SQL SUM ( ): This function is used to get the sum of a numeric


column
SQL Operators
There are two type of Operators, namely Comparison Operators
and Logical Operators. These operators are used mainly in the
WHERE clause, HAVING clause to filter the data to be selected.

Comparison Operators:
Comparison operators are used to compare the column data with
specific values in a condition.

Comparison Operators are also used along with the SELECT


statement to filter data based on specific conditions.
Comparison
Description
Operators
= equal to
<>, != is not equal to
< less than
> greater than
>= greater than or equal to
<= less than or equal to
SQL Logical Operators
There are three Logical Operators namely, AND, OR, and NOT.

These operators compare two conditions at a time to determine


whether a row can be selected for the output.

When retrieving data using a SELECT statement, you can use logical
operators in the WHERE clause, which allows you to combine more than
one condition.
Logical Operators Description
For the row to be
selected at least one of
OR
the conditions must be
true.
For a row to be selected
AND all the specified
conditions must be true.
For a row to be selected
NOT the specified condition
must be false.
SQL Comparison Keywords
There are other comparison keywords available in sql which are
used to enhance the search capabilities of a sql query. They are
"IN", "BETWEEN...AND", "IS NULL", "LIKE".

Comparision
Description
Operators
column value is similar
LIKE
to specified character(s).
column value is equal to
IN any one of a specified
set of values.
column value is between
two values, including
BETWEEN...AND
the end values specified
in the range.
column value does not
IS NULL
exist.
SQL INSERT Statement

The INSERT Statement is used to add new rows of data to a table.


We can insert data to a table in two ways,

1) Inserting the data directly to a table.


Syntax for SQL INSERT is:
INSERT INTO TABLE_NAME
[ (col1, col2, col3,...colN)]
VALUES (value1, value2, value3,...valueN);

col1, col2,...colN -- the names of the columns in the table into


which you want to insert data.
While inserting a row, if you are adding value for all the columns
of the table you need not specify the column(s) name in the sql
query. But you need to make sure the order of the values is in the
same order as the columns in the table. The sql insert query will
be as follows
INSERT INTO TABLE_NAME
VALUES (value1, value2, value3,...valueN);
SQL UPDATE Statement

The UPDATE Statement is used to modify the existing rows in a


table.

The Syntax for SQL UPDATE Command is:

UPDATE table_name
SET column_name1 = value1,
column_name2 = value2, ...
[WHERE condition]

table_name - the table name which has to be updated.


column_name1, column_name2.. - the columns that gets changed.
value1, value2... - are the new values.

The SET clause then determines how these rows are to be


modified.

NOTE:In the Update statement, WHERE clause identifies the rows


that get affected. If you do not include the WHERE clause, column
values for all the rows get affected.
SQL Delete Statement

The DELETE Statement is used to delete rows from a table.

Syntax of a SQL DELETE Statement

DELETE FROM table_name [WHERE condition];

table_name -- the table name which has to be updated.

NOTE: The WHERE clause in the sql delete command is


optional and it identifies the rows in the column that gets
deleted. If you do not include the WHERE clause all the rows in
the table is deleted, so be careful while writing a DELETE query
without WHERE clause.
To delete an employee with id 100 from the employee table, the sql
delete query would be like,

DELETE FROM employee WHERE id = 100;

To delete all the rows from the employee table, the query would be like,

DELETE FROM employee;


Difference between DELETE and TRUNCATE Statements:

DELETE Statement: This command deletes only the rows from the
table based on the condition given in the where clause or deletes
all the rows from the table if no condition is specified. But it does
not free the space containing the table.

TRUNCATE statement: This command is used to delete all the


rows from the table and free the space containing the table.
Data Control Language
SQL GRANT REVOKE Commands
DCL commands are used to enforce database security in a multiple user
database environment. Two types of DCL commands are GRANT and
REVOKE. Only Database Administrator's or owner's of the database object
can provide/remove privileges on a database object.
SQL GRANT Command
SQL GRANT is a command used to provide access or privileges on the
database objects to the users.

The Syntax for the GRANT command is:

GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
privilege_name : is the access right or privilege granted to the user. Some
of the access rights are ALL, EXECUTE, and SELECT.

ALL PRIVILEGES-allows user all access to a designated database (or if no


database is selected, across the system)
CREATE- allows them to create new tables or databases
DROP- allows them to them to delete tables or databases
DELETE- allows them to delete rows from tables
INSERT- allows them to insert rows into tables
SELECT- allows them to use the Select command to read through databases
UPDATE- allow them to update table rows
GRANT OPTION- allows them to grant or remove other users' privileges.

object_name : is the name of an database object like TABLE, VIEW, STORED


PROC and SEQUENCE.

user_name : is the name of the user to whom an access right is being


granted.

PUBLIC : is used to grant access rights to all users.

ROLES : are a set of privileges grouped together.

WITH GRANT OPTION : - allows a user to grant access rights to other


users.
To Allow a User to create Table:

grant create table to username;

Ex: GRANT CREATE ON *.* TO 'testuser'@'localhost';

TO create new user:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

SQL REVOKE Command:

The REVOKE command removes user access rights or privileges to


the database objects.
The Syntax for the REVOKE command is:

REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}
TCL command

Transaction Control Language(TCL) commands are used to manage


transactions in database.These are used to manage the changes made
by DML statements. It also allows statements to be grouped together into
logical transactions.

Commit command:
Commit command is used to permanently save any transaction into
database.
Following is Commit command's syntax,
Syntax: commit;

Rollback command:
This command restores the database to last committed state. It is also
use with savepoint command to jump to a savepoint in a transaction.
Following is Rollback command's syntax,
Syntax: rollback to savepoint-name;

Savepoint command:
savepointcommand is used to temporarily save a transaction so that
you can rollback to that point whenever necessary.
Following is savepoint command's syntax,
Syntax: savepoint savepoint-name;
Example of Savepoint and Rollback:

Following is the class table,

ID NAME
1 abhi
2 adam
4 Alex

INSERT into class values(5,'Rahul');


commit;
UPDATE class set name='abhijit' where id='5';
savepoint A;
INSERT into class values(6,'Chris');
savepoint B;
INSERT into class values(7,'Bravo');
savepoint C;
SELECT * from class;
The resultant table will look like,
ID NAME
1 abhi
2 adam
4 alex
5 abhijit
6 chris
7 Bravo

Now rollback to savepoint B

rollback to B;
SELECT * from class;
The resultant table will look like

ID NAME
1 abhi
2 adam
4 alex
5 abhijit
8 Chris
Now rollback to savepoint A

rollback to A;
SELECT * from class;
The result table will look like

ID NAME
1 abhi
2 adam
4 alex
5 abhijit
Integrity constraints
An integrity constraints is a condition specified on a database
scheme and restrict the data that can be stored in an instance of the
database.
A legal instance of a relation is one that satisfies all specified ICs.

Ics are specified and enforced at different times:


1. when the DBA or end user defines the database schema, Ics
must hold on any instance of this database.
2. When database application is run , the DBMS checks for the
violations and disallows changes to the data that violate the
specified Ics.
Different types of Ics are:

The domain integrity:


Domain integrity means the definition of a valid set of values
for an attribute. You define
- data type,
- length or size
- is null value allowed
- is the value unique or not for an attribute.

Key constraints
Key constraints:
Key constraints is a statement that a certain minimum subset of the fields which
uniquely identifies the tuple.
Key constraints are of two types:
1. primary key constraints
2. foreign key constraints
Candidate key is the set of fields that uniquely identifies the tuple according to key
constraints .A relation can have multiple Candidate key .
One of the candidate key is called as primary key by database designer.
Ex: login and age for student relation is also a key {login , age}

Primary key:
A primary key is one of the candidate keys chosen by the database designer to
uniquely identify the tuple.
Ex: sid
Foreign key:
A foreign key is a column or group of columns in a relational database table that
provides a link between data in two tables.
the foreign key is defined in a second table, but it refers to the primary key in the
first table.
Specifying primary key:

Syntax to define a Primary key at column level:


column name datatype [CONSTRAINT constraint_name] PRIMARY
KEY

Syntax to define a Primary key at table level:

[CONSTRAINT constraint_name] PRIMARY KEY


(column_name1,column_name2,..)

column_name1, column_name2 are the names of the columns which


define the primary Key.
The syntax within the bracket i.e. [CONSTRAINT constraint_name] is
optional.
For Example: To create student table with Primary Key constraint, the query
would be like.
Primary Key at column level:

Create table students (sid char(20) primary key, name char(20), login
char(30), age int, gpa real)
OR

Syntax to define a Primary key at table level:

Create table students (sid char(20), name char(20), login char(30),


age int, gpa real,
unique (name. age)
constraint student key primary key (sid))
OR
We can add the primary key constraints in the already created table by ALTER
command.
Syntax is:
Alter table tablename add constraint constraint_name(column_name);
To delete the primary key:
ALTERTABLEtable_name DROPPRIMARYKEY
Foreign key constraints:

Sometime the information stored in one relation is linked to another


relation then foreign key constraints is used.

Ex: consider another relation enrolled as


enrolled(studid: char(20), cid: char(20), grade: char(5))

Specifying the foreign key constraints:

create table enrolled(studid char(20), cid char(20) , grade char(10) ,


primary key(studid, cid) ,
foreign key (studid) references student(sid));
Logical DB Design: ER to Relational

Entity sets to tables.

name
ssn lot

Employees
CREATE TABLE Employees
(ssn CHAR(11),
name CHAR(20),
lot INTEGER,
PRIMARY KEY (ssn))
Relationship Sets to
Tables CREATE TABLE Works_In(
ssn CHAR(1),
In translating a did INTEGER,
relationship set to a since DATE,
relation, attributes of the PRIMARY KEY (ssn, did),
relation must include: FOREIGN KEY (ssn)
Keys for each
REFERENCES Employees,
FOREIGN KEY (did)
participating entity set
REFERENCES Departments)
(as foreign keys).
This set of attributes
forms a superkey for
the relation.
All descriptive attributes.
Review: Key
Constraints
since
name dname
Each dept has at
most one ssn lot did budget

manager,
according to the Employees Manages Departments
key constraint
on Manages.

Translation to
relational model?

1-to-1 1-to Many Many-to-1 Many-to-Many


Translating ER Diagrams with Key
Constraints
CREATE TABLE Manages(
Map relationship ssn CHAR(11),
to a table: did INTEGER,
Note that did is since DATE,
PRIMARY KEY (did),
the key now! FOREIGN KEY (ssn) REFERENCES
Separate tables Employees,
for Employees FOREIGN KEY (did) REFERENCES
and Departments)
Departments. CREATE TABLE Dept_Mgr(
did INTEGER,
Since each dname CHAR(20),
department has a budget REAL,
unique manager, ssn CHAR(11),
we could instead since DATE,
PRIMARY KEY (did),
combine Manages FOREIGN KEY (ssn) REFERENCES Employees)
and Departments.
Review: Participation
Constraints
Does every department have a manager?
If so, this is a participation constraint: the participation
of Departments in Manages is said to be total (vs.
partial).
Every did value in Departments table must appear
in a row of the Managessincetable (with a non-null ssn
value!) name dname
ssn lot did budget

Employees Manages Departments

Works_In

since
Participation Constraints
in SQL
We can capture participation constraints
involving one entity set in a binary relationship,
but little else (without resorting to CHECK
constraints).
CREATE TABLE Dept_Mgr(
did INTEGER,
dname CHAR(20),
budget REAL,
ssn CHAR(11) NOT NULL,
since DATE,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees,
ON DELETE NO ACTION)
Review: Weak Entities
A weak entity can be identified uniquely only by
considering the primary key of another (owner)
entity.
Owner entity set and weak entity set must participate
in a one-to-many relationship set (1 owner, many weak
entities).
Weak entity set must have total participation in this
identifying relationship set.
name
cost pname age
ssn lot

Employees Policy Dependents


Translating Weak Entity
Sets
Weak entity set and identifying
relationship set are translated into a
single table.
When the owner entity is deleted, all owned
weak entities
CREATE TABLEmust also be
Dep_Policy ( deleted.
pname CHAR(20),
age INTEGER,
cost REAL,
ssn CHAR(11) NOT NULL,
PRIMARY KEY (pname, ssn),
FOREIGN KEY (ssn) REFERENCES Employees,
ON DELETE CASCADE)
name
ssn lot

Review: ISA Hierarchies Employees

hourly_wages hours_worked
ISA
As in C++, or other PLs, attributes contractid
are inherited.
If we declare A ISA B, every A entity Hourly_Emps Contract_Emps
is also considered to be a B entity.

Overlap constraints: Can Joe be an Hourly_Emps as


well as a Contract_Emps entity?
(Allowed/disallowed)
Covering constraints: Does every Employees entity
also have to be an Hourly_Emps or a Contract_Emps
entity? (Yes/no)
Translating ISA Hierarchies to
Relations
General approach:
3 relations: Employees, Hourly_Emps and
Contract_Emps.
Hourly_Emps: Every employee is recorded in
Employees. For hourly emps, extra info recorded in
Hourly_Emps (hourly_wages, hours_worked, ssn);
must delete Hourly_Emps tuple if referenced
Employees tuple is deleted).
Queries involving all employees easy, those
involving just Hourly_Emps require a join to get
some attributes.
Alternative: Just Hourly_Emps and
Contract_Emps.
Hourly_Emps: ssn, name, lot, hourly_wages,
hours_worked.
Views
A view is just a relation, whose rows are
not stored in the database but computed
as needed from a view definition.
CREATE VIEW YoungActiveStudents (name, grade)
AS SELECT S.name, E.grade
FROM Students S, Enrolled E
WHERE S.sid = E.sid and S.age<21

Views can be dropped using the DROP VIEW


command.
How to handle DROP TABLE if theres a view on the table?
DROP TABLE command has options to let the user specify this.
Views
A view is nothing more than a SQL
statement that is stored in the database
with an associated name. A view is actually
a composition of a table in the form of a
predefined SQL query.
A view can contain all rows of a table or

select rows from a table. A view can be


created from one or many tables which
depends on the written SQL query to create
a view.
Views, which are kind of virtual tables, allow
users to do the following:
Structure data in a way that users or classes

of users find natural or intuitive.


Restrict access to the data such that a user

can see and (sometimes) modify exactly


what they need and no more.
Summarize data from various tables which

can be used to generate reports.


Views and Security
Views can be used to present necessary
information (or a summary), while hiding
details in underlying relation(s).
Given YoungStudents, but not Students or
Enrolled, we can find students s who have are
enrolled, but not the cids of the courses they
are enrolled in.
Relational Model:
Summary
A tabular representation of data.
Simple and intuitive, currently the most widely
used.
Integrity constraints can be specified by the DBA,
based on application semantics. DBMS checks
for violations.
Two important ICs: primary and foreign keys
In addition, we always have domain constraints.
Powerful and natural query languages exist.
Rules to translate ER to relational model

You might also like