0% found this document useful (0 votes)
9 views

10 - SQL

Uploaded by

Heather
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

10 - SQL

Uploaded by

Heather
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 91

COMP1204:

Relational Data
SQL

Tom Blount
[email protected]
Today
• SQL
• For data definition
• For data manipulation
• SQLite
• Useful SQL tools
Prerequisites
• To take part in today’s session you must have made
a planet

• https://ofb-interactive.soton.ac.uk/space/planet

• Please do this now if you haven’t!


Now we have the model, we want
to build it
• Put it into a database management system
• MySQL
• SQLite
• Oracle
• Etc.
• Turn our model into tables
• Populate with data
What is SQL
• SQL: Structured query language
• Specifies a Data Definition Language (DDL)
• Tables and views (virtual tables).
• Convert a data model to a (physical) database
• Specifies a Data manipulation (DML)
• Programmatic data manipulation
• Declarative (desired result)
• INSERT, DELETE, UPDATE or retrieve (SELECT) data.
• Provides Administration commands
• Can support
• Referential integrity
• Transactions
• Checks keys for consistency
• Access control
• Concurrent access
SQLite
• All of the database • But it’s more basic than
contained inside a single alternatives
file • It’s just a file
• Perfect for when you need • Not good for large operations
a simple database on the • Not multi-user
go, without needing to run • No concurrency
a fully fledged database
server (such as SQL server, • Not client/server
MySQL etc.) • Lacks some SQL features
• Small
• No configuration
• Serverless - no background • And you might not be keen
process/server on some of the design
• Lightweight
• Efficient decisions
• Supports most SQL and • It uses dynamic typing – you
some extensions can put a string in an integer
• Cross-platform field etc.
• Open source
Getting Started with SQLite
• Download https://www.sqlite.org/download.html
• You will want the precompiled tools for your OS
• Mac users can install via Homebrew
https://formulae.brew.sh/formula/sqlite
• Linux users can install from their Package Manager

Getting started is easy – just type sqlite3 <database>.db


It will create the database if it doesn’t already exist
Now you can issue commands!
Introducing some SQL
• Definition
• CREATE TABLE
• ALTER TABLE
• DROP TABLE
• Manipulation
• SELECT
• INSERT
• UPDATE
• DELETE
Definition
Create a Table
CREATE TABLE Table
(
Field field_type [Options],
Field field_type [Options],
Field field_type [Options],

[Keys]
);
Note: Each individual SQL command ends with a ;
Create a Table: Example
In this simple form,
CREATE TABLE planets ( we create a planets
table with these
name, columns
description,
ruler_id,
race_id,
quadrant,
sector_x,
sector_y
);
Adding Types
• We can add data types to our columns by specifying the type after the
name

• Note: Different DB systems use different types, so make sure you are
familiar with the system you are using

• In SQLite, there are 5 main types:


• INTEGER
• REAL
• TEXT
• BLOB
• NULL

• No BOOLEAN!
• No DATE!
Adding Types
CREATE TABLE planets (
name TEXT,
description TEXT,
ruler_id INTEGER,
race_id INTEGER,
quadrant TEXT,
sector_x INTEGER,
sector_y INTEGER
);
Adding Keys
CREATE TABLE planets (
name TEXT,
Here we have specified the
description TEXT, primary key of the table
ruler_id INTEGER,
race_id INTEGER, If an operation is attempted
later that will invalidate this, the
quadrant TEXT, key will be enforced and the
sector_x INTEGER, insertion/edit will be rejected

sector_y INTEGER,
PRIMARY KEY
(quadrant,sector_x,sector_y)
)
Adding Properties
We can specify whether
columns can take null
CREATE TABLE planets ( values or not
name TEXT NOT NULL,
description TEXT NULL,
ruler_id INTEGER NOT NULL,
race_id INTEGER NOT NULL,
quadrant TEXT NOT NULL,
sector_x INTEGER NOT NULL,
sector_y INTEGER NOT NULL,
PRIMARY KEY
(quadrant,sector_x,sector_y)
)
Rename a Table
ALTER TABLE OldName
RENAME TO NewName
Remove a Table
• If you want to make changes to a table, there are
some limited operations you can do with ALTER
TABLE
• However, it is usually easiest to remove the table
and recreate it as you want it

• To do this, use
• DROP TABLE <Table Name>

• Example: DROP TABLE planets


Activity 1: Getting Physical
• Now it’s time to turn our model into a database
• Create the tables
• Create the attributes
• Create the keys
• Use CREATE TABLE to create a table
• Use DROP TABLE to remove a table
• https://ofb-interactive.soton.ac.uk/space/physical
My Examples
My Examples
Using UI Tools
• While it’s good to know your SQL, there are plenty
of tools to help you
• Web-based tools
• Local tools

• Different user interfaces for different database


systems
Datagrip
• Completely free for students
• https://www.jetbrains.com/student/

• Along with various of their other tools

• Works with SQLite and most major database systems

• Getting started:
https://www.jetbrains.com/help/datagrip/sqlite.html
Datagrip

You will need to set up and install the driver and then open your SQLite File
Datagrip
But for these exercises…
• We’ve set up an SQL environment you can use from
the comfort of your web browser ☺
Manipulation
SELECT
• The SELECT statement allows us to retrieve information the
database
• We specify what data we want and where we want it from
• We can place further constraints on the conditions, grouping,
ordering and amount of data that is returned
SELECT expression
FROM tables
WHERE conditions
All of these parts are optional!
GROUP BY expression
ORDER BY expression
LIMIT expression
SELECT Examples
• SELECT * FROM Table
• * Means all columns
• SELECT Column,Column2… FROM Table
• Specify specific columns
• SELECT Columns FROM Table WHERE Conditions
• Specify conditions on results returned
• SELECT Columns FROM Table LIMIT Number
• Limit the number of results
• SELECT * FROM Table ORDER BY Column ASC/DESC
• Specify an ordering
• You can combine them but they must be in the right
order
Using WHERE
• WHERE column=number
• If the column is a number
• Example: WHERE id=3
• WHERE column=‘string’
• If the column contains text
• Example: WHERE name=‘Oli'
• WHERE column=anotherColumn
• If you want to compare values between two columns
• Example: WHERE sector_x=sector_y
A simple example
Activity 2: Our First Query Together
• Go to https://ofb-interactive.soton.ac.uk/game/
• If this doesn’t work for you, create a planet and let me know to spawn your ship!

• Your ship awaits, but hasn’t been fully programmed yet. It is currently docked at
Earth in the Ship Yard and linked to GalNet which provides data about the
quadrant
• We are going to be using the Ship Computer to query the GalNet database
• Click the GREEN computer screen to access the Ship Computer

• First, type ‘DOWNLOAD’ to update the Ship Computer with the GALNET
database
• Next, get the list of all the planets in the quadrant
• You will want to use the SELECT command
• The table is called planets
• Can you get them in alphabetical order?
• Can you get just the name, quadrant and sectors?
• How many planets are there in the alpha quadrant?
Activity 2: Example

* Accurate as of the time this screenshot was taken


Activity 2: Example

We restricted to just the name, quadrant, sector_x and sector_y columns here
More Complex Querying
• We can join conditions
together with AND, OR and
NOT
• SELECT * FROM table WHERE
id=1 OR id=2
Reference table

• We can use brackets to group


conditions
• SELECT * FROM table
WHERE id=1 OR id=2 AND
name='Bob’;
• SELECT * FROM table
WHERE (id=1 OR id=2)
AND name='Bob';
Note the difference!
More Complex Querying
• We can use more complex operators
beyond =
• =, >, <, >= (numerical comparison)
• SELECT * FROM table WHERE id > Reference table
5

• <> or != (not equal)


• SELECT * FROM table WHERE id !=
5
More Complex Querying
• LIKE
• You can use % as a Wildcard in a String
• SELECT * FROM table WHERE id
LIKE ‘%1%’

• IN or NOT IN
• SELECT * FROM table WHERE id
NOT IN (1,3,5)
Activity 3: More Complex Queries
• Go to https://ofb-interactive.soton.ac.uk/game/

• Can you get an alphabetically ordered list of the


names of the resources?
• Use the resources table
• Use the ORDER BY clause
Activity 3: Examples
Activity 3: More Complex Queries
• Go to https://ofb-interactive.soton.ac.uk/game/

• Can you get all the planets that are in sector_x: 5 in


the alpha quadrant
• Use the planets table
• The attributes you want are sector_x and quadrant
• Use the WHERE condition
Activity 3: Examples
Activity 3: More Complex Queries
• Go to https://ofb-interactive.soton.ac.uk/game/

• Can you get the names, quadrants, sectors and


sizes of just the Class M planets, with the largest
first?
Activity 3: Examples
SQLite: Inspecting the Database
with SQL
• You can also inspect the database with SQL directly

• SELECT * FROM sqlite_master


• The sqlite_master table contains all the meta-information
about your database
Sweetcorn pizza

Joins JOIN
Non-sweetcorn pizza

• When you want to select data from more than one


table
• We do this using JOINS

• You split your relations to avoid redundant data,


now you want to bring them back together…
• Join column is usually Key column
• Nulls will never join
• Need compatible data types
Join Types
• JOIN (or INNER join): returns just rows
with matching keys (join column values)
• LEFT join: returns all rows from left (first)
table, whether they match a row in the
second table or not
• RIGHT join: returns all rows from right
(second) table, whether they match a row
in the first table or not
• FULL OUTER join: Returns all rows from
both tables, whether they match or not 45

45
Joining in SQL
SELECT Table1.*, Table2.*
FROM Table1
LEFT JOIN Table2
ON Table1.Column=Table2.Column

• You use ON to specify how the tables join to each


other
• You will usually use a LEFT JOIN or an INNER JOIN
• JOIN by itself usually refers to an INNER JOIN
• But different languages will vary!
Joining in SQL
• Can have multiple tables and multiple joins

SELECT Table1.id, Table2.*, Table3.*


FROM Table1
LEFT JOIN Table2
ON Table1.Column=Table2.Column
LEFT JOIN Table3
ON Table2.Column=Table3.Column
Join Example
SELECT planets.name,rulers.ruler_name
FROM planets
LEFT JOIN rulers ON planets.ruler_id=rulers.id
Renaming Columns
• SELECT Table.Column AS Column1,
Table2.Column2 AS Columns2

• This lets us specify which columns from which


tables we want and what we want to call them
Renaming Example
SELECT
planets.name AS planet,
rulers.ruler_name AS ruler
FROM planets
LEFT JOIN rulers
ON planets.ruler_id=rulers.id
Activity 4: Selecting and Joining

• Go to https://ofb-interactive.soton.ac.uk/game/

• Make sure to type DOWNLOAD first to update your ship


computer
• Write a query to get all the planets in the alpha quadrant
• No joins required – a little bit of revision for you!
Activity 4: Examples
Activity 4: Selecting and Joining

• Go to https://ofb-interactive.soton.ac.uk/game/

• Write a query to get the planet, races and rulers


information for a given planet (e.g. planet 1)
• We want the planet name, race name and ruler name
• You will need to join planets to races and rulers
Activity 4: Examples
Activity 4: Selecting and Joining

• Go to https://ofb-interactive.soton.ac.uk/game/

• Write a query to get the resource name and amount for


every resource on a given planet
• You will need to join planets, planets_resources and resources
Activity 4: Examples
We’ll pause here for
today!
Inserting
You can insert into all columns in order:

INSERT INTO Table


VALUES (value1,value2…,valueN)

You can insert into only specific columns like so:

INSERT INTO Table (Column1,Column2,Column3)


VALUES (”Value1”,”Value2”,3)
Inserting with a SELECT
• We can use a SELECT query inside an INSERT query

We are going to make a quick lookup table that has the resource name and planet details

We bring in the results from our select query, matching the number of columns to our table

We have populated the table with results from the query

Note: these values are duplicated, not references!


Views
• Views are like virtual tables
• They have a name
• They run a query
• You can then query them directly
• You can join them against other
tables
• But they don’t store any data
themselves
• You create a view by defining the
query
• CREATE VIEW Name AS SELECT …
• You can also remove views by
dropping them
• DROP VIEW Name
• To modify a view
• Drop it and recreate it!
Views
• Our better_resources table would make more sense as
a view
• No replicated data, but a human-readable version of the data

Now we can query it just like a normal table, but it will


always be up to date
Altering a Table with Data
By doing this, we can alter a table by creating a new structure and reading in the old data

CREATE TABLE NewTable (



)
INSERT INTO NewTable(col1,col2…)
SELECT col1,col2… FROM OldTable

DROP TABLE OldTable

ALTER TABLE NewTable RENAME TO


OldTable
Updating
You update by specifying the new values for 1 or more columns
and optionally one or more condition (otherwise all rows will
be updated)

UPDATE Table
SET column=value,column=value…
WHERE column=value AND column=value…

Update
Update all with
conditions
Deleting
Deleting works in the same way!
National Space Centre,
Leicester

If you do not specify conditions, all rows will be deleted

DELETE FROM Table


[WHERE Conditions]
Activity 5: Inserting and Updating
• Go to https://ofb-interactive.soton.ac.uk/game/

• Rename your Ship (UPDATE)


• Can you UPDATE your ships name in the ships table
• UPLOAD it to GALNET after changing it’s identity
• Send a Galactic Message (INSERT)
• Can you INSERT a message into the messages table
• Give it a subject, a ruler_id to send to and a message
• You should leave the other fields empty
• Type UPLOAD to upload your message to GALNET
• Type DOWNLOAD to check for any received messages and
check your messages table
Activity 5: Examples
Useful SQL Functions
• SQL contains various useful functions for working with
maths, strings, dates and other useful operations on the
database
• SQLite has the following
• Scalar
• Date and Time
• Aggregation
• Window
• Maths
• JSON
• https://sqlite.org/lang_corefunc.html

• Functions are written as function(parameter1,parameter2)


Aggregation
SELECT column_name,
aggregate_function(another_column)
FROM table_name
WHERE column_name=operator_value
[GROUP BY column_name];

• Two most useful aggregation functions


• COUNT
• Count the number of values
• SUM
• Sum (add) all values
Aggregate Functions Example
• avg(x)
• count(x)
• count(x)
• max(x)
• min(x)
• sum(x)
Other useful functions
• abs(x): Absolute numeric value
• changes(): Number of modifications
last query
• last_insert_rowid(): Row ID of last
inserted row
• length(x): String length of x
• lower/upper(x):
Lowercase/Uppercase of x
• max/min(x,y,z…): Max/Min from
values
• random(): Pseudo-random integer
• replace(x,y,z): Replace y with z in X
• round(x): Round x to nearest integer
Grouping
• We can group aggregate functions using the GROUP
BY clause
Activity 6: Aggregation
• Go to https://ofb-interactive.soton.ac.uk/game/

• Can you get the COUNT of all the planets in the


alpha quadrant?
• You can use COUNT(*) on the planets table
Activity 6: Examples
Activity 6: Aggregation
• Go to https://ofb-interactive.soton.ac.uk/game/

• Can you get the SUM of all the iron?


• You will need to join the resources, planets_resources
tables
• Use SUM(amount) and WHERE
Activity 6: Examples
Activity 6: Aggregation
• Go to https://ofb-interactive.soton.ac.uk/game/

• Can you get the SUM of all the different resources


in the alpha quadrant?
• You will need to join resources, planets_resources and
planets
• One column should be the name of the resource
• The second column should be the total of that resource
in the quadrant
• You will need to use GROUP BY
Activity 6: Examples
Primary Keys
• A primary key ensures every row is unique
• You cannot have two rows with the same primary key
• A primary key can be on a single field or multiple fields (composite)
• If you don’t provide one, SQLite uses an automatically generated
rowid
• You can add a primary key at table creation
• CREATE TABLE Table (
id INTEGER PRIMARY KEY

)
• CREATE TABLE Table (
field1 INTEGER,
field2 INTEGER,

PRIMARY KEY (field1,field2)
)
• In SQLITE, you cannot add/remove a primary key after
creation
• You will need to create a new table, copy the data, remove the old
table and rename – so get it right at creation!
Foreign Keys
• Foreign keys can help ensure referential integrity
between tables
• If a column in Table1 references a column in Table2, we can’t
have a value in Table1 that isn’t in Table2
• Foreign keys are created when the table is created
• FOREIGN KEY (column) REFERENCES Table(column)
• Foreign key constraints have to be turned on for this to
work
• The PRAGMA command changes the way the DBMS is setup
• We can use it to turn on foreign key support
• PRAGMA foreign_keys = ON;
Foreign Keys Example
• We create a planets_notes table to create notes on planets, which takes a
planet_id and a text note
• CREATE TABLE planets_notes (
id INTEGER PRIMARY KEY,
planet_id INTEGER,
note TEXT,
FOREIGN KEY (planet_id) REFERENCES planets (id));

We now can’t have a note on a planet that doesn’t exist (once foreign_keys is on)

We also can’t delete a planet that has an associated note

This ensures that regardless of what our application does, the database cannot end up inconsistent
Maintaining Referential Integrity
• We can tailor this behaviour using ON DELETE and FOREIGN KEY (planet_id)
ON UPDATE REFERENCES planets
• ON DELETE ON DELETE <Action>
• When the parent record is deleted ON UPDATE <Action>
• ON UPDATE
• When the parent key is updated
• Not overly useful when it’s an automatically generated
key

• Potential actions:
• CASCADE the deletion/updates to the referring
tables
• May not be appropriate
• Delete a booking, you don’t want to delete the member!
• Delete a member when they’ve left, you don’t want to
delete they had a booking
• RESTRICT: Prevent the parent being updated/deleted
• SET DEFAULT: Set the key value to the default
• SET NULL: Set the key value to NULL
Back to the Example
We will set our table to cascade updates and deletes

CREATE TABLE planets_notes (


id INTEGER PRIMARY KEY,
planet_id INTEGER, note TEXT,
FOREIGN KEY (planet_id) REFERENCES planets (id)
ON DELETE CASCADE
ON UPDATE CASCADE);

When planet 1 was deleted, the note corresponding to planet 1 was deleted (rather than
prevented, as it was before)

When planet 2 had it’s key changed, the key was also updated in planet_notes
Indexes
• Data structures associated with a table to support
queries
• Logically ordered by the values of a key
• Usually created for columns you’ll be accessing regularly
• Greatly improves database performance
• Instead of having to load all records (O(n) linear complexity)
where you have to examine each and check if a clause holds
• Can quickly lookup and access associated rows based on the
value of the index
• For example, ID columns, boolean properties etc.
• But adds cost to INSERT/UPDATE/DELETE
• Also have to update the indexes
• Efficiently querying large relations would be impossible
without them!
Index Mechanics
• Chosen attribute becomes an index key
• Sorted, searchable - Logarithmic time, rather than linear time
• Don’t have to scan the whole table
• Typically implemented as balanced binary trees
• Before, Between and After values to follow (can be used for
comparisons and quality)
• Can also be implemented as hash indexes
• Only for equality (= or != operations)
• The database system will use the indexes to plan the best
possible way to execute your query
Creating Indexes
• CREATE INDEX <index_name>
ON <table_name>(<column_name>)
• DROP INDEX <index_name>

Instant results even on a large data set!


Unique Indexes
• CREATE UNIQUE INDEX <index_name>
ON <table_name>(<column_name>)

• If you know there will only ever be 1 value in that


index. The database will enforce this restriction
with this index

• E.g. CREATE UNIQUE INDEX module_code


ON modules(module_code)
Multi-Column Indexes
• CREATE INDEX <index_name>
ON <table_name>(<column_name>,...)

• Concatenated indexes: More than one column


• The order does matter
• First sorted by the first index, then the second index and so on
• CREATE INDEX names ON contacts(surname,firstname)
• Like a telephone directory (e.g. by Surname, then First
Name)
Multi-Column Indexes
• CREATE INDEX test ON table(a,b,c)

• SELECT * FROM table WHERE a=1 AND b=1 AND c=1


• Entire index used
• SELECT * FROM table WHERE a=1 AND b=1
• Use the first 2 columns of the index
• SELECT * FROM table WHERE a=1 AND c=1
• Use only the first column of the index
• SELECT * FROM table WHERE b=1 AND c=1
• The index cannot normally be used (no leading column), but some
database systems can do an Index Skip and use the rest of the index
• SELECT * FROM table WHERE c=1
• The index cannot be used (no leading columns)
• Moral: Make sure you think about your indexes and use single
column indexes unless multi-column indexes make sense!
SQLite: Helpful dot commands
• Show tables
.tables
• Show the schema for a
table
.schema table
• Change the
input/output/import
separator
.seperator seperator
• Import data into a table
.import file table
• Dump the
database/table to SQL
.dump [table]
Note: These commands are not SQL and only work in the SQLite client
Activity 7: Fixing the Navigation
Computer
• The functions table in the ships computer allows you to write SQL
queries mapped to function names
• Type the name of a function on the computer to test it out
• You must INSERT INTO the functions table
• The name is the function name (one word)
• The function is the SQL to execute
• You can use variables
• $1 = argument 1
• $quadrant = current quadrant
• $me = your ruler ID
• $ship = your current ship
• To fix the navigation computer
• Use your queries from the earlier activities
• Create a function called planets that gets all the planets in the quadrant
• Create a function called planet that gets all the planet, ruler and race info
where planets.id=$1
• Create a function called resources that gets the resource.name and
planets_resources.amount where planets.id=$1
That’s all for Relational DBs!
• It’s been fun ☺ (I hope)

• Any last questions?

• Check the Notes Wiki for revision and


coursework support sessions!

You might also like