comp101-lect05
comp101-lect05
Foundations
of Information
Systems
1
Today
2
Logical data models
3
Relational model of data
The inspiration behind SQL databases
4
The Relational Model of
Data E.F. Codd, 1923–2003
6
Relational Model Components
7
Properties of Relational Model
8
Properties of Relational Model
9
Mapping from an ERD to relational
(more detail shortly)
10
Database Management
Systems (DBMS)
Implementation and management of logical database models
11
Every database has a schema
12
Database management system
14
SQL
Structured Query Language
15
Structured Query Language
16
Some notes on SQL
syntax
• Generally not case-sensitive:
• cReAtE tAbLe somEtaBLE (a INTeGer); is valid
• String comparison is case-sensitive ('A' ≠ 'a')
17
Creating databases in SQL
SQL Data Definition Language (DDL)
18
The essential DDL statements
CREATE <schema
object> ALTER
<schema object>
20
Types of schema “object” in SQL
(Note: not “objects” as in Java!)
21
Creating tables
22
SQL demonstration
Creating a table
23
A simple student table (Oracle)
-- Create a table called Student
CREATE TABLE Student (
Student_ID
VARCHAR(10),
First_Name
VARCHAR(50),
Last_Name
VARCHAR(50)
);
24
DROP TABLE Student;
25
Dealing with different types of
data: SQL data types
A whirlwind tour — this doesn’t really become well-understood until
you start using them in table definitions (essentially, this is reference
material for the labs)
26
Data types
27
Text data
types
CHAR(<n>)
• Fixed-length of <n> bytes, padded with blanks
• Use this if value length never changes
• e.g., to store Paper_Code at Otago (e.g. COMP101), use CHAR(7)
VARCHAR(<n>)
• Variable-length text, not blank-padded
• Must specify maximum length (of <n> bytes) — DBMS typically limits to a few
thousand
• e.g., to store First_Name of a customer, use VARCHAR(50)
TEXT
• Character Large Object (CLOB)
• “long” variable-length text (multiple thousands of bytes)
• e.g., to store a blog article or email, use TEXT
28
Numbers: Exact integer
29
Numbers: Exact decimal
NUMERIC(<p>,<s>), DECIMAL(<p>,<s>)
• NUMERIC may be slightly preferable (but often identical)
• <p> = precision = number of significant digits
• <s> = scale = number of digits after decimal point
(effectively)
30
Numbers: Approximate decimal
31
Boolean (True/False) data types
BOOLEAN
32
Dates and times
DATE
TIME [WITH TIME ZONE]
TIMESTAMP [WITH TIME ZONE]
Notes:
• DATE has precision of one day
• Range 1/1/4713 BC → 31/12/294276 AD (no year 0)
• Date/time literal values expressed as strings (e.g., '12-MAY-16', '2016-05-12’)
• CURRENT_DATE returns current date
• Date/Time manipulation by arithmetic operators (+, -)
33
See “The Problem with Time & Timezones” in
the Computerphile channel on YouTube for
the full insanity of handling dates and times
https://www.youtube.com/watch?v=-5wpm-gesOY
34
What next in SQL? (in COMP 101)
36
Thanks!
Questions?