0% found this document useful (0 votes)
5 views

Lecture 5 SQL Part 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 5 SQL Part 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

International University, VNU-HCMC

School of Computer Science and Engineering

Lecture 5: SQL part 1

Instructor: Nguyen Thi Thuy Loan


[email protected], [email protected]
https://nttloan.wordpress.com/

International University, VNU-HCMC

Announcements
• If you are enrolled to the class, but have not seen the
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

information on Blackboard IU, please send me an


email.

• Lecture “slides” (incomplete) will be uploaded before


the class, “slides” (completed) after the class

Duke CS, Fall 2021 2

1
International University, VNU-HCMC

Recap: Lecture 2
• Why use a DBMS?
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Structured data model: Relational data model


– table, schema, instance, tuples, attributes
– bag and set semantic
• Data independence
– Physical independence: Can change how data is
stored on disk without affecting applications
– Logical independence: can change schema without
affecting apps

Duke CS, Fall 2021 3

International University, VNU-HCMC

Summary: Data Models


• Relational data model is the most standard for
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

Duke CS, Fall 2021 4

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

Duke CS, Fall 2021 5

International University, VNU-HCMC

Acknowledgement
• The following slides have been created adapting the
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

instructor material of the [RG] book provided by the


authors Dr. Ramakrishnan and Dr. Gehrke.
• The following slides are referenced from Dr. Sudeepa
Roy, Duke University.

Duke CS, Fall 2021 6

3
International University, VNU-HCMC

What is SQL?

• SQL is Structured Query Language, which is a


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

computer language for storing, manipulating


and retrieving data stored in a relational
database.

• SQL is the standard language for Relational


Database System.

https://www.tutorialspoint.com

Duke CS, Fall 2021 7

International University, VNU-HCMC

A Brief History of SQL


1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the
father of relational databases. He described a relational
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

model for databases.

1974 − Structured Query Language appeared.

1978 − IBM worked to develop Codd's ideas and released


a product named System/R.

1986 − IBM developed the first prototype of relational


database and standardized by ANSI. The first relational
database was released by Relational Software which
later came to be known as Oracle.
Duke CS, Fall 2021 8

4
International University, VNU-HCMC

A Brief History of SQL


• Standards:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– SQL-86
– SQL-89 (minor revision)
– SQL-92 (major revision)
– SQL-99 (major extensions, current
standard)
– More: MS SQL Server history on the
internet

Duke CS, Fall 2021 9

International University, VNU-HCMC

Purposes of SQL
• Data Manipulation Language (DML)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– Querying: SELECT-FROM-WHERE
– Modifying: INSERT/DELETE/UPDATE

• Data Definition Language (DDL)


– CREATE/ALTER/DROP

Duke CS, Fall 2021 10

5
International University, VNU-HCMC

DML - Data Manipulation Language


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 11

International University, VNU-HCMC

DDL - Data Definition Language


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 12

6
International University, VNU-HCMC

Relational Model
column/
attribute/
field
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

sid name login age gpa


row / 53666 Jones jones@cs 18 3.4
tuple /
53688 Smith smith@ee 18 3.2
record
53650 Smith smith1@math 19 3.8
53831 Madayan madayan@music 11 1.8
53832 Guldu guldu@music 12 2.0

• mathematically, relation is a set of tuples


– each tuple appears 0 or 1 times in the table
– order of the rows is unspecified
Duke CS, Fall 2021 13

International University, VNU-HCMC

SQL (“sequel”)
• Standard query language for relational data
– used for databases in many different contexts
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– inspires query languages for non-relational (e.g. SQL++)


• Everything not in quotes (‘…’) is case insensitive
• Provides standard types.
• Examples:
– numbers: INT, FLOAT, DECIMAL(p,s)
• DECIMAL(p,s): Exact numerical, precision p, scale s. Example:
decimal(5,2) is a number that has 3 digits before the decimal and
2 digits after the decimal
– strings: CHAR(n), VARCHAR(n)
• CHAR(n): Fixed-length n
• VARCHAR(n): Variable length. Maximum length n

Duke CS, Fall 2021 14

7
International University, VNU-HCMC

SQL (“sequel”) – Cont.


– BOOLEAN
– DATE, TIME, TIMESTAMP
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• DATE: Stores year, month, and day values


• TIME: Stores hour, minute, and second values
• TIMESTAMP: Stores year, month, day, hour, minute, and
second values
• Additional types in here

Duke CS, Fall 2021 15

International University, VNU-HCMC

Exact Numeric Data Types


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 16

8
International University, VNU-HCMC

Approximate Numeric Data Types


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 17

International University, VNU-HCMC

Date and Time Data Types


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 18

9
International University, VNU-HCMC

Character Strings Data Types


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 19

International University, VNU-HCMC

Unicode Character Strings Data Types


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 20

10
International University, VNU-HCMC

Binary Data Types


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 21

International University, VNU-HCMC

Miscellaneous Data Types


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Duke CS, Fall 2021 22

11
International University, VNU-HCMC

Demo on SQLite
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• E.g., type sqlite3


• https://www.db-book.com/university-lab-dir/sqljs.html

Duke CS, Fall 2021 23

International University, VNU-HCMC

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 ...

Duke CS, Fall 2021 24

12
International University, VNU-HCMC

SQL statements
Syntax:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

CREATE DATABASE DatabaseName:


CREATE DATABASE testDB;

DROP DATABASE DatabaseName:


DROP DATABASE testDB;

USE DatabaseName:
USE testDB;

all attributes

Duke CS, Fall 2021 25

International University, VNU-HCMC

CREATING TABLE
The basic syntax of the CREATE TABLE statement is as
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

follows:

CREATE TABLE table_name(


column1 datatype,
Column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY (one or more columns)
); s

Duke CS, Fall 2021 26

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

– the type (domain) of each


name CHAR(15),
field is specified
login CHAR(20),
– enforced by the DBMS age INTEGER,
whenever tuples are added or gpa REAL/DECIMAL(2,1))
modified
• As another example, the CREATE TABLE Enrolled
(sid CHAR(20), ??
“Enrolled” table holds
cid CHAR(20),
information about courses that grade CHAR(2))
students take
sid cid grade
sid nam e login age gpa
53831 Carnatic101 C
53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53831 Reggae203 B
53650 Topology112 A
53650 Smith smith@math 19 3.8
53666 History105 B
Students
Enrolled
Duke CS, Fall 2021 27

International University, VNU-HCMC

Example: CREATE TABLE


Customers(ID: int, name: string(20), age: int, address:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

string(25), salary decimal(18,2))

CREATE TABLE Customers(

);all attributes

Duke CS, Fall 2021 28

14
International University, VNU-HCMC

Destroying Relation/table
Syntax: Drop table Table_name
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

DROP TABLE Customers;

• Destroys the relation Customers


– The schema information and the tuples are
deleted.

Duke CS, Fall 2021 29

International University, VNU-HCMC

Altering Relation/Table
ALTER TABLE Students
ADD COLUMN firstYear: integer
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• The schema of Students is altered by adding a new


field;
• What’s the value in the new field?
every tuple in the current instance is extended
with a null value in the new field.

Duke CS, Fall 2021 30

15
International University, VNU-HCMC

Adding/ Insert into


Syntax:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

INSERT INTO TABLE_NAME (column1, column2, column3, ...,


columnN) VALUES (value1, value2, value3,..., valueN);

INSERT INTO TABLE_NAME


VALUES (value1, value2, value3, ..., valueN);

Duke CS, Fall 2021 31

International University, VNU-HCMC

Adding and Deleting Tuples

• Can insert a single tuple using:


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

INSERT INTO Students (sid, name, login, age, gpa)


VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)

• Can delete all tuples satisfying some condition


(e.g., name = Smith):
DELETE
FROM Students S
WHERE S.name = ‘Smith’

Duke CS, Fall 2021 32

16
International University, VNU-HCMC

Example: Adding/ Insert into


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)


VALUES (7, 'Muffy', 24, 'Indore', 10000.00);

INSERT INTO CUSTOMERS


VALUES (7, 'Muffy', 24, 'Indore', 10000.00);
Duke CS, Fall 2021 34

International University, VNU-HCMC

update ... set ... where ...


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

UPDATE Student
SET age = age + 2
where sid = ‘53680';

Duke CS, Fall 2021 35

17
International University, VNU-HCMC

Integrity Constraints (ICs)


• IC: condition that must be true for any instance of the
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

database
– e.g., domain constraints
– ICs are specified when schema is defined
– ICs are checked when relations are modified

• A legal instance of a relation is one that satisfies all


specified ICs
– DBMS will not allow illegal instances

• If the DBMS checks ICs, stored data is more faithful to


real-world meaning
– Avoids data entry errors, too!

Duke CS, Fall 2021 36

International University, VNU-HCMC

Integrity Constraints
NOT NULL Constraint − Ensures that a column cannot have NULL
value.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

DEFAULT Constraint − Provides a default value for a column


when none is specified.
UNIQUE Constraint − Ensures that all values in a column are
different.
PRIMARY Key − Uniquely identifies each row/record in a table.
FOREIGN Key − Uniquely identifies a row in any of the given
table.
CHECK Constraint − Ensures that all the values in a column
satisfies certain conditions.
INDEX − Used to create and retrieve data from the database
very quickly.
Duke CS, Fall 2021 37

18
International University, VNU-HCMC

Integrity Constraints-NOT NULL


CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

NAME VARCHAR (20) NOT NULL,


AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

If CUSTOMERS table has already been created

ALTER TABLE CUSTOMERS


ALTER COLUMN SALARY DECIMAL (18, 2) NOT NULL

Duke CS, Fall 2021 38

International University, VNU-HCMC

Integrity Constraints-DEFAULT
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

NAME VARCHAR (20) NOT NULL,


AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2) DEFAULT 5000.00,
PRIMARY KEY (ID)
);
If the CUSTOMERS table has already been created
ALTER TABLE CUSTOMERS
DROP column SALARY;
ALTER TABLE CUSTOMERS
ADD SALARY DECIMAL (18, 2) DEFAULT 5000.00;
Duke CS, Fall 2021 39

19
International University, VNU-HCMC

Integrity Constraints-UNIQUE

CREATE TABLE CUSTOMERS(


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

ID INT NOT NULL,


NAME VARCHAR (20) NOT NULL UNIQUE,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

Duke CS, Fall 2021 40

International University, VNU-HCMC

Integrity Constraints-UNIQUE
If the CUSTOMERS table has already been created
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

ALTER TABLE CUSTOMERS


ADD CONSTRAINT UniqueConstraint UNIQUE(NAME);

DROP a UNIQUE Constraint

ALTER TABLE CUSTOMERS


DROP CONSTRAINT UniqueConstraint;

Duke CS, Fall 2021 41

20
International University, VNU-HCMC

Integrity Constraints-CHECK

CREATE TABLE CUSTOMERS(


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

ID INT NOT NULL,


NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL CHECK (AGE >= 18),
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

Duke CS, Fall 2021 42

International University, VNU-HCMC

Integrity Constraints-CHECK
If the CUSTOMERS table has already been created
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

ALTER TABLE CUSTOMERS


ADD CONSTRAINT CheckConstraint CHECK(AGE
>=18);

DROP a CHECK Constraint

ALTER TABLE CUSTOMERS


DROP CONSTRAINT CheckConstraint;

Duke CS, Fall 2021 43

21
International University, VNU-HCMC

Integrity Constraints-INDEX
Syntax:
CREATE INDEX index_name
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

ON table_name ( column1, column2.....);

To create an INDEX on the AGE column, to optimize the


search on customers for a specific age

CREATE INDEX idx_age


ON CUSTOMERS ( AGE );

DROP an INDEX Constraint

ALTER TABLE CUSTOMERS


DROP INDEX idx_age;
Duke CS, Fall 2021 44

International University, VNU-HCMC

Keys in a Database
• Key/ Candidate Key
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

• Primary Key
• Super Key
• Foreign Key

• Primary key attributes are underlined in a


schema
– Person(pid, address, name)
– Person2(address, name, age, job)

Duke CS, Fall 2021 45

22
International University, VNU-HCMC

Primary Key Constraints


• Key = subset of columns that uniquely identifies tuple
• Another constraint on the table
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– 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”

Duke CS, Fall 2021 46

International University, VNU-HCMC

Primary and Candidate Keys in SQL


• Possibly many candidate keys
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– specified using UNIQUE


– one of which is chosen as the primary key.

• 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 ???)

Duke CS, Fall 2021 47

23
International University, VNU-HCMC

Primary and Candidate Keys in SQL


• Possibly many candidate keys
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– specified using UNIQUE


– one of which is chosen as the primary key.
• “For a given student and course, there is a single
grade.”

CREATE TABLE Enrolled


(sid CHAR(10)
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid) )

Duke CS, Fall 2021 48

International University, VNU-HCMC

Primary and Candidate Keys in SQL


• Possibly many candidate keys
– specified using UNIQUE CREATE TABLE Enrolled
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– one of which is chosen as the (sid CHAR(10)


primary key. cid CHAR(20),
grade CHAR(2),
• “For a given student and PRIMARY KEY (sid,cid)
course, there is a single
grade.” vs CREATE TABLE Enrolled (sid
vs. CHAR(10)
• “Students can take only one cid CHAR(20),
course, and receive a single grade CHAR(2), PRIMARY
grade for that course; further, KEY ???, UNIQUE ??? )
no two students in a course
receive the same grade.”

Duke CS, Fall 2021 49

24
International University, VNU-HCMC

Primary and Candidate Keys in SQL


• Possibly many candidate keys
– specified using UNIQUE CREATE TABLE Enrolled
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– one of which is chosen as the ( sid CHAR(10)


primary key. cid CHAR(20),
grade CHAR(2),
• “For a given student and PRIMARY KEY (sid, cid)
course, there is a single
grade.” vs CREATE TABLE Enrolled
vs. ( sid CHAR(10)
• “Students can take only one
cid CHAR(20),
course, and receive a single grade CHAR(2),
grade for that course; further, PRIMARY KEY sid,
no two students in a course UNIQUE (cid, grade))
receive the same grade.”

Duke CS, Fall 2021 50

International University, VNU-HCMC

Primary and Candidate Keys in SQL


• Possibly many candidate keys
– specified using UNIQUE CREATE TABLE Enrolled
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

– one of which is chosen as the ( sid CHAR(10)


primary key. cid CHAR(20),
grade CHAR(2),
• “For a given student and course, PRIMARY KEY (sid, cid)
there is a single grade.” vs
• “Students can take only one CREATE TABLE Enrolled
vs.
course, and receive a single grade ( sid CHAR(10)
for that course; further, no two cid CHAR(20),
grade CHAR(2),
students in a course receive the
PRIMARY KEY sid,
same grade.” UNIQUE (cid, grade))
• Used carelessly, an IC can prevent
the storage of database instances
that arise in practice!
Duke CS, Fall 2021 51

25
International University, VNU-HCMC

Foreign Keys, Referential Integrity


• Foreign key: Set of fields in one relation that is
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

used to 'refer' to a tuple in another relation


– Must correspond to primary key of the second relation
– Like a 'logical pointer'

• E.g. sid is a foreign key referring to Students:


– Enrolled(sid: string, cid: string, grade: string)
– If all foreign key constraints are enforced,
referential integrity is achieved
– i.e., no dangling references

Duke CS, Fall 2021 52

International University, VNU-HCMC

Foreign Keys in SQL


• Only students listed in the Students relation should be
allowed to enroll for courses
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

CREATE TABLE Enrolled


(sid CHAR(10), cid CHAR(20), grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid) REFERENCES Students )
• Enrolled
• Students
sid cid grade
sid name login age gpa
53666 Carnatic101 C
53666 Jones jones@cs 18 3.4
53666 Reggae203 B
53650 Topology112 A 53688 Smith smith@eecs 18 3.2
53666 History105 B 53650 Smith smith@math 19 3.8

Duke CS, Fall 2021 53

26
International University, VNU-HCMC

Enforcing Foreign-Key Constraints


If there is a foreign-key constraint from relation R
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

to relation S, two violations are possible:


1. An insert or update to R introduces values
not found in S.
2. A deletion or update to S causes some tuples
of R to “dangle.”

Duke CS, Fall 2021 54

International University, VNU-HCMC

Action taken
Example: suppose R = Enrolled, S = Students
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

An insert or update to Enrolled that introduces a


nonexistent Students must be rejected.

A delete or update to Students that removes a


student value found in some tuples of Enrolled can
be handled in four ways (next slide)

Duke CS, Fall 2021 55

27
International University, VNU-HCMC

Action taken
1. Default: Reject the modification.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

2. Cascade: Make the same changes in Enrolled.


Deleted Students: delete Enrolled tuple.
Updated Students: change value in Enrolled.

3. Set NULL: Change the sid in E to NULL.


4. Default is No action: (delete/update is
rejected)

Duke CS, Fall 2021 56

International University, VNU-HCMC

Example: Cascade
Delete the 53666 tuple from Students:
Then delete all tuples from Enrolled that
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

have sid = ’53666’.

Update the 53666 tuple by changing ’53666’


to ’53686’:
Then change all Enrolled tuples with
sid = ’53666’ to sid = ’53686’.

Duke CS, Fall 2021 57

28
International University, VNU-HCMC

Example: Set NULL


Delete the 53666 tuple from Students:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Change all tuples of Enrolled that have


sid = ‘53666’ to have sid = NULL.

Update the 53666 tuple by changing ’53666’ to


’53686’:
Same change as for deletion.

Duke CS, Fall 2021 58

International University, VNU-HCMC

Referential Integrity in SQL


• SQL/92 and SQL:1999 support all 4 options on
deletes and updates
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

CREATE TABLE Enrolled


( sid CHAR(10),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid) REFERENCES Students
ON DELETE CASCADE
ON UPDATE SET DEFAULT )

Duke CS, Fall 2021 59

29
International University, VNU-HCMC

Where do ICs Come From?


• ICs are based upon the semantics of the real-world
enterprise that is being described in the database
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

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

Duke CS, Fall 2021 60

International University, VNU-HCMC

Example Instances
Sailor
• We will use these instances of
Assoc. Prof. Nguyen Thi Thuy Loan, PhD

sid sname rating age


the Sailors and Reserves
22 dustin 7 45
relations in our examples
31 lubber 8 55
58 rusty 10 35
• If the key for the Reserves
relation contained only the
attributes sid and bid, how Reserves
would the semantics differ? sid bid day
22 101 10/10/96
58 103 11/12/96

Duke CS, Fall 2021 61

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

Duke CS, Fall 2021 62

International University, VNU-HCMC


Assoc. Prof. Nguyen Thi Thuy Loan, PhD

Thank you for your attention!

Duke CS, Fall 2021 63

31

You might also like