Database System 3.19
Database System 3.19
[email protected]
Database VS Data Warehouse
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.
Page 1 Page 2
10
SQL DDL
The SQL Data Definition Language includes statements for creating, altering,
and dropping tables, indexes, and views.
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
Course 2021310725 4 95
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
【 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.
【 Ex: 5 】 Search the student number, name and age of students aged 18 and
above.
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.
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.
【 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
21
Group By
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
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.
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: 23 】 Delete all course selection information from the Student table.
32