数据操作:增删改查
新增数据基本语法
Insert into 表名 [(字段列表)] values (值列表);
在数据插入的时候,假设主键对应的值已经存在:插入一定会失败!
主键冲突当主键存在冲突的时候(Duplicate key),可以选择 性的进行处理:更新和替换
主键冲突:更新操作
Insert into 表名 [(字段列表:包含主键)] values (值列表) on duplicate key update 字段=新值;
select * from my_class;
insert into my_class values ('Java0810','B205');
-- 主键冲突:更新
insert into my_class values('Java0810','B205')
-- 冲突处理
on duplicate key update
-- 更新教室
room = 'B205';
select * from my_class;
主键冲突:替换
replace into 表名 [(字段列表:包含主键)] values(值列表);
insert into my_class values('Java0710','B203');
select * from my_class;
-- 主键冲突:替换
replace into my_class values('Java0710','A203');
select * from my_class;
蠕虫复制:从已有的数据中去获取数据,然后将数据又进行新增操作:数据成倍的增加。
表创建高级操作:从已有表创建新表(复制表结构)
create table 表名 like 数据库.表名;
desc my_gbk;
-- 复制创建表
create table my_copy like my_gbk;
desc my_copy;
蠕虫复制:先查出数据,然后将查出的数据新增一遍
Insert into 表名[(字段列表)] select 字段列表/*from 数据表名;
desc my_collate_bin;
select * from my_collate_bin;
-- 蠕虫复制
insert into my_copy select * from my_collate_bin;
select * from my_copy;
蠕虫复制的意义
1.从已有表拷贝数据到新表中
2.可以迅速的让表中的数据膨胀到一定数量级:测试表的压力以及效率
更新数据基本语法
update 表名 set 字段 = 值 [where 条件];
高级新增语法
update 表名 set 字段 = 值 [where 条件][limit 更新数量];
-- 更新部分a变成c
update my_copy set name = 'c' where name = 'a' limit 3;
select * from my_copy;
与更新类似:可以通过limit来限制数量
Delete from 表名 [where条件][limit数量];
-- 删除数据:限制记录数为10
delete from my_copy where name = 'b' limit 10;
删除:如果表中存在主键自增长,那么当删除之后,自增长不会还原。
show create table student;
delete from student;
show create table student;
思路:数据的删除是不会改变表结构,只能删除表后重建表
truncate 表名; -- 先删除改变,后新增改变
show create table student;
-- 清空表:重置自增长
truncate student;
show create table student;
基本语法
select 字段列表/* from 表名 [where 条件];
完整语法
select[select选项] 字段列表 [字段别名]/* from 数据原 [where条件子句] [group by 子句] [having子句] [order by 子句] [limit子句];
Select选项select选项:select对查出来的结果的处理方式
All:默认的,保留所有的结果。
Distinct:去重,查出来的结果,将重复给去除(所有字段都相同)
字段别名:当数据进行查询出来的时候,有时候名字并不一定满足需求(多表查询的时候,会有同名字段)。需要对字段名进行重命名:别名
语法
字段名 [as] 别名;
select * from my_student;
-- 字段别名
select
id,
number as 学号,
name as 姓名,
sex 性别 from my_student;
insert into my_student values (1,'itcast0001','张三','男');
insert into my_student values (null,'itcast0002','李四','女');
数据源:数据的来源,关系型数据库的来源都是数据表:本质上只要保证数据类似二维表,最终都可以作为数据源。
数据源分为多种:单表数据源,多表数据源,查询语句
单表数据源 select * from 表名;
多表数据源:select * from 表名1,表名2....;
-- 多表数据源
select * from my_student,my_class;
从一张表中取出记录,去另外一张表中匹配所有记录,而且全部保留:(记录数和字段数),将这种结果称为:笛卡尔积没什么卵用,所以应该尽量避免。
子查询:数据的来源是一条查询语句(查询语句的结果是二维表)
select * from (select语句) as 表名;
-- 子查询
select * from my_student;
select * from (select * from my_student);
select * from (select * from my_student) as s;
where子句:用来判断数据,筛选数据。
where子句返回结果:0或者1,0代表false,1代表true。
判断条件:
比较运算符:>,-,=185 and height =2; select c_id, count(*) from my_student where count(*) >=2 group by c_id ;
2.having能够使用字段别名:where不能:where是从磁盘取数据,而名字只可能是字段名:别名是在字段进入到内存后才会产生。
select c_id,count(*) as total from my_student group by c_id having total>=2;
select c_id,count(*) as totabl from my_student where total>=2 group by c_id;
order by:排序,根据某个子弟进行升序或者降序排序,依赖校对集。
使用基本语法
order by 字段名 [asc|desc]; -- asc是升序(默认的),desc是降序
-- 排序
select * from my_student group by c_id;
select * from my_student order by c_id;
排序可以进行多字段排序:先根据某个字段进行排序,然后排序好的内部,再按照某个数据进行再次排序
select * from my_student order by c_id;
-- 多字段排序:先班级排序,后性别排序
select * from my_student order by c_id,sex desc;
Limit子句是一种限制结果的语句:限制数量。
Limit有两种使用方式
方案1:只用来限制长度(数据量):limit数据量;
-- 查询学生:前两个
select * from my_student limit 2;
方案2:限制起始位置,限制数量:limit起始位置,长度;
-- 查询学生:前两个
select * from my_student limit 0,2; -- 记录数是从0开始编号
select * from my_student limit 2,2;
select * from my_student limit 4,2;
Limit方案2主要用来实现数据的分页:为用户节省时间,提交服务器的响应效率,减少资源的浪费。
对应用户来讲:可以点击的分页按钮:1,2,3,4
对应服务器来讲:根据用户选择的页码来获取不同的数据:limit offset,length;
Length:每页显示的数据量:基本不变
Offset: offset = (页码 -1)* 每页显示量