0% found this document useful (0 votes)
33 views33 pages

Database System 3.19

Uploaded by

zahidneu64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views33 pages

Database System 3.19

Uploaded by

zahidneu64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Database System

School of Software Xiaoyue Feng, Luna

[email protected]
Database VS Data Warehouse

 Data warehouses typically need to extract data from


multiple databases and other data sources, then integrate,
clean, and transform this data before storing it. Therefore,
databases can be one of the data sources for data
warehouses.
 Data from databases can be part of a data warehouse and
used to build data models such as dimension tables and
fact tables within the data warehouse.
Core uses of the Database

 Database is data infrastructure


 Database is core infrastructure Insert Data
software Query Result
 Database has empowered
thousands of industries
Location of Database in Computer System
Database is one of the core basic software of computer
Leveraging computing power downwards,
supporting applications upwards

Application

Database

Network

Security
Theory

Compiler

OS

Hardware
Basic Features of Database
 Data is organized, described and stored according to a
certain data model
 Support for data addition, deletion and updating
 Support concurrent query processing
 Generalizability and replication
Basic Concepts of Database

 Database (DB)
• A collection of related data

 Database Management
System (DBMS)
• A software package/system to
facilitate the creation and
maintenance of the database.

 Database System (DBS)


• The DBMS software together
with the data itself. Sometimes,
the applications are also
included.
Basic Concepts of Database

 Query Interface layer


 Responsible for user interaction with
the database management system
 SQL query language
 Security Management
 Responsible for full life cycle security
management of databases
Basic Concepts of Databases
 Query Processing Layer
 Query parsing, query rewriting
 Query optimization (base, cost
estimation, plan selection)
 Query Execution Layer
 Generate execution plans for database
language statements
 Execution models, execution operators
 Storage Management System
 Complete the operation of adding,
deleting, correcting and Searching data
in the data memory
 Data organization, buffer management
 Transaction processing, concurrency
control, fault recovery
Database Management System (DBMS)
select s.name, c.name, sc.score
from student as s, course as c, sc
SQL where s.sno=sc.sno and c.cno=sc.cno and
s.name=“Tom” and c.name=“database”

ρs.name, c.name, sc.score


⋈sc.cno=c.cno
Query Plan ⋈s.sno=sc.sno
σsname=“Tom” σcname=“databse”
S SC C

Cache Page1 Page4


log

Page 1 Page 2

Disk Page 3 Page 4


Log
Page 5 Page 6
How to query——SQL

 SQL: Structured Query Language


 SQL is a language used to manage relational databases and
perform a series of operations on the data in them (including data
insert, query, update, delete, etc.)
• SQL Data Definition Language (DDL)
• SQL Data Manipulation Language (DML)

10
SQL DDL

 The SQL Data Definition Language includes statements for creating, altering,
and dropping tables, indexes, and views.

Create Alter Drop

Table CREATE TABLE ALTER TABLE DROP TABLE

Index CREATE INDEX ALTER INDEX DROP INDEX

View CREATE VIEW ALTER VIEW DROP VIEW

10
SQL DML

 SQL data manipulation language includes data query, insert, delete, update.
 Data query
• Select, Project, Aggregate, Group by, Order by, Join
 Data update
• Insert, Delete, Update

11
An example——Student Course Selection Database
 Contains three tables (relation) : Student, Course and SC
Student SC
Sno Sname Sgender Sage Sdept Sno Cno Grade

2021310721 Tom Male 17 CS 2021310721 5 98


2021310722 Mike Male 19 CS 2021310722 1 87
2021310723 Lisa Female 18 CS 2021310723 1 92
2021310724 David Male 18 MA 2021310723 5 76
2021310725 Alice Female 17 MA 2021310724 7 84

Course 2021310725 4 95

Cno Cname Cpno Ccredit


Relational model: Two-dimensional tables (tables) to
1 Database 2 4 represent data
Data structures and • Each row in the table represents a record (entity
2 6 4
algorithms or tuple)
3 Operating system 2 3 • Each column in the table represents an attribute
4 Advanced mathematics 4 (field)
5 Software engineering 6 2 Query Tom's database course results
6 Programming 3 1) Find Tom's Sno in the Student table
7 Numeric analysis 4 2 2) Find Database's Cno in the Course table
3) Find Grade in the SC table
13
Data Query
 Data query is the core operation of the database to obtain the data that meets
certain conditions. The basic syntax format for data queries using SQL is (one
clause for each action) :

SELECT [ALL | DISTINCT] <Column Expressions>[ , <Column Expressions>, ǥ , <Column Expressions> ]


FROM <TableName or ViewName> [ , <TableName or ViewName>, ǥ , <TableName or ViewName> ]
[ WHERE <Condition Expression> ]
[ GROUP BY <ColumnName> [ , <ColumnName>, ǥ , <ColumnName> ]
[ HAVING <Condition Expression> ]]
[ ORDER BY <Column Expression> [<Order>] [ , <Column Expressions> [<Order>], ǥ , <Column Expressions> [ <Order> ] ] ];

• SELECT clause : Specify the attribute columns to display


• FROM clause : Specify which tables or views to query data from
• WHERE clause : Filtering tuples that satisfy conditions
• GROUP BY clause : Specify which columns to group tuples by
• HAVING clause : Filtering of grouped results
• ORDER BY clause : Sort the query results in the specified order

13
Selection and Projection
 The Selection operation selects a number of tuples in a table and is implemented through
the WHERE clause
 The Projection operation can select several columns in the table through the SELECT
clause
 Combine the selection operation and the projection operation to form a simple SQL
statement to query on a single table (the query table is specified by the FROM clause)
 【 Ex: 1 】 Query the student number, name and age of the students of the
Department of Computer Science.

 Query results :
Sno Sname Sgender Sage

2021310721 Tom Male 17


2021310722 Mike Male 19
2021310723 Lisa Female 18 14
Projection
 The Projection operation can select several columns in the table, mainly
reflected in the column expression after the SELECT clause.
 Column expression :
• Query for specified columns: list the names of the columns to be searched in order in the
SELECT clause
• Query all columns: replace all columns with the symbol *.
• Query computed values: expression after computation on columns

 【 Ex: 2 】 Search the student number, name, department and age of all
students. Sno Sname Sdept Sage

2021310721 Tom CS 17
2021310722 Mike CS 19
2021310723 Lisa CS 18
2021310724 David MA 18
2021310725 Alice MA 17
15
Projection
 【 Ex: 3 】 Search the student number, name, department and age of all students.

Equal
 Both of these SQLs will output all student information (i.e. the original student table).

 【 Ex: 4 】 Search the student number, name, and year of birth of all students.

 Query results : Sno Sname 2021-Sage

2021310721 Tom 2004


2021310722 Mike 2002
2021310723 Lisa 2003
2021310724 David 2003
16
2021310725 Alice 2004
Selection
 The selection operation selects a number of tuples that satisfy the condition via
the WHERE clause
 The WHERE clause is followed by a conditional expression, and the tuples that
satisfy the conditional expression are returned

 【 Ex: 5 】 Search the student number, name and age of students aged 18 and
above.

 Query results : Sno Sname Sage

2021310722 Mike 19

2021310723 Lisa 18

2021310724 David 18
17
Selection
 【 Ex: 6 】 Search the student number, name and department of the Computer
Science and Mathematics Departments.

 Query results : Sno Sname Sdept

2021310721 Tom CS
2021310722 Mike CS
2021310723 Lisa CS
2021310724 David MA
2021310725 Alice MA
 【 Ex: 7 】 To query courses that do not require prerequisites, output the course
number, course name and its credits.

Cno Cname Ccredit


 Query results :
4 Advanced mathematics 4
18
6 Programming 3
Aggregation
 In order to query the results of some data aggregation, such as querying some
statistical values (average, maximum, minimum, etc.), SQL provides many
aggregation functions.

 When the DISTINCT option is specified, the aggregation function is to be applied


to the columns to eliminate duplicate values
19
Aggregation
 【 Ex: 8 】 Search the total number of students.
Query result

 【 Ex: 9 】 Search the number of students who have taken the course.
Query result

 【 Ex: 10 】 Search the number of students who have taken the course (specify
DISTINCT option)
Query result

 【 Ex: 11 】 Search the average grade of students taking Course 1.


Query result
(87+92)÷2=89.5
20
Group By
 GROUP BY clause allows you to group the tuples that satisfy the conditions of
the query according to the values of one or more columns, and those with equal
values are a group.
 Group student course selection forms by course number column:

21
Group By

 【 Ex: 12 】 Search the number of


students enrolled in each course

 Query results:

22
Group By
 【 Ex: 13 】 Search the student number and average grade of all students.

Query result

 【 Ex: 14 】 Search the student numbers and average grade for students with a
average grade over 90.

Query result

 The HAVING clause is used to filter the group obtained after GROUP BY
23
Order By
 ORDER BY clause sorts the query results by the values of a column (or columns)
 DESC : descending sort ; ASC : Ascending sort (default option)
 【 Ex: 15 】 Search the student numbers and grades of students taking the Course 1,
and sort the query results in descending order of grades.
Query result

 【 Ex: 16 】 Search the student number and average grade of all students, and sort
the query result in descending order of average grade, and sort the students with the
same average grade in ascending order of student number.

Query result

24
Join
 A query that involves multiple tables (both and above) at the same time is called a
join query.
 In the Join operation, specify the table to be joined in the FROM clause and the join
condition in the WHERE clause.

 【 Ex: 17 】 Search the student number, name, course number and grade of all
students who have selected the course.

Tom Male
Mike Male
Lisa Female
Lisa Female
David Male
Alice Female

25
Join
 【 Ex: 18 】 Search all courses and their prerequisites. Outputs the course name
and the course name of the prerequisite course.

Cname Cpno_name
Database Data structures and algorithms
Data structures and algorithms Programing
Operating system Data structures and algorithms
Software Engineering Programing
Numeric analysis Advanced mathematics

• When the two tables of a join operation are the same table, you need to rename the
table using AS in the FROM clause in order to distinguish them.
26
Exercise
 Search the total credits of each student's selected courses, output the student's
number, name, and total credits, and sort the results in descending order according
to the total credits, and students with the same total number of credits are sorted in
ascending order by their student numbers.
Sno Sname Sgender Sage Sdept

2021310721 Tom Male 17 CS


SC
2021310722 Mike Male 19 CS Student Sno Cno Grade
2021310723 Lisa Female 18 CS 2021310721 5 98
2021310724 David Male 18 MA 2021310722 1 87
2021310725 Alice Female 17 MA 2021310723 1 92
2021310723 5 76
Cno Cname Cpno Ccredit
2021310724 7 84
1 Database 2 4
Data structures and 2021310725 4 95
2 6 4
algorithms
3 Operating system 2 3 Course
4 Advanced mathematics 4
5 Software engineering 6 2
6 Programming 3
7 Numeric analysis 4 2 27
SC
Exercise Student Sno Cno Grade

Sno Sname Sgender Sage Sdept 2021310721 5 98

2021310721 Tom Male 17 CS 2021310722 1 87

2021310722 Mike Male 19 CS 2021310723 1 92

2021310723 Lisa Female 18 CS 2021310723 5 76

2021310724 David Male 18 MA 2021310724 7 84

2021310725 Alice Female 17 MA 2021310725 4 95

Course
Cno Cname Cpno Ccredit
1 Database 2 4
Data structures and
2 6 4
algorithms
3 Operating system 2 3
4 Advanced mathematics 4
5 Software engineering 6 2
6 Programming 3
 Query results: Lisa
7 Numeric analysis 4 2
Mike
Alice
Tom
David 28
Data Update

 Insert data
 Insert a single tuple
 Insert subquery results
 Delete data
 Update data

29
Insert Data
 The syntax format for inserting a single tuple :
INSERT
INTO <TableName> [ ( < CloumnName_1> [ , <ColumnName_2>, ǥ , <ColumnName_n> ] ) ]
VALUE ( <Constant_1> [ , <Constant_2>, ǥ , <Constant_n> ] );

 The meaning of this statement is to insert a new tuple into the specified table

 【 Ex: 19 】 Insert the new tuple (student number: 2021310726, name: Lili, age:
17, gender: female, department: MA) into the Student table.
INSERT INSERT
INTO Student (Sno, Sname, Sage, Sgender, Sdept )
VALUE (Dz2021310726dz, Dz Lilidz, 17, Dz
Femaledz ,Dz
MAdz ); OR INTO Student
VALUE (Dz2021310726dz, Dz
Lilidz
,DzFemaledz
, 17, Dz
MAdz
);

 The column names in the INTO clause can be omitted, then the VALUES clause
lists the values of all the columns in the order in which they appear in the table.

30
Update data
 The syntax format for updating data in databases:
UPDATE <TableName>
SET <ColumnName_1>=<Expression_1> [ ,<ColumnName_2>=<Expression_2>, ǥ , <ColumnName_n>=<Expression_n> ]
[ WHERE <Condition> ];

 The meaning of this statement is to update the tuple in the specified table that satisfies
the condition in the WHERE clause

 【 Ex: 20 】 Add one to the age of the student with student number 2021310721.

 【 Ex: 21 】 Add the ages of all students to one.

31
Delete Data
 The syntax format for deleting data in the database :
DELETE
FROM <TableName>
[ WHERE <Condition> ];

 The meaning of this statement is to delete the tuple in the specified table that satisfies the
condition in the WHERE clause
If the WHERE clause is not included, all tuples in the table will be deleted

 【 Ex: 22 】 Delete student record with student number 2021310721

 【 Ex: 23 】 Delete all course selection information from the Student table.

32

You might also like