SQL Introduction

COMP3311 20T3 ♢ SQL Intro ♢ [0/12]
❖ SQL vs Relational Model

The relational model is a formal system for

SQL is a "programming" language for SQL extends the relational model in some ways (e.g bags vs sets of tuples)

SQL omits some aspects of the relational model (e.g. general constraints)

COMP3311 20T3 ♢ SQL Intro ♢ [1/12]
❖ 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

Conforming to standard should ensure portability of database applications
COMP3311 20T3 ♢ SQL Intro ♢ [2/12]
❖ SQL Intro

SQL has several sub-languages ...

Meta-data languages manage the database schema

Data update language manages sets of tuples

COMP3311 20T3 ♢ SQL Intro ♢ [3/12]
❖ SQL Intro (cont)

Syntax-wise, SQL is similar to other programming languages

In the standard, all non-quoted identifiers map to all upper-case In PostgreSQL, all non-quoted identifiers map to all lower-case In all standards-adhering DBMSs, different quoted identifiers are different
COMP3311 20T3 ♢ SQL Intro ♢ [4/12]
❖ SQL Syntax in a Nutshell

SQL definitions, queries and statements are composed of:

Similar means "often the same, but not always" ...
COMP3311 20T3 ♢ SQL Intro ♢ [5/12]
❖ SQL Syntax in a Nutshell (cont)

Comments: everything after -- is a comment

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',  but no '\n'   (use e'\n')

Numbers: like C, e.g. 1,  -5,  3.14159,  ...

Types:  integer,  float,  char(n),  varchar(n),  date, ...

Operators:  =,  <>,  <,  <=,  >,  >=,  AND,  OR,  NOT, ...

COMP3311 20T3 ♢ SQL Intro ♢ [6/12]
❖ Names in SQL

Identifiers denote:

Naming conventions that I (try to) use in this course: We initially write SQL keywords in all upper-case in slides.
COMP3311 20T3 ♢ SQL Intro ♢ [7/12]
❖ Types/Constants in SQL

Numeric types: INTEGER, REAL, NUMERIC(w,d)

10    -1    3.14159    2e-5    6.022e23 

String types: CHAR(n), VARCHAR(n), TEXT

'John'   'some text'   '!%#%!$'   'O''Brien'
'"'   '[A-Z]{4}\d{4}'   'a VeRy! LoNg String'

PostgreSQL provides extended strings containing \ escapes, e.g.

E'\n'   E'O\'Brien'   E'[A-Z]{4}\\d{4}'   E'John'

Type-casting via Expr::Type   (e.g. '10'::integer)

COMP3311 20T3 ♢ SQL Intro ♢ [8/12]
❖ Types/Constants in SQL (cont)

Logical type: BOOLEAN, TRUE and FALSE   (or true and 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.

COMP3311 20T3 ♢ SQL Intro ♢ [9/12]
❖ 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', ... )

COMP3311 20T3 ♢ SQL Intro ♢ [10/12]
❖ 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');

COMP3311 20T3 ♢ SQL Intro ♢ [11/12]
❖ 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

COMP3311 20T3 ♢ SQL Intro ♢ [12/12]


Produced: 19 Sep 2020