Relational database fundamentals
PLAN
Relational database
A relational database is a
database that has a
collection of tables of data
items and links them,
based on defined
relationships.
Entity
A database contains one or more related tables.
Each table holds all of the information about an entity(object, person or thing.)
Entity
Car
House
Film Teacher
Reservation
Attribute
▪ an entity may be a person, place, thing or concept about which data can be collected.
▪ Each entity is made up of a number of 'attributes' which represent that entity.
Example
Car (entity): - Brand
- Model
- Serial
- Number
Tuple ▪ it's an ordered collection of elements in a row of table
brand Model Serial Number
Renault 9 RL 4774 DH 12
Renault Megane RL 7457 PS 12
Renault Symbole RL 8596 LL 45
Peugeot 307 KID 9875 LN 78
Peugeot 607 Chorus 3655 PR 45
Ford Ranger Match 8559 Fd 44
KIA Rio Drop 2658 GB 85
Row
Column
Car
Relationships
▪ The link between entities
is known as
"Relationship"
▪ Relationships on
relational database
enable users to retrieve
and combine data from
one or more tables.
Relationships
▪ CAR
▪ Rent
▪ Consumer
Consumer makes Rent
Rent consists of cars
There is two links between:
-Consumer and Rent (Makes).
-Rent and Car(consists)
Makes and consists is known as "Relationship"
Consumer
Rent
Car
Makes Consists
Relationships Forms
These relationships take the form of:
▪ One-to-one (1-1)
▪ One-to-many (1-n)
▪ Many-to-many (n-n)
Relationships Forms
One_to_one:
▪ Think about a husband and wife:
▪ A husband can only have one wife
▪ A wife can only have one husband
▪ this would be known as a 'one-to-one relationship'
Husband Wife
Relationships Forms
One_to_many:
Think about a mother and her children:
▪ A mother can have many children
▪ A child can have only one mother
Mother Children
Relationships Forms
Many_to_many
Think about videos in teacher and student
▪ A student can be taught by many teachers
▪ A teacher can teach many students
Teacher Student
Constraints
Constraints are part of a database schema definition.
They define certain properties that data in a database must comply with.
They can apply to a column, a whole table, more than one table or an entire schema.
These 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 database.
NOT NULL Constraint:
Ensures that a column cannot have NULL value.
Example
Entity : employee
Attribute: EMERGENCY_PHONE
Constraint: NOT NULL
DEFAULT Constraint
Provides a default value for a column when none is specified
Example
Entity : employee
Attribute: Salary
Constraint: DEFAULT 5000
Unique Constraint
Ensures that all values in a column are different.
Example
Entity : Department
Attribute: Department Number
Constraint: Unique
Department
Department
number Name
01 Computer Sciences
02 Mathematic
03 Literature
04 Religions(1)
04 Religions(2)
05 Human sciences
Primary key Constraint
Uniquely identified each rows/records in a database table.
It must be unique, and must have the NOT NUL
Example
Entity : Teacher
Attribute: Id
Constraint: Primary key
Teacher
Name Id
Sadok Sassi 00098585
Rima Ben Salah 45454500
Mohamed Ouerteni 12545454
Eric Jakson 45454555
Omar Waleed 87787822
Foreign key Constraint
enable you to define required relationships between and within
tables..
Example
Entity : Teacher Attribute: Department number
Constraint: Foreign key
Department
Department number Name
01 Computer Sciences
02 Mathematic
03 Literature
Teacher
Name Department number
Sadok Sassi 01
Eric Jakson 02
Omar Waleed 03
Sadok Sassi 55
Check Constraint
The CHECK constraint is used to limit the
value range that can be placed in a column..
Example
Entity : Student
Attribute: Note
Constraint: check (Note>0 and Note<20)
Index
▪ Indexes allow the database application to find data fast; without reading the whole table.
▪ An index can be created in a table to find data more quickly and efficiently.
▪ The users cannot see the indexes, they are just used to speed up searches/queries.
▪ Indexes are used by the database manager to:
o Improve performance.
o Ensure uniqueness (A table with a unique
index cannot have rows with identical keys.)
Types of index
Non-unique indexes
They are not used to enforce constraints on the tables with which they are associated. Instead, non-unique indexes are used solely to improve query performance by maintaining a sorted order of data values that are used frequently.
Example
Index name: BirthDt_teacher
Table: Teacher
Attribute: BirthDate
Order: descendant
CREATE INDEX BirthDt_teacher ON Teacher (Desc BirthDate)
BirthDt_teacher
Birthdate
02/12/1988
04/12/1986
12/04/1982
12/04/1982
18/09/1981
14/12/1981
23/11/1980
08/05/1979
06/04/1976
23/05/1974
13/11/1969
Index
Types of index
Unique indexes
▪ Unique indexes guarantee that no two rows of a table have duplicate values in the key column (or columns).
Example :
Index name: UniqueName
Table: Department
Attribute: Name
Create unique index UniqueName ON Department (name);
14/12/1981
UniqueName
Name Management
Human sciences
Mathematics
Religion (1)
Religion (2)
Computer Sciences
Biology
Geology
Sociology
Economy
Laws
Index
Views
▪ It’s 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
▪ Views, which are kind of virtual tables, allow users to restrict access to the data summarize and structure data
Example of view : from table.
Table Teacher with records
From table we will create a view to be used to have Teacher
, and
Teacher
Name Surname id Born date Nationality
Sassi Sadok 00098585 02/12/1988 Tunisian
Ben Salah Rima 45454500 12/04/1982 Tunisian
Ouerteni Mohamed 12545454 18/09/1981 Moroccan
Jakson Eric 45454555 14/12/1981 American
Waleed Omar 87787822 23/11/1980 Egyptian
Teacher_View
Name Surname Nationality
Sassi Sadok Tunisian
Ben Salah Rima Tunisian
Ouerteni Mohamed Moroccan
Jakson Eric American
Waleed Omar Egyptian
CREATE VIEW Teacher_View AS
SELECT name, surname ,
nationality
FROM Teacher;