1、SQL数据表操作
(想要注释掉的话,Ctrl + /)
1、1 查询
查询所有字段
select * from student
查询指定字段
select 列1,列2,… from 表名
select name,age from student2
1、2 创建表
create table 表名(
字段名 类型 约束
字段名 类型 约束
…
)
例如:创建学生表,字段要求如下:
姓名(长度为10)
create table students(
name varchar(10)
)
例:创建学生表,姓名(长度为10),年龄
(两个字段的话,第一个字段后面应该跟一个逗号)
create table student(
name varchar(10),
age int unsigned
)
例:创建学生表,字段要求年龄如下:
姓名(长度为10),年龄,身高(保留小数点后两位)
create table student2(
id int unsigned primary key auto_increment,
name varchar(10),
age int unsigned,
height decimal(5,2)
)
(第二行代码是用来约束,表示它的唯一性,主键,int,无符号,自动递增)
1、3 删除表
格式一:drop table 表名
格式二:drop table if exists 表名
例如:删除学生表
drop table student
drop table if exists student
如果表存在的话,先删除再去创建
drop table if exists student;
create table student(
name varchar(10),
age int unsigned
)
(两条完整的语句的话,第一个后面用分号)
1、4 添加数据
添加一个列
alter table student2 add column sex varchar(1)
所有字段设置值,值的顺序与表中字段的顺序对应
说明:主键列是自动增长,插入时需要占位,通常用0或者default或者null来占位,插入成功后以实际数据为准
insert into 表名 values(…)
例如:插入一个学生,设置所有字段信息
create table student2(
id int unsigned primary key auto_increment,
name varchar(10),
age int unsigned,
height decimal(5,2)
)
插入上表中
格式一:
insert into student2 values(0,'亚瑟',18,166.5)
格式二:部分字段设置值,值的顺序与给出的字段顺序对应
insert into 表名(字段1,…) values(值1,…)
insert into student2(name) values('小明')
添加多行数据
方式一:写多条 insert 语句,语句之间用英文分号隔开
insert into student2(name) values('小红');
insert into student2(name) values('小新');
insert into student2(name,age) values('哈哈',20)
方式二:写一条 insert 语句,设置多条数据,数据之间用英文逗号隔开
insert into student2(name) values('大桥1'),('大桥2'),('大桥3')
insert into student2(name,age) values('鲁班1',10),('鲁班2',30),('鲁班3',40)
1、5 修改数据
格式:update 表名 set 列1=值1,列2=值2… where 条件
例如:将所有人年龄修改为33岁
update student2 set age=33
例如:修改id为5的学生数据,姓名改为狄仁杰,年龄改为20
update student2 set name='狄仁杰',age=20 where id=5
修改列名
比如把表 student2 中id一列修改为studentNo
alter table student2 change id studentNo int
修改表名:alter table t_book rename to bbb;
添加列:alter table 表名 add column 列名 varchar(30);
删除列:alter table 表名 drop column 列名;
1、6 删除数据
delete from 表名 where 条件
(注意:要是后面不跟条件,则会清空整个表)
例如:删除id为6的学生数据
delete from student2 where id=6
逻辑删除:对于重要的数据,不能轻易执行delete语句进行删除,一旦删除,数据无法恢复,这时可以进行逻辑删除
(相当于假的删除,并不会真的把数据删掉)
1、给表添加字段,代表数据是否删除,一般起名isdelete,0代表未删除,1代表删除,默认值为0
2、若要删除某条数据时,只需要设置这条数据的isdelete字段为1
3、以后查询数据时,只查询出isdelete为0的数据即可
例如:
1、给学生表添加字段(isdelete),默认值为0,若表中已有数据,需要把所有数据的isdelete字段更新的0
update student2 set isdelete=0
2、删除id为1的学生
update student2 set isdelete=1 where id=1
3、查询未删除的数据
select * from student2 where isdelete=0
2、查询
2.1 简单查询
查询指定字段
select 列1,列2,… from 表名
select name,age from student2
2.2 字段起别名
可以使表头显示的更清楚,但不会修改表中的数据
select name as 姓名,age as 年龄 from student2
2.3 给表名起别名
但是前面列名要跟s.
(查询多个表时可使用)
select s.name,s.age from student2 as s
删除重复数据
比如说性别有很多重复的,在sex前面加一个distinct,可以删除只剩下一个男和一个女
select distinct sex from student2
多行的话也可以
select distinct sex,class from student2
3、条件查询
select 字段1,字段2… from 表名 where 条件
查询表student2中的第一行所有信息,id为1
*from
select * from student2 where id=1
只要姓名和性别
select name,sex from student2 where id=1
查询小红的年龄
select age from student2 where name = '小红'
3.1 比较运算符
>
<
=
!=
<>
>=
<=
查询20岁以下的学生
select * from student2 where age<20
查询不是男生的,即不等于男生
select * from student2 where sex != '男'
3.2 逻辑运算符
and 且
or 非
not 或
查询年龄小于20的女同学
select * from student2 where age<20 and sex = '女'
查询女学生或者年龄时18的学生
select * from student2 where sex='女' or age=18
查询非18岁的学生
(可以用!=,也可以用not)
select * from student2 where not age = 18
3.3 模糊查询
like
% 表示任意多个任意字符
-(下划线) 表示一个任意字符
查询姓大的学生
(%意思只要以大开头,无论后面有多少字符,都可以查询出来)
select * from student2 where name like '大%'
查找姓孙且名字时一个字的学生
select * from student2 where name like '孙_'
(后面是2个字的话就用两个下划线)
查询叫明的学生
select * from student2 where name like '%明'
查询姓名含小的学生
select * from student2 where name like '%小%'
3.4 范围查询
in 表示在一个非连续的范围内
查询家乡是北京或上海或陕西的学生
select * from student2 where hometown in('北京','上海','陕西')
between…and… 表示在一个连续的范围内
查询年龄为16到20的学生
select * from student2 where age between 16 and 20
(包含16和20,而且小值要写在前面)
3.5 空判断
null 与 ‘’ 空字符串是不同的
判空 is null
查询没有填写身高的学生
select * from student2 where height is null
查询填了身高的学生
select * from student2 where height is not null
4、排序
语法
select * from 表名
order by 列1 asc|desc,列2 asc|desc
(不加asc默认情况下就是升序)
select * from student2 order by age
select * from student2 order by age asc
降序
select * from student2 order by age desc
查询所有学生信息,按年龄从小到大排序,年龄相同时,再按学号从小到大排序
select * from student2 order by age asc,id asc
4.1 聚合函数
主要是为了快速得到统计数据,比如总列数,最大值,最小值,平均值,总和
count(*)表示计算总行数,括号中写星与列名,结果是相同的
聚合函数不能在where中使用
例一:查询学生总数(表中共有多少行)
(*)表示只要一行中有字段,就会被统计进去
select count(*) from student2
统计年龄
select count(age) from student2
(统计的时候,null不算在内,空字段算的)
一般都用count(*)
例二:查询女生中的最大年龄
select max(age) from student2 where sex = '女'
例三:查询男生中最低身高
select min(height) from student2 where sex = '男'
例四:查询北京学生的年龄总和
select sum(age) from student2 where hometown = '北京'
例五:查询女生的平均年龄
select avg(age) from student2 where sex = '女'
4.2 分组
group by
比如,按性别分组
select * from student2 group by sex
(按性别分为两组,随机取一个人)
可以对分组后的数据进行统计,做聚合运算
例一:查询各种性别的人数
select sex,count(*) from student2 group by sex
例二:查询各种年龄的人数
select age,count(*) from student2 group by age
例三:查询男生总人数
(用having过滤掉,having只能用在group by后面)
select sex,count(*) from student2 group by sex having sex = '男'
也可用这种方式
select count(*) from student2 where sex = '男'
4.3 分页
获取部分行
当数据量过大时,在一页中查看数据是一件非常麻烦的事
select * from 表名
limit start,count
例一:获取前3行学生信息
(0,3 表示从0位置开始,获取其中3条)
select * from student2 limit 0,3
4,4 表示从4位置开始,获取其中4条
select * from student2 limit 4,4
分页
已知:每页显示m条数据,求:显示第n页的数据
select * from students limit (n-1)*m,m
例如:表中共有13条记录,想要分每页5条显示,一共可以分3页
select * from student2 limit 5*(n-1),5
第一页
(从第0 条开始取5条)
select * from student2 limit 0,5
第二页
(从第5条开始取5条)
select * from student2 limit 5,5
第三页
(从第10条开始取3条)
select * from student2 limit 10,3