You are on page 1of 11

3/16/2020 Lab 5. Databases.

Introduction to SQL [CS Open CourseWare]

Lab 5. Databases. Introduction to SQL

Introduction to Databases
A database (DB) is a structured set of data that allows the storage of information and efficient processing by multiple
users.

A database management system (DBMS) is a software product that ensures interaction with a database, allowing the
definition, consultation and updating of data in the database. All requests for access to the database are handled and
controlled by the DBMS.

The database schema represents a database structure described in a formal language supported by the DBMS. The
term “schema” refers to the organization of data as a blueprint of how the database is constructed (divided into
database tables in the case of relational databases)

Relational Databases. Introduction to SQL


Structured Query Language (SQL) is a well-established data language. It is used for managing data in a relational
database management system (RDBMS) which stores data and relationships between data in the form of tables.

Inspired by Relational Algebra, the model of relational databases can be considered as a collection of two-dimensional
tables, defined by the following concepts:

An entity in relational algebra becomes a table in a database


A table is made up of columns and lines, and the field is a value that is at the intersection of a column and a line
An association relationship is a link between two or more entities.
An attribute is a descriptive property of an entity or relationship.

SQL is a programming language to operate databases. It includes database creation, deletion, fetching rows, modifying
rows, etc. There are multiple SQL dialects such as MySQL, Oracle, Microsoft SQL Server, PostgreSQL, SQLite, of which
MySQL is the most used by companies, being also open-source and free. You can find more information about SQL at
SQL Tutorial - w3resource [https://www.w3resource.com/sql/tutorials.php]. Now we will go through the basics.

SQL is used to communicate to a database in a human readable form. How does it look like?

SELECT *
FROM employees AS e
LEFT JOIN salaries AS s
ON e.emp_no = s.emp_no
WHERE e.emp_no = 10004;

Modeling Relational Databases

In a Relational Database, data is represented as tables, defined by rows and columns.


https://ocw.cs.pub.ro/courses/ewis/laboratoare/05 1/11
3/16/2020 Lab 5. Databases. Introduction to SQL [CS Open CourseWare]

The data or information for the database is stored in these tables.


Columns are defined by the column name, data type and other attributes such as constraints.
Rows contain the records or data that are stored into the table.
A database can have multiple tables for storing different data.
Relationships can exist between tables such as: one-to-one, one-to-many, many-to-many and self-referencing.

SQL Commands
The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT, UPDATE, DELETE and
DROP. These commands can be classified into the following groups:

DDL - Data Definition Language

CREATE: create a new table*


ALTER: modify an existing table*
DROP: delete an entire table*

*table, view or other object in the database

DML - Data Manipulation Language

SELECT: retrieves records


INSERT: creates a record
UPDATE: modify records
DELETE: delete records

DCL - Data Control Language

GRANT: gives a privilege to user

https://ocw.cs.pub.ro/courses/ewis/laboratoare/05 2/11
3/16/2020 Lab 5. Databases. Introduction to SQL [CS Open CourseWare]

REVOKE: revokes a privilege from user

MySQL Setup
For installing MySQL on your computer (task) you can use this tutorial: MySQL Local Setup Tutorial

After completing the steps, you will have MySQL Server and MySQL Workbench GUI installed and ready to use.

Data Definition Language (DDL)


In this section we will focus on working with MySQL Workbench GUI to make things easier when creating and editing
databases and tables (DDL). This tutorial will show you a step-by-step guide for creating the database in the figure:
MySQL Workbench Tutorial

You may use MySQL Workbench GUI for DDL operations, and keep the commands presented in this section as reference.

CREATE DATABASE (creating a database)

The command for creating a database is CREATE DATABASE (equivalent in MySQL: CREATE SCHEMA):

CREATE DATABASE schema;


CREATE SCHEMA schema;

Syntax description:

schema - the name of the database/schema to be created

CREATE TABLE (creating a table)

A table can be created for an existing database. The command for creating a table is CREATE TABLE:

CREATE TABLE [ schema. ] TABLE_NAME


(
column_name_1 datatype [DEFAULT VALUE|expression] [inline_constraints] ,
column_name_2 datatype [DEFAULT VALUE|expression] [inline_constraints] , ....
)

Syntax description:

schema - the name of the database/schema where the table will be created
table_name - the name of the table
column_name_k - the name of the column k
datatype - the data type for a column
DEFAULT VALUE|expression - specifies if a column will have a default value
inline_constraints - a column can have constraints such as: UNIQUE, PRIMARY KEY, NOT NULL

Examples

The following example shows how you can create the company database schema using DDL commands:

CREATE TABLE department (


id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) DEFAULT NULL,
location VARCHAR(100) DEFAULT NULL,
PRIMARY KEY (id)
);

https://ocw.cs.pub.ro/courses/ewis/laboratoare/05 3/11
3/16/2020 Lab 5. Databases. Introduction to SQL [CS Open CourseWare]
CREATE TABLE POSITION (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) DEFAULT NULL,
description VARCHAR(200) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY name_UNIQUE (name)
);

CREATE TABLE employee (


id INT NOT NULL AUTO_INCREMENT,
department_id INT DEFAULT NULL,
position_id INT DEFAULT NULL,
manager_id INT DEFAULT NULL,
name VARCHAR(50) DEFAULT NULL,
salary DECIMAL(10,2) DEFAULT NULL,
hire_date datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY fk_employee_department_idx (department_id),
KEY fk_employee_position_idx (position_id),
KEY fk_employee_manager_idx (manager_id),
CONSTRAINT fk_employee_department FOREIGN KEY (department_id) REFERENCES department (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_employee_manager FOREIGN KEY (manager_id) REFERENCES employee (id) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT fk_employee_position FOREIGN KEY (position_id) REFERENCES POSITION (id) ON DELETE CASCADE ON UPDATE CASCADE
);

Constraints

Constraints are column properties that have to be achieved by the data at any given moment. The most common
constraints are:

NOT NULL - the value of a column must not be NULL


UNIQUE - the value of a column must be unique for every row in the table
PRIMARY KEY - the unique identifier for a row in the table; must not be NULL and must be UNIQUE for every row
in the table
FOREIGN KEY - the value of a column is related to another table by referring to the UNIQUE/PRIMARY KEY of that
table; makes possible the associations between tables

You can add constraints to an existing table using the ALTER TABLE command:

-- Primary Keys --
ALTER TABLE department ADD CONSTRAINT pk_department PRIMARY KEY (id);

-- Foreign Keys --
ALTER TABLE employee ADD CONSTRAINT fk_employee_department FOREIGN KEY (department_id);

-- Unique --
ALTER TABLE POSITION ADD CONSTRAINT uq_name UNIQUE (name);

Note: You cannot create a FOREIGN KEY if the referenced table does not exist. You have to create the referenced table
first.

Data Manipulation Language (DML)


In SQL, the DML is used to modify data but not the schema or database objects. The operations that are part of DML are
similar to CRUD operations (create, read, update, delete) with more advanced queries that can be written in simple
language.

INSERT (inserting data)

Data can be inserted into an existing table. The command for inserting data into a table is INSERT:

-- With column specification; Required: columns with constraints (PRIMARY KEY, NOT NULL) and no DEFAULT values --
INSERT INTO TABLE_NAME(column_name_1, column_name_2, ...) VALUES(value_1, value_2, ...)

-- Without column specification; Required: all columns, in the same order they have been declared --
INSERT INTO TABLE_NAME VALUES(value_column_1, value_column_2, ...)

Syntax description:

table_name - the name of the table in which to insert data

https://ocw.cs.pub.ro/courses/ewis/laboratoare/05 4/11
3/16/2020 Lab 5. Databases. Introduction to SQL [CS Open CourseWare]

column_name_k - the name of the column k


value_column_k - the value to be inserted for column k

Examples

The following examples present some INSERT queries that you can run to add data to thee company database.

INSERT INTO department VALUES (NULL,'finance','New York'),(NULL,'PR','Paris'),(NULL,'marketing','Geneva'),(NULL,'development','Bucharest');

SELECT (retrieving data)

The command for retrieving data from a database is SELECT. The data can be requested from an existing table and is
retrieved as rows. SELECT is used to specify what to retrieve from the database, while the DBMS handles the execution
of the query.

SELECT [* | {column_name_1, column_name_2, ...}]


FROM TABLE_NAME
[WHERE conditions]

Syntax description:

table_name - the name of the table from which to retrieve data


* - select all columns from a table
column_name_k - the name of the column k
conditions - some additional conditions for data to be retrieved

GROUP BY - groups rows sharing a property so that an aggregate function can be applied to each group
HAVING - selects from the groups defined by the GROUP BY clause
ORDER BY - specifies the order in which to return the rows
AS - provides an alias which can be used for tables or columns

SELECT [* | {column_name_1, column_name_2, ..., aggregate_function(expression)}]


FROM TABLE_NAME AS table_alias
[WHERE conditions]
[GROUP BY column_name_1]
[ORDER BY column_name_1 [ASC | DESC]]

Example:

-- List the employees name and the name of their department --


SELECT emp.name AS employee, dept.name AS department FROM employee AS emp
INNER JOIN department AS dept
ON dept.id = emp.department_id;

-- Get the number of analysts for each department --


SELECT department_id, position_id, COUNT(id) AS employee_count FROM employee
WHERE position_id = 3
GROUP BY department_id;

Examples

The following examples present some SELECT queries that you can run to get data from the company database.

-- List all employees from the department with id 1 --


SELECT * FROM employee
WHERE department_id = 1;

-- List the employees name and salary. Show only those having the salary between 5000 and 7000 EUR and order them descending by salary --
SELECT name, salary FROM employee
WHERE salary BETWEEN 5000 AND 7000
ORDER BY salary DESC;

UPDATE (modifying data)

The command for modifying data is UPDATE:

https://ocw.cs.pub.ro/courses/ewis/laboratoare/05 5/11
3/16/2020 Lab 5. Databases. Introduction to SQL [CS Open CourseWare]

UPDATE TABLE_NAME
SET column_name_1 = new_value_1, column_name_2 = new_value_2, ...
[WHERE conditions]

Syntax description:

table_name - the name of the table from which to modify data


column_name_k - the name of the column k
new_value_k - the value to be set (updated) for column k
conditions - some additional conditions for data to be updated

Examples

-- Change the salary for an employee by its name --


SET SQL_SAFE_UPDATES = 0;

UPDATE employee
SET salary = 8000
WHERE name = "Larry Code";

DELETE (Removing data)

The command for removing data is DELETE:

DELETE FROM TABLE_NAME [WHERE conditions];

Syntax description:

table_name - the name of the table from which to remove data


conditions - some additional conditions for data to be removed

Examples

-- Delete the "marketing" department --


DELETE FROM department
WHERE name = "marketing";

Because we declared the FOREIGN KEY constraints with the CASCADE option for UPDATE and DELETE, the employees
will also be removed when removing the department. This is useful to keep the data consistent. Take for example the
case where we remove a position (from the positions table). It doesn't make sense to keep the employees for the
position that we want to remove, so this would be a good use case for the CASCADE constraint, as we don't have to
worry about inconsistencies in the database. The other options are: RESTRICT, SET NULL, NO ACTION.

Case Study
This section presents some example database schemas and discussion about the way they are designed and the
relationships between table, to better understand the design concepts.

Blog

In this example, the schema describes a Blog, with users, articles, comments and tags.

There can be multiple users defined in users table.


Each user can have (post) multiple articles, while an article can belong to a single user. This is described as a
one-to-many relationship between the users table and the articles table. To define this into the DBMS, the
column iduser in the articles table is defined as a FOREIGN KEY and it references the id from the users table
Each user can leave comments to other articles, and articles can have comments from multiple users. This is
defined as a many-to-many relationship between the users table and the articles table. To implement this into a
database, we need to create a link table comments and transform the many-to-many relationship into 2 one-to-
many relationships (users - comments, comments - articles). The comments table will have 2 FOREIGN KEYs
(iduser, idarticle)

https://ocw.cs.pub.ro/courses/ewis/laboratoare/05 6/11
3/16/2020 Lab 5. Databases. Introduction to SQL [CS Open CourseWare]

Each article may have one or more tags, while a tag can belong to multiple articles. This is again a many-to-
many relationship and is described by adding a link table tags-articles

CookBook

In this example, the schema describes a cook book, with recipes and ingredients.

There can be multiple recipes defined in recipes table.


Each recipe can have multiple ingredients, while an ingredient can belong to a multiple recipes. This is described
as a many-to-many relationship between the recipes table and the ingredients table. To implement this into a
database, we need to create a link table link and transform the many-to-many relationship into 2 one-to-many
relationships (recipes - link, link - ingredients). The link table will have 2 FOREIGN KEYs (id_recipe,
id_ingredient)
Each ingredient may belong to a category, while a category can have multiple ingredients. This is described as a
one-to-many relationship between the ingredients table and the categories table. To define this into the DBMS,
the column id_category in the ingredients table is defined as a FOREIGN KEY and it references the id_category
from the categories table

RideSharing

In this example, the schema describes a car fleet, with cars, drivers, producers (car makers), and driver licenses.

https://ocw.cs.pub.ro/courses/ewis/laboratoare/05 7/11
3/16/2020 Lab 5. Databases. Introduction to SQL [CS Open CourseWare]

There can be multiple cars and drivers defined in cars and drivers tables.
Each car can have multiple drivers, while each driver may have (drive) multiple cars. This is described as a
many-to-many relationship between the cars table and the drivers table. To implement this into a database, we
need to create a link table cars_drivers and transform the many-to-many relationship into 2 one-to-many
relationships (cars - cars_drivers, drivers - cars_drivers). The cars_drivers table will have 2 FOREIGN KEYs
(id_car, id_driver) and a composite PRIMARY KEY (a row in the cars_drivers table can be uniquely identified by
the combination of id_car and id_driver)
Each car can belong to a single producer (car maker) and car type
Each driver can have one or more licenses, while a license (type) can belong to multiple drivers (many-to-many
relationship, with link table licenses-drivers

Library

In this example, the schema describes a library, with books, publishers and authors.

Each book may have multiple authors while each author may have multiple books (many-to-many relationship
with link table books-authors)
Each book may have one publisher and a book type, while publishers may have multiple books, while multiple
books can belong to a book type (both are defined as one-to-many relationships)

https://ocw.cs.pub.ro/courses/ewis/laboratoare/05 8/11
3/16/2020 Lab 5. Databases. Introduction to SQL [CS Open CourseWare]

SoundCloud

In this example, the schema describes a song database with songs, albums, bands and artists.

Each song may be a part of an album (one-to-many relationship between songs and albums), and may belong to
a category (one-to-many relationship between songs and categories)
Each artist may have multiple songs, while a song can have multiple artists (many-to-many relationship between
songs and artists with link table composers)
Each artist can be in a single band (one-to-many relationship between artists and bands), bands can have
multiple albums (one-to-many relationship between bands and albums), and albums can have multiple songs
(one-to-many relationship between albums and songs).

When designing a database, it's important to make sure that the relationships are well defined for the use case it's
being designed for. Modifying the database schema can be a difficult task when it already contains large amounts of
data, so it's important to get it right from the beginning.

NOTE: In this example, the one-to-many relationship between artists and band can be an incomplete definition as in
reality an artist can be in multiple bands. The same can be an issue with the one-to-many relationship between songs
and albums, as a song may sometimes belong to multiple albums.

https://ocw.cs.pub.ro/courses/ewis/laboratoare/05 9/11
3/16/2020 Lab 5. Databases. Introduction to SQL [CS Open CourseWare]

Task

Questions (2.5p)

Submit the responses on Moodle:

1. What is a database schema (0.5p)


2. What is and how do you define a many-to-many relationship in a DBMS (0.5p)
3. How is the PRIMARY KEY defined in the Blog database example for the tags_articles table (0.5p)
4. What is a FOREIGN KEY and how is it defined in the Blog database example for the tags_articles table (0.5p)
5. How would you define the relationship between artists and bands in the SoundCloud database schema,
considering that an artist can belong to one or more bands (0.5p)

Example database (7.5p)

1. Create the example database in MySQL Workbench. See MySQL Local Setup Tutorial for installing MySQL on your
computer and MySQL Workbench Tutorial for working with MySQL Workbench and creating the example database
(company database) (1.5p)
2. Execute the SQL script (Company Database File) to add data into the database (1p)
3. Execute 5 of the SQL queries (your choice) from the MySQL Workbench Tutorial and save the results for each
query as screenshots or csv files. You will have to upload the results on Moodle for grading (5p)

Resources
MySQL Local Setup Tutorial

MySQL Workbench Tutorial

Company Database File

Setup a Local MySQL Database [https://ladvien.com/data-analytics-mysql-localhost-setup/]

SQL Tutorial [https://www.w3schools.com/sql/default.asp]

SQL exercises on employee Database [https://www.w3resource.com/sql-exercises/employee-database-exercise/index.php]

https://ocw.cs.pub.ro/courses/ewis/laboratoare/05 10/11
3/16/2020 Lab 5. Databases. Introduction to SQL [CS Open CourseWare]

Example SQL queries

-- Q1. Write a query in SQL to display all the information of the employees --
SELECT * FROM employee;

-- Q2. List the employees name and salary --


-- Show only those having the salary between 5000 and 7000 EUR and order them descending by salary --
SELECT name, salary FROM employee
WHERE salary BETWEEN 5000 AND 7000
ORDER BY salary DESC;

-- Q3. List the employees name and the name of their department --
SELECT emp.name AS employee, dept.name AS department FROM employee AS emp
INNER JOIN department AS dept
ON dept.id = emp.department_id;

-- Q4. Get the number of employees in the company --


SELECT COUNT(id) AS employee_count FROM employee;

-- Q5. Get the number of employees FROM department id 4 --


SELECT department_id, COUNT(id) AS employee_count FROM employee
WHERE department_id = 4
GROUP BY department_id;

-- Q6. Get the number of employees for each department --


SELECT department_id, COUNT(id) AS employee_count FROM employee
GROUP BY department_id;

-- Q7. Get the average salary for each department --


SELECT department_id, AVG(salary) AS average_salary FROM employee
GROUP BY department_id;

-- Q8. Get the number of analysts FROM department id 3 --


SELECT department_id, position_id, COUNT(id) AS employee_count FROM employee
WHERE department_id = 3 AND position_id = 3;

-- Q9. Get the number of analysts for each department --


SELECT department_id, position_id, COUNT(id) AS employee_count FROM employee
WHERE position_id = 3
GROUP BY department_id;

-- Q10. List the employees name and the name of their manager --
SELECT manager.name AS manager_name, emp.name AS employee_name FROM employee AS emp
INNER JOIN employee AS manager ON emp.manager_id = manager.id
ORDER BY manager_name ASC;

-- Q11. List the employees name, salary, the name of their position --
SELECT emp.name AS employee, pos.name AS POSITION, emp.salary FROM employee AS emp
INNER JOIN POSITION AS pos
ON pos.id = emp.position_id;

-- Q12. List the employees name, salary, the name of their department and position --
SELECT emp.name AS employee, dept.name AS department, pos.name AS POSITION, emp.salary
FROM employee AS emp
INNER JOIN department AS dept
ON dept.id = emp.department_id
INNER JOIN POSITION AS pos
ON pos.id = emp.position_id;

ewis/laboratoare/05.txt · Last modified: 2020/03/16 18:59 by alexandru.predescu

https://ocw.cs.pub.ro/courses/ewis/laboratoare/05 11/11

You might also like