Week7 Data Management 1
Week7 Data Management 1
Data Management - I
▪ Introduction to Databases
▪ Introduction to SQL
Why Database?
▪ There is another type of data storage model used in applications like big data
and machine learning.
▪ The information for courses and instructors can be joined back together.
▪ In a relational database, tables are joined together using matched pairs of
data values.
– For example, the InstructorID is stored in a column in COURSE_INSTRUCTOR table.
Whenever we need information about an instructor, we can use InstructorID to look up
the instructor information in the INSTRUCTOR Table.
COURSE COURSE_INSTRUCTOR INSTRUCTOR
Course ID Instructor ID
COMP7990 3
COMP4125 3
COMP7820 3
COMP4115 3
COMP4405 2
COURSE_INSTRUCTOR
Relational Databases
– Database application(s)
– Databases
Database System
▪ Users
▪ Database application(s)
– User interacted with database
applications (e.g. website)
▪ Database Management System
(DBMS)
– A gate-keeper for database. The
database application needs go through
DBMS to interact with the database
▪ Databases
– Store raw data in separate tables
User
– Use different user interface forms to enter, read, delete and query data
– Produce reports
Database Applications
▪ These application can not directly access data in database. They need to go
through DBMS to touch the data.
Database Management System (DBMS) Oracle
▪ Self-describing
Instructor Instructor Office Phone
– The database itself contains the definition of ID Number
its structure.
元数据 – Metadata are data describing the structure of Zhang, Lu. 3 RRS708 34115880
data in the database (e.g, the data type for LIU, Yang 2 RRS631 34112798
Instructor ID is Integer)
• User data
• Metadata
• Indexes and other overhead data
• Application metadata
Functions of a DBMS
▪ Create databases
▪ Create tables
▪ Create relationships between tables
▪ Read database data
▪ Modify database data (insert, update, delete)
▪ Maintain database structures
▪ Enforce rules
▪ Control concurrency 并发控制
▪ Provide security
▪ Perform data backup and recovery
Enforce Rules: Referential Integrity Constraints
– If Instructor ID 4 does not exist, the DBMS would not allow us to add the record.
Course ID Instructor ID
Instructor Instructor ID Office
COMP7990 3
Zhang, Lu 3 RRS708
COMP4125 3
LIU, Yang 2 RRS631
COMP7820 3
New record COMP4115 3
COMP4405 2 Does InstructorID 4 exist in INSTRUCTOR table?
COMP4005 4
Outline
▪ Introduction to Databases
▪ Introduction to SQL
Entity 实体、对象
▪ Database entity is a thing, person, place, unit, object or any item about which the
data should be captured and stored in the form of properties, workflow and tables.
All relations are tables. However, not all tables are relations
Synonyms 同义词
▪ A key is one (or more) columns of a relation whose values are used to
identify a row
▪ Unique Keys
– Candidate Key
➢ A candidate key is called “candidate” because it has the potential to become the primary key.
– Primary Key
➢ A primary key is a candidate key chosen to be the main key for the relation.
– Composite Key 复合键
唯一性
➢ A composite key is a key that is composed of two or more attributes for uniqueness.
➢ e.g., Flight number + Date
– Surrogate Key 代理键
➢ A surrogate key is a unique, numeric value that is added to a relation to serve as the primary
key.
➢ Surrogate key values have no meaning to users.
➢ e.g., Instructor ID
▪ Non-unique Keys
– Foreign Key
Primary Key
▪ If you know the value of the primary key, you will be able to uniquely identify
a single row within table.
Primary Key
Composite Key
Surrogate key
Relationship Between Tables
InstructorID
CourseID
InstructorName
InstructorID
Office
PhoneNumber
Foreign key
Referential Integrity 参照完整性
▪ Referential integrity states that every value of a foreign key must match a
value of an existing primary key.
Foreign key Primary key
Course ID Instructor ID
InstructorID InstructorName Office Phone Number
COMP7990 3
3 Zhang, Lu RRS709 34115880
COMP4125 3
2 LIU, Yang RRS631 34112798
COMP7820 3
COMP4115 3 9 WANG, Alan AAB556 34110001
COMP4405 2
Does InstructorID 4 exist in INSTRUCTOR table?
COMP4005 4
New record
Null Values
▪ This is different from a zero, space character, empty string, or tab character
▪ Example
– The price of one cookie can determine the price of a box of 12 cookies
(cookiePrice, Qty) → BoxPrice
Candidate/Primary Keys and Functional Dependency
▪ Likewise, a primary key of a relation will functionally determine all other non-
key attributes in the row.
▪ Introduction to Databases
▪ Introduction to SQL
Structured Query Language (SQL)
▪ Structured
首字母缩略词
Query Language
– Acronym: SQL
– Pronounced “sequel” or “S-Q-L”
– Original developed by IBM as the SEQUEL language in the 1970s
– Based on relational algebra
代数
SQL
除了在命令窗口中,分号;不是必须的,空格也不是必须的
CREATE TABLE Employee ( 关键字的大小写不敏感
empId Integer NOT NULL, case insensitive
case sensitive
empName Char(25) NOT NULL
);
▪ Creating database tables with composite primary keys using PRIMARY KEY
constraints
– The SQL CREATE TABLE statement
– The SQL CONSTRAINT keyword
2 101 4
3 Sheldon 103 Python
2 103 5
3 102 3
3 102 3
foreign key in italic
3 102 5 primary key in bold and
underscore
When we delete a record (e.g. employee 1) from Employee table, “cascade delete” will
automatically delete related records from other related tables.
Cascade Update
3 102 3
foreign key in italic
3 102 5 primary key in bold and
underscore
What happen if we delete that data? Any cascade update/delete?
No
Primary Key Constraint: ALTER
1 Dan
2 Penny
3 Sheldon
Composite Primary Key Constraints: ALTER
1 102 3
2 101 4
2 103 5
3 102 3
3 102 5
Foreign Key Constraint: ALTER
▪ INSERT INTO
– Add a new row to a table
▪ UPDATE
– Update the rows in a table which match the specified criteria
▪ DELETE FROM
– Delete the rows in a table which match the specified criteria
Adding Data: INSERT INTO
Employee Employee
1 2 Brown 1 2 Brown
22 7 Smith 22 7 Smith
62 11 Halpert
Quick Question
▪ How to insert the record for "Kevin" with salaryCode 3, his empId is 10
▪ Which of the following records can be insert successfully?
• "Brown", salaryCode 2, salary 23000, empId 3
• "Steve", salaryCode 5, salary 30000, empId 4
• "Brady", salaryCode 7, empId 22
Salary Employee
2 23000 1 2 Brown
3 26000 22 7 Smith
7 35000 62 11 Halpert
Changing Data Values: UPDATE
▪ To change the data values in an existing row (or a set of rows) use the
UPDATE statement 匹配或过滤标准
▪ The WHERE clause specifies the matching or filtering criteria for the records
(rows) that are to be displayed
UPDATE Employee Update the phone number to
SET phone = '34111001' ‘34111001’ for the employee
WHERE empId = 29; whose empID is 29
Employee Employee
18 69981245 18 69981245
29 12345678 29 34111001
Changing Data Values: UPDATE
▪ To change the data values in an existing row (or a set of rows) use the
UPDATE statement
▪ The WHERE clause specifies the matching or filtering criteria for the records
(rows) that are to be displayed
UPDATE Employee Update the deptID to
SET deptId = 4 4 for the employee
WHERE empName LIKE 'Da%'; names starts with "Da"
Employee Employee
29 Dan 4 Dan
Quick Question
▪ What if we say
UPDATE Employee
SET deptId = 5
WHERE empName Like '%k'
▪ And what if we say
UPDATE Employee
SET deptId = 5
Employee
deptId empName
29 Dan
Deleting Data: DELETE
▪ To delete a row (or a set of rows) from a table use the DELETE FROM
statement
29 34110000
DELETE FROM Employee;
Next Week:
▪ SQL Query
• SELECT
• WHERE
• GROUP BY
• HAVING
• join tables