You are on page 1of 31

Learning SQL

with MySQL

‐ based on the College Case

Presenter: M de Vries
Outline
 The demonstration scope
 (Re‐)creating the oms_data database from a 
script file
 SQL to:
 Select a sub‐set of records
 Constrain the selection, using operators
 Joining tables
 Inserting rows into tables
 Creating a copy of a table
 Deleting rows in a table
 Updating rows in a table
SQL demonstration scope
(Re‐)creating the oms_data database
 Open the script file:
 File
 Open SQL Script
 browse to open “oms_data.sql”
 Execute the content of the file (using the yellow 
thunder icon)
 Refresh
 Result: In you list of schemas, the oms_data schema 
should have been added, including 14 tables.
 Activate the oms_data database by either:
 Double‐clicking on oms_data so that it appears in 
bold
 USE oms_data
The SELECT statement
The SELECT statement: optional WHERE and ORDER BY

Lindie, then Pieter
SELECT only certain columns
Operators: IS NULL  / IS NOT NULL
Operators: Comparison operators
=
!=
same meaning: not equal to
<>
>
<=
<
<=
Operators: Comparison operators
Where comparison value is a string

Where comparison value is a date


Operators: AND   OR   NOT

AND

Use parenthesis, 
else AND will 
receive precedence

OR

NOT
Operators: IN
Operators: BETWEEN
Operators: LIKE
…starting
with

…including a
Operators: LIKE vs REGEXP
LIKE,
end with

REGEXP,
end with
$

REGEXP,
start with
^
Operators: REGEXP…
Multiple
patterns
with |

Multiple
characters
[]
Operators: LIMIT
The first
number of
records

Records Pay attention to the strict sequence of 
exclude the clauses: SELECT, FROM, WHERE, 
first ORDER BY, LIMIT
number
of items
Inner Joins  You need to prefix the person_id column, 
else you will get an error. Reason is that 
person_id exists as a column in both the 
student table and the person table.
 We used an alias for tables student (alias s) 
and person (alias p).
 For the last clause (line 4) we could also use 
a shorter notation:
USING (person_id)
Self joins

First add a record to table 
course_per_curriculum and “Apply” the 
changes. The added record indicates that 
DST320 is replacing course_per_cur_id = 4 
(i.e. replacing BID320 that forms part of 
Dipl_Inf_Man)

The keyword AS is used to define an 
alias, giving more meaning to the result 
set.
Multiple joins
Outer joins Not every 
person is a 
student

Re‐visit a previous query ‐>
26 rows returned

INNER JOIN: Only return records where a student_no exists 
(Note JOIN has the same meaning than INNER JOIN).

Re‐configure the 
LEFT JOIN query, using
31 rows returned RIGHT JOIN
31 rows returned
Hint: table 
associated with  Hint: table 
LEFT JOIN has a  associated with 
less  RIGHT JOIN has a 
comprehensive  more 
set comprehensive 
set
Outer joins from multiple tables

22 rows returned

Due to the normal JOIN to 
student_curriculum_registration, only 
students registered for a curriculum are 
shown.

Changing to LEFT JOIN
31 rows returned

Note that Botsi Dlamini is a student, but is not 
registered for a curriculum.
Inserting rows into tables

AI: When 
inserting a  When default 
Use the “tool” icon to  new  is NULL, we 
access the table design  record, we  need not 
properties. need not  specify a value 
specify the  for this 
person_id attribute

If no columns are selected, a value/default for  Alternatively, you need to specify the column 
every column should be provided. names. You may leave out AI‐columns and 
attributes that allow for NULL
Inserting rows into multiple tables

For every 
student_curriculum_
registration instance, 
many 
course_registrations
Use the LAST_INSERT_ID () function to get the ID that  must be inserted
was last inserted on the database, i.e. the one auto‐
increment id that was inserted for 
student_curriculum_registration

Instance was added in 
student_curriculum_registration

Instance was also added in 
course_registration, with st_cur_reg_id = 23
Selecting from multiple tables to populate a table (1)

The submission entity was not included in 
your script to generate tables in the 
oms_data database.
We need to create it manually.

1. Specify 
the column 
designs

For each FK, 
first select the  …then select the referenced 
referenced  column
table…
Selecting from multiple tables to populate a table (2)

We will want to 
automatically populate the 
submission table by 
selecting from multiple 
tables

We want to source values 
from the parent tables 
(only these 2 columns) to 
populate the submissions 
table
Selecting from multiple tables to populate a table (3)

Change the SELECT query to 
be a sub‐query within the 
INSERT statement

Once the script was 
executed, 10 records 
should appear in the 
submission table
Creating a copy of a table, CREATE TABLE ….AS
Refresh

Once the query is executed, 
a submission_archive table 
should appear in the list of 
tables.

NOTE: use the refresh icon.

The newly‐created 
table does not have an 
auto‐increment or PK.
Deleting rows in a table

Before deleting

After executing 
the DELETE 
FROM query
Updating rows in a table
Scenario: We receive instruction from the 
admission officer at the Student Administration 
Department that all 2019 registrations have been 
approved on 1 April 2019.
We would like to do a “batch” update.

If you get an error on 
multiple updates, you 
need to first un‐check 
the safe‐mode:
‐> Edit
‐> Preferences
‐> SQL Editor
Uncheck “Safe 
Updates”
Preparation for Test vs Exam

Scope for a5 test


Scope for exam

You might also like