INTRODUCTION TO RELATIONAL DATABASE SYSTEMS
DATENBANKSYSTEME 1 (INF 3131)
Torsten Grust Universität Tübingen Winter 2017/18
1
INTRODUCTION TO RELATIONAL DATABASE SYSTEMS DATENBANKSYSTEME 1 (INF - - PowerPoint PPT Presentation
INTRODUCTION TO RELATIONAL DATABASE SYSTEMS DATENBANKSYSTEME 1 (INF 3131) Torsten Grust Universitt Tbingen Winter 2017/18 1 THE RELATIONAL DATA MODEL Relational Data Model In the relational data model, data is exclusively organized in
1
2
lego=# table colors; +-------+-------------------------+-------------+--------+-----------+---------+ | color | name | finish | rgb | from_year | to_year | +-------+-------------------------+-------------+--------+-----------+---------+ | 0 | (Not Applicable) | N/A | ▢ | 1954 | 2013 | | 41 | Aqua | Solid | b5d3d6 | 1998 | 2006 | | 11 | Black | Solid | 212121 | 1957 | 2013 | | 7 | Blue | Solid | 0057a6 | 1950 | 2013 | | 97 | Blue-Violet | Solid | 506cef | 2004 | 2005 | | 36 | Bright Green | Solid | 10cb31 | 1950 | 2013 | | 105 | Bright Light Blue | Solid | 9fc3e9 | 2004 | 2013 | | 110 | Bright Light Orange | Solid | f7ba30 | 2000 | 2013 | | 103 | Bright Light Yellow | Solid | f3e055 | 2004 | 2013 | | 104 | Bright Pink | Solid | ffbbff | 2003 | 2013 | | 8 | Brown | Solid | 532115 | 1974 | 2006 |
3
lego=# \dt List of relations +--------+--------------+-------+-------+ | Schema | Name | Type | Owner | +--------+--------------+-------+-------+ | lego | available_in | table | grust | | lego | bricks | table | grust | | lego | categories | table | grust | | lego | colors | table | grust |
| lego | minifigs | table | grust | | lego | pieces | table | grust | | lego | sets | table | grust | +--------+--------------+-------+-------+
4
lego=# \d colors Table "lego.colors" +-----------+-----------------------+-----------+----------+---------+ | Column | Type | Collation | Nullable | Default | +-----------+-----------------------+-----------+----------+---------+ | color | integer | | not null | | | name | character varying(30) | | | | | finish | character varying(15) | | | | | rgb | rgb | | | | ⚠ | from_year | integer | | | | | to_year | integer | | | | +-----------+-----------------------+-----------+----------+---------+
5
lego=# \dTS List of data types +------------+---------------+-------------------------------------------------+ | Schema | Name | Description | +------------+---------------+-------------------------------------------------+ | lego | id | | ⚑ | lego | rgb | | ⚑ | lego | type | | ⚑ | pg_catalog | boolean | boolean, 'true'/'false' | | pg_catalog | integer | -2 billion to 2 billion integer, 4-byte storage | | pg_catalog | bytea | variable-length string, binary values escaped | | pg_catalog | json | | | pg_catalog | point | geometric point '(x, y)' | | pg_catalog | money | monetary amounts, $d,ddd.cc |
6
7
8
CREATE DOMAIN ‹t'› [ AS ] ‹t› [ CHECK (‹expression›) ]
9
=> \h ‹SQL command› => \?
10
CREATE DOMAIN rgb AS text CHECK (VALUE ~ '^(\d|[a-f]){6}$');
CREATE DOMAIN type AS character(1) CHECK (VALUE IN ('B', 'M')); -- (B)rick or (M)inifigure
CREATE DOMAIN id AS character varying(20);
11
lego=# \dD List of domains +--------+------+---------------------+------------------------------------------+ | Schema | Name | Type | Check | +--------+------+---------------------+------------------------------------------+ | lego | id |character varying(20)| | | lego | rgb |text |CHECK (VALUE ~ '^(\d|[a-fA-F]){6}$'::text)| | lego | type |character(1) |CHECK (VALUE = ANY (ARRAY['B', 'M'])) | +--------+------+---------------------+------------------------------------------+
12
SELECT ‹expression› [ AS ‹output_name› ] [, …]
=> SELECT 1904 > 1893 AS result; +--------+ | result | +--------+ | t | +--------+ (1 row)
13
CAST (‹expression› AS ‹t'›) ‹expression› :: ‹t'›
14
lego=# SELECT 'ff00ff' :: rgb, 'B' :: type; +--------+------+ | rgb | type | +--------+------+ | ff00ff | B | +--------+------+ lego=# SELECT 'foobar' :: rgb; ERROR: value for domain rgb violates check constraint "rgb_check"
lego=# SELECT 'M' :: type = 'X', '#' || ('ff00ff' :: rgb); +----------+----------+ | ?column? | ?column? | +----------+----------+ | f | #ff00ff | +----------+----------+
15
CREATE TYPE ‹t'› AS ENUM ( [ '‹label›' [, …] ] )
16
CREATE TYPE workday AS ENUM ('Mon', 'Tue', 'Wed', 'Thu', 'Fri');
CREATE TYPE brick_minifig AS ENUM ('B', 'M');
=> SELECT 'Sat' :: workday; ERROR: invalid input value for enum workday: "Sat" LINE 1: SELECT 'Sat' :: workday;
17
=> SELECT 'Wed' :: workday < 'Thu' :: workday, 'Wed' < 'Thu'; +----------+----------+ | ?column? | ?column? | +----------+----------+ | t | f | +----------+----------+
=> SELECT 'Mon' :: workday = 'Mon' :: text; ERROR: operator does not exist: workday = text LINE 1: SELECT 'Mon' :: workday = 'Mon' :: text; ^
18
lego=# \d colors Table "lego.colors" +-----------+-----------------------+-----------+----------+---------+ | Column | Type | Collation | Nullable | Default | +-----------+-----------------------+-----------+----------+---------+ | color | integer | | not null | | | name | character varying(30) | | | | | finish | character varying(15) | | | | | rgb | rgb | | | | | from_year | integer | | | |
19
20
CREATE TABLE ‹t› ( [ ‹column_name› ‹data_type› [, …] ] )
21
CREATE TABLE minifigs ( piece id, -- unique piece identifier assigned by LEGO type type, -- = 'M' (this is a minifig) name text, -- human-readable minifig name cat integer, -- category (LEGO theme) this minifig is part of weight real, -- in g img text -- URL pointing to piece's image at BrickLink.com );
22
23
CREATE SCHEMA ‹schema_name›
24
=# show search_path; +----------------+ | search_path | +----------------+ | "$user",public | -- $user expands to the user running the psql session +----------------+
=# set search_path to lego, public; =# SELECT current_schema(); +----------------+ | current_schema | +----------------+ | lego | +----------------+
25
26
>>> {'a': 42, 'b': 'LEGO', 'c': False} == {'c': False, 'a': 42, 'b': 'LEGO'} True
27
28
Communications of the ACM, 13(6), June 1970
29
30
31
1
n
1
n
32
TABLE ‹t›
33
CSV Relational Model SQL — Domain Domain — Type Type — Schema Schema File Relation Table Line Tuple Row Field Attribute Column
34
INSERT INTO ‹t› (‹column_name› [, …]) VALUES (‹expression› [, …]) [, …]
35
DELETE FROM ‹t› [ AS ] ‹alias› [ WHERE ‹condition› ]
36
UPDATE ‹t› [ AS ] ‹alias› SET ‹column_name› = ‹expression› [, …] [ WHERE ‹condition› ]
37
INSERT INTO ‹t› (‹column_name› [, …]) { VALUES (‹expression› [, …]) [, …] | ‹query› }
38
VALUES (‹expression [, …]) [, …]
39
Codd, E.F. “A Relational Model of Data for Large Shared Data Banks”. Communications of the ACM 13 (6): 377–387. ↩ 1.
40