DDL
Data types Numbers integer, smallint, bigint real, double precision Characters and Strings char(n): up to n characters, blank padded varchar(n): up to n characters, variable length Date & Time date (e.g., 2006-03-01 for the 1st of March) time (e.g., 12:30:00 ) timestamp: date&time together (e.g., 2006-03-01 12:30:00 ) interval: quantity unit (e.g., 2 hours ) Network cidr: network address, with netmask (e.g, 192.168.2.0/24 ) inet: host address, with optional netmask (e.g., 192.168.2.1/24 ) macaddr: MAC address (e.g., 00:0A:19:67:45:12 ) Bytes & bits bytea for raw bytes bit(n), varying bit(n): strings of bits Geometric types, boolean types, etc. (refer to the manual) Possible to create your own types CREATE TYPE 2
Toolbox: managing tables CREATE TABLE person( name varchar(20), age integer, address varchar(30), id serial, PRIMARY KEY(id) ); DROP TABLE person; DROP TABLE person CASCADE; drops also objects depending on this one (careful) ALTER TABLE person ADD CONSTRAINT will see this later on 3
Toolbox: managing tables CREATE TABLE person( name varchar(20), age integer, address varchar(30), id serial, PRIMARY KEY(id), UNIQUE (name) ); keyword UNIQUE candidate keys attributes whose values MUST be unique, although not used to index the table entries (like the PRIMARY KEY) 4
Toolbox: managing tables CREATE TABLE person( name varchar(20) DEFAULT pippo, age integer, address varchar(30), id serial, PRIMARY KEY(id) ); 5
Toolbox: managing table content INSERT INTO person(name,age,address,id) VALUES ( gianni,23, Via Battelli, 5,1); correspondence between the names of the fields and the values UPDATE person P SET name= giovanni WHERE P.name= gianni ; the table is scanned row by row if the row satisfies the WHERE clause the update in SET is applied DELETE FROM person P WHERE P.addr= Via di Pratale,6 ; CREATE TABLE person( name varchar(20), age integer, address varchar(30), id serial, primary key (id) ); DELETE FROM person; removes all tuples from person and all tuples in related tables with cascading behaviour (careful) 6
Toolbox: serial&sequences CREATE TABLE person( name varchar(20), age integer, address varchar(30), id serial, PRIMARY KEY(id) ); Used to automatically assign integer values from a sequence Starting from 1 INSERT INTO person(name,age,address,id) VALUES ( gianni,23, Via Battelli, 5,1); INSERT INTO person(name,age,address) VALUES ( gianni,23, Via Battelli, 5 ); Equivalent (in an empty table) 7
Toolbox: serial&sequences Conflicts INSERT INTO person(name,age,address) VALUES ( gianni,23, Via Battelli, 5 ); INSERT INTO person(name,age,address,id) VALUES ( andrea,40, Via Roma, 30,2); INSERT INTO person(name,age,address) VALUES ( antonio,25, Via di Pratale, 4 ); Not allowed (conflict on id=2) Next value = 1 Next value = 2 Next value = 2 CREATE TABLE person( name varchar(20), age integer, address varchar(30), id serial, primary key (id) ); 8
Toolbox: foreign keys buildings CREATE TABLE buildings( ); addr varchar(30), addr CREATE TABLE person( person FOREIGN KEY (address) REFERENCES buildings(addr) ON UPDATE CASCADE ON DELETE SET NULL, ); Get the same result by altering the table definition after creating it ALTER TABLE person ADD CONSTRAINT person_fk FOREIGN KEY (address) REFERENCES buildings(addr) ON UPDATE CASCADE ON DELETE SET NULL; address 9
Toolbox: constraints CREATE TABLE person( CONSTRAINT check_age CHECK (age>18), ); Check out operators for non-trivial types (look at the manual) Network addresses, geometric, date, CREATE TABLE person( name varchar(20), ageinteger, address varchar(30), id serial, primary key (id) ); 10
Translating relationships Entity A rel Entity B att4 att3 att5 att1 att2 In general (for any kind of relationship) Take primary keys of entities and build the primary key of the relationship Additional attributes for the relationship s attributes CREATE TABLE rel( rel_att3 <type> REFERENCES entitya(att3), rel_att2 <type> REFERENCES entityb(att2), att5 <type>, PRIMARY KEY (rel_att3,rel_att2) ); 11
Translating 1-to-many relationships Entity A rel Entity B att4 att3 att5 att1 att2 In case of 1-to-many relationships A tuple of Entity B ( many side ) can uniquely identify the corresponding tuple of Entity A ( 1 side ) son #1 mother son #2 son #3 12
Translating 1-to-many relationships Entity A rel Entity B att4 att3 att5 att1 att2 In case of 1-to-many relationships A tuple of Entity B ( many side ) can uniquely identify the corresponding tuple of Entity A ( 1 side ) CREATE TABLE entityb( att1 <type>, att2 <type> PRIMARY KEY, pippo <type> REFERENCES entitya(att3), att5 <type> ); pippo does not necessarily belong to the initial set of Entity B attributes it may be added just to translate the relationship att5 is added to EntityB only for the purpose of incorporating the attributes of the relationship 13
Translating 1-to-many relationships mother rel son id mother_id CREATE TABLE mother( id PRIMARY KEY, ); CREATE TABLE son(..., mother_id <type> REFERENCES mother(id),... ); 14
Translating 1-to-many relationships Entity A rel Entity B att4 att3 att5 att1 att2 With total participation (thick arrow) CREATE TABLE entityb( att1 <type>, att2 <type> PRIMARY KEY, pippo <type> NOT NULL REFERENCES entitya(att3), att5 <type> ); 15
Translating 1-to-many relationships mother rel son id mother_id CREATE TABLE mother( id PRIMARY KEY, ); CREATE TABLE son(..., mother_id <type> NOT NULL REFERENCES mother(id),... ); 14
Exercise: ER gw addr mac ip_addr admin net admin host id net topology host 1-to-many relationship Total participation of host How should topology be represented? Do we need a distinct table? 17
Cross foreign keys from page 72, slightly modified Every student has a main course Every course has a representative student CREATE TABLE students( stud_id INTEGER PRIMARY KEY, name VARCHAR(20), main_course INTEGER REFERENCES courses(c_id), ); CREATE TABLE courses( c_id INTEGER PRIMARY KEY, name VARCHAR(20), repr_stud INTEGER REFERENCES students(stud_id), ); Two problems PostrgeSQL does not allow to use unknown table ids How should one insert rows starting from an empty table? 18
Cross foreign keys Table definitions: use ALTER TABLE: CREATE TABLE students( stud_id INTEGER PRIMARY KEY, name VARCHAR(20), main_course INTEGER, ); CREATE TABLE courses( c_id INTEGER PRIMARY KEY, name VARCHAR(20), courses students stud_id repr_stud repr_stud INTEGER REFERENCES students(stud_id), ); 19
Cross foreign keys Table definitions: use ALTER TABLE: CREATE TABLE students( stud_id INTEGER PRIMARY KEY, name VARCHAR(20), main_course INTEGER, ); CREATE TABLE courses( c_id INTEGER PRIMARY KEY, name VARCHAR(20), courses students stud_id main_ course repr_stud INTEGER REFERENCES students(stud_id), ); ALTER TABLE students ADD CONSTRAINT stud_course FOREIGN KEY (main_course) REFERENCES courses(c_id); repr_stud c_id 20
Cross foreign keys By default each SQL statement is a transaction DBMS checks consistency at the end of each transaction INSERT INTO students VALUES (1, gianni,,15, ); is not allowed if course 15 does not already exist INSERT INTO courses VALUES (15, dbms,,1, ); is not allowed if student 1 does not already exist How to initialise the DB? Explicitly define the transaction boundaries: Begin; INSERT INTO students VALUES (1, gianni,,15, ); INSERT INTO courses VALUES (15, dbms,,1, ); commit; transaction 21
Cross foreign keys Explicitly define the transaction boundaries:" Begin; INSERT INTO students VALUES (1, gianni,,15, ); INSERT INTO courses VALUES (15, dbms,,1, ); commit; Force to check at commit time, with regard to the whole transaction (not with regard to the single statements) CREATE TABLE courses( repr_stud INTEGER REFERNCES students(stud_id) DEFERRABLE INITIALLY DEFERRED, ); ALTER TABLE students ADD CONSTRAINT foreign key (main_course) REFERENCES courses(c_id) DEFERRABLE INITIALLY DEFERRED;" 22