❖ SQL vs Relational Model |
The relational model is a formal system for
SQL omits some aspects of the relational model (e.g. general constraints)
❖ SQL History |
Developed at IBM in the mid-1970's (System-R)
Standardised in 1986, and then in 1989, 1992, 1999, 2003, ... 2019
Many database management systems (DBMSs) have been built around SQL
DBMSs vs the standard
❖ SQL Intro |
SQL has several sub-languages ...
create table
alter table, drop table
insert, update, delete
select ... from ... where
Data update language manages sets of tuples
❖ SQL Intro (cont) |
Syntax-wise, SQL is similar to other programming languages
'...'
'a'
'abc123'
'John''s bag'
"..."
BankBranches
bankbranches
BANKBRANCHES
BankBranches
BANKBRANCHES
bankbranches
"BankBranches"
"bankbranches"
"BANKBRANCHES"
❖ SQL Syntax in a Nutshell |
SQL definitions, queries and statements are composed of:
--
CREATE
DROP
TABLE
integer
date
❖ SQL Syntax in a Nutshell (cont) |
Comments: everything after --
Identifiers: alphanumeric (a la C), but also "An Identifier"
Reserved words: many e.g. CREATE
SELECT
TABLE
Reserved words cannot be used identifiers unless quoted e.g "table"
Strings: e.g. 'a string'
'don''t ask'
'\n'
e'\n'
Numbers: like C, e.g. 1
-5
3.14159
Types: integer
float
char(
)
varchar(
)
date
Operators: =
<>
<
<=
>
>=
AND
OR
NOT
❖ Names in SQL |
Identifiers denote:
Branches
Students
name
code
firstName
staff
staff_id
teaches
❖ Types/Constants in SQL |
Numeric types: INTEGER
REAL
NUMERIC(
,
)
10 -1 3.14159 2e-5 6.022e23
String types: CHAR(
)
VARCHAR(
)
TEXT
'John' 'some text' '!%#%!$' 'O''Brien' '"' '[A-Z]{4}\d{4}' 'a VeRy! LoNg String'
PostgreSQL provides extended strings containing \
E'\n' E'O\'Brien' E'[A-Z]{4}\\d{4}' E'John'
Type-casting via Expr::
'10'::integer
❖ Types/Constants in SQL (cont) |
Logical type: BOOLEAN
TRUE
FALSE
true
false
PostgreSQL also allows 't'
'true'
'yes'
'f'
'false'
'no'
Time-related types: DATE
TIME
TIMESTAMP
INTERVAL
'2008-04-13' '13:30:15' '2004-10-19 10:23:54' 'Wed Dec 17 07:37:16 1997 PST' '10 minutes' '5 days, 6 hours, 15 seconds'
Subtraction of timestamps yields an interval, e.g.
now()::TIMESTAMP - birthdate::TIMESTAMP
PostgreSQL also has a range of non-standard types, e.g.
'...'
❖ Types/Constants in SQL (cont) |
Users can define their own types in several ways:
-- domains: constrained version of existing type CREATE DOMAIN Name AS Type CHECK ( Constraint ) -- tuple types: defined for each table CREATE TYPE Name AS ( AttrName AttrType, ... ) -- enumerated type: specify elements and ordering CREATE TYPE Name AS ENUM ( 'Label', ... )
❖ Examples of Defining Domains/Types |
-- positive integers CREATE DOMAIN PosInt AS integer CHECK (value > 0); -- a UNSW course code CREATE DOMAIN CourseCode AS char(8) CHECK (value ~ '[A-Z]{4}[0-9]{4}'); -- a UNSW student/staff ID CREATE DOMAIN ZID AS integer CHECK (value betweem 1000000 and 9999999); -- standard UNSW grades (FL,PS,CR,DN,HD) CREATE DOMAIN Grade AS char(2) CHECK (value in ('FL','PS','CR','DN','HD')); -- or CREATE TYPE Grade AS ENUM ('FL','PS','CR','DN','HD');
❖ Tuple and Set Literals |
Tuple and set constants are both written as:
( val1, val2, val3, ... )
The correct interpretation is worked out from the context.
Examples:
INSERT INTO Student(studeID, name, degree) VALUES (2177364, 'Jack Smith', 'BSc') -- tuple literal CONSTRAINT CHECK gender IN ('male','female','unspecified') -- set literal
Produced: 19 Sep 2020