BDD (Databases Introduction)
BDD (Databases Introduction)
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
a. Entity ................................................................................................................4
b. Attribute ...........................................................................................................5
c. Relationship .....................................................................................................6
d. Cardinality........................................................................................................6
1.2. Transition of the entity- relationship diagram to the relational model ..............8
1
2.10. Normalization ................................................................................................13
3. SQL .........................................................................................................................15
b. Multi-Relationship Query..............................................................................22
2
c. Updating Data through a View ......................................................................29
4. Exercices .................................................................................................................30
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.
a. Entity
be entities.
4
b. Attribute
adjectives.
Student
- Name
- Student_ID
- Date_of_birth
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,
5. Enumerated Attributes: Attributes that can take on a limited set of values from
c. Relationship
d. Cardinality
Describes the number of occurrences of one entity that are associated with a
Common cardinality notations include "1" (one), "0..1" (zero or one), "0..n"
e. Primary Key
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,
7
1.2. Transition of the entity- relationship diagram to the relational model
3. Primary Keys
Identify a unique attribute for each entity (usually an ID) to be the primary key.
For relationships between entities, add a column (foreign key) in one table that
This establishes a link between the two tables, representing the relationship.
6. Composite Attributes
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
If needed, break down large tables into smaller ones using normalization
techniques.
Besides the primary key, consider adding indexes on columns frequently used for
9
2.3. Database Management System (DBMS)
A software system that provides an interface for users and applications to interact
The DBMS handles tasks like data storage, retrieval, modification, and security.
2.4. Tables
10
Each row represents a single record, and each column represents a specific
2.5. Schema
The structure of a database, including the tables, relationships between them, and
The schema specifies the data types, allowed values, and relationships between
different tables.
11
It ensures that each record is uniquely identifiable and helps establish
A foreign key is a field in one table that refers to the primary key in another table.
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.
2.10. Normalization
data integrity.
It involves breaking down large tables into smaller ones and establishing
13
2.11. Backup and Recovery
Regular backups of the database are crucial to prevent data loss in case of
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
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.
15
3.2. Organisation of SQL language
manipulation.
16
a. DDL operations
1. CREATE TABLE
In this example, we're creating a table named "Employees" with columns for
2. ALTER TABLE
3. DROP TABLE
The DROP TABLE operation is used to delete an existing table along with all
17
This example would delete the "Employees" table.
4. TRUNCATE TABLE
The TRUNCATE TABLE operation removes all the data from a table while
This example would remove all data from the "Employees" table
b. DML operations
DML (Data Manipulation Language) operations are used to interact with and
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
3. DELETE
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
19
This example retrieves the EmployeeID, FirstName, and LastName of employees
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
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
20
Subqueries
This example retrieves the first names and last names of employees who belong
Single and multi-relationship queries are commonly used when dealing with
relational databases and involve using SQL to retrieve data based on the
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.
21
In this example, we're retrieving the OrderID, OrderDate, and CustomerName
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
Students can enroll in multiple courses, and the "Enrollments" table acts as a
In this example, we're retrieving the StudentName and CourseName for students
22
The query involves two JOIN operations to connect the three tables.
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
In this example, we're retrieving the names of employees who have a salary
The main query then retrieves the names of employees whose salary is higher
In this example, we're retrieving the number of orders placed by each customer.
23
Here, the subquery (SELECT CustomerID, COUNT(*) AS OrderCount FROM
customer.
The main query then joins this subquery with the "Customers" table to retrieve
In this example, we're retrieving customers who have placed at least one order.
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,
They ensure that data remains valid and follows certain rules, preventing actions
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.
system.
This constraint ensures that a column (or set of columns) uniquely identifies
25
b. Foreign Key Constraint
ensuring that the values in a column of one table match the values in a column of
c. Unique Constraint
The unique constraint ensures that the values in a column (or set of columns)
26
c. Check Constraint
The check constraint defines a condition that must be true for all rows in a
table.
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
Views are particularly useful for simplifying complex queries, restricting access
a. Creating a View
28
In this example, we're creating a view named "EmployeeDetails" that combines
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
In some cases, you can even update data through a view, depending on the
However, there are limitations, and not all views are updatable.
d. Benefits of Views
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
Retrieve the first and last names of employees with a salary greater than 50000.
Write a query to retrieve the names of employees and their corresponding department
30
4.3. Level 3: Subqueries
Write a query to retrieve the names of employees who are in the same department as
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,
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.
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
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