Session 2 - SQL Coding & Database Management Technique
Session 2 - SQL Coding & Database Management Technique
YOUR INTRODUCTION TO
DATABASES & SQL PROGRAMMING LANGUAGE
SESSION 7 SESSION 8
Database design Project
and data presentations
visualisation
SESSION 6
Data management
using SQL coding
techniques
SESSION 5 SESSION 4
Data manipulation Data analysis for
with SQL complex structures
programming
language
SESSION 3
Data analysis
SESSION 1 SESSION
MODULE 22
Introduction to SQL SQL coding and
programming Database
language and Data management
Science technique
SQL CODING
DB MANAGEMENT
TECHNIQUE
MODULE 1: HTML
2. SQL Coding:
• Data Modification Techniques
• Data Retrieval Techniques (SELECT statements)
02
PART 1:
DB DESIGN AND
MANAGEMENT
• The idea behind normalisation is to organise a database into tables in such way
that a table is created about one specific topic only.
1 Mary FINANCE,TAX
2 Edith HR
NORMALISATION
EXAMPLE 1 3 Anna ADMIN
1 Mary 1 FINANCE
2 Edith 1 TAX
3 Anna 2 HR
3 ADMIN
NORMALISATION
EXAMPLE 2
source: http://salinaitmind.blogspot.com/2012/10/data-warehouse-data-normalization.html
Normalisation
“…make sure that every attribute is single valued and provides a fact completely
and only about its primary key”
• Single Valued
• The attribute can only contain one piece of information
• E.g. Polly, Moore should be two attributes – first name and surname
Name Address
Country is dependent on
location, not the ID
Third Normal Form (3NF)
• In other words, we can specify the limit on the type of data that can be stored
in a particular column in a table. Using constraints ensures the accuracy and
reliability of the data in the table.
SQL
• If there is a mismatch or any violation between the constraint we set and the
CONSTRAINT data, then the action that we are trying to perform would be aborted.
TYPES
NOT NULL UNIQUE CHECK
EX
A
NT
AM
SY
PL
E
CREATE TABLE customers CREATE TABLE + TABLE
CREATE TABLE <table_name> NAME
(col1 Type KEY DEFINITION, (customer_id INTEGER PRIMARY KEY, PRIMARY KEY
col2 Type, name VARCHAR(50), COLUMNS
col3 Type);
surname VARCHAR(50) NOT NULL,
telephone INTEGER);
X
EX
A
NT
AM
SY
PL
E
CREATE TABLE <table_name> CREATE TABLE customers CREATE TABLE + TABLE
(customer_id INTEGER, NAME
(col1 Type,
name VARCHAR(50),
col2 Type,
surname VARCHAR(50) NOT NULL,
col3 Type,
telephone INTEGER,
CONTRAINT CONSTRAINT CONSTRAINT
<constraint_name> pk_ customer _id
<constraint_type> PRIMARY KEY
(<col_that_it_applies_to>) (customer _id)
); );
Unique PK Table Customers
Unique
customer_id name surname telephone
Column
001 Mary Jones 123-4567
002 Julie Smith 789-4561
DB
source: guru99.com
DATABASE
RELATIONSHIPS
● Primary keys are an example of indexes
● They are best to use with big databases instead of using a non-key or
non-indexed values.
● Instead of scanning the whole table, index allow MySQL to quickly find rows
with a specific value which makes the search quicker
TASKS
• Set NOT NULL constraints on the columns that you think must have
values.
• Let’s do it together!
PART 2:
SQL CODING
• DDL stands for "Data Definition Language“.
• It is a subset of SQL statements that create the database schema and change its
structure.
• Typically and structural changes of the database schema refer to creating, deleting, or
modifying schema objects such as databases or tables.
CORE COMMANDS
DDL
CREATE DROP
ALTER TRUNCATE
EX
A
NT
AM
SY
PL
E
CREATE TABLE <table_name> ( CREATE TABLE customers (
<col_name> <col_data type>, CustomerID int,
<col_name> <col_data type>, FirstName varchar(255)
… LastName varchar(255)
); );
• Removes table and all data from database
EX
A
NT
AM
SY
PL
E
DROP TABLE <table_name>; DROP TABLE customers;
• Used to change an existing table
• Add/remove column
EX
A
NT
AM
SY
PL
E
ALTER TABLE <table_name>; ALTER TABLE customers;
RENAME TABLE RENAME TABLE all_customers;
<table_new_name>;
X
EX
A
NT
AM
SY
PL
E
ALTER TABLE <table_name> ALTER TABLE orders CREATE TABLE + TABLE
NAME
ADD CONSTRAINT ADD CONSTRAINT
<constraint_name> fk_customer_id
<constraint_type> FOREIGN KEY
(<col_that_it_applies_to>)
(customer_id) CONSTRAINT
REFERENCES
REFERENCES
<table_name2>
(<col2_that_it_applies_to>) customers
); (customer_id);
• DROP deletes the table itself.
• TRUNCATE deletes the data inside of table but not the table itself.
TRUNCATE
TABLE
X
EX
A
NT
AM
SY
PL
E
TRUNCATE TABLE <table_name>; TRUNCATE TABLE customers;
• DML stands for "Data Manipulation Language“.
• It is a subset of SQL statements that change the structure of the database schema.
CORE COMMANDS
DML
SELECT INSERT
UPDATE DELETE
EX
A
NT
AM
SY
PL
E
SELECT <col_name> SELECT FirstName SELECT COMMAND
FROM customers;
• There are many ways that enable us to constrain the number of results
returned by our query
EX
A
NT
AM
SY
PL
What are the fist names
E
of people in my class?
Table name Alias
first_name first_name
SELECT DISTINCT Julie Julie
<alias>.<column_name>, Mary Mary
Mary Joanna
FROM <table_name> Joanna
AS <alias>; Julie
• The WHERE clause describes the conditions to match for rows to qualify
for result set
WHERE
• It comes after the FROM statement
• Only rows that match a condition are selected for the result set
X
EX
A
NT
AM
SY
PL
What is the surname of all my
E
classmates who are called Mary?
🡨 SELECT clause
SELECT SELECT
<alias>.<column_name>, p.surname
FROM <table_name> AS FROM person AS p FROM clause
• Refer to the table ‘projects’ and return all projects that are run in London.
• INSERT new data in a table
EX
A
NT
AM
SY
PL
E
INSERT INTO <table_name> INSERT INTO customers
(col1, col2, col3, …) (FirstName,LastName)
VALUES (val1, val2, val3, …)
VALUES (“John”, “Doe”)
INSERT INTO <table_name> INSERT INTO customers Our customer table has
3 columns: CustomerID,
VALUES (val1, val2, val3, …) FirstName and
VALUES (654258, “Jessica”, LastName.
“Day”) 🡨 CustomerID = 654258
🡨 FirstName = “Jessica”
🡨 LastName = “Day”
• Modifies column(s) in a single table
EX
A
NT
AM
SY
PL
E
UPDATE table_name UPDATE contacts UPDATE COMMAND
TABLE NAME
SET SET SET KEYWORD
table_name.col1 = new_value VALUES
contacts.mobile = 123456789
WHERE WHERE CLAUSE
table_name.col2 = value; WHERE
contacts.surname = ‘Andrews’
• DELETES one or more rows in a table
• Permanent!
DELETE
• DELETE FROM is actual full command
EX
A
NT
AM
SY
PL
E
DELETE FROM table_name; • DELETE FROM customers; DELETE COMMAND
BAD PRACTICE ☹
GOOD PRACTICE ☺
We have our pizzeria customers database. Let’s modify some tables in the
database, so we add Foreign Keys to tables and define relationships
between our tables.
PRACTICE
TASKS
HOMEWORK
THANK YOU
HAVE A GREAT
WEEK!
REFERENCE
MATERIALS
• A foreign key is a field in a table that matches another field of another table. A
foreign key places constraints on data in the related tables.
FOREIGN KEY • A foreign key can be a column or a set of columns. The columns in the child table
often refer to the primary key columns in the parent table.
• A table may have more than one foreign key, and each foreign key in the child
table may refer to a different parent table.
● Instead of scanning the whole table, index allow MySQL to quickly find rows
with a specific value which makes the search quicker
EX
A
NT
AM
SY
PL
E
CREATE TABLE customers
CREATE TABLE <table_name> ( (customer_id INT PRIMARY KEY,
col1 INT PRIMARY KEY, Personal_id INT,
col2 INT,
name VARCHAR(50),
col3 INT,
INDEX (c2,c3) ); surname VARCHAR(50) NOT NULL,
telephone INT,
INDEX (personal_id) );
X
EX
A
NT
AM
SY
PL
E
DROP INDEX <index_name> ON DROP INDEX person_id ON HOW TO DROP INDEX
<table_name>; customers;
FOREIGN KEY
• Columns in a table that refer to a Primary Key of another table
• Enforces referential integrity
• Foreign key reinforces relationships between tables:
• One-to-one
• One-to-many
• Many-to-many