数据库
基本操作
1. 数据增添
- 对数据表 book 中所有字段进行插入
insert into book value ('红楼梦', '曹雪芹', 100, '清代长篇人情小说');
- 对数据表 book 中指定字段进行插入
insert into book(name, author) value ('红楼梦', '曹雪芹');
- 对数据表所有字段进行多行插入
insert into book values ('三国演义', '罗贯中', 88, '古典小说'),
('水浒传', '施耐庵', 120, '古典小说');
2. 数据查询
无条件查询:
<1> 查询数据表 book 中的所有信息
select * from book;
<2> 指定列查询
select name, author from book;
a> where
select name, author from book where name = '三国演义';
b> order by
order by 是数据库条件查询中的关键字, 使用时如果不指定升序还是降序, 默认为升序. asc 为升序, desc 为降序.
<1> 升序 asc
select name, price from book order by price asc;
<2> 降序
select name, price from book order by price desc;
c> 大于小于等于null
<1> 查询价格在 100 以下的书籍
select name, price from book where price < 100;
<2> 查询价格不小于 100 的书籍
select name, price from book where price >= 100;
<3> 查询价格作者是 null 的书籍的信息
select * from book where author is null;
d> between and
查询价格在 [ 80, 100] 的书的信息
select * from book where price between 80 and 100;
e> in
查询价格是 80, 88 的书籍和价格
select name, price from book where price in (80, 88);
f> 模糊匹配 like (_和%)
<1> % 能匹配多个字符.
select * from book where author like '罗%';
<2> _ 只能匹配多一个字符.
下面这个例子写的是两个_, 看起来不清楚, 像一个.
select * from book where author like '罗__';
g> group by
将书按名字进行分组(名字一样的是一组, 只显示一次)
select name from book group by name;
3. 数据修改
修改数据:
update 表名 set 列名1=新数据1,列名2=新数据2列名3=新数据3 where 查询条件
eg: 将名称是三国演义价格是90 的数据改成名字是红岩, 作者是罗广斌, 价格是110, 类别是长篇小说
update book set name = '红岩', author = '罗广斌', price = 110, sort = '长篇小说' where name = '三国演义' and price = 90;
4. 数据删除
delete from 表名 where 查询条件
delete from book where name = '红旗谱';