Lecture 5 SQL Part 1
Lecture 5 SQL Part 1
Announcements
• If you are enrolled to the class, but have not seen the
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
1
International University, VNU-HCMC
Recap: Lecture 2
• Why use a DBMS?
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
database managements
– and is the main focus of this course
• Semi-structured model/XML is also used in
practice – you will use them in hw/
assignments
• Unstructured data (text/photo/video) is
unavoidable, but won’t be covered in this
class
2
International University, VNU-HCMC
Today’s topic
• SQL basic:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
ü Create database
ü Create tables, constraints, import data
– Reading material: [RG] Chapters 3 and 5
– Additional reading for practice: [GUW]
Chapter 6
Acknowledgement
• The following slides have been created adapting the
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
3
International University, VNU-HCMC
What is SQL?
https://www.tutorialspoint.com
4
International University, VNU-HCMC
– SQL-86
– SQL-89 (minor revision)
– SQL-92 (major revision)
– SQL-99 (major extensions, current
standard)
– More: MS SQL Server history on the
internet
Purposes of SQL
• Data Manipulation Language (DML)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
– Querying: SELECT-FROM-WHERE
– Modifying: INSERT/DELETE/UPDATE
5
International University, VNU-HCMC
6
International University, VNU-HCMC
Relational Model
column/
attribute/
field
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
SQL (“sequel”)
• Standard query language for relational data
– used for databases in many different contexts
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
7
International University, VNU-HCMC
8
International University, VNU-HCMC
9
International University, VNU-HCMC
10
International University, VNU-HCMC
11
International University, VNU-HCMC
Demo on SQLite
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
SQL statements
• Create database …
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
• create table …
• drop table ...
• alter table ... add/remove ...
• insert into ... values ...
• delete from ... where ...
• update ... set ... where ...
12
International University, VNU-HCMC
SQL statements
Syntax:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
USE DatabaseName:
USE testDB;
all attributes
CREATING TABLE
The basic syntax of the CREATE TABLE statement is as
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
follows:
13
International University, VNU-HCMC
Creating Relation/Table
§ Creates the “Students” relation CREATE TABLE Students
(sid CHAR(10),
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
);all attributes
14
International University, VNU-HCMC
Destroying Relation/table
Syntax: Drop table Table_name
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Altering Relation/Table
ALTER TABLE Students
ADD COLUMN firstYear: integer
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
15
International University, VNU-HCMC
16
International University, VNU-HCMC
UPDATE Student
SET age = age + 2
where sid = ‘53680';
17
International University, VNU-HCMC
database
– e.g., domain constraints
– ICs are specified when schema is defined
– ICs are checked when relations are modified
Integrity Constraints
NOT NULL Constraint − Ensures that a column cannot have NULL
value.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
18
International University, VNU-HCMC
Integrity Constraints-DEFAULT
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
19
International University, VNU-HCMC
Integrity Constraints-UNIQUE
Integrity Constraints-UNIQUE
If the CUSTOMERS table has already been created
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
20
International University, VNU-HCMC
Integrity Constraints-CHECK
Integrity Constraints-CHECK
If the CUSTOMERS table has already been created
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
21
International University, VNU-HCMC
Integrity Constraints-INDEX
Syntax:
CREATE INDEX index_name
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Keys in a Database
• Key/ Candidate Key
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
• Primary Key
• Super Key
• Foreign Key
22
International University, VNU-HCMC
– no two tuples can have the same values for those columns
• Examples:
– Movie(title, year, length, genre): key is (title, year)
– what is a good key for Student?
Students(sid: string, name: string, login: string, age: integer,
gpa: real).
• Can have multiple keys for a table
• Only one of those keys may be “primary”
– DBMS often makes searches by primary key fastest
– other keys are called “secondary”
• Example:
• “For a given student and
course, there is a single grade.” CREATE TABLE Enrolled
(sid CHAR(10)
• What a primary key is in a
cid CHAR(20),
table? grade CHAR(2),
PRIMARY KEY ???)
23
International University, VNU-HCMC
24
International University, VNU-HCMC
25
International University, VNU-HCMC
26
International University, VNU-HCMC
Action taken
Example: suppose R = Enrolled, S = Students
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
27
International University, VNU-HCMC
Action taken
1. Default: Reject the modification.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Example: Cascade
Delete the 53666 tuple from Students:
Then delete all tuples from Enrolled that
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
28
International University, VNU-HCMC
29
International University, VNU-HCMC
relations
• Can we infer ICs from an instance?
– We can check a database instance to see if an IC is
violated, but we can NEVER infer that an IC is true
by looking at an instance.
– An IC is a statement about all possible instances!
– From example, we know name is not a key, but the
assertion that sid is a key is given to us.
• Key and foreign key ICs are the most common; more
general ICs supported too
Example Instances
Sailor
• We will use these instances of
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
30
International University, VNU-HCMC
Q/A
1. Is a NULL value the same as zero or a blank
space? If not, then what is the difference?
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
31