Databases SQL
Databases SQL
Agenda
1. Relational databases
2. SQL
3. Tables
4. Data Definition Language
5. Data Manipulation Language
6. Data Query Language
7. Primary and foreign keys
8. Table relationships
9. Joins
10. SQL extras
11. Database modeling exercises
12. Optional:
a. Transactions
b. ACID
c. Views
d. Procedures
© 2019 Software Development Academy. All Rights Reserved. 2
Relational databases
Relational databases store data in multiple tables that are linked through
different kinds of relationships.
While one database can store multiple datasets, a table stores only one
dataset.
2. Fitness gym database - must store information related to trainers, classes and subscriptions:
a. trainers information table: first name, last name and specialities,
b. classes information table: class name, description and schedule,
c. subscriptions information table: start date, end date and customer.
3. Cinema database - must store information related to movies, schedules and tickets:
a. movies information table: movie title and description,
b. schedule information table: which movie, which room, start and end time,
c. tickets: movie, adult/children ticket and seat number.
In order to interact with a relational database we can use the SQL (Structured
Query Language) language.
● DDL - data definition language. Helps users define what kind of data they
are going to store and how they are going to model this data.
● DML - data manipulation language. Allows users to insert, update and
delete data from the database.
● DQL - data query language. Helps users retrieve information from the
database.
● DCL - data control language. Allows users to restrict and control access to
the database.
Table name
employees Column name
Column data type id firstName lastName dateOfBirth
INT(6) VARCHAR(30) VARCHAR(30) DATE
1 John Smith 1980-01-04
Table field
Table column
Data item
The column data types define the type of information you can store in that
particular column:
● numeric data types such as int, tinyint, bigint, float, real, etc.,
● date and time data types such as Date, Time, Datetime, etc.,
● character and string data types such as char, varchar, text, etc.,
● logical values are represented by a TINYINT type value (0 or 1).
When defining a table the user can set certain properties on the columns:
● data type controls the type of values stored in the column,
● NOT NULL defines whether a column must be filled or not,
● AUTOINCREMENT states that the column value will be generated
automatically (incrementation of the last inserted value) - this only
works for numeric columns,
● UNIQUE states that there cannot be more than one row with the same
value for that particular column.
Example:
SELECT * FROM owners;
A primary key must contain unique values. If the primary key consists of
multiple columns, the combination of values in these columns must be unique.
Because MySQL works faster with integers, the data type of the primary key
column should be integer e.g., INT, BIGINT. And you should ensure sure that
value ranges of the integer type for the primary key are sufficient for storing all
possible rows that the table might have.
The table containing the foreign key is called the child table, and the
referenced table is the parent table.
Typically, the foreign key columns of the child table often refer to the primary
key columns of the parent table.
A table can have more than one foreign key where each foreign key references
to a primary key of the different parent tables.
Once a foreign key constraint is in place, the foreign key columns from the
child table must have the corresponding row in the parent key columns of the
parent table or values in these foreign key columns must be NULL.
Take for example an online store which has information about: customers,
orders, items, etc. We would have separate tables for all of these. But we also
need to have relationships between these tables. For instance a customer can
place several orders, an order belongs to only one customer, an order can
contain multiple items. These relationships need to be represented in the
database.
A SQL Join statement is used to combine data or rows from two or more
tables based on a common field between them:
● CROSS JOIN
● INNER JOIN
● LEFT JOIN
● RIGHT JOIN
Example:
SELECT *
FROM pets
INNER JOIN owners
ON pets.ownerId = owners.ownerId
What kind of information do we need to store? What are the different concepts a cinema needs to
interact with?
How is that information connected? What are the relationships between the different datasets?
What business needs and what kind of processes does the database have to satisfy?
Think about the process you go through when you go to a cinema or book a ticket online:
● you need to see a list of movies and the times they run,
● you need to see if there are any available seats,
● you need to be able to make a reservation for one or more seats,
● you need to be able to buy a ticket.
Think about the trainers, students, teams, classrooms, modules and attendance list. There are
multiple students in a team. A trainer can teach one or multiple modules. A team can have classes
in one or multiple classrooms. For the attendance, we usually say that a student was or wasn’t
present on a particular day.
What’s the difference between trainers and students? Do we need multiple tables to store their
information or would one suffice?
If a team consists of multiple students but a student can only belong to one team what kind of
relationship would we use?
If a module represents a trainer teaching a specific topic, for a specific team, in a specific location,
between certain dates, how would you model this?
© 2019 Software Development Academy. All Rights Reserved. 61
SDA scheduler database - exercises
1. List all students.
2. List all students for team Python1Tallin.
3. List all groups that had classes in location BucharestCowork.
4. List all groups that had classes in location Tallin Cozy Space in March 2020.
5. List all students that already finished the SQL module.
6. List all students with 100% attendance rate.
7. List all trainers that teach Java Fundamentals.
8. List all trainers that teach at BucharestCowork location.
9. List all trainers that taught students with 100% attendance rate.
10. List the trainer that taught the highest number of modules.
Transactions are atomic units of work that can be committed or rolled back.
When a transaction makes multiple changes to the database, either all the
changes succeed when the transaction is committed, or all the changes are
undone when the transaction is rolled back.
The ACID model is a set of database design principles that emphasize aspects
of reliability that are important for business data and mission-critical
applications.
A view contains rows and columns, just like a real table. The fields in a view
are fields from one or more real tables in the database.
A view always shows up-to-date data. The database engine recreates the data,
using the view's SQL statement, every time a user queries a view.
You can also pass parameters to a stored procedure, so that the stored
procedure can act based on the parameter value that is passed.
CALL GetPetsByOwner('Jim');