数据库
四、表中数据的增删查改
(说明:该博客中表的操作一律默认为是对 stu 表的一系列操作)
(一)简单查询:select
select* from stu; //从 stu 表中查看所有信息
(二)新增:insert
(1)新增单个记录:
insert into stu values(1001,'张三',18,'男',178,56.6);
// 插入数据的顺序需要与创建表的顺序相同
(2)新增指定信息:
//insert into stu (name,sn,sex) value('李四',1002,'男');
//指定顺序,指定内容插入
(3)新增多个记录:
insert into stu values(1003,'王伊',19,'女',156,45.5),
(1004,'李梅',20,'女',178,56.3);
// 一次性插入多条记录
(三)修改数据:update
// update <表名> set <列名1>=<修改后的值>,<列名2>=<值>,......, where <条件语句>
update stu set weight=65.3,age=19 where sn=1004;
// 更新 stu 表中学号为 1004 的学生的体重和年龄
// 若未添加 where 条件,默认为表中列的数据全部都修改
(四)删除数据:delete
delete from stu ; // 表示删除 stu 表中所有信息
delete from stu where sn=1002;
//where 引出条件,表示删除学号为 1002 的学生信息
(五)查询: select
1)查询所有信息:
select * from stu;
2)查询指定列信息:
select sn,height,weight from stu;
3)表达式查询:
例如:
select height+weight from stu;
// 查询身高与体重的和
4)取别名操作:as
select height+weight as hw from stu;
// as 用来定义别名,也可以省略
(六)去重查找:distinct
(七)排序:order by
select * from stu order by height desc / asc;
//将身高降序排列
// asc 升序排序,一般默认升序排列
//desc 降序排序
(八)分页查询:limit…offset…
limit 后边跟一个数字:表示限制最多获取多少数据
offset 后边跟一个数字:描述从第几条数据开始进行获取
select* from stu limit 3;
//查询前三条信息
select* from stu order by height desc limit 3;
// 按身高降序排列,并展示前三条记录
select* from stu order by height desc limit 3 offset 1;
// 按照身高降序排列,并从第一个数据开始展示三条记录(offset 表示距起始记录的偏移量)
(九)条件查询:select … where
关系运算符
1)where ‘=’
2)关系运算符 :
(大于)、 <(小于)、 >=(大于等于)、 <=(小于等于)、
!=(不等于)、<=> (等于,空值NULL查询时)、<>(不等于)
3)控制判断:
is null ;(空值查询判断)
is not null ;(非空值查询判断)
select * from stu where sn is null;
4)范围判断:between…and…
select* from stu where height between 170 and 180;
//查询身高170~180之间的信息,包含边界 []
5)子集匹配:in
select* from stu where name in('张三','王五');
6)模糊匹配:like
_:下划线,表示匹配单个任意字符
%:百分号,表示匹配任意长度字符
逻辑运算符
1)与:双目,and-连接两个比较条件,两者都为真则结果为真
2)或:双目,or-链接两个比较条件,一个为真结果为真
3)非:单目,not-连接单个条件
例子:
select* from stu where height>=160 and height<=180;
Tips:
博文内容为原创,有任何问题欢迎留言~