Information Systems for Engineers Fall Data Definition with SQL

Similar documents
Ghislain Fourny. Information Systems for Engineers Fall Outlook

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

Ghislain Fourny. Big Data 2. Lessons learnt from the past

Big Data for Engineers Spring Data Models

Basic SQL. Basic SQL. Basic SQL

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

SQL Data Definition Language: Create and Change the Database Ray Lockwood

SQL OVERVIEW. CS121: Relational Databases Fall 2017 Lecture 4

Programming and Database Fundamentals for Data Scientists

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

SQL Data Definition and Data Manipulation Languages (DDL and DML)

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Full file at

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

INFORMATION TECHNOLOGY NOTES

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Lab # 4. Data Definition Language (DDL)

user specifies what is wanted, not how to find it

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Constraints. Primary Key Foreign Key General table constraints Domain constraints Assertions Triggers. John Edgar 2

SQL DATA DEFINITION LANGUAGE

Database Management Systems,

SQL: Data Definition Language

CSCC43H: Introduction to Databases. Lecture 4

Relational Model. CSE462 Database Concepts. Demian Lessa. Department of Computer Science and Engineering State University of New York, Buffalo

Introduction to SQL on GRAHAM ED ARMSTRONG SHARCNET AUGUST 2018

SQL DATA DEFINITION LANGUAGE

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

MTAT Introduction to Databases

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

Database Programming with PL/SQL

The Relational Model of Data (ii)

Principles of Data Management

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

COMP 430 Intro. to Database Systems

CS143: Relational Model

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

How to define a relational schema for a data base?

From theory to practice. Designing Tables for a postgresql Database System. psql. Reminder. A few useful commands

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

1) Introduction to SQL

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

CS634 Architecture of Database Systems Spring Elizabeth (Betty) O Neil University of Massachusetts at Boston

Database Systems CSE 303. Lecture 02

SQL DATA DEFINITION LANGUAGE

Database Systems CSE 303. Lecture 02

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

IBM DB2 UDB V7.1 Family Fundamentals.

Lecture 5: SQL s Data Definition Language

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

SQL Data Definition Language

The Relational Model

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

An Introduction to Structured Query Language

An Introduction to Structured Query Language

Lecture 1: Relational Databases

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Exam code: Exam name: Database Fundamentals. Version 16.0

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

SQL (Structured Query Language)

An Introduction to Structured Query Language

The SQL data-definition language (DDL) allows defining :

Chapter 3: Introduction to SQL

2.9 Table Creation. CREATE TABLE TableName ( AttrName AttrType, AttrName AttrType,... )

SQL Commands & Mongo DB New Syllabus

Applied Databases. Sebastian Maneth. Lecture 7 Simple SQL Queries. University of Edinburgh - February 1 st, 2016

Lecture 04: SQL. Wednesday, October 4, 2006

Introduction to SQL Server 2005/2008 and Transact SQL

UFCEKG 20 2 : Data, Schemas and Applications

An Introduction to Structured Query Language

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

An Introduction to Structured Query Language

Slides by: Ms. Shree Jaswal

CSC 453 Database Technologies. Tanu Malik DePaul University

Lecture 04: SQL. Monday, April 2, 2007

Content: How to transform the relational model into a database schema DDL: create table students(studentnr int, name varchar);

From theory to practice. Designing Tables for an Oracle Database System. Sqlplus. Sqlplus. Technicalities. Add the following to your.

Big Data 11. Data Models

Advanced Constraints SQL. by Joe Celko copyright 2007

Databases. Jörg Endrullis. VU University Amsterdam

CMPT 354: Database System I. Lecture 2. Relational Model

GridDB Advanced Edition SQL reference

MySQL Creating a Database Lecture 3

Basic SQL. Dr Paolo Guagliardo. University of Edinburgh. Fall 2016

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

QQ Group

DATABASE TECHNOLOGY - 1MB025

Applied Databases. Sebastian Maneth. Lecture 7 Simple SQL Queries. University of Edinburgh - February 6 st, 2017

Lecture 2: Introduction to SQL

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

Transcription:

Ghislain Fourny Information Systems for Engineers Fall 2018 3. Data Definition with SQL Rare Book and Manuscript Library, Columbia University.

What does data look like? Relations 2

Reminder: relation 0 to many A B 3

Reminder: partial function 0 to 1 A B 4

Reminder: function exactly 1 A B 5

Reminder: the two alternate definitions Definition 1: A table is a relation over its attribute domains. Extension + Domain + a 0 Domain + a 3 Definition 2: A table is a set of partial functions mapping strings to values, and with identical support. Extension + S V s.t. t Extension +, support t = Attributes + 6

The two alternate definitions Definition 1: A table is a relation over its attribute domains. Extension + Domain + a 0 Domain + a 3 (Domain integrity is always satisfied by definition) Definition 2: A table is a set of partial functions mapping strings to values, and with identical support. (Domain integrity is defined separately) Extension + S V s.t. t Extension +, support t = Attributes + 7

The two alternate definitions Definition 1: A table is a relation over its attribute domains. Extension + Domain + a 0 Domain + a 3 Very popular in database books, but imprecise regarding attribute (non-)order Definition 2: A table is a set of partial functions mapping strings to values, and with identical support. Extension + S V s.t. t Extension +, support t = Attributes + More rigorous, and pedagogically nice to separate definition from domain integrity 8

Reminder: definition with partial functions A relation R is made of 1. Attributes Attributes + Attributes + S 9

Reminder: definition with partial functions A relation R is made of 1. Attributes Attributes + Attributes + S 2. An extension (set of tuples) Extension + Extension + S V such that: t Extension +, support t = Attributes + 10

Domain integrity Given a relation R and a domain constraint Domain + Domain integrity is fulfilled if Attributes + Domain + Extension + a d t Extension +, a Attributes + In other words: t. a Domain + (a) Tuples must have their values in the domains associated with their attributes. t t.a d 11

Reminder: definition with relations A relation R is made of Schema +, Domain +, Extension + as follows: Attributes + 1. a set of schema attributes, ordered for convenience (arbitrarily) a 0, a L,..., a 3 12

Reminder: definition with relations A relation R is made of Schema +, Domain +, Extension + as follows: 1. a set of schema attributes, ordered for convenience (arbitrarily) Attributes + Domain + a 0, a L,..., a 3 2. a family of domains associated with these attributes (the types) a Domain + (a) 13

Reminder: definition with relations A relation R is made of Schema +, Domain +, Extension + as follows: 1. a set of schema attributes, ordered for convenience (arbitrarily) a 0, a L,..., a 3 Attributes + Domain + Extension + 2. a family of domains associated with these attributes (the types) a Domain + (a) 3. a set of tuples (the extension) Extension + Domain + a 0 Domain + a 3 Domain integrity holds by definition. Instead of partial function 14

In practice Customer ID Security ID Quantity Integer String Integer 1 CH00000000000 135 2 CH00000000000 3 3 CH00000000000 87 4 CH00000000000 2 5 CH00000000000 7 6 CH00000000000 4 This is how database people think. 15

SELECT name, mayor WHERE country = "Switzerland" FROM cities SQL 16

SQL History Don Chamberlin Raymond Boyce 17

The early days (early 1970s) First commercial relational database System R + SEQUEL First commercial relational query language Almaden (San Jose) First customer (1977) 18

SEQUEL Structured English QUEry Language Declarative language Set-based (Manipulates entire relations with a single command) 19

Renaming SEQUEL (Trademark issue) SQL ESS-kew-EL or SEE-kwəl 20

Domain types 21

Data types Customer ID Security ID Quantity Integer String Integer 1 CH00000000000 135 2 CH00000000000 3 3 CH00000000000 87 4 CH00000000000 2 5 CH00000000000 7 6 CH00000000000 4 22

Kinds of domains 23

Kinds of domains Strings Booleans Numbers Date & Time Enumeration Binary Duration 24

Strings "foo" "Zurich" "Ilsebill salzte nach." 25

Strings "foo" "Zurich" "Ilsebill salzte nach." f o o Z u r i c h I l s e b i l l s a l z t e n a c h. 26

Strings Fixed length 27

Strings Fixed length Example: Swiss cantons ZH AG ZG GE BE NE AI AR... 28

Strings Fixed length Variable length with limit 29

Strings Fixed length Variable length with limit Example: international licence plate country code F D GB CH EIR 30

Strings Fixed length Variable length with limit Variable length without limit 31

Strings Fixed length Variable length with limit Variable length without limit Example: the content of a webpage on a blog 32

Strings Fixed length character(10) Variable length with limit Variable length without limit 33

Strings Fixed length character(10) char(10) char Variable length with limit Variable length without limit 34

Strings Fixed length Variable length with limit character(10) char(10) char character varying(10) Variable length without limit 35

Strings Fixed length Variable length with limit character(10) char(10) char character varying(10) varchar(10) Variable length without limit 36

Strings Fixed length Variable length with limit Variable length without limit character(10) char(10) char character varying(10) varchar(10) clob 37

Strings Fixed length Variable length with limit Variable length without limit character(10) char(10) char character varying(10) varchar(10) clob text Non-standard varchar Non-standard 38

String types: Behavior A B C char(3) varchar(5) text 39

String types: Behavior A B C char(3) varchar(5) text 40

String types: Behavior f A B C char(3) varchar(5) text 41

String types: Behavior f A B C char(3) varchar(5) text f f f 42

String types: Behavior f foo A B C char(3) varchar(5) text f f f 43

String types: Behavior f foo A B C char(3) varchar(5) text f f f foo foo foo 44

String types: Behavior f foo foobar A B C char(3) varchar(5) text f f f foo foo foo 45

String types: Behavior f foo foobar A B C char(3) varchar(5) text f f f foo foo foo foobar 46

String types: Behavior f foo foobar fo A B C char(3) varchar(5) text f f f foo foo foo foobar 47

String types: Behavior f foo foobar fo A B C char(3) varchar(5) text f f f foo foo foo foobar fo fo fo 48

String types: Design choices text is not standard Favor varchar(n) char(n) eats up space 49

Numbers: Integer types 50

Numbers: Integer types Mathematically: Z (A ring with addition and multiplication) 51

Lexical space vs. value space Value space Lexical space 52

Lexical space vs. value space "1" "01"... Value space Lexical space 53

Lexical space vs. value space "4" "04" "100b"... Value space Lexical space 54

If you program in Java... 55

If you program in Java... byte short int long 56

If you program in Java... byte short int long 8 bits 16 bits 32 bits 64 bits 57

Numbers: Integer types Small integers -32768 to +32767 (16 bits) 58

Numbers: Integer types Small integers -32768 to +32767 Integers -2147483648 to +2147483647 (16 bits) (32 bits) 59

Numbers: Integer types Small integers -32768 to +32767 Integers -2147483648 to +2147483647 Big Integers -9223372036854775808 to +9223372036854775807 (16 bits) (32 bits) (64 bits) 60

Numbers: Integer types Small integers -32768 to +32767 Integers -2147483648 to +2147483647 Big Integers -9223372036854775808 to +9223372036854775807 smallint integer int bigint 61

Binary numeral system Decimal Binary 0 0 2 10 5 101 11 1011 62

Binary numeral system 3S0 number = O d Q. 2 Q QTU = d 3S0 d L d 0 d U Decimal Binary 0 0 2 10 5 101 11 1011 63

Numbers: Arbitrary precision 3141592653.5897932384626433832795 64

Numbers: Arbitrary precision 3141592653.5897932384626433832795 Precision 32 digits 65

Numbers: Arbitrary precision 3141592653.5897932384626433832795 Scale 22 digits 66

Numbers: Arbitrary precision types Any precision and scale numeric decimal 3141592653.5897932384626433832795 67

Numbers: Arbitrary precision types Any precision and scale Limit precision, any scale numeric decimal numeric(32) decimal(32) 68

Numbers: Arbitrary precision types Any precision and scale Limit precision, any scale numeric decimal numeric(32) decimal(32) Example: disk capacity 1.024 69

Numbers: Arbitrary precision types Any precision and scale Limit precision, any scale Limit precision and scale numeric decimal numeric(32) decimal(32) numeric(32,22) decimal(32,22) 70

Numbers: Arbitrary precision types Any precision and scale Limit precision, any scale numeric decimal numeric(32) decimal(32) Limit precision and scale Example: amount of money 314159.58 numeric(32,22) decimal(32,22) 71

Arbirary precision types: Behavior 0 A B C numeric numeric(3) numeric(3,1) 72

Arbirary precision types: Behavior 0 A B C numeric numeric(3) numeric(3,1) 0 0 0 73

Arbirary precision types: Behavior 0 000003 A B C numeric numeric(3) numeric(3,1) 0 0 0 74

Arbirary precision types: Behavior 0 000003 A B C numeric numeric(3) numeric(3,1) 0 0 0 3 3 3 75

Arbirary precision types: Behavior 0 000003 3.10000 A B C numeric numeric(3) numeric(3,1) 0 0 0 3 3 3 76

Arbirary precision types: Behavior 0 000003 3.10000 A B C numeric numeric(3) numeric(3,1) 0 0 0 3 3 3 3.1 3 3.1 77

Arbirary precision types: Behavior 0 000003 3.10000 3.14 A B C numeric numeric(3) numeric(3,1) 0 0 0 3 3 3 3.1 3 3.1 78

Arbirary precision types: Behavior 0 000003 3.10000 3.14 A B C numeric numeric(3) numeric(3,1) 0 0 0 3 3 3 3.1 3 3.1 3.14 3 3.1 79

Arbirary precision types: Behavior 0 000003 3.10000 3.14 3141.5 A B C numeric numeric(3) numeric(3,1) 0 0 0 3 3 3 3.1 3 3.1 3.14 3 3.1 80

Arbirary precision types: Behavior 0 000003 3.10000 3.14 3141.5 A B C numeric numeric(3) numeric(3,1) 0 0 0 3 3 3 3.1 3 3.1 3.14 3 3.1 3141.5 81

Numbers: Approximate (finite) precision 3141592653.5897932384626433832795 82

Numbers: Approximate (finite) precision 3141592653.5897932384626433832795 Base 2, canonical 1.4629180431365967... x 2 31 83

Numbers: Approximate (finite) precision 3141592653.5897932384626433832795 Base 2, canonical 1.4629180431365967... x 2 31 Encoding + 31 1.462918 01001111001110110100000011100110 84

Numbers: Approximate (finite) precision 3141592653.5897932384626433832795 Base 2, canonical 1.4629180431365967... x 2 31 Encoding + 31 1.462918 01001111001110110100000011100110 Read back 3141592576 85

IEEE 754 standard 32 bits ca. 7 digits 3141592000 10-37 to 10 37 single precision 86

IEEE 754 standard 32 bits 64 bits ca. 7 digits ca. 15 digits 3141592000 3141592653.58979 10-37 to 10 37 10-307 to 10 308 single precision double precision 87

IEEE 754 types Single precision real float(24) Double precision Arbitrary precision 88

IEEE 754 types Single precision real float(24) Double precision double precision float(53) Arbitrary precision 89

IEEE 754 types Single precision real float(24) Double precision double precision float(53) Arbitrary precision float(1)... float(24) float(25)... float(53) real double precision Non-standard 90

IEEE 754: special values - '-Infinity' 91

IEEE 754: special values - + '-Infinity' 'Infinity' 92

IEEE 754: special values - + NaN '-Infinity' 'Infinity' 'Nan'! Behavior differs from IEEE754: Equal to itself Higher than all other numbers 93

Booleans 94

Booleans TRUE FALSE 95

Booleans TRUE FALSE t true y yes Non-standard f false n no Non-standard on off 1 0 96

Booleans: three-valued logics? TRUE FALSE NULL t true y yes on Non-standard f false n no off Non-standard 1 0 97

Boolean type Boolean boolean 98

Dates and times Date 99

Dates and times Date Time 100

Dates and times Date Time Timestamp 101

Dates (Gregorian calendar) 102

Dates (Gregorian calendar) Year + Month + Day 2017 August 1st (AD) 103

Times 104

Times Hours + Minutes + Seconds 10 : 31 : 15.109378 Up to the µs (6 decimals) 105

Timestamps 106

Timestamps Year + Month + Day + Hours + Minutes + Seconds 2017 August 1 st 10 : 31 : 15.109378 (AD) 107

Dates and times: types Dates date 108

Dates and times: types Dates Times No time zone date time time(3) time without time zone time(5) without time zone 109

Dates and times: types Dates Times No time zone With time zone! [usage discouraged] date time time(3) time without time zone time(5) without time zone time with time zone time(1) with time zone 110

Dates and times: types Dates Times Timestamps No time zone! [usage discouraged] With time zone No time zone date time time(3) time without time zone time(5) without time zone time with time zone time(1) with time zone timestamp timestamp(6) timestamp without time zone timestamp(4) without time zone 111

Dates and times: types Dates Times Timestamps No time zone! [usage discouraged] With time zone No time zone With time zone date time time(3) time without time zone time(5) without time zone time with time zone time(1) with time zone timestamp timestamp(6) timestamp without time zone timestamp(4) without time zone timestamp with time zone timestamp(0) with time zone112

Durations 113

Duration kinds Year Month Day Hour Minute Second 114

Duration kinds Year Month Day Hour Minute Second Example: 2 years and 4 months 115

Duration kinds Year Month Day Hour Minute Second Example: 2 years and 4 months 116

Duration kinds Year Month Day Hour Minute Second Example: 2 years and 4 months Example: 3 hours and 14 minutes 117

Duration kinds Year Month Day Hour Minute Second Example: 2 years and 4 months Example: 3 hours and 14 minutes 118

"Duration wall": why not month to days? 2 months and 4 days 119

"Duration wall": why not month to days? 2 months and 4 days 2 months and 30 days 2 months and 31 days 3 months Ambiguity zone!! 120

Durations: types interval interval(3) Durations 121

Durations: types Durations interval interval(3) interval year interval year to month interval month (0) interval day (6) interval day to hour interval day to minute (2) interval day to second interval hour interval hour to minute (5) interval hour to second interval minute interval minute to second (4) interval second 122

Binaries 01010101001011010101011110100100101010010110010101101001010101001 01101010101111010010010101001011001010110100101010100101101010101 11101001001010100101100101011010010101010010110101010111101001001 01010010110010101101001010101001011010101011110100100101010010110 01010110100101010100101101010101111010010010101001011001010110100 10101010010110101010111101001001010100101100101011010010101010010 11010101011110100100101010010110010101101001010101001011010101011 11010010010101001011001010110100101010100101101010101111010010010 10100101100101011010010101010010110101010111101001001010100101100 10101101001010101001011010101011110100100101010010110010101101001 01010100101101010101111010010010101001011001010110100101010100101 10101010111101001001010100101100101011010010101010010110101010111 10100100101010010110010101101001010101001011010101011110100100101 01001011001010110100101010100101101010101111010010010101001011001 01011010010101010010110101010111101001001010100101100101011010010 10101001011010101011110100100101010010110010101101001010101001011 01010101111010010010101001011001010110100101010100110101011110100 123

Binary content 124

Binary content: types Fixed length binary(10) 125

Binary content: types Fixed length Variable length with limit binary(10) varbinary(100) 126

Binary content: types Fixed length Variable length with limit binary(10) varbinary(100) Variable length without limit blob bytea Non-standard 127

Summary of standardized SQL types Kind Types Character char(p) varchar(p) clob 128

Summary of standardized SQL types Kind Types Character Binary char(p) varchar(p) clob binary(p) varbinary(p) blob 129

Summary of standardized SQL types Kind Types Character Binary Number, exact char(p) varchar(p) clob binary(p) varbinary(p) blob numeric(p,s) decimal(p,s) smallint integer(p) bigint 130

Summary of standardized SQL types Kind Types Character Binary Number, exact Number, approximate char(p) varchar(p) clob binary(p) varbinary(p) blob numeric(p,s) decimal(p,s) smallint integer(p) bigint float(p) real double precision 131

Summary of standardized SQL types Kind Types Character Binary Number, exact Number, approximate Boolean char(p) varchar(p) clob binary(p) varbinary(p) blob numeric(p,s) decimal(p,s) smallint integer(p) bigint float(p) real double precision boolean 132

Summary of standardized SQL types Kind Types Character Binary Number, exact Number, approximate Boolean Date and time char(p) varchar(p) clob binary(p) varbinary(p) blob numeric(p,s) decimal(p,s) smallint integer(p) bigint float(p) real double precision boolean date time time with time zone timestamp timestamp with time zone 133

Summary of standardized SQL types Kind Types Character Binary Number, exact Number, approximate Boolean Date and time Intervals char(p) varchar(p) clob binary(p) varbinary(p) blob numeric(p,s) decimal(p,s) smallint integer(p) bigint float(p) real double precision boolean date time time with time zone timestamp timestamp with time zone interval year to month interval day to second 134

Creating and dropping tables Vitaly Korovin / 123RF Stock Photo 135

DDL on tables + Create a table 136

DDL on tables + Create a table * Modify a table 137

DDL on tables + Create a table * Modify a table - Drop a table 138

Relations (the math, for database scientists) A relation R is made of 1. Attributes Attributes + Attributes + S 2. An extension (set of tuples) Extension + Extension + S V such that: t Extension +, attributes t = Attributes + 139

Domain integrity Given a relation R and a domain constraint Domain + Domain integrity is fulfilled if Attributes + Domain + Extension + a d t Extension +, a Attributes + In other words: t. a Domain + (a) Tuples must have their values in the domains associated with their attributes. t t.a d 140

Creating a table 141

Creating a table + A new table has an empty extension 142

Creating a table + So we only specify: - a name - the attributes - the associated domains 143

Creating a table: hello, world! CREATE TABLE helloworldtable ( name text ); 144

Creating a table: hello, world! CREATE TABLE helloworldtable ( name text ); helloworldtable name text 145

Creating a table: more columns CREATE TABLE persons ( first_name varchar(30), middle_initial char(1), last_name text, birth_date date, gender boolean, passportscan bytea ); 146

Creating a table: more columns CREATE TABLE persons ( first_name varchar(30), middle_initial char(1), last_name text, birth_date date, gender boolean, passportscan bytea ); persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea 147

Table with no columns CREATE TABLE emptytable (); (because we can?) important for the theory 148

Table with no columns (yes) CREATE TABLE emptytable (); (because we can?) important for the theory emptytable 149

Dropping a table - We only specify: - the name 150

Dropping a table: hello, world! DROP TABLE helloworldtable; helloworldtable name text 151

Dropping a table: hello, world! DROP TABLE helloworldtable; helloworldtable name text 152

Dropping a non-existing table: error DROP TABLE tablethatdoesntexist; 153

Dropping a non-existing table: error DROP TABLE tablethatdoesntexist; 154

Silently avoiding errors upon dropping a table DROP TABLE tablethatmayormaynotexist IF EXISTS; 155

Populating tables Inserting and deleting tuples Vitaly Korovin / 123RF Stock Photo 156

DML on tuples Insert a tuple + 157

DML on tuples Insert a tuple + Update a tuple * 158

DML on tuples Insert a tuple + Update a tuple * Delete a tuple - 159

Inserting tuples helloworldtable name text INSERT INTO helloworldtable VALUES Einstein; 160

Inserting tuples helloworldtable name text INSERT INTO helloworldtable VALUES Einstein; helloworldtable name text Einstein 161

Inserting tuples: explicit version helloworldtable name text Einstein INSERT INTO helloworldtable (name) VALUES Gödel; 162

Inserting tuples: explicit version helloworldtable name text Einstein INSERT INTO helloworldtable (name) VALUES Gödel; helloworldtable name text Einstein Gödel 163

Inserting tuples: multiple rows persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea INSERT INTO persons VALUES ('James', 'T', 'Kirk', DATE '2233-03-22', TRUE, E'\\xAD10E7'), ('Beverly', 'C', 'Crusher', DATE '2324-10-13', FALSE, E'\\xAD234F7') ; 164

Inserting tuples: multiple rows persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea INSERT INTO persons VALUES ('James', 'T', 'Kirk', DATE '2233-03-22', TRUE, E'\\xAD10E7'), ('Beverly', 'C', 'Crusher', DATE '2324-10-13', FALSE, E'\\xAD234F7') ; persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Crusher 2324-10-13 FALSE AD234F7 165

Inserting tuples: omitting columns and NULL persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Crusher 2324-10-13 FALSE AD234F7 INSERT INTO persons (name, birth_date, gender, passport_scan) VALUES ('Spock', DATE '2230-01-06', TRUE, E'\\xAD234F7') ; 166

Inserting tuples: omitting columns and NULL persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Crusher 2324-10-13 FALSE AD234F7 INSERT INTO persons (name, birth_date, gender, passport_scan) VALUES ('Spock', DATE '2230-01-06', TRUE, E'\\xAD234F7') ; persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Crusher 2324-10-13 FALSE AD234F7 Spock NULL NULL 2230-01-06 TRUE AD234F7 167

NULL By default, domains all contain an additional value NULL 168

NULL: interpretations? Not known 169

NULL: interpretations? Not known Not applicable 170

NULL: interpretations? Not known Not applicable Secret 171

Allowing (or not) NULL in a domain CREATE TABLE persons ( first_name varchar(30), middle_initial char(1), last_name text, birth_date date, gender boolean, passportscan bytea ); 172

Allowing (or not) NULL in a domain not allowed CREATE TABLE persons ( first_name varchar(30) NOT NULL, middle_initial char(1), last_name text, birth_date date, gender boolean, passportscan bytea ); 173

Allowing (or not) NULL in a domain not allowed CREATE TABLE persons ( first_name varchar(30) NOT NULL, middle_initial char(1) NULL, last_name text, birth_date date, allowed (default) gender boolean, passportscan bytea ); 174

NULL: best practice CREATE TABLE persons ( first_name varchar(30) NOT NULL, middle_initial char(1) NULL, last_name text NULL, birth_date date NOT NULL, gender boolean NOT NULL, passportscan bytea NOT NULL ); Mark as many columns as possible as not null. 175

Updating tuples persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Crusher 2324-10-13 FALSE AD234F7 Spock NULL NULL 2230-01-06 TRUE AD234F7 UPDATE persons SET last_name = 'Picard' WHERE last_name = 'Crusher'; 176

Updating tuples persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Crusher 2324-10-13 FALSE AD234F7 Spock NULL NULL 2230-01-06 TRUE AD234F7 UPDATE persons SET last_name = 'Picard' WHERE last_name = 'Crusher'; persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Picard 2324-10-13 FALSE AD234F7 Spock NULL NULL 2230-01-06 TRUE AD234F7 177

Removing tuples persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Picard 2324-10-13 FALSE AD234F7 Spock NULL NULL 2230-01-06 TRUE AD234F7 DELETE FROM persons WHERE name = 'Spock'; 178

Removing tuples persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Picard 2324-10-13 FALSE AD234F7 Spock NULL NULL 2230-01-06 TRUE AD234F7 DELETE FROM persons WHERE name = 'Spock'; persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Picard 2324-10-13 FALSE AD234F7 179

Default values CREATE TABLE persons ( first_name varchar(30), middle_initial char(1), last_name text, birth_date date, gender boolean, passportscan bytea ); 180

Default values CREATE TABLE persons ( first_name varchar(30), middle_initial char(1) DEFAULT 'N', last_name text DEFAULT 'NONE', birth_date date, gender boolean, passportscan bytea ); 181

Default values: inserting tuples persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Crusher 2324-10-13 FALSE AD234F7 INSERT INTO persons (name, birth_date, gender, passport_scan) VALUES ('Spock', DATE '2230-01-06', TRUE, E'\\xAD234F7') ; 182

Default values: inserting tuples persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Crusher 2324-10-13 FALSE AD234F7 INSERT INTO persons (name, birth_date, gender, passport_scan) VALUES ('Spock', DATE '2230-01-06', TRUE, E'\\xAD234F7') ; persons name middle_initial last_name birth_date gender passport_scan varchar(30) char(1) text date boolean bytea James T Kirk 2233-03-22 TRUE AD10E7 Beverly C Crusher 2324-10-13 FALSE AD234F7 Spock N NONE 2230-01-06 TRUE AD234F7 183

"Default default values": NULL CREATE TABLE persons ( first_name varchar(30) DEFAULT NULL, middle_initial char(1) DEFAULT 'N', last_name text DEFAULT 'NONE', birth_date date DEFAULT NULL, gender boolean DEFAULT NULL, passportscan bytea DEFAULT NULL ); 184

Modify tables Milosh Kojadinovich / 123RF Stock Photo 185

DDL on tables + Create a table 186

DDL on tables + Create a table * Modify a table 187

DDL on tables + Create a table * Modify a table - Drop a table 188

Add a column ALTER TABLE helloworldtable ADD COLUMN flag boolean; helloworldtable name text 189

Add a column ALTER TABLE helloworldtable ADD COLUMN flag boolean; helloworldtable name text helloworldtable name flag text boolean 190

Add a column: default values, null ALTER TABLE helloworldtable ADD COLUMN flag2 boolean not null default true; helloworldtable name flag text boolean 191

Add a column: default values, null ALTER TABLE helloworldtable ADD COLUMN flag2 boolean not null default true; helloworldtable name flag text boolean helloworldtable name flag flag2 text boolean boolean 192

Rename a column ALTER TABLE helloworldtable RENAME COLUMN flag TO f; helloworldtable name flag text boolean 193

Rename a column ALTER TABLE helloworldtable RENAME COLUMN flag TO f; helloworldtable name flag text boolean helloworldtable name f text boolean 194

Remove a column ALTER TABLE helloworldtable DROP COLUMN f; helloworldtable name f text boolean 195

Remove a column ALTER TABLE helloworldtable DROP COLUMN f; helloworldtable name f text boolean helloworldtable name text 196

Rename a table ALTER TABLE helloworldtable RENAME TO table; helloworldtable name text 197

Rename a table ALTER TABLE helloworldtable RENAME TO table; helloworldtable name text table name text 198

Consistency constraints Milosh Kojadinovich / 123RF Stock Photo 199

Consistency constraints There are three fundamental consistency constraints: Not null Check Unique 200

Not null (already covered) CREATE TABLE persons ( name text NOT NULL, student boolean ); 201

Not null (already covered) not allowed CREATE TABLE persons ( name text NOT NULL, student boolean ); allowed (default) 202

Not null (already covered) not allowed CREATE TABLE persons ( name text NOT NULL, student boolean ); allowed (default) persons name text foobar NULL student boolean NULL TRUE 203

Check CREATE TABLE persons ( name text check (foo <> 'Spock'), student boolean ); 204

Check CREATE TABLE persons ( name text check (foo <> 'Spock'), student boolean ); persons name text foobar Spock student boolean NULL TRUE 205

Unique CREATE TABLE persons ( name text UNIQUE, student boolean, ahv varchar(13) UNIQUE, ); 206

Unique CREATE TABLE persons ( name text UNIQUE, student boolean, ahv varchar(13) UNIQUE, ); persons name student ahv text boolean varchar(13) foobar NULL 1234567890123 Spock TRUE 1234567890124 Spock TRUE 1234567890125 207

Primary keys Sergey Nivens / 123RF Stock Photo 208

Primary key persons ahv varchar(13) name text 1234567890123 Albert Einstein 1234567890124 Kurt Gödel 1234567890125 Alan Turing 209

Primary key persons ahv varchar(13) name text 1234567890123 Albert Einstein 1234567890124 Kurt Gödel 1234567890125 Alan Turing A primary key uniquely identifies a row 210

Primary key persons ahv varchar(13) name text 1234567890123 Albert Einstein 1234567890124 Kurt Gödel NULL Alan Turing Can't be NULL 211

Primary keys persons ahv varchar(13) name text 1234567890123 Albert Einstein 1234567890124 Kurt Gödel NULL Alan Turing Must be unique 212

Primary key: first idea CREATE TABLE persons ( ahv varchar(13) UNIQUE NOT NULL, name text ); 213

Primary key: first idea CREATE TABLE persons ( ahv varchar(13) UNIQUE NOT NULL, name text ); persons ahv varchar(13) name text 1234567890123 Albert Einstein 1234567890124 Kurt Gödel 1234567890125 Alan Turing 214

Primary key: there is more to it persons ahv varchar(13) name text 1234567890123 Albert Einstein 1234567890124 Kurt Gödel 1234567890125 Alan Turing 1234567890126 Niklaus Wirth 1234567890127 Isaac Newton 1234567890128 Srinivasa Ramanujan 1234567890129 Pythagoras 1234567890130 Thales 1234567890131 Alonso Church 215

Primary key: there is more to it persons ahv varchar(13) name text 1234567890123 Albert Einstein 1234567890124 Kurt Gödel We want to quickly retrieve a row with its key 1234567890125 Alan Turing 1234567890126 Niklaus Wirth 1234567890127 Isaac Newton 1234567890128 Srinivasa Ramanujan 1234567890129 Pythagoras 1234567890130 Thales 1234567890131 Alonso Church (We won't say how just yet) 216

Primary key: syntax CREATE TABLE persons ( ahv varchar(13) PRIMARY KEY, name text ); persons ahv varchar(13) name text 1234567890123 Albert Einstein 1234567890124 Kurt Gödel 1234567890125 Alan Turing 217

Primary key: alternate syntax CREATE TABLE persons ( ahv varchar(13), name text, PRIMARY KEY(ahv) ); persons ahv varchar(13) name text 1234567890123 Albert Einstein 1234567890124 Kurt Gödel 1234567890125 Alan Turing 218

Primary key: compound keys CREATE TABLE persons ( first text, last text, city text, PRIMARY KEY(first, last) ); 219

Primary key: compound keys CREATE TABLE persons ( first text, last text, city text, PRIMARY KEY(first, last) ); persons first last city text text text Albert Einstein Zurich Kurt Gödel Berlin Kurt Turing London 220

Foreign key taxes ahv varchar(13) fiscal residence char(2) 1234567890123 CH 1234567890124 AT 1234567890126 GB 221

Foreign key taxes ahv varchar(13) fiscal residence char(2) 1234567890123 CH 1234567890124 AT 1234567890126 GB persons ahv varchar(13) name text 1234567890123 Albert Einstein 1234567890124 Kurt Gödel 1234567890125 Alan Turing 222

Foreign key taxes ahv varchar(13) fiscal residence char(2) 1234567890123 CH 1234567890124 AT 1234567890126 GB CREATE TABLE taxes ( ahv varchar(13) REFERENCES persons (ahv), fiscal residence char(2) ); Can be any column (not necessary primary key, but must be UNIQUE) 223

Foreign key: no column specified means primary key taxes ahv varchar(13) fiscal residence char(2) 1234567890123 CH 1234567890124 AT 1234567890126 GB CREATE TABLE persons ( ahv varchar(13) REFERENCES persons, fiscal residence char(2) ); 224

Foreign key: duplicate references allowed taxes ahv varchar(13) fiscal residence char(2) 1234567890123 CH 1234567890124 AT 1234567890126 GB 1234567890126 US CREATE TABLE persons ( ahv varchar(13) REFERENCES persons, fiscal residence char(2) ); 225

Foreign key: compound keys persons first last city text text text taxes first last course text text text Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London Albert Einstein Relativity Kurt Gödel Completeness Srinivasa Ramanujan Taxi numbers Srinivasa Ramanujan Formulas 226

Foreign key: compound keys persons first last city text text text taxes first last course text text text Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London Albert Einstein Relativity Kurt Gödel Completeness Srinivasa Ramanujan Taxi numbers Srinivasa Ramanujan Formulas CREATE TABLE persons ( first text, last text, course text, FOREIGN KEY (first, last) REFERENCES persons (first, last) ); 227

Foreign key: NULL references are allowed persons first last city text text text taxes first last course text text text Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London Albert Einstein Relativity Kurt Gödel Completeness Srinivasa Ramanujan Taxi numbers NULL NULL Formulas CREATE TABLE persons ( first text, last text, course text, FOREIGN KEY (first, last) REFERENCES persons (first, last) ); 228

Deletion of referenced values persons first last city text text text DELETE Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London 229

Deletion of referenced values persons first last city text text text DELETE Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London taxes first last course text text text Albert Einstein Relativity Kurt Gödel Completeness Srinivasa Ramanujan Taxi numbers Srinivasa Ramanujan Formulas What do we do here? 230

Deletion: cascading FOREIGN KEY (first, last) REFERENCES persons (first, last) ON DELETE CASCADE persons first last city text text text Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London 231

Deletion: cascading FOREIGN KEY (first, last) REFERENCES persons (first, last) ON DELETE CASCADE persons first last city text text text Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London taxes first last course text text text Albert Einstein Relativity Kurt Gödel Completeness Srinivasa Ramanujan Taxi numbers Srinivasa Ramanujan Formulas 232

Deletion: restricting FOREIGN KEY (first, last) REFERENCES persons (first, last) ON DELETE RESTRICT persons first last city text text text Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London 233

Deletion: restricting FOREIGN KEY (first, last) REFERENCES persons (first, last) ON DELETE RESTRICT "I'm sorry, Dave. I'm afraid I can't do that." persons first last city text text text Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London taxes first last course text text text Albert Einstein Relativity Kurt Gödel Completeness Srinivasa Ramanujan Taxi numbers Srinivasa Ramanujan Formulas 234

Deletion: set to NULL FOREIGN KEY (first, last) REFERENCES persons (first, last) ON DELETE SET NULL persons first last city text text text Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London 235

Deletion: set to NULL FOREIGN KEY (first, last) REFERENCES persons (first, last) ON DELETE SET NULL persons first last city text text text Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London taxes first last course text text text Albert Einstein Relativity NULL NULL Completeness Srinivasa Ramanujan Taxi numbers Srinivasa Ramanujan Formulas 236

Deletion: set to default value FOREIGN KEY (first, last) REFERENCES persons (first, last) ON DELETE SET DEFAULT persons first last city text text text Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London 237

Deletion: set to default value FOREIGN KEY (first, last) REFERENCES persons (first, last) ON DELETE SET DEFAULT persons first last city text text text Albert Einstein Zurich Kurt Gödel Berlin Srinivasa Ramanujan London taxes first last course text text text Albert Einstein Relativity Albert Einstein Completeness Srinivasa Ramanujan Taxi numbers Srinivasa Ramanujan Formulas 238