4.5 SQL实战
4.5.1 什么是SQL
SQL是由IBM公司在1974~1979年之间根据E.J.Codd发表的关系数据库理论为基础开发的,其前身是“SEQUEL”,后更名为SQL。由于SQL语言具有集数据查询、数据操纵、数据定义和数据控制功能于一体,类似自然语言、简单易用以及非过程化等特点,得到了快速的发展,并于1986年10月,被美国国家标准协会(American National Standards Institute,ANSI)采用为关系数据库管理系统的标准语言,后为国际标准化组织(International Organization for Standardization,ISO)采纳为国际标准。
4.5.2 SQL分类
SQL(Structured Query Language)是结构化查询语言的简称,它是一种数据库查询和程序设计语言,同时也是目前使用最广泛的关系型数据库操作语言。在数据库管理系统中,使用SQL语言来实现数据的存取、查询、更新等功能。SQL是一种非过程化语言,只需要提出“做什么”,而不需要指明“怎么做”。

4.5.3 DQL数据查询
数据查询不应只是简单查询数据库中存储的数据,还应该根据需要对数据库进行筛选,同时进行一些逻辑运算,最终确定数据以什么样的格式显示。
MySQL提供了功能强大、灵活的语句来实现这些操作。
1MySQL从数据表中查询数据的基本语句为SELECT语句,SELECT语句的基本格式为:2SELECT3 {*|<字段列表>}4 [5 FROM 表1,表2,...6 WHERE <表达式>7 [GROUP BY <group by definition>]8 [HAVING <expression> [<operator> <expression>]]9 [ORDER BY <order by definition>]10 [LIMIT [<offset>,] <row count>]11]12精简版格式:13SELECT *| 列名 FROM 表 WHERE 条件
4.5.4 DML数据操纵
MySQL提供了功能丰富的数据库管理语句,包括有效的向数据库插入数据的INSERT语句,更新数据的UPDATE语句以及当数据不再使用时删除数据的DELETE语句。
1--插入2Goods 数据库中有一个表:3create table book(4 id int primary key auto_increment,5 title varchar(200) not null ,6 description varchar(1000) default '',7 price decimal(12, 4),8 isbn char(16) not null ,9 publish_at date not null10);11create unique index idx_book_isbn on book(isbn);1213那么下列哪个选项的代码可以执行成功?C14A.15insert into book(title, price, isbn, publish_at) select 'a book title', 'unknown', 'xx-xxxx-xxxx', '2019-12-1';16insert into book(title, price, isbn, publish_at) select 'a other book title', 'unknown', 'xx-xxxx-xxxx', '2019-12-1';17B.18insert into book(title, price, isbn, publish_at) select null, 'unknown', 'xx-xxxx-xxxx', '2019-12-1';19insert into book(title, price, isbn, publish_at) select null, 'unknown', 'xx-xxxx-xxxx', '2019-12-1';20C.21insert into book(title, price, isbn, publish_at) select 'a book title', 25.4, 'xx-xxxx-xxxx', '2019-12-1';22insert into book(title, price, isbn, publish_at) select 'a other book title', 25.4, 'yy-yyyy-xxxx', '2019-12-1';23D.24insert into book(price, isbn, publish_at) select 25.4, 'xx-xxxx-xxxx', '2019-12-1';25insert into book(price, isbn, publish_at) select 35.4, 'yy-yyyy-xxxx', '2019-12-1';2627--更新数据28现有 employee 表如下:29create table employee30(31 id serial primary key,32 name varchar(64),33 dept varchar(64),34 salary decimal(12, 4)35);3637Joe 希望修改销售部(dept 字段为 sale)员工 Dora Muk 的工资,将其增加 1000。38正确的修改语句是:A39A.40update employee set salary = salary + 1000 where dept = 'sale' and name = 'Dora Muk';41B.42update employee set salary = salary + 1000 where name = 'Dora Muk';43C.44update employee set salary = salary + 1000;45D.46update employee set salary += 1000;474849--删除50现在 orders 表结构如下:51create table orders (52 id int primary key auto_increment,53 item_id int,54 amount int,55 unit_price decimal(12, 4),56 price decimal(12, 4),57 description varchar(2000),58 ts timestamp default now(),59 deal bool default false60);6162有一个业务系统会实时的将已经成交(deal 字段为 true)的订单数据转储,63现在我们仅需要一个清理程序,将已经成交的数据从 orders表删除并记录被删除的数据id。64下面哪个操作是对的? C6566A.在一个独立的定时任务中执行67delete68from orders;6970B.在一个独立的定时任务中执行71truncate orders;7273C.在一个独立的定时任务中执行74delete75from orders76where deal;7778D.建立视图79create view order_view as80select id, meta, content, created_at81from orders82where not deal;83并要求业务系统只能访问这个视图。