SQL Intro
Immanuel Trummer
[email protected]
www.itrummer.org
Database Management
Systems (DBMS)
Application 1 Connections, Security, Utilities, ...
Application 2
DBMS Interface Query Processor
Query Parser Query Rewriter
... Query Optimizer Query Executor
Storage Manager
Data Access Buffer Manager
Transaction Manager Recovery Manager
[RG, Sec. 3, 5]
Slides by Immanuel Trummer, Cornell University
Data
The SQL Language
• Used to issue commands to the DBMS
• SQL = Structured Query Language
• The SQL standard is around since the 70s
• Lots of features, we only see a subset
Slides by Immanuel Trummer, Cornell University
SQL Command Types
• DDL: Data Definition Language
• Define admissible database content (schema)
• DML: Data Manipulation Language
• Change and retrieve database content
• TCL: Transaction Control Language
• Groups SQL commands (transactions)
• DCL: Data Control Language
• Assign data access rights
Slides by Immanuel Trummer, Cornell University
SQL Command Types
• DDL: Data Definition Language
Now
• Define admissible database content (schema)
• DML: Data Manipulation Language
Next
• Change and retrieve database content
• TCL: Transaction Control Language
Later
• Groups SQL commands (transactions)
• DCL: Data Control Language
-
• Assign data access rights
Slides by Immanuel Trummer, Cornell University
Defining Database Schema
• Define relations with their schemata
• What columns and column types?
• Define constraints restricting admissible content
• Constraints on single relations
• Constraints linking multiple relations
Slides by Immanuel Trummer, Cornell University
Schema Definition in SQL
• CREATE TABLE <table> (<table-def>)
• <table> is the table name
• <table-def> is comma-separated column definitions
• Column definition is of form <col-name> <col-type>
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
Students
Sid Sname Gpa
CREATE TABLE Students(Sid int, Sname text, Gpa real);
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
Students Enrollment
Sid Sname Gpa Sid Cid
CREATE TABLE Enrollment(Sid int, Cid int);
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
Students Enrollment Courses
Sid Sname Gpa Sid Cid Cid Cname
CREATE TABLE Courses(Cid int, Cname text);
Slides by Immanuel Trummer, Cornell University
What Constraints Do
We Want?
Slides by Immanuel Trummer, Cornell University
Integrity Constraints
• Constraints that limit admissible content of tables
• DBMS enforces integrity constraints
• Can be added to tables via "ALTER TABLE" command
• Alternatively, can define when creating table
Slides by Immanuel Trummer, Cornell University
Primary Key Constraint
• A primary key constraint refers to a single table
• It identifies a subset of columns as key columns
• Fixing values for key columns must identify row
• No two rows have same values in key columns
Slides by Immanuel Trummer, Cornell University
Primary Key Syntax
• ALTER TABLE <table>
ADD CONSTRAINT Primary Key (<key-cols>);
• <table> is the table name
• <key-cols> is comma-separated list of column names
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students Enrollment Courses
PK
Sid Sname Gpa Sid Cid Cid Cname
ALTER TABLE Students ADD PRIMARY KEY(Sid);
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students e y
Enrollment ey Courses
PK PK PK
Sid Sname Gpa Sid Cid Cid Cname
ALTER TABLE Enrollment ADD PRIMARY KEY(Sid, Cid);
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students e y
Enrollment ey e y Courses
PK PK PK PK
Sid Sname Gpa Sid Cid Cid Cname
ALTER TABLE Courses ADD PRIMARY KEY(Cid);
Slides by Immanuel Trummer, Cornell University
Foreign Key Constraint
• A foreign key constraint links two tables
• Identifies set of foreign key columns in table 1
• Maps foreign key columns to primary key of table 2
• Values in foreign key column must appear as primary key
• Maps each row in table 1 to a row from table 2
Slides by Immanuel Trummer, Cornell University
Foreign Key Syntax
• ALTER TABLE <table-1>
ADD Foreign Key (<fkey-columns>)
REFERENCES <table-2> (<pkey-columns>);
• <table-1> is table with foreign key columns
• <fkey-column> is comma-separated foreign key columns
• <table-2> is table with primary key columns
• <pkey-columns> is comma-separated primary keys
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students e y
Enrollment ey e y Courses
PK PK PK PK
Sid Sname Gpa FKe Sid Cid Cid Cname
y
ALTER TABLE Enrollment
ADD FOREIGN KEY(Sid) REFERENCES Students(Sid);
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students e y
Enrollment ey e y Courses
PK PK PK PK
Sid Sname Gpa FKe Sid Cid
FKe Cid Cname
y y
ALTER TABLE Enrollment
ADD FOREIGN KEY(Cid) REFERENCES Courses(Cid);
Slides by Immanuel Trummer, Cornell University
Exercise (5 Minutes)
• Zamona wants to start selling books via a Web shop
• Create a database for Zamona for information on books
• Each book has a unique integer ID and a book title
• Writers have a unique name
• Writers are the authors of books
• Define a database with three tables for this scenario
Slides by Immanuel Trummer, Cornell University
Working With Data (DML)
• Can insert data into a table
• Can delete data from a table
• Can update data in a table
• Can analyze data in a table
Slides by Immanuel Trummer, Cornell University
Inserting Data
• Inserting one (fully specified) row into a table:
• INSERT INTO <table> VALUES (<value-list>)
• Inserting one (partially specified) row into a table:
• INSERT INTO <table> (<column-list>)
VALUES (<value-list>)
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students e y
Enrollment ey e y Courses
PK PK PK PK
Sid Sname Gpa FKe Sid Cid
FKe Cid Cname
y y
3 Alice 4.0
INSERT INTO Students VALUES (3, 'Alice', 4.0)
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students e y
Enrollment ey e y Courses
PK PK PK PK
Sid Sname Gpa FKe Sid Cid
FKe Cid Cname
y y
3 Alice 4.0
5 Bob NULL
INSERT INTO Students (Sid, Sname) VALUES (5, 'Bob')
Slides by Immanuel Trummer, Cornell University
Inserting Data From Files
• Loading data from a file into a table:
• COPY <table> FROM <path>
DELIMITER <delimiter> NULL <null-string> CSV
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students e y
Enrollment ey e y Courses
PK PK PK PK
Sid Sname Gpa FKe Sid Cid
FKe Cid Cname
y y
3 Alice 4.0 2 CS4320
5 Bob NULL 5 CS4321
9 CS6320
12 CS7390
COPY Courses FROM 'courses.csv' DELIMITER ',' CSV
Slides by Immanuel Trummer, Cornell University
Deleting Data
• Deleting rows from a table that satisfy condition:
• DELETE FROM <table> WHERE <condition>
• <condition> specifies Boolean predicate
• E.g., (in)equalities between columns
• Will discuss conditions in detail later
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students e y
Enrollment ey e y Courses
PK PK PK PK
Sid Sname Gpa FKe Sid Cid
FKe Cid Cname
y y
3 Alice 4.0 2 CS4320
5 Bob NULL 5 CS4321
9 CS6320
12 CS7390
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students e y
Enrollment ey e y Courses
PK PK PK PK
Sid Sname Gpa FKe Sid Cid
FKe Cid Cname
y y
3 Alice 4.0 2 CS4320
5 Bob NULL 5 CS4321
12 CS7390
DELETE FROM Courses WHERE Cname = 'CS6320'
Slides by Immanuel Trummer, Cornell University
Updating Data
• Updating specific rows and columns to new value:
• UPDATE <table>
SET <column> = <value>
WHERE <condition>
• Changes rows satisfying <condition>
by writing <value> in <column>
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students e y
Enrollment ey e y Courses
PK PK PK PK
Sid Sname Gpa FKe Sid Cid
FKe Cid Cname
y y
3 Alice 4.0 2 CS4320
5 Bob NULL 5 CS4321
12 CS7390
Slides by Immanuel Trummer, Cornell University
Defining Example Schema
e y Students e y
Enrollment ey e y Courses
PK PK PK PK
Sid Sname Gpa FKe Sid Cid
FKe Cid Cname
y y
3 Alice 4.0 7 CS4320
5 Bob NULL 5 CS4321
12 CS7390
UPDATE Courses SET Cid = 7 WHERE Cname = 'CS4320'
Slides by Immanuel Trummer, Cornell University
Exercise
• Try inserting books and authors into the database
• Try deleting and updating them as well
• Verify that updates violating constraints are rejected
Slides by Immanuel Trummer, Cornell University