一、创建表
1. 创建数据库
CREATE DATABASE demo1;
2.删除数据库
DROP DATABASE demo1;
3.添加数据表的列
(列名,类型,约束条件)
alter table student add (
school VARCHAR(11)
)
4. 修改数据表的列类型
(alter table 表名 modify 列名 新类型)
alter table student modify school int
5. 修改数据表的名称
(alter table 旧表名 rename to 新表名)
alter table student rename to students
6. 修改数据表的列名
(alter table 表名 change 旧列名 新列名 类型)
alter table students change social socials varchar(11)
7. 删除数据表的列
(alter table 表名 drop 列名)
alter table student drop school
8. 添加数据
(insert into 表名(列名) value(列值))
insert into students(name,StuNo,age,sex) values('aa','1001','3','0')
9. 删除数据
(delete from 表名)删除整张表
delete from students
10. 根据条件删除数据(delete from 表名 where 列名=值)
delete from students where StuNo='1001'
11. 修改数据
(update 表名 set 列=新值 where 旧列名=旧值)
update students set name='宝' where StuNo='1001'
二、查询
1.查询语句
select * (代表所有) from 表名
select * from students
2.查询指定列语句
select (指定查询的列)name,addres from 表名
select name,age from students
3.条件查询语句
select * from 表名 where 列名=值
-
比较运算符
= > < >= <= != 或<>
-
逻辑运算符有
and(并且,与),or(或者),not(非)
select * from students where StuNo=1002
select * from students where age>7
#eg:学号是001并且大于10岁的人
select * from students where StuNO=1002 and age>10
#eg:学号是001或大于10岁的人
select * from students where StuNO=1002 or age>10
#eg:学号是001或大于等于5岁并且小于等于30的人
select * from students where StuNO=1002 or age>=5 and age<=30
select * from students where StuNO=1002 or age between 5 and 30
4.模糊查询
select * from 表名 where 列名 like(关键字)’%(百分号)’
- %(百分号)多个任意字符
- _(下划线)任意一个字符
select * from students where name like '%2'
5.范围查询
- in
- between
- and
select * from students where name in('宝')
6.排序
- order by asc 升序(从小到大)
- order by desc(从大到小)
- 默认是asc(升序)
select * from students order by age asc
7.聚合函数
- 数量,总量 count(*)
- 最大 max(*)
- 最小 min(*)
- 平均值 avg(*)
- 求和,总和 sum(*)
select count(*) from students
select max(age) from students
select name,min(age) from students
select avg(age) from students
select sum(age) from students
8.分组
group by(关键字) having (分组之后的再次筛选)
SELECT * FROM 表名 GROUP BY 需要分组的列名
select sex,count(*) from students GROUP BY sexselect sex,count(*) from students GROUP BY sex having COUNT(*)>1
9.top几
limit(限制显示行数)从几开始,显示接下来的几个
select * from students LIMIT 0,3
10.顺序
- select --> where -->group by --> having --> order by
三、连接
左连接 ,右连接 ,内连接
左连接(左表为基础连接其他表)
右连接(右表为基础连接其他表)
内连接(将连接表的所有数据显示)
基本用不到
完整外连接(full join)
交叉连接(cross join)
alter table students add(
courseId varchar(30)
)
create table course(
courseId varchar(30) primary key,
name varchar(30)
)
1.左连接
left join(关键字) on(列名连接)
select * from 表名 left join 表2 on 表1.表1列名=表2.表2列名
select * from students left JOIN course on students.courseId=course.courseId
起别名
1.空格后加别名名称
2.as(关键字)后加别名名称
select * from students a left join course b on a.courseId=b.courseId
select * from students as a left join course as b on a.courseId=b.courseId
2.左连接连n张表
select * from students as a
left join course as b on a.courseId=b.courseId
left join clss as cla on a.StuId=cla.StuId
3.右连接
right join(关键字) on(列名连接)
select * from students as a
right join course as b on a.courseId=b.courseId
4.内连接
inner join(关键字) on(列名连接)
select * from students as a
inner join course as b on a.courseId=b.courseId
5.内连接用法2
select * from students as a ,
(select * from course) as b where a.courseId=b.courseId
6.子查询
select * from students where age>(select age from students where name='宝4')