CIT 225
Learning SQL
Chapter 1 – A Little Background
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
What This PowerPoint Covers:
• How we got to the relational model
• Hierarchical databases - a single parent hierarchy
• Networked databases - a multi-parent hierarchy
• How the relational model works
• How to use basic SQL commands
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Relational Model Example
The relational model stores data in tables.
cust_id fname lname
1 George Blake
2 Sue Smith
Redundant Data
account_id product_cd cust_id balance
103 CHK 1 $75.00
104 SAV 1 $250.00
105 CHK 2 $700.00
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Relational Model Definition
A relational model evolved from:
o Hierarchical databases
o Network databases
A relational model replaced networked database pointers with redundant
data
o A pointer redirects a hierarchical search to a new location
o Redundant data are copies of primary key values called foreign keys
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Terms and Definitions #1
Table relationships & redundant data are Surrogate key:
built with primary and foreign keys: o A stand-in for the natural key
o Primary keys uniquely identify rows o A sequence-driven number
o Foreign keys are copies of primary keys o A surrogate key must exist for each
(redundant data) that let you join rows unique natural key
Natural key:
o A descriptive column or set of columns
can be a primary key
o A set of descriptive columns is a
compound key
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Terms and Definitions #2
Term Definition
Entity Something of interest to the database user community. Examples include customers, parts,
geographic locations, etc.
Column An individual piece of data stored in a table
Row A set of columns that together completely describe an entity or some action on an entity.
Also called a record.
Table A set of rows, held either in memory (nonpersistent) or on permanent storage (persistent)
Result Set Another name for a nonpersistent table, generally the result of an SQL query
Primary Key One or more columns that can be used as a unique identifier for each row in a table
Foreign Key One or more columns that can be used together to identify a single row in another table.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
SQL Statement Classes
• SQL Schema Statement - define data structures
• SQL Data Statement - manipulate data in tables (or structures)
• SQL Transaction Statement - determine when to begin, commit or rollback
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Structured Query Language - Create
SQL schema language is used to create a database.
CREATE TABLE corporation
(corp_id SMALLINT,
name VARCHAR(30),
CONSTRAINT pk_corporation PRIMARY KEY (corp_id)
);
This creates a table with 2 columns, corp_id and name, with the corp_id
as the primary key for the table.
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Structured Query Language - Select
• SQL data statement language may also select data
• SQL SELECT statement returns a temporary table or result set
mysql < SELECT name
-> FROM corporation
-> WHERE corp_id = 27;
This statement retrieves this data:
name
Acme Paper Corporation
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Structured Query Language - Insert
SQL schema language is used to insert a row into the database.
INSERT INTO corporation (corp_id, name)
VALUES (27, ‘Acme Papre Corporation’);
This adds a row to the corporation table with the value 27 for the
corp_id column and the value Acme Paper Corporation for the
name column
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Structured Query Language - Update
Whoops! “Paper” was spelled wrong. To fix it:
UPDATE corporation
SET name = ‘Acme Paper Corporation’
WHERE corp_id = 27;
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
Points to Remember:
Relational databases are tables related by primary and foreign (redundant)
column values.
SQL schema statements define structures that are stored in the data dictionary
SQL data statements manipulate table data
SQL data statements also select data into temporary tables or result sets
Beaulieu, A. (2020). Learning SQL: Generate, manipulate, and retrieve data (3rd ed.). O’Reilly Media, Inc.
MySQL 8.0.21 Installation
Steps
1. The first thing you need to do is grant privileges to
allow the MySQL Installer application to work in
Windows 10. Click the Yes button to authorize
the MySQL Installer to run.
2. The next thing you need to do is grant privileges to
allow the MySQL Installer Launcher application to work
in Windows 10. Click the Yes button to authorize
the MySQL Installer to run.
MySQL 8.0.21 Installation
Steps
3. Now you start the install by choosing a setup type. As
a rule, I recommend you install the Developer Default. It
is the default selection and preselected for you. Click
the Next button to verify that you can install what
you’ve selected.
4. The next workflow step checks requirements and lists
any unmet requirements. The workflow lists the
requirements for MySQL for Excel 1.3.8 as unmet
because Microsoft Excel is not installed. Click
the Next button when there are no unmet requirements
in the list. Click the Back button to remove MySQL for
Excel 1.3.8 from the setup selection.
MySQL 8.0.21 Installation
Steps
5. Returning to the setup type workflow, you should
select the Custom radio button. Click the Next button to
view the list of selected types.
6. Use the green arrow pointing to the left to remove
MySQL for Excel 1.3.8 from the list. Click
the Next button to continue the MySQL Installer to
install the selected MySQL libraries.
MySQL 8.0.21 Installation
Steps
7. This dialog will display for several minutes as each of
the MySQL Installer modules is. Click the Next button to
move forward in the MySQL Installer workflow.
8. This dialog qualifies that there are three products to
configure. Click the Next button to begin configuration
of these products.
MySQL 8.0.21 Installation
Steps
9. This dialog lets you choose between a Standalone
MySQL Server / Classic MySQL Replication and InnoDB
Cluster. You should choose the Standalone MySQL
Server for a developer installation on a laptop or
desktop. Click the Next button to continue the MySQL
Installer configuration.
10. This dialog lets you choose set the type and
networking values. They should be preselected as they
are in the screen image. Click the Next button to
continue the MySQL Installer configuration.
MySQL 8.0.21 Installation
Steps
11. This dialog lets you choose between SHA256-based
and the older MD5 encryption. Click Use Strong
Password Encryption for Authentication
(RECOMMENDED) radio button. Click the Next button to
continue the MySQL Installer configuration.
12. This dialog lets you enter the MySQL Root Password.
Click the Next button to continue the MySQL
Installer configuration.
MySQL 8.0.21 Installation
Steps
13. This dialog lets you enter a Windows Service Name
and install a Standard System Account or Custom
User account. Click the Next button to continue
the MySQL Installer configuration.
14. This dialog lets you apply the configuration of
the MySQL Server or Custom User product. Click
the Next button to continue the MySQL
Server configuration.
MySQL 8.0.21 Installation
Steps
15. This dialog lets you watch the progress of the MySQL
Server or Custom User configuration. Click
the Finish button to complete the MySQL
Server configuration.
16. This dialog lets you choose the next MySQL
Router product for configuration. Click the Next button
to begin the MySQL Router configuration.
MySQL 8.0.21 Installation
Steps
17. This dialog allows you to configure the MySQL
Router Configuration product. Leave the Hostname
and Password fields blank when you do not want to
configure the MySQL Router Configuration product.
Click the Finish button to complete the MySQL Router
workflow.
18. This dialog lets you choose the next Samples and
Examples product for configuration. Click
the Next button to begin the Samples and
Examples configuration.
MySQL 8.0.21 Installation
Steps
19. This dialog lets you create a Windows 10 MySQL
Service. You enter the root password that you entered
in Step #12. After you enter root password, click
the Check button to verify the root password.
The Check button enables the Next button when
the root account’s password is validated. Click the now
enabled Next button to create the MySQL Service.
20. This dialog lets you create a Windows 10 MySQL
Service. Click the now enabled Next button to create the
MySQL Service.
MySQL 8.0.21 Installation
Steps
21. This dialog applies all the configurations selected.
Click the Execute button to proceed with the
configuration.
22. This dialog lets you watch the progress of the
configuration. Click the Finish button to complete the
product configurations.
MySQL 8.0.21 Installation
Steps
23. This dialog the product configurations. You should
note that the MySQL Router was not configured or
needed. Click the Next button to complete the
installation.
24. This dialog completes the workflow and launches
MySQL Workbench and Shell. Click the Finish button to
complete the installation and configuration processes.
MySQL Provisioning
I’ve been using MySQL 8 on Linux for a couple years but the Linux repository version didn’t
install the new MySQL Shell. So, I discovered the new MySQL Shell when I installed MySQL 8
(8.0.21) on Windows to teach my student how to use it to learn SQL commands. I encourage
you to read the full MySQL Shell document.
The following is a tutorial to provision a student user and a studentdb database in MySQL.
It uses the MySQL Shell (mysqlsh) and stages for the uploads of comma-separated values
files.
MySQL Provisioning
After installing MySQL on the Windows 10 OS, open the Window OS Command Line Interface
(CLI) shell by entering the following in the search field at the bottom left:
cmd
It launches a CLI interface to the Windows OS. The cmd (command) utility opens the CLI in
the following directory (where you substitute your user’s name for the username placeholder
below):
C:\Users\username
At the command prompt, you would enter mysql to launch the old MySQL CLI. Instead of
that, you enter mysqlsh to launch the new MySQL CLI as the root user. The command to
launch the MySQL Shell as the root user is:
Mysqlsh –uroot -p
MySQL Provisioning
It should return the following and prompt you for a single character entry to save the
password for the Operating System user. As a rule, in a development instance that’s a good
idea and practice.
MySQL Shell 8.0.21
Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other names may be trademarks of their respective owners.
Type '\help' or '\?' for help; '\quit' to exit.
Creating a session to 'root@localhost'
Please provide the password for 'root@localhost:33060': ********
Save password for 'root@localhost:33060'? [Y]es/[N]o/Ne[v]er (default No): y
Fetching schema names for autocompletion... Press ^C to stop.
Your MySQL connection id is 9 (X protocol)
Server version: 8.0.21 MySQL Community Server – GPL
No default schema selected; type \use <schema> to set one.
MySQL localhost:33060+ ssl JS >
MySQL Provisioning
The prompt will not accept SQL commands because it always initializes in the JavaScript (JS)
context. The MySQL Shell supports three interactive interfaces: JavaScript, Python, and SQL.
You can verify the integrity of the shell from the JavaScript (JS) context as the root user
with the following command:
MySQL localhost:33060+ ssl JS > shell.status()
MySQL Provisioning
As the root user, it should return something like this:
MySQL Shell 8.0.21
Connection Id: 9
Default schema:
Current schema:
Current user: root@localhost
SSL: Cipher in use: TLS_AES_256_GCM_SHA384 TLSv1.3
Using delimiter: ;
Server version: 8.0.21 MySQL Community Server - GPL
Protocol versión: X protocol
Client library: 8.0.21
Connection: localhost via TCP/IP
TCP port: 33060
Server characterset: utf8mb4
Schema characterset: utf8mb4
Client characterset: utf8mb4
Conn. characterset: utf8mb4
Result characterset: utf8mb4
Compression: Enabled (DEFLATE_STREAM)
Uptime: 20 hours 4 min 19.0000 sec
MySQL Provisioning
You can switch to the SQL context as the root or any other user with this command. The
switch only changes your form of interaction with the server and you remain connected as
the root user:
MySQL localhost:33060+ ssl JS > \sql
You can verify that your session is still owned by the root user with the following SELECT
statement:
MySQL localhost:33060+ ssl SQL > SELECT user();
It returns:
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.0005 sec)
MySQL Provisioning
The next step shows you how to set up a sample studentdb database. Some syntax has
changed from prior MySQL releases. Here are the three steps:
1. Create the studentdb database with the following command as the MySQL root user:
MySQL localhost:33060+ ssl SQL > CREATE DATABASE studentdb;
2. Grant the root user the privilege to grant to others, which root does not have by default.
You use the following syntax as the MySQL root user:
MySQL localhost:33060+ ssl SQL > GRANT ALL ON *.* TO 'root'@'localhost';
3. Create the user with a clear English password and grant the user student full privileged on
the studentdb database:
MySQL localhost:33060+ ssl SQL > CREATE USER 'student'@'localhost' IDENTIFIED WITH mysql_native_password BY 'student';
MySQL localhost:33060+ ssl SQL > GRANT ALL ON studentdb.* TO 'student'@'localhost';
4. Our sample database uses large file uploads with MySQL’s LOAD command, which means you
need to grant on additional global privilege:
show variables like 'secure_file_priv';
MySQL Shell Parsing
The reply in the bug explained the behavior difference between MySQL Workbench and the MySQL Shell
(mysqlsh) environments. MySQL Workbench uses the MySQL client, which supports multiple client statements
with the CLIENT_MULTI_STATEMENTS option. Recognizing that, the logging entry SELECT statement should
move to a position before setting the DELIMITER, like:
-- Set a label for the log file.
SELECT 'CREATE PROCEDURE test' AS "Statement";
-- Reset the delimiter so that a semicolon can be used as a statement and block terminator.
DELIMITER $$
CREATE PROCEDURE test
( pv_input1 CHAR(12)
, pv_input2 CHAR(19)) MODIFIES SQL DATA
BEGIN
SELECT CONCAT(pv_input1,', ',pv_input2) AS message;
END;
$$
-- Reset the standard delimiter to let the semicolon work as an execution command.
DELIMITER ;
-- Call the test procedure.
CALL test('One','Two');
MySQL Shell Parsing
The new test case only submits one statement at a time. The logging query is submitted by the
semicolon, and the test procedure by the double dollar ($$) symbol set.
So, I was correct identifying a parsing behavior difference between MySQL Workbench and MySQL
Shell. It appears to be a difference by design but the MySQL Shell documentation fails to explain it
can’t manage multiple statements, I hope identifying this saves others time.
It’s also true that the MySQL client software supports TEE and NOTEE to write log files.
Unfortunately, MySQL Shell (mysqlsh) doesn’t support the TEE and NOTEE syntax. You can only do
minimal logging with the control of standard error (stderr) by using the application and AdminAPI
log utilities, which are covered in Chapter 8 of MySQL Shell 8.0 documentation.