SQL 速查

MySQL SQL 速查

把 25 讲里出现的语句按主题汇总,每条都标了出处,方便写作业和上机时对照。

数据库操作 16 条

4.1 数据库操作 · 查看上下文 →

SQL
create database 软件3班; #创建数据库

4.1 数据库操作 · 查看上下文 →

SQL
show databases; #显示其中所有数据库名

4.1 数据库操作 · 查看上下文 →

SQL
Use 软件3班; #选择数据库

4.1 数据库操作 · 查看上下文 →

SQL
create DATABASE 软件4班 DEFAULT CHARACTER SET GB2312 DEFAULT collate GB2312_chinese_ci;

4.1 数据库操作 · 查看上下文 →

SQL
show create database 软件4班;

4.1 数据库操作 · 查看上下文 →

SQL
show create database 软件3班;

4.1 数据库操作 · 查看上下文 →

SQL
CREATE DATABASE `软件3班` /*!40100 DEFAULT CHARACTER SET latin1 */

4.1 数据库操作 · 查看上下文 →

SQL
alter DATABASE 软件3班 DEFAULT CHARACTER SET GB2312;

4.1 数据库操作 · 查看上下文 →

SQL
alter DATABASE 软件3班 DEFAULT collate GB2312_chinese_ci;

4.1 数据库操作 · 查看上下文 →

SQL
alter DATABASE 软件3班 DEFAULT CHARACTER SET GB2312 DEFAULT collate GB2312_chinese_ci;

4.1 数据库操作 · 查看上下文 →

SQL
drop database 软件3班;

4.1 数据库操作 · 查看上下文 →

SQL
show databases;

4.1 数据库操作 · 查看上下文 →

SQL
create database 库名 [DEFAULT CHARACTER SET 字符集]

4.1 数据库操作 · 查看上下文 →

SQL
alter DATABASE 库名 DEFAULT CHARACTER SET 字符集名 DEFAULT collate 排序规则名;

4.1 数据库操作 · 查看上下文 →

SQL
show create database 库名;

4.2 数据表操作 · 查看上下文 →

SQL
Use 软件4班; #切换数据库到“软件4班”

数据表与约束 44 条

4.2 数据表操作 · 查看上下文 →

SQL
create table 学生表

4.2 数据表操作 · 查看上下文 →

SQL
create table 学生表1(学号 varchar(3) not null,姓名 varchar(4), 性别 varchar(1),年龄 int, 生日 date, 成绩 float);

4.2 数据表操作 · 查看上下文 →

SQL
ALTER TABLE 学生表1 ADD PRIMARY KEY (学号);

4.2 数据表操作 · 查看上下文 →

SQL
create table 学生表2 (学号 varchar(3), 姓名 varchar(4), 性别 varchar(1),年龄 int, 生日 date, 成绩 float, primary key (学号));

4.2 数据表操作 · 查看上下文 →

SQL
show create table 学生表2;

4.2 数据表操作 · 查看上下文 →

SQL
create table 学生表3

4.2 数据表操作 · 查看上下文 →

SQL
create table 学生表4

4.2 数据表操作 · 查看上下文 →

SQL
ALTER TABLE 学生表4 ALTER 性别 SET DEFAULT ‘女’;

4.2 数据表操作 · 查看上下文 →

SQL
show create table 表名;

4.2 数据表操作 · 查看上下文 →

SQL
Alter table 旧表名 rename 新表名;

4.2 数据表操作 · 查看上下文 →

SQL
Alter table 学生表 rename 学生信息表;

4.2 数据表操作 · 查看上下文 →

SQL
Alter table 表名 change 原字段名 新字段名 数据类型定义

4.2 数据表操作 · 查看上下文 →

SQL
Alter table 表名 change 字段名 字段名 新数据类型

4.2 数据表操作 · 查看上下文 →

SQL
SHOW CREATE table 学生表;

4.2 数据表操作 · 查看上下文 →

SQL
Alter table 表名 modify 字段名 数据类型定义

4.2 数据表操作 · 查看上下文 →

SQL
Alter table 表名 add 字段名 数据类型

4.2 数据表操作 · 查看上下文 →

SQL
Alter table 学生表 add 籍贯 varchar(10);

4.2 数据表操作 · 查看上下文 →

SQL
Alter table 表名 drop 字段名

4.2 数据表操作 · 查看上下文 →

SQL
Alter table 学生表 drop 籍贯;

4.2 数据表操作 · 查看上下文 →

SQL
alter table 表名 drop primary key;

4.2 数据表操作 · 查看上下文 →

SQL
alter table 学生表 drop primary key;

4.2 数据表操作 · 查看上下文 →

SQL
alter table 表名 add primary key (字段名);

4.2 数据表操作 · 查看上下文 →

SQL
alter table 学生表 add primary key (学号);

4.2 数据表操作 · 查看上下文 →

SQL
alter table 表名 modify 主键字段 数据类型 primary key;

4.2 数据表操作 · 查看上下文 →

SQL
Alter table表名 change 主键字段 主键字段 数据类型 primary key;

4.2 数据表操作 · 查看上下文 →

SQL
alter table 学生表 modify 学号 varchar(3) primary key;

4.2 数据表操作 · 查看上下文 →

SQL
Alter table 学生表 change 学号 学号 varchar(3) primary key;

4.2 数据表操作 · 查看上下文 →

SQL
Drop table 表名

4.2 数据表操作 · 查看上下文 →

SQL
Drop table 学生表3;

4.2 数据表操作 · 查看上下文 →

SQL
如:Alter table 学生表 add 籍贯 varchar(10);

数据操作(增删改) 31 条

4.3 数据操作 · 查看上下文 →

SQL
Update 表名 set 字段名=值 [where 条件]

4.3 数据操作 · 查看上下文 →

SQL
Update student set pwd=’ABCDEFG’ where stuname=’ 林斌’

4.3 数据操作 · 查看上下文 →

SQL
update 表名 set 字段名1=新值1,字段名2=新值2 where 条件

4.3 数据操作 · 查看上下文 →

SQL
Update student set pwd=’ 88888888’,classno=’20000002’ where stuname=’ 林斌’

4.3 数据操作 · 查看上下文 →

SQL
Update student set pwd=’12345678’

4.3 数据操作 · 查看上下文 →

SQL
Update student set 成绩=80

4.3 数据操作 · 查看上下文 →

SQL
Update student set 成绩=成绩+5 where ClassNo='20000001'

4.3 数据操作 · 查看上下文 →

SQL
delete from 表名 [where 条件]

4.3 数据操作 · 查看上下文 →

SQL
Delete from 表名 where 条件

4.3 数据操作 · 查看上下文 →

SQL
Delete from 表名

4.4 多表操作 · 查看上下文 →

SQL
Insert into 目的表 select * from 源表

4.4 多表操作 · 查看上下文 →

SQL
Insert into 学生表2 select * from student;

4.4 多表操作 · 查看上下文 →

SQL
如:insert into 学生表3(stuno,stuname) select stuno,stuname from student;

4.4 多表操作 · 查看上下文 →

SQL
insert into 学生 select * from student where classno='20000001';

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
INSERT INTO l VALUES ('1', '2'), ('3', '4');

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
INSERT INTO R VALUES (‘5', ‘6‘, ’7’), (‘8', ‘9‘, ’0’), (‘a', ‘b‘, ’c’) ;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
update r set e='2';

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
修改数据:update r set A=‘3';

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
Update 表名 set 字段名=新值 where 子查询;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
update student set pwd='123456' where ClassNo in (select classno from class where ClassName like '00%');

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
delete from 表名 where 子查询;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
delete from student where ClassNo in (select classno from class where ClassName like '00%');

4.5 综合练习 · 查看上下文 →

SQL
insert into 表2(字段1, 字段2,……) select 字段1, 字段2,…… from 表1 where 条件

4.5 综合练习 · 查看上下文 →

SQL
insert into 得票表(姓名,性别) select 姓名,性别 from 选举表

5.2 字符函数 · 查看上下文 →

SQL
update student set stuname=ltrim(stuname)

5.2 字符函数 · 查看上下文 →

SQL
update student set stuname=rtrim(stuname)

5.2 字符函数 · 查看上下文 →

SQL
update student set stuname=replace(stuname,'丽','莉') where stuname like '%丽%'

5.2 字符函数 · 查看上下文 →

SQL
update student set stuname=replace(stuname,'莉','丽') where stuname like '%莉%'

5.2 字符函数 · 查看上下文 →

SQL
update student set stuname=concat('中国',rtrim(StuName));

5.2 字符函数 · 查看上下文 →

SQL
update student set stuname=replace(stuname,'中国','') where stuname like '%中国%‘

分组与聚合 19 条

5.1 聚合函数 · 查看上下文 →

SQL
Sum(字段名)

5.1 聚合函数 · 查看上下文 →

SQL
select sum(credit) as '学分和' from course

5.1 聚合函数 · 查看上下文 →

SQL
AVG(字段名)

5.1 聚合函数 · 查看上下文 →

SQL
select avg(credit) as '平均值' from course

5.1 聚合函数 · 查看上下文 →

SQL
Max(字段名)

5.1 聚合函数 · 查看上下文 →

SQL
select max(credit) as '最大值' from course

5.1 聚合函数 · 查看上下文 →

SQL
Select max(teacher) as '最大值' from course

5.1 聚合函数 · 查看上下文 →

SQL
Min(字段名)

5.1 聚合函数 · 查看上下文 →

SQL
select min(credit) as '最小值' from course

5.1 聚合函数 · 查看上下文 →

SQL
Select min(teacher) as '最小值' from course

5.1 聚合函数 · 查看上下文 →

SQL
Count(字段名), 也可以用 count(*)

5.1 聚合函数 · 查看上下文 →

SQL
select count(credit) as '行数' from course where credit=2

5.1 聚合函数 · 查看上下文 →

SQL
select count(*) as '总行数' from course

5.1 聚合函数 · 查看上下文 →

SQL
Select 字段名 from 表名 group by 字段名

5.1 聚合函数 · 查看上下文 →

SQL
select kind, count(*) as '计数' from course group by kind

5.1 聚合函数 · 查看上下文 →

SQL
Select kind,sum(credit) as '学分和' from course group by kind

5.1 聚合函数 · 查看上下文 →

SQL
select kind,avg(credit) as '学分平均' from course group by kind

5.1 聚合函数 · 查看上下文 →

SQL
select kind,count(*) as '课程数',sum(credit) as '学分和', avg(credit) as '学分平均' from course group by kind

5.1 聚合函数 · 查看上下文 →

SQL
select kind, SUM(credit) as '学分和' from course group by kind having sum(credit)>9

多表连接 22 条

4.4 多表操作 · 查看上下文 →

SQL
或select classname,stuno,stuname from class join student on class.ClassNo=student.ClassNo;

4.4 多表操作 · 查看上下文 →

SQL
select a,b from A union select c,d from B;

4.4 多表操作 · 查看上下文 →

SQL
select classno,classname from class union select classno,stuname from student;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
Select 字段列表 from 左表 cross join 右表

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
Select * FROM L CROSS JOIN R

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
Select 字段列表 from 左表 join 右表 on 条件

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select * from l join r;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select * from l join r on a>1;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select A,B,C from l join r on a>1 and c='8';

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select * from l join r on a>1 where c='8';

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
等同于select * from l join r on a>1 and c='8';

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select classname,stuname,pwd from student join class on student.classno=class.ClassNo;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select * from l join r on b>e;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select * from l join r on a<e;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select L.A, B,C,D,R.A as 'ra' from L join R on R.A=L.A;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
再看自然连接:select L.A, B,C,D from L join R on R.A=L.A;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select * from l left join r on 1; (#on表示为真)

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select L.A, B,C,D,R.A as 'ra' from L left join R on L.A=2;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select * from l right join r ON 1;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select L.A, B,C,D,R.A as 'ra' from L right join R on L.A=2;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select * from L full join R on 1;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select a,b from l union select c,d from r;

查询与排序 44 条

3.1 相等查询 · 查看上下文 →

SQL
select * from student where stuname='林斌‘

3.1 相等查询 · 查看上下文 →

SQL
select 字段列表 from 表名 [where 条件]

3.1 相等查询 · 查看上下文 →

SQL
select * from student;

3.1 相等查询 · 查看上下文 →

SQL
Select classno, stuname from student;

3.1 相等查询 · 查看上下文 →

SQL
Select * from student where classno='20000002';

3.1 相等查询 · 查看上下文 →

SQL
Select classno, stuname from student where classno='20000002';

3.2 模糊查询 · 查看上下文 →

SQL
select * from student where stuname like '林%'

3.2 模糊查询 · 查看上下文 →

SQL
select * from student where stuname like '%丽%'

3.2 模糊查询 · 查看上下文 →

SQL
select * from student where stuname like '%丽'

3.2 模糊查询 · 查看上下文 →

SQL
select * from student where stuname like '_丽%'

3.2 模糊查询 · 查看上下文 →

SQL
select * from student where stuname like '__丽%'

3.5 显示限制(LIMIT) · 查看上下文 →

SQL
select * from student limit [m,]n

3.5 显示限制(LIMIT) · 查看上下文 →

SQL
select * from student limit 5;

3.5 显示限制(LIMIT) · 查看上下文 →

SQL
select * from student limit 4,6;。

3.5 显示限制(LIMIT) · 查看上下文 →

SQL
select distinct ClassNo from student;

3.5 显示限制(LIMIT) · 查看上下文 →

SQL
select stuno as '学号', stuname as '姓名',pwd from student

3.6 排序(ORDER BY) · 查看上下文 →

SQL
select * from student order by stuname asc

3.6 排序(ORDER BY) · 查看上下文 →

SQL
select * from student order by stuname desc

3.6 排序(ORDER BY) · 查看上下文 →

SQL
select * from student order by classno asc, stuname desc

4.3 数据操作 · 查看上下文 →

SQL
select * from Student

4.3 数据操作 · 查看上下文 →

SQL
SELECT STUNAME, PWD FROM STUDENT

4.3 数据操作 · 查看上下文 →

SQL
SELECT STUNAME, stuno FROM STUDENT

4.3 数据操作 · 查看上下文 →

SQL
select * from student where classno='20000001'

4.3 数据操作 · 查看上下文 →

SQL
select stuno,stuname from student where classno='20000001‘

4.4 多表操作 · 查看上下文 →

SQL
如:select classname,stuno,stuname from class,student where class.ClassNo=student.ClassNo;

4.4 多表操作 · 查看上下文 →

SQL
select 字段列表2 from 表2 where 条件。

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
Select 字段列表 from 左表, 右表 where 条件

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select A,B,C from l,r where a>1 and c='8';

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
或:select classname,stuname,pwd from student, class where student.classno=class.ClassNo;

4.4+ 多表操作(进阶) · 查看上下文 →

SQL
select * from student where ClassNo=(select classno from class where ClassName='00电子商务');

字符函数 14 条

5.2 字符函数 · 查看上下文 →

SQL
Left(字符串, n)

5.2 字符函数 · 查看上下文 →

SQL
Select left('青岛港湾职业技术学院',2)

5.2 字符函数 · 查看上下文 →

SQL
right(字符串, n)

5.2 字符函数 · 查看上下文 →

SQL
Select right('青岛港湾职业技术学院',2)

5.2 字符函数 · 查看上下文 →

SQL
Substring(字符串,m,n) 或写成substr(字符串,m,n)

5.2 字符函数 · 查看上下文 →

SQL
Select substring('青岛港湾职业技术学院',3,2)

5.2 字符函数 · 查看上下文 →

SQL
Replace(字符串1,字符串2,字符串3)

5.2 字符函数 · 查看上下文 →

SQL
Lower()

5.2 字符函数 · 查看上下文 →

SQL
Select lower('ABCD') abcd

5.2 字符函数 · 查看上下文 →

SQL
Upper()

5.2 字符函数 · 查看上下文 →

SQL
select UPPER('abcd') ABCD

5.2 字符函数 · 查看上下文 →

SQL
select concat(CAST(123 AS char),'456');

5.2 字符函数 · 查看上下文 →

SQL
Concat(‘字符串1’,’字符串2’)

5.2 字符函数 · 查看上下文 →

SQL
select concat('123','456');

日期与数学函数 14 条

5.3 日期时间函数 · 查看上下文 →

SQL
select HOUR(now());

5.3 日期时间函数 · 查看上下文 →

SQL
select MINUTE(now());

5.3 日期时间函数 · 查看上下文 →

SQL
select SECOND(now());

5.3 日期时间函数 · 查看上下文 →

SQL
select datediff(now(),'2023-10-1');

5.3 日期时间函数 · 查看上下文 →

SQL
12、DATE_ADD(日期1,INTERVAL n 类型);

5.3 日期时间函数 · 查看上下文 →

SQL
select DATE_ADD(now(),INTERVAL 2 year);

5.3 日期时间函数 · 查看上下文 →

SQL
select DATE_sub(now(),INTERVAL 2 year);

5.4 数学函数 · 查看上下文 →

SQL
select SQRT(3)

5.4 数学函数 · 查看上下文 →

SQL
select round(10.3454,2)

5.4 数学函数 · 查看上下文 →

SQL
select round(10.3445,2)

5.4 数学函数 · 查看上下文 →

SQL
select 10*rand() 0到10之间,不包括0和10 0.0001-----9.9999

5.4 数学函数 · 查看上下文 →

SQL
select ceiling(10*rand()) 1到10的整数,包括1和10

5.4 数学函数 · 查看上下文 →

SQL
select floor(10*rand()) 0到9的整数,包括0和9

5.4 数学函数 · 查看上下文 →

SQL
select ceiling(52*rand()) 1---52 类似:抽大奖,抽号码