✅ The idea of the SQL language is universal. MySQL and Oracle actually have the same semantic essence, but the syntax is slightly different .
Article directory
- 1. Learning objectives and requirements
- 2. Operating environment
- 3. Relevant principles and learning content
-
- 3.1 Understand and master the use of SQL * Plus environment
- 3.2 Create the base table with the DDL statement of SQL in the library management system
- 3.3 Supplementary definition for the base table "reader": the professional title can only take one of primary, intermediate and senior.
- 3.4 Use SQL DML statements to add, modify and delete data in the above base table
- 3.5 Complete the query with the QL statement of SQL
- 3.6 Create views and indexes with SQL DDL statements
- 4. Learning experience
1. Learning objectives and requirements
(1) Familiarize yourself with Oracle's SQL*Plus environment and usage methods through hands-on practice.
(2) Master the SQL language, and be able to use the SQL language proficiently for data definition and data manipulation.
(3) Deepen the understanding of the data structure and constraints of the relational data model.
2. Operating environment
Hardware: microcomputer.
Software: Windows operating system, ORACLE 11G.
3. Relevant principles and learning content
3.1 Understand and master the use of SQL * Plus environment
(1) Log in to Oracle SQL*Plus as an administrator.
(2) Create a user with the student number as the user name and grant it permissions.
(3) The administrator logs out of Oracle and logs in to Oracle with the newly created user.
create user B1903xxxx identified by 123;
Grant DBA to B1903xxxx;
Connect B1903xxxx/123;
◆ Description : The user withB1903xxxx
the account name and password is authorized.123
3.2 Create the base table with the DDL statement of SQL in the library management system
The library management system has three base tables, and the base table schema is as follows (the underline is the primary key, and the two italics are the two foreign keys):
(1) Books ( book number , classification number, title, author, publisher, unit price)
CREATE TABLE 图书
(图书编号 CHAR(10) NOT NULL,
分类号 CHAR(8) NOT NULL,
书名 VARCHAR(15) NOT NULL,
作者 VARCHAR(6),
出版单位 VARCHAR(10),
单价 DEC(5,2),
primary key(图书编号));
(2) Readers ( library card number , name, unit, title)
CREATE TABLE 读者
(借书证号 CHAR(10) NOT NULL,
姓名 CHAR(10) NOT NULL,
单位 VARCHAR(10),
职称 VARCHAR(10),
primary key(借书证号));
(3) Borrowing ( library card number, book number , borrowing date, remarks)
CREATE TABLE 借阅
(借书证号 CHAR(10) NOT NULL,
图书编号 CHAR(10) NOT NULL,
借阅日期 DATE NOT NULL,
备注 VARCHAR(30),
primary key(借书证号,图书编号),
foreign key(借书证号) references 读者(借书证号),
foreign key(图书编号) references 图书(图书编号));
3.3 Supplementary definition for the base table "reader": the professional title can only take one of primary, intermediate and senior.
ALTER TABLE 读者 ADD CHECK (职称 IN ('初级','中级','高级'));
3.4 Use SQL DML statements to add, modify and delete data in the above base table
(1) Insert data into the three base tables
● Insert all 图书
data :
INSERT INTO 图书 VALUES('0001','TP31','计算机基础','WANG','高等教育',17.00);
INSERT INTO 图书 VALUES('0002','TP32','数据库原理',NULL,NULL,16.50);
INSERT INTO 图书 VALUES('0003','TN31','并行计算机','YANG','清华大学',12.80);
INSERT INTO 图书 VALUES('0004','AA04','数据结构','CAO','南京大学',11.30);
INSERT INTO 图书 VALUES('0005','BB06','算法设计','ZHOU','清华大学',9.20);
INSERT INTO 图书 VALUES('0006','CC07','计算机原理','XIAN','北京大学',15.80);
INSERT INTO 图书 VALUES('0009','NJ09','数据库实验书','WU','人民教育',23.50);
● Insert all 读者
data :
INSERT INTO 读者 VALUES('T201','LIXIN','计算机系','中级');
INSERT INTO 读者 VALUES('S981','WANG','通信系','高级');
INSERT INTO 读者 VALUES('Z003','CHEN','工厂','初级');
INSERT INTO 读者 VALUES('B123','LI','工厂','中级');
● Insert all 借阅
data :
INSERT INTO 借阅 VALUES('T201','0001',To_Date('2001-03-10','yyyy-mm-dd'),NULL);
INSERT INTO 借阅 VALUES('T201','0003',To_Date('2001-04-01','yyyy-mm-dd'),NULL);
INSERT INTO 借阅 VALUES('S981','0002',To_Date('2001-02-20','yyyy-mm-dd'),NULL);
INSERT INTO 借阅 VALUES('Z003','0001',To_Date('2001-03-03','yyyy-mm-dd'),NULL);
INSERT INTO 借阅 VALUES('Z003','0002',To_Date('2001-03-20','yyyy-mm-dd'),NULL);
INSERT INTO 借阅 VALUES('Z003','0003',To_Date('2001-03-23','yyyy-mm-dd'),NULL);
INSERT INTO 借阅 VALUES('Z003','0004',To_Date('2001-04-01','yyyy-mm-dd'),NULL);
INSERT INTO 借阅 VALUES('Z003','0005',To_Date('2001-04-06','yyyy-mm-dd'),NULL);
INSERT INTO 借阅 VALUES('T201','0002',To_Date('2001-04-04','yyyy-mm-dd'),NULL);
INSERT INTO 借阅 VALUES('T201','0004',To_Date('2001-04-05','yyyy-mm-dd'),NULL);
INSERT INTO 借阅 VALUES('T201','0005',To_Date('2001-04-06','yyyy-mm-dd'),NULL);
INSERT INTO 借阅 VALUES('S981','0001',To_Date('2001-05-06','yyyy-mm-dd'),NULL);
(2) Correction and deletion of data
① Fill in the author and publisher for the book numbered 0002.
UPDATE 图书
SET 作者='SA',出版单位='高等教育'
WHERE 图书编号='0002';
② Increase the unit price of all books by 5% (ie: original value × 1.05).
UPDATE 图书
SET 单价=单价*1.05;
③ Change the classification number of the book whose title contains 'Computer' to 'TP38'.
UPDATE 图书
SET 分类号='TP38'
WHERE 书名 LIKE '%计算机%';
④ Delete all reader information and borrowing information whose library card number starts with S.
DELETE FROM 借阅 WHERE 借书证号 LIKE 'S%';
DELETE FROM 读者 WHERE 借书证号 LIKE 'S%';
3.5 Complete the query with the QL statement of SQL
(1) List the titles and publishers of all books in the library.
SELECT 书名,出版单位 FROM 图书;
(2) Query the names and titles of all readers who have borrowed books in the factory.
SELECT 姓名,职称
FROM 读者
WHERE 借书证号 IN (SELECT 借书证号 FROM 借阅) AND 单位='工厂';
(3) Query the books in the collection that have a higher unit price than all the books of the Higher Education Press.
SELECT * FROM 图书
WHERE 单价 > ALL
(SELECT 单价
FROM 图书
WHERE 出版单位='高等教育');
(4) Query the highest price, lowest price and average price of books from each publishing house.
SELECT 出版单位,MAX(单价),MIN(单价),AVG(单价)
FROM 图书
GROUP BY 出版单位;
(5) List the readers and their units who have borrowed at least 5 books currently.
SELECT 姓名,单位 FROM 读者
WHERE 借书证号 IN
(SELECT 借书证号FROM 借阅
GROUP BY 借书证号
HAVING COUNT(*)>=5);
3.6 Create views and indexes with SQL DDL statements
(1) Create a simple statistical view of the current borrowing of books by each unit, which includes the name of the unit, the number of borrowers and the number of borrowers.
CREATE VIEW 统计(单位,借书人数,借阅人次)
AS
SELECT 单位,COUNT(DISTINCT 读者.借书证号),COUNT(借阅.借书证号)
FROM 读者,借阅
WHERE 读者.借书证号=借阅.借书证号
GROUP BY 单位;
(2) Query the view.
SELECT * FROM 统计;
(3) Create a descending index for the book table by publisher.
CREATE INDEX 图书表
ON 图书(出版单位 DESC);
4. Learning experience
● To learn a language, the fastest way to become familiar with it is to start typing codes. In the beginning, whether you use Oracle or MySQL, its syntax is quite different from that of traditional programming languages. In addition, we didn't understand enough about its "query thinking" at the beginning, so we often didn't know what keywords or methods to use when doing the questions. Later, I read the textbook again, and after summarizing and reflecting on the questions, I vaguely felt the routine of "inquiring thoughts". For example, how to deal with query statements containing "at least, most, all" and so on.
⭐️ ⭐️