5COSC020W DATABASE SYSTEMS – LECTURE 04
Database Implementation & Querying – Creating tables and Retrieving
Records in SQL
Dr Francois ROUBERT
[email protected]
1
Lecture 04 – Outline
oDBMS and DB Languages.
• Data Definition Language (DDL); Data Manipulation Language (DML); Data
Control Language (DCL).
oSQL – Structured Query Language.
• Capabilities, Properties & Statements.
oPopular RDBMSs
• Oracle and MySQL; MySQL architecture; MySQL compliance with standard SQL
oSQL to create and populate tables.
• Creating tables with CREATE; populating tables with INSERT INTO.
oSQL to query existing tables.
• Retrieving columns with SELECT FROM; arithmetic expressions; concatenating.
• Restricting rows with WHERE; comparison and logical operators; precedence.
• Sorting data with ORDER BY.
2
Phases and outputs of Database Design (recap)
Data Requirements BUSINESS USER’S DATA NEEDS
to support business processes
CONCEPTUAL DATA MODEL OR CONCEPTUAL SCHEMA
CONCEPTUAL DESIGN
of the data required by business users
LOGICAL DATA MODEL OR LOGICAL SCHEMA
LOGICAL DESIGN (Mapping)
based on specific file data organisation (e.g. relational model)
PHYSICAL OR INTERNAL SCHEMA
PHYSICAL DESIGN
storage structures, file organizations, indexes, access paths
DATABASE IMPLEMENTED & QUERIED IN DBMS
DATABASE IMPLEMENTATION & QUERYING data structures, constraints, data values, data description
3
Database System
5COSC024W
Server-side
Web Development
5COSC020W
DATABASE
SYSTEMS
4
Database Management System (DBMS)
Software that allows users to:
oDefine a database Data Definition Language (DDL)
• Specify data types, structures & constraints of the data
• Specify the meta-data: description of the data
oConstruct a database Data Manipulation Language (DML)
• Store data in storage medium controlled by DBMS
oManipulate a database Data Manipulation Language (DML)
• Querying, updating and reporting on the data
oControl access to a database Data Control Language (DCL)
• Ensuring security, integrity, concurrency & recovery 5
SQL – Structured Query Language
oPronounced “S.Q.L” or “Sequel”.
oComprehensive database language, allows users to:
• create the database and relation structures;
• perform insertion, modification, deletion of data from relations;
• perform simple and complex queries.
oSQL enables the CRUD operations
• Create new records in the database tables.
• Read or Retrieve records from the database tables.
• Update existing records in database tables.
• Delete records from database tables
6
SQL Capabilities
oSupporting data definition, queries and updates.
• CRUD operations
oDefining views on the database.
• Customised access to a subset of the database.
oSpecifying security and authorisation.
• Ensure only authorised users can access DB: which DB objects & which operation.
oDefining integrity constraints
• Entity integrity (rule on PK) and referential integrity (rule on FK).
oEmbedding SQL statements into other programming language
• C/C++, Java, Python, PHP, etc. 7
SQL Properties
oPortable and standardised
• ANSI/ISO make it the formal and standard language for relational DBs.
• Core: implemented by all RDBMs vendors.
• Specialised extensions: spatial data, temporal data, data mining, data
warehousing, OLAP.
oMulti-user
• DBA, Database Designer, Application Developer.
oNon procedural
• Specify what data is required, not how to get it
oClose to natural language and easy to learn: simple English keywords
• CREATE; INSERT INTO; ALTER; DELETE
• SELECT FROM WHERE 8
SQL Statements and subsets of SQL
SELECT
INSERT
UPDATE Data Manipulation Language (DML)
DELETE
MERGE
CREATE
ALTER
DROP
RENAME Data Definition Language (DDL)
TRUNCATE
COMMENT
GRANT
REVOKE Data Control Language (DCL)
COMMIT
ROLLBACK Transaction Control
SAVEPOINT 9
2 essential DBMSs: Oracle and MySQL
oOracle
• Commercial multi-model DBMS: primarily ORDBMS with add-ons.
• Commonly used for online transaction processing (OLTP) and Data
Warehousing (DW) and mixed (OLTP & DW).
• Available on-prem, on-cloud or as hybrid cloud installation.
• Latest release – Oracle Database 21c: native JSON data type, In-database
JavaScript execution, SQL Macros to call PL/SQL, optimised graph models, in-
database machine learning, blockchain tables, etc. (Oracle Database Insider, 2021)
oMySQL ⚠️Not to be confused with language SQL
• Open-source multi-model DBMS: primarily RDBMS with add-ons.
• Bought by Sun Microsystems (2008) which was then bought by Oracle (2010).
• Latest release – MySQL 8.0: transactional data dictionary, SQL roles for
permissions, MySQL DB service for cloud-native apps, document store for
NoSQL apps, JSON support (MySQL, 2021) 10
Classifying DBMSs based on Data Models
oRelational Databases or SQL Databases (covered in lectures 01-07)
• Structured Data as collection of tables with fields, records, PKs and FKs.
• Uses high level Structured Query Language (SQL) for CRUD operations.
oXML Databases (covered in lectures 08-10)
• Semistructured data as elements that can be nested to create tree structures.
• Standard for structuring and exchanging data over the Web.
oNoSQL Databases (covered in lectures 11)
• Key-value data model: each data value associated to unique key for fast retrieval.
• Graph data model: data represented as graphs i.e. labelled nodes and edges.
• Document data model: data as collection of documents in JSON or XML.
oObject Databases (not covered in this module)
• Data as collection of objects (in classes) with properties & operations. 11
Most popular Database Management Systems (August 2021)
DBMS DB Model Multi-Model (MM) or Single-Model (SM)
1 Oracle Relational MM: relational, document store, graph, RDF store, spatial
2 MySQL Relational MM: relational, document store, spatial
3 MS SQL Server Relational MM: relational, document store, graph, spatial
4 PostgreSQL Relational MM: relational, document store, spatial
5 MongoDB Document MM: document store, spatial, search engine, time series
6 Redis Key-value MM: key-value, document store, graph, spatial, search engine, time series
7 IBM Db2 Relational MM: relational, document store, RDF store, spatial
8 Elasticsearch Search Engine MM: search engine, document store, spatial
9 SQLite Relational SM: relational only
10 MS Access Relational SM: relational only
(DB-Engines,2021) 12
MySQL Storage engine architecture
(MySQL Developer Zone, 2021a) 13
MySQL compliance with Standard SQL
oStandard SQL
• Defined by the ISO/ANSI standardisation.
• Multiple revisions. SQL:1999; SQL:2003; SQL:2006; SQL:2011; SQL:2016
oMySQL DBMS
• Claims to comply with standard SQL and to support “most of the W3C XPath
standard”. (MySQL Developer Zone, 2021b)
• Adds extensions to SQL or support for non-SQL features to increase the usability
of MySQL Server. (MySQL Developer Zone, 2021b)
oExamples of differences
• General syntax: ' and " both accepted; \ is the escape character
• SQL syntax: use IF EXISTS to drop a table; drop multiple tables, etc.
• Operators: can be used in SELECT clause
• Database objects & data types: see (MySQL Developer Zone, 2021c). 14
Database Objects
Standard SQL (Oracle) SQL in MySQL
Table / Basic unit of storage; Basic unit of storage;
Relation composed of rows composed of rows
Subsets of data from one or more Subsets of data from one or
View
tables more tables
Improves performance of some Improves performance of some
Index
queries queries
Separate object; Not a separate object;
Sequence
generates numeric values use AUTO_INCREMENT
Separate object;
Synonym Not a separate object
gives alternative names to objects
15
Simple Logical ERD has
Dept Emp
deptNo{PK} 0..1 0..* empId {PK}
dName fName
dLoc lName
createDate position
hireDate
salary
commPct
Optional on Optional on email
Dept side Emp side deptNo{FK}
oCardinalities are 1 and *
• One-to-Many Relationship, Dept is the Parent table, Emp is the Child table.
• Child Emp carries a FK deptNo that references deptNo the PK of its parent Dept
oParticipations are both 0
• A record in Dept may not have any corresponding records in Emp.
• A record in Emp may not have any corresponding records in dept: FK can be null.
16
Creating Dept table
CREATE TABLE Dept
(
deptNo INTEGER,
dName VARCHAR(20) UNIQUE NOT NULL,
dLoc VARCHAR(30) NOT NULL,
createDate DATE,
constraint d_dno_pk PRIMARY KEY (deptNo)
);
17
Creating Emp table
CREATE TABLE Emp
(
empId INTEGER,
fName VARCHAR(50) NOT NULL,
lName VARCHAR(50) NOT NULL,
position VARCHAR(50) NOT NULL,
hireDate DATE NOT NULL,
salary DECIMAL(7,2) NOT NULL,
commPct DECIMAL(2,2),
email VARCHAR(100) UNIQUE NOT NULL,
deptNo INTEGER,
constraint e_eid_pk PRIMARY KEY (empId),
constraint e_dno_fk FOREIGN KEY (deptNo) references Dept(deptNo)
);
18
Populating Dept tables
INSERT INTO
Dept (deptNo, dName, dLoc, createDate)
VALUES
(10, 'Database Management', 'New Cav', '2021-03-21'),
(20, 'Systems Design', 'Harrow', '2021-01-06'),
(30, 'IT Development', 'New Cav', '2021-04-25'),
(40, 'Project Management', 'Regents', '2021-02-12'),
(50, 'Systems Testing', 'Marylebone', NULL);
19
Populating Emp tables
INSERT INTO
Emp (empId, fName, lName, position, hireDate, salary, commPct, email, deptNo)
VALUES
(101, 'Joe', 'Bloggs', 'Project Manager', '2021-03-01', 5200.00, 0.25, '[email protected]', 40),
(102, 'Jim', 'Marts', 'Database Admin', '2021-03-22', 4400.00, NULL, '[email protected]', 10),
(103, 'Jen', 'Fonts', 'Python Developer', '2021-04-28', 4800.00, 0.12, '[email protected]', 30),
(104, 'Jon', 'Pop', 'Database Architect', '2021-03-23', 4000.00, NULL, '[email protected]', 10),
(105, 'Tom', 'Dogs', 'UI Designer', '2021-01-10', 4000.00, NULL, '[email protected]', 20),
(106, 'Tek', 'Roggs', 'Project Manager', '2021-01-01', 5200.00, 0.13, '[email protected]', 40),
(107, 'Tim', 'Clogs', 'Java Developer', '2021-04-29', 4300.00, 0.15, '[email protected]', NULL),
(108, 'Tam', 'Kelps', 'UX Designer', '2021-01-15', 3900.00, NULL, '[email protected]', NULL);
20
Retrieving columns with SELECT…FROM clauses
oSelecting all columns
SELECT *
FROM Dept;
oSelecting specific columns
SELECT dName, dLoc
FROM Dept;
21
Arithmetic Expressions
oUse arithmetic operators with number data and date data.
Operator Description
+ Add
- Substract
* Mulltiply
/ Divide
oExample:
SELECT lName, salary, salary + 300
FROM Emp;
22
Operator Precedence
oWithout brackets: * takes precedence over +.
SELECT lName, salary, 12 * salary + 200
FROM Emp;
oWith brackets: override * precedence and make + take precedence
SELECT lName, salary, 12 * (salary + 200)
FROM Emp;
Which one is better from an employee’s perspective? 23
Null Values
oA null value is an unavailable, unassigned, unknown or inapplicable value.
oA null value is not the same as zero or a blank value.
SELECT lName, salary, commPct
FROM Emp;
oAn arithmetic expression containing a null value will return a null value.
SELECT lName, salary, 12 * salary * commPct
FROM Emp;
24
Column Aliases
oA column alias renames a column heading.
oIt follows the AS keyword and uses " " to display the exact same label.
SELECT lName AS "Surname", salary AS "Monthly Salary",
commPct AS "Commission"
FROM Emp;
oIt is useful as part of calculations.
SELECT lName AS "Surname", 12 * salary AS "Yearly Salary",
12 * (salary + salary * commPct) AS "Final Intake"
FROM Emp;
25
Concatenation
oConcatenate columns: use CONCAT function.
SELECT CONCAT (fName,lName) AS "Full Name"
FROM Emp;
oConcatenate and introduce a blank space.
SELECT CONCAT (fName,' ',lName) AS "Full Name"
FROM Emp;
oConcatenate and introduce a sting.
SELECT CONCAT (fName,' ',lName, ' is a ',position)
AS "Full Name and Job"
FROM Emp;
26
Duplicate Rows
oDefault display: all rows, including duplicates.
SELECT deptNo
FROM Emp;
oTo remove duplicates use DISTINCT.
SELECT DISTINCT deptNo
FROM Emp;
Why are many values of deptNo duplicated in the Emp table? 27
Restricting rows with the WHERE clause
oNo quotes required in WHERE clause with numbers.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE deptNo = 10;
oQuotes required in WHERE clause with strings.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE position = 'Database Architect';
oQuotes required in WHERE clause with dates.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE hireDate >= '2021-03-01'; 28
Comparison Operators
Operator Description
= Equal to
> Strictly greater than
>= Greater than or equal to
< Strictly less than
<= Less than or equal to
<> Not equal to
BETWEEN … AND … Between 2 values (inclusive)
IN (SET) Match any of the list of values
LIKE Match a character pattern
IS NULL Is a null value 29
Logical Operators
Operator Description
AND Returns TRUE if BOTH conditions are true
OR Returns TRUE if EITHER conditions are true
NOT Returns TRUE if condition is FALSE
30
Comparing with one or two values with BETWEEN…AND
oComparison operator in WHERE clause.
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE salary >= 4500;
oBETWEEN … AND operator (by default boundaries values are included!).
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE salary BETWEEN 4000 AND 4800;
oTwo comparison operators with the AND logical operator.
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE salary > 4000 AND salary < 4800; 31
Comparing with a list of values with IN
o Using IN operator to see if a value match any of the values in a list.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE deptNo IN (10, 30, 40);
oThree = comparison operators with OR logical operator to do the same.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE deptNo = 10 OR deptNo = 30 OR deptNo = 40;
oCombining NOT and IN operator to get the opposite.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE deptNo NOT IN (10, 30, 40); 32
Matching Character Patterns with LIKE
oFinding words that start with a specific letter.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE lName LIKE 'M%';
oFinding words that contain a specific letter anywhere.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE lName LIKE '%o%';
oFinding words that contain a specific letter in a specific position.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE lName LIKE '_o%'; 33
Finding null values with IS NULL
oRetrieving records for which one attribute has no values.
SELECT deptNo, dName, dLoc, createDate
FROM Dept
WHERE createDate IS NULL;
oRetrieving records for which one attribute has values.
SELECT empId, lName, position, commPct, deptNo
FROM Emp
WHERE commPct IS NOT NULL;
oRetrieving children records with no values in their FKs.
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE deptNo IS NULL; 34
Using logical operators
oAND operator
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE position LIKE '%Database%' AND salary > 4300;
oOR operator
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE position LIKE '%Database%' OR salary > 4300;
oAND with NOT
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE deptNo NOT IN (10,30) AND salary > 4300; 35
Rules of Precedence
Precedence Description
1 Arithmetic Operators
2 Concatenation
3 Comparison conditions
4 IS NULL, LIKE, IN
5 BETWEEN
6 Not equal to
7 NOT Logical conditions
8 AND Logical conditions
9 OR Logical conditions
Use brackets to override rules of precedence! 36
Precedence
oWithout brackets: AND takes precedence over OR.
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE position LIKE '%Database%'
OR position LIKE '%Developer%'
AND salary > 4400;
oWith brackets: override AND precedence and make OR take precedence.
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE (position LIKE '%Database%'
OR position LIKE '%Developer%')
AND salary > 4400;
37
Sort DATA with ORDER BY
oSorting in ascending order: the use ASC keyword is optional.
SELECT empId, lName, hireDate, salary
FROM Emp
ORDER BY hireDate;
oSorting in descending order and using an alias.
SELECT empId, lName, hireDate, salary * 12 AS AnnSal
FROM Emp
ORDER BY AnnSal DESC;
oSorting by multiple columns
SELECT empId, lName, hireDate, salary, deptNo
FROM Emp
ORDER BY deptNo, salary DESC; 38
References
• Reading List: https://rl.talis.com/3/westminster/lists/6371EC49-D283-78EB-6FC7-
170081C19D89.html
• Connolly, T. & Begg, C. E. (2015). Database systems: a practical approach to design,
implementation and management. 6th Edition (Global Edition). Pearson Education.
• DB-Engines (2021). DB-Engines. Available from https://db-engines.com/. [Accessed 10
August 2021].
• Elmasri, R. & Navathe, S. (2017). Fundamentals of Database Systems. 7th Edition
(Global Edition). Pearson Education.
• MySQL(2021). MySQL Enterprise Edition. https://www.mysql.com/products/enterprise/.
[Accessed 02 April 2021].
• MySQL Developer Zone (2021a). Overview of MySQL Storage Engine Architecture.
https://dev.mysql.com/doc/refman/8.0/en/pluggable-storage-overview.html. [Accessed 02
April 2021].
• MySQL Developer Zone (2021b). MySQL Standards Compliance.
https://dev.mysql.com/doc/refman/8.0/en/compatibility.html. [Accessed 02 April 2021].
• MySQL Developer Zone (2021c). MySQL Data Types.
https://dev.mysql.com/doc/refman/8.0/en/data-types.html [Accessed 02 April 2021].
39