Table of Contents
- Data Models, Part 1: Thinking About Your Data
- Data Models, Part 2: The Evolution of Data Models
- Data Models, Part 3: Relational vs. Transactional Models
- Retrieving Data with a SELECT Statement
- Creating Tables
- Creating Temporary Tables
- Adding Comments to SQL
It's important to think about requirements before implementing anything. Front-loading this work saves time later.
- data modeling: the organising of multiple tables, and how they relate to
each other
- usually represents a business process
- can help in understanding business processes
- data models should always represent a real world problem as closely as possible
- relational:
- allows for easy querying and data manipulation
- logical and intuitive querying
- transactional:
- used for storing data, and not necessarily for querying data
- e.g. medical database of patient data
- data usually needs to be extracted into a relational model to be made sense of
- entities
- these are discrete 'things'
- e.g. person, place, event, etc.
- attributes
- characteristics of entities
- e.g. age, height, etc.
- relationships
- associations between entities
- one-to-many - a customer's invoices
- many-to-many - students assigned to classes
- one-to-one - a manager for a store
ER (entity-relationship) diagrams are composed of entity types, and specify relationships between those entities.
They have the following functions:
- show entity relationships
- show business processes
- are visual aids
- show links through primary keys
Primary and foreign keys are the mechanism by which tables are related to each other.
- primary key
- a column, or set of columns, that uniquely identifies every row in a table
- foreign key
- one or more columns that can be used together to identify a single row in another table
Uses 1
and M
to signify relationships on entities:
--||-
: 1--|ᗕ
: many
- 1.1: 1
- 1.*: many
SELECT
is used to retrieve data from tables, and FROM
indicates where to
retrieve the data from.
Select one field from a table:
SELECT my_field
FROM my_table;
Select multiple fields from a table:
SELECT
my_field_a
, my_field_b
, my_field_c
FROM my_table;
Select all fields from a table using a wildcard:
SELECT *
FROM my_table;
To limit the number of results retrieved, use the LIMIT
statement.
SELECT [fields]
FROM table_name
LIMIT quantity;
Syntax differs depending on the flavour of SQL.
CREATE TABLE table_name (
id [dataType] PRIMARY KEY,
field_a [dataType] NOT NULL,
field_b [dataType] NOT NULL,
field_c [dataType] NULL,
)
id
is the primary key for the table - how we will reference this field from
other tables. Primary keys cannot accept NULL
values.
field_a
and field_b
may not ever contain NULL
. NULL
is not the same as
falsy values in programming languages. An empty string or 0 are not NULL
.
NULL
indicates no presence of any value.
Fields that are defined as NOT NULL
will return an error if not provided a
value.
We use INSERT INTO
and VALUES
to define the table to write data to. There are two ways to
insert data; implicitly without field names, or explicitly with field names.
Given a table shoes
with fields for id
, brand
, color
, price
, and desc
:
INSERT INTO shoes
VALUES (
1, 'Gucci', 'Black', '25000', NULL
);
Given a table shoes
with fields for id
, brand
, color
, price
, and desc
:
INSERT INTO shoes
(id, brand, color, price, desc)
VALUES
(1, 'Gucci', 'Black', '25000', NULL);
This method is more flexible, as we're not relying on column order, and we can specify exactly which fields we want to write data to.
Temporary tables are useful when we want to clone a table, or get a subset of a table without having to create an actual table.
Features of temporary tables:
- deleted once the session is over
- are faster to create than real tables
- are useful for complex queries that use subsets or joins
To create temporary tables, we use the TEMPORARY
statement:
CREATE TEMPORARY TABLE sandals_temp AS (
SELECT * from shoes
WHERE shoe_type = 'sandals'
)
For MySQL there's no guarantee that a temporary table will be deleted. Dropping temporary tables once they are no longer required is considered good practice.
DROP TEMPORARY TABLE sandals_temp;
Although there's nothing preventing one from creating a temporary table with the same name as an existing table, it could lead to issues in deleting non-temporary data.
Use --
to comment out single lines.
SELECT shoe_id
--, brand_id
, shoe_name
FROM shoes
MySQL allows for #
to be used in addition to --
.
Use /* */
to comment out multiple lines.
SELECT shoe_id
/*, brand_id
, shoe_name
*/
FROM shoes