University M’hamed Bougara-Boumerdes
Faculty of Sciences
IT departement
Introduction to
databases
Course notes
Directed By :
Benabderrezak Youcef
-Phd student in Cyber security and future Pr-
[email protected]
Telegram : https://t.me/infoumbb2
2023 / 2024
Table of contents
1. Entity-Relationship Diagram ERD ...........................................................................4
1.1. Key components of an ERD: ..............................................................................4
a. Entity ................................................................................................................4
b. Attribute ...........................................................................................................5
c. Relationship .....................................................................................................6
d. Cardinality........................................................................................................6
e. Primary Key .....................................................................................................6
f. Foreign Key .....................................................................................................7
1.2. Transition of the entity- relationship diagram to the relational model ..............8
2. Database basic concepts ...........................................................................................9
2.1. What is Database ? ..............................................................................................9
2.2. Data types ............................................................................................................9
2.3. Database Management System (DBMS) ..........................................................10
2.4. Tables ................................................................................................................10
2.5. Schema ..............................................................................................................11
2.6. Primary Key ......................................................................................................11
2.7. Foreign Key .......................................................................................................12
2.8. Indexes ...............................................................................................................12
2.9. SQL (Structured Query Language) ...................................................................13
1
2.10. Normalization ................................................................................................13
2.11. Backup and Recovery ....................................................................................14
3. SQL .........................................................................................................................15
3.1. Origin and evolution .........................................................................................15
3.2. Organisation of SQL language .........................................................................16
a. DDL operations .............................................................................................17
b. DML operations .............................................................................................18
c. DQL operations .............................................................................................19
3.3. single and multi-relationship query ..................................................................21
a. Single-Relationship Query ............................................................................21
b. Multi-Relationship Query..............................................................................22
3.4. Nested queries ...................................................................................................23
3.5. Integrity Constraints ..........................................................................................25
a. Primary Key Constraint .................................................................................25
b. Foreign Key Constraint .................................................................................26
c. Unique Constraint ..........................................................................................26
c. Check Constraint ...........................................................................................27
d. Not Null Constraint........................................................................................27
3.6. Views .................................................................................................................28
a. Creating a View .............................................................................................28
b. Using a View .................................................................................................29
2
c. Updating Data through a View ......................................................................29
d. Benefits of Views ..........................................................................................29
4. Exercices .................................................................................................................30
4.1. Level 1: Basics ..................................................................................................30
4.2. Level 2: Joins and Aggregation ........................................................................30
4.3. Level 3: Subqueries ...........................................................................................31
4.4. Level 4: Advanced Concepts ............................................................................31
3
1. Entity-Relationship Diagram ERD
An Entity-Relationship Diagram (ERD) is a graphical representation of the
entities (objects or concepts), attributes (properties), and relationships
between those entities in a database.
ERDs are commonly used during the database design phase to visualize and
communicate the structure of a database system.
1.1. Key components of an ERD:
a. Entity
Represents a real-world object, concept, or thing with its own distinct
properties. Entities are usually nouns.
For example, in a university database, "Student," "Course," and "Faculty" could
be entities.
4
b. Attribute
Describes a property or characteristic of an entity.
Attributes are represented by ovals or ellipses and are typically shown as
adjectives.
For instance, a "Student" entity might have attributes like "Name,"
"Student_ID", and "Date_of_Birth"
Student
- Name
- Student_ID
- Date_of_birth
Attributes can also have different types, such as:
1. Text/String Attributes: Attributes that store textual information, like names or
addresses.
2. Numeric Attributes: Attributes that store numerical values, like age or salary.
3. Date/Time Attributes: Attributes that store date and time values, like birthdates
or order dates.
5
4. Boolean Attributes: Attributes that store true/false or yes/no values,
representing binary choices.
5. Enumerated Attributes: Attributes that can take on a limited set of values from
a predefined list, like gender (male/female/other).
6. Composite Attributes: Attributes that can be subdivided into smaller sub-parts
with their own meanings. For instance, an "Address" attribute could be
subdivided into "Street," "City," "State," and "Zip Code."
c. Relationship
Represents the connection or association between two or more entities.
Relationships are usually verbs.
Examples of relationships could be "Enrolls In" between "Student" and
"Course," or "Teaches" between "Faculty" and "Course."
Student follow Course
d. Cardinality
Describes the number of occurrences of one entity that are associated with a
single occurrence of another entity in a relationship.
Common cardinality notations include "1" (one), "0..1" (zero or one), "0..n"
(zero to many), and "1..n" (one to many).
e. Primary Key
An attribute (or combination of attributes) that uniquely identifies each instance
of an entity.
6
It ensures the integrity of the data and is usually depicted with an underline or a
bold font.
f. Foreign Key
An attribute within an entity that refers to the primary key of another entity,
establishing a connection between the two entities.
7
1.2. Transition of the entity- relationship diagram to the relational model
1. Entities become Tables
Each entity in the ER diagram becomes a table in the relational model.
The table's name corresponds to the entity's name.
2. Attributes become Columns
Each attribute of an entity becomes a column in the corresponding table.
The column's name reflects the attribute's name.
3. Primary Keys
Identify a unique attribute for each entity (usually an ID) to be the primary key.
The primary key uniquely identifies each row in the table.
4. Relationships become Foreign Keys
For relationships between entities, add a column (foreign key) in one table that
references the primary key of another related table.
This establishes a link between the two tables, representing the relationship.
5. Cardinality and Modality
If a relationship has cardinality (such as "one-to-many" or "many-to-many"),
adjust the foreign key appropriately to represent this.
If a relationship has modality (such as "optional" or "mandatory"), decide
whether the foreign key can be NULL or not.
6. Composite Attributes
If an attribute has sub-parts (composite attribute), create separate columns for
each sub-part in the table.
7. Derived Attributes
8
Derived attributes (those calculated from other attributes) are usually not
included in the table. They can be calculated when needed using queries.
8. Normalization
Ensure that each table represents a single, atomic piece of information to avoid
redundancy and anomalies.
If needed, break down large tables into smaller ones using normalization
techniques.
9. Keys and Indexes
Besides the primary key, consider adding indexes on columns frequently used for
searching or joining tables to improve performance.
2. Database basic concepts
2.1. What is Database ?
A database is a structured and organized collection of data that is stored,
managed, and accessed electronically.
It serves as a central repository for storing various types of information, such as
text, numbers, images, and more.
2.2. Data types
Databases store structured data, which can be text, numbers, dates...etc
Here's a simple representation of common data types used in databases
9
2.3. Database Management System (DBMS)
A software system that provides an interface for users and applications to interact
with the database.
The DBMS handles tasks like data storage, retrieval, modification, and security.
Examples include MySQL, PostgreSQL, Oracle Database, Microsoft SQL
Server, and MongoDB.
2.4. Tables
Databases are typically organized into tables.
A table is a collection of rows (records) and columns (fields).
10
Each row represents a single record, and each column represents a specific
attribute of the records.
2.5. Schema
The structure of a database, including the tables, relationships between them, and
constraints, is defined by the schema.
The schema specifies the data types, allowed values, and relationships between
different tables.
2.6. Primary Key
A primary key is a unique identifier for each record in a table.
11
It ensures that each record is uniquely identifiable and helps establish
relationships between tables.
2.7. Foreign Key
A foreign key is a field in one table that refers to the primary key in another table.
It establishes relationships between tables and enforces referential integrity,
ensuring that data remains consistent.
2.8. Indexes
Indexes are data structures that enhance the speed of data retrieval operations.
12
They provide a way to quickly locate rows based on the values in specific
columns.
Indexes are especially useful for large datasets.
2.9. SQL (Structured Query Language)
SQL is a standardized language used to communicate with and manipulate databases.
2.10. Normalization
A process of organizing data in a database to minimize redundancy and improve
data integrity.
It involves breaking down large tables into smaller ones and establishing
relationships between them.
13
2.11. Backup and Recovery
Regular backups of the database are crucial to prevent data loss in case of
hardware failures, human errors, or other emergencies.
Recovery mechanisms help restore the database to a consistent state after such
incidents.
14
3. SQL
3.1. Origin and evolution
The history of SQL begins with the development of the first relational database
system, which was created by Dr. E.F. Codd at IBM in the early 1970s.
His paper titled "A Relational Model of Data for Large Shared Data Banks" laid
the groundwork for the relational database model.
The relational model introduced the concept of representing data as tables
(relations) consisting of rows (tuples) and columns (attributes).
Dr. Codd proposed a query language called "Structured English Query
Language" (SEQUEL) to interact with these relational databases.
SQL, born from IBM's SEQUEL in the 1970s, evolved from an experimental
query language into a standardized and powerful tool for managing relational
databases.
As ANSI established the first SQL standard in 1986, subsequent versions refined
syntax and added features, enabling users to create, modify, and query databases
consistently.
Despite proprietary extensions by various vendors, SQL became integral to
database administration, data manipulation, and querying.
Advancements introduced triggers, stored procedures, and views, while adapting
to changing data landscapes.
The rise of NoSQL databases challenged SQL's dominance in diverse data
models, but its evolution remains emblematic of structured data management's
journey, continually accommodating new demands and innovations.
15
3.2. Organisation of SQL language
The SQL (Structured Query Language) is organized into several major
categories, each serving a specific purpose in database management and
manipulation.
These categories cover various aspects of working with relational databases.
Here's an overview of the key organizational components of SQL:
16
a. DDL operations
1. CREATE TABLE
This operation is used to create a new table in a database
In this example, we're creating a table named "Employees" with columns for
EmployeeID, FirstName, LastName, and Department.
2. ALTER TABLE
The ALTER TABLE operation is used to modify an existing table structure.
This example adds a new column "Salary" to the "Employees" table.
3. DROP TABLE
The DROP TABLE operation is used to delete an existing table along with all
its data. Be careful with this operation, as it's irreversible.
17
This example would delete the "Employees" table.
4. TRUNCATE TABLE
The TRUNCATE TABLE operation removes all the data from a table while
keeping the table structure intact.
It's faster than the DROP and CREATE combination.
This example would remove all data from the "Employees" table
b. DML operations
DML (Data Manipulation Language) operations are used to interact with and
manipulate data within a database
1. INSERT INTO
The INSERT INTO operation is used to add new records into a table.
18
This example adds a new employee record to the "Employees" table.
2. UPDATE
The UPDATE operation is used to modify existing data in a table.
This example increases the salary of employees in the HR department by 10%.
3. DELETE
The DELETE operation is used to remove data from a table.
This example deletes the employee record with EmployeeID 1 from the
"Employees" table.
c. DQL operations
DQL (Data Query Language) operations are used specifically for querying data
from a database.
In SQL, DQL operations are primarily centered around the SELECT statement.
1. SELECT
The SELECT operation is used to retrieve data from a table.
19
This example retrieves the EmployeeID, FirstName, and LastName of employees
who belong to the HR department.
Joins
Joins are used to combine data from multiple tables based on related columns.
This example retrieves the OrderID and CustomerName for orders, joined with
their respective customers using the INNER JOIN operation.
Aggregation Functions
Aggregation functions are used to calculate summary values from data in a table.
This example calculates the average and maximum salary for each department
using the AVG and MAX aggregation functions.
20
Subqueries
Subqueries are queries nested within other queries.
This example retrieves the first names and last names of employees who belong
to departments listed in the subquery result from the "Managers" table.
3.3. single and multi-relationship query
Single and multi-relationship queries are commonly used when dealing with
relational databases and involve using SQL to retrieve data based on the
relationships between tables.
a. Single-Relationship Query
In a SRQ, you're working with two tables that are related in some way,
usually through a foreign key in one table that references the primary key in
another table.
Consider two tables, "Orders" and "Customers."
Each order is associated with a specific customer through a foreign key
"CustomerID" in the "Orders" table that references the primary key
"CustomerID" in the "Customers" table.
21
In this example, we're retrieving the OrderID, OrderDate, and CustomerName
for orders made by customers in the USA.
The JOIN operation combines data from both tables based on the common
"CustomerID" key.
b. Multi-Relationship Query
In a multi-relationship query, you're working with more than two tables that are
related in a more complex manner.
For example we have three tables: "Students," "Courses," and "Enrollments."
Students can enroll in multiple courses, and the "Enrollments" table acts as a
junction table to represent this many-to-many relationship.
In this example, we're retrieving the StudentName and CourseName for students
enrolled in courses within the "Computer Science" department.
22
The query involves two JOIN operations to connect the three tables.
3.4. Nested queries
Nested queries (also known as subqueries) are queries that are embedded within
other queries.
They allow you to perform more complex operations by using the result of one
query as part of another query.
Example 1: Subquery in WHERE Clause
In this example, we're retrieving the names of employees who have a salary
higher than the average salary of all employees.
The subquery (SELECT AVG(Salary) FROM Employees) calculates the
average salary of all employees.
The main query then retrieves the names of employees whose salary is higher
than this calculated average.
Example 2: Subquery in FROM Clause
In this example, we're retrieving the number of orders placed by each customer.
23
Here, the subquery (SELECT CustomerID, COUNT(*) AS OrderCount FROM
Orders GROUP BY CustomerID) calculates the number of orders for each
customer.
The main query then joins this subquery with the "Customers" table to retrieve
the customer names and their corresponding order counts.
Example 3: Subquery with EXISTS
In this example, we're retrieving customers who have placed at least one order.
The subquery (SELECT 1 FROM Orders WHERE Orders.CustomerID =
Customers.CustomerID) checks if there is at least one order for each customer.
The main query then retrieves the names of customers for whom the subquery
condition is true.
24
3.5. Integrity Constraints
Integrity constraints are rules defined on database tables to maintain the accuracy,
consistency, and reliability of the data stored in a relational database.
They ensure that data remains valid and follows certain rules, preventing actions
that could compromise the integrity of the database.
These integrity constraints help maintain data quality and consistency in the
database, preventing accidental errors and ensuring that the data adheres to the
specified rules.
They play a crucial role in ensuring the reliability of a relational database
system.
a. Primary Key Constraint
This constraint ensures that a column (or set of columns) uniquely identifies
each row in a table.
It prevents duplicate or null values in the primary key column(s).
25
b. Foreign Key Constraint
A foreign key constraint establishes a relationship between two tables by
ensuring that the values in a column of one table match the values in a column of
another table's primary key.
c. Unique Constraint
The unique constraint ensures that the values in a column (or set of columns)
are unique across the table, except for null values.
26
c. Check Constraint
The check constraint defines a condition that must be true for all rows in a
table.
It restricts the values that can be inserted or updated.
d. Not Null Constraint
The not null constraint ensures that a column cannot contain null values.
27
3.6. Views
In SQL, a view is a virtual table derived from one or more tables or other views.
A view doesn't store data itself but provides a way to present data in a customized
manner without changing the underlying tables.
Views are particularly useful for simplifying complex queries, restricting access
to certain columns, or aggregating data.
a. Creating a View
28
In this example, we're creating a view named "EmployeeDetails" that combines
data from the "Employees" and "Departments" tables.
This view shows the employee's ID, first name, last name, and the name of their
department.
b. Using a View
Once a view is created, you can query it just like you would query a regular
table:
This query retrieves all columns from the "EmployeeDetails" view, effectively
showing the combined data from the underlying tables.
c. Updating Data through a View
In some cases, you can even update data through a view, depending on the
complexity of the view and the underlying tables.
However, there are limitations, and not all views are updatable.
d. Benefits of Views
1. Data Abstraction: Views abstract the underlying complexity of tables, allowing
users to interact with a simplified version of the data.
2. Security: Views can be used to restrict access to specific columns or rows, providing
an additional layer of security.
3. Simplification: Views can encapsulate complex joins, calculations, or filtering
conditions, making it easier for users to work with the data.
29
4. Data Aggregation: Views can be used to aggregate data and provide summarized
information.
5. Consistency: Views ensure that the same data transformation is applied consistently
across queries.
4. Exercices
4.1. Level 1: Basics
Create a table named "Employees" with columns for EmployeeID, FirstName,
LastName, and Salary.
Insert a few records into the "Employees" table.
Retrieve all records from the "Employees" table.
Retrieve the first and last names of employees with a salary greater than 50000.
Update the salary of an employee with a specific EmployeeID.
4.2. Level 2: Joins and Aggregation
Create a table named "Departments" with columns for DepartmentID and
DepartmentName. Insert a few departments.
Create a table named "EmployeeDepartments" with columns for EmployeeID and
DepartmentID. Insert some records to indicate employee assignments.
Write a query to retrieve the names of employees and their corresponding department
names using a join.
Write a query to calculate the average salary of employees in each department.
Write a query to find the employee with the highest salary.
30
4.3. Level 3: Subqueries
Write a query to retrieve the names of employees who are in the same department as
another employee with a specific name.
Write a query to find the employees who have placed orders, using a subquery.
Write a query to find customers who have never placed an order, using a subquery.
Write a query to find the courses that have the highest enrollment count, using a
subquery.
Write a query to retrieve employees and the total amount of orders they've placed,
using a correlated subquery.
4.4. Level 4: Advanced Concepts
Create a view that shows the names of employees and their department names.
Write a query to calculate the cumulative salary for each department, ordered by the
cumulative salary.
Use the CASE statement to categorize employees as 'Junior', 'Intermediate', or
'Senior' based on their salary.
Write a query that retrieves the top 5 customers with the highest total order amounts.
Use the JOIN, UNION, and EXCEPT operators to retrieve a list of customers who
have either placed an order or made a purchase directly.
References
1. https://www.shekhali.com/view-in-sql-server/
2. https://intellipaat.com/blog/tutorial/oracle-dba-tutorial/database-backup-restore-
and-recovery/
3. https://www.w3schools.com/sql/sql_view.asp
4. https://www.javatpoint.com/view-in-sql-server
5. https://chat.openai.com/
31