MySQL篇 ---- 基础语法

                                        下一篇:多表连接查询

1.sql语言中的注释

  • -- 行注释
  • /* 注释内容 */

2.mysql的常用数据类型

3.数据库中的元素

  • 数据库-----database
  • 表----table
  • 字段(列)---field
  • 记录(行)---record

4.创建表

  • 语法:create table 表名(字段名 字段类型, 字段名 字段类型)
-- 例 1:创建表 a,字段要求:name(姓名),数据类型:varchar(字符串),长度为 10
CREATE TABLE a(name varchar(10));
  • 创建两个字段的表
-- 例 2:创建表 b,字段要求: name(姓名),数据类型为 varchar(字符串),长度为 10; 
-- height(身高),数据类型为 decimal(小数),一共 5 位, 其中 3 位整数,2 位 小数。
CREATE TABLE b(
	name varchar(10),
	height DECIMAL(5, 2)
);
  • 创建三个字段的表
-- 例 3:创建表 c,字段要求如下:
-- id:数据类型为 int (整数); 
-- name 姓名:数据类型为 varchar(字符串)长度为 20, 
-- age 年龄:数据类型为 tinyint unsigned(无符号小整数);
CREATE TABLE c(
	id int,
	name VARCHAR(20),
	age TINYINT UNSIGNED
);

5.插入数据

  • 语法 : insert into 表名 values (值, 值,值);
-- 往表c插入一条记录
INSERT into c VALUES (0, '张飞', 30);
  • 指定字段插入,语法:insert into 表名 (字段名, 字段名) values (值, 值);
-- 例 2:表 c 插入一条记录,只设置 id 和姓名 name
INSERT into c (id, name) values (3, '曹操');
-- 例 2:表 c 插入一条记录,只设置 id 和姓名 age
INSERT into c (id, age) values (4, 100);

6.插入多条记录

  • 多条insert语句,用分号隔开
-- 例 3:表 c 插入三条记录,写三条 insert 语句, 语句之间用分号隔开
INSERT into c values (5, '周瑜', 50);
insert into c (id, name) values (6, '鲁肃');
INSERT into c (name) values ('诸葛亮');
  • 一条insert插入多条记录
  • 语法:insert into 表名 values (值,值),(值, 值),(值,值);
-- 例 4:表 c 插入多条记录,用一条 insert 语句, 数据之间用逗号隔开
insert into c values (10, '张三', 10),
(11, '李四', 20),
(12, '王五', 30);
  • 一条insert指定字段插入多条记录
  • 语法:insert into 表名 (字段名, 字段名) values (值, 值), (值, 值), (值, 值)
-- 例 4:表 c 插入多条记录,用一条 insert 语句, 数据之间用逗号隔开
insert into c (id, name) values (13, '光绪'),
(14, '康熙'),
(15, '雍正');

7.select查询表

查询所有字段

  • 语法: select * from 表名;

  • 查询表的所有字段

-- 查询表c的所有字段
select * from c;

指定字段名查询

  • 语法:select 字段名, 字段名 from 表名;
-- 查询表c的id字段
select id from c;
-- 查询表c的id和age字段
select id, age from c;
-- 查询表c的所有字段,但顺序自定义
select name, id, age from c;

8.update修改数据

  • 语法:update 表名 set 字段=值, 字段=值 where 条件
    • 如果没有where 条件代表修改表中所有的记录
-- 例 1:修改表 c,所有人的年龄(age 字段)改为 50
update c set age = 50;
  • 带有条件的update语句
-- 例 2:修改表 c,
-- id 为 3 的记录,
-- 姓名(name 字段)改为 ‘狄仁杰‘,年龄(age 字段)改为 20

update c set name = '狄仁杰', age = 20 where id = 3;
  • 另外的例子
-- 修改name为刘备的记录为李白
update c set name = '李白' where name = '刘备';
-- id大于10的记录,长一岁
update c set age = age + 1 where id > 10;

9.delete删除记录

  • 语法:'delete from 表名 where 条件'
-- 例 1:删除表 c 中 id 为 6 的记录
DELETE from c where id = 6;
-- 例 1:删除表 c 中 name 为 诸葛亮 的记录
DELETE from c where name = '诸葛亮';

-- 删除年龄大于50的记录
DELETE from c where age > 50;

-- 删除id小于3的记录
delete from c where id < 3;

-- 删除所有记录
DELETE from c;

10.truncate table删除表的数据

  • truncate table 表名
-- 删除表c中所有的记录
truncate table c;

11.小结

    • insert
    • delete
    • update
    • select

12.删除表

  • 语法一: drop table 表名
-- 删除表a
drop table a;
  • 语法二: drop table if exists 表名
-- 如果表a存在,就删除表a,如果不存在,什么也不做
DROP table if EXISTS a;

-- 如果表b存在,就删除表b,如果不存在,什么也不做
DROP table if EXISTS b;

13.字段的约束

14.主键

  • 主键的值不能重复
  • 自增长,auto_increment
    • 值会系统自动维护,自动增长
-- 例 1:创建表 d,字段要求如下: 
-- id : 数 据 类 型 为 
-- int unsigned( 无 符 号 整 数 ) ) ,primary key( 主键),auto_increment(自增长); 
-- name 姓名:数据类型为 varchar(字符串)长度为 10; 
-- age 年龄:数据类型为 int(整数);
create TABLE d (
id int UNSIGNED PRIMARY key auto_increment,
name varchar(10),
age int);

INSERT into d (name, age) values ('张飞', 30);

-- 插入的时候指定了id的值
INSERT into d (id, name, age) values (6, '曹操', 30);
-- 不指定id的值
INSERT into d (name, age) values ('周瑜', 30);
select * from d;
-- 如果不指定字段,主键自增长字段的值可以用占位符,0或者null
INSERT into d VALUES (0, '康熙', 30);
INSERT into d VALUES (NULL, '溥仪', 50);

15.非空

  • 非空not null
    • 这个字段必须有值,如果没有值,insert插入会失败
-- 例 1:创建表 e,字段要求如下: 
-- id:数据类型为 int unsigned (无符号整数);
--  name 姓名:数据类型为 varchar(字符串)长度为 10,not null(非空), 
-- age 年龄:数据类型为 int(整数);

CREATE table e (
id int UNSIGNED,
name varchar(10) not null,
age int);

insert into e values (1, '张三', 20);
insert into e (id, age) values (1, 20);
select * from e;

16.唯一

  • 唯一unique
    • 字段的约束为唯一,表示字段的值不能重复
-- 例 1:创建表 f,字段要求如下: 
-- id:数据类型为 int (整数); 
-- name 姓名:数据类型为 varchar(字符串)长度为 10,unique(唯一); 
-- age 年龄:数据类型为 int(整数);
CREATE TABLE f (
id int,
name varchar(10) UNIQUE,
age int);

insert into f VALUES (1, '张三', 20);
insert into f VALUES (2, '李四', 20);
select * from f;

17.默认值

  • default 值
  • 当一个字段有默认值约束,插入数据时,如果指定了值,那么默认值无效,如果没有指定值,会使用默认值
-- 例 1:创建表 g,
-- 字段要求如下: 
-- id:数据类型为 int (整数); 
-- name 姓名:数据类型为 varchar(字符串)长度为 10; 
-- age 年龄:数据类型为 int(整数),default(默认值)30;
CREATE TABLE g(
id int,
name varchar(10),
age int DEFAULT 30);
insert into g values (1, '张三', 20);
-- 插入的时候不指定age的值
insert into g (id, name) values (2, '李四');

select * from g;

18.字段的别名

  • 通过 字段名 as 别名 的语法,可以给字段起一个别名,别名可以是中文
  • as可以省略
  • 字段名 as 别名 和 字段名 别名 结果是一样的
-- 通过as 给字段起一个别名
select card as 身份证, name as 姓名,sex as 性别 from students;

-- 别名的as可以省略
select card 身份证, name 姓名,sex 性别 from students;

19.表的别名

  • 通过 表名 as 别名 给表起一个别名
  • as可以省略
-- 通过as 给表students起一个别名
select * from students as stu;

-- 可以省略as
select * from students stu;

20.distinct过滤重复记录

  • 通过select distinct 字段名, 字段名 from 表名 来过滤select查询结果中的重复记录
SELECT DISTINCT sex, class from students;

21.where子句

  • where 后面跟一个条件,实现有选择的查询
  • select * from 表名 where 条件
-- 例 1:查询 students 表中学号 studentNo 等于’001’ 的记录
select * from students where studentNo = '001';
-- 例 2:查询 students 表中年龄 age 等于 30 的姓名 name,班级 class
select name, class from students where age = 30;

22.select查询的基本规律

  • select * 或者select 字段名 控制了查询返回什么样的字段(列)
  • where 条件 控制了查询返回什么样的记录(行)

23.比较运算符

  • =等于
  • < 小于
  • <= 小于等于
  • >大于
  • >=大于等于
  • !=和<>不等于
-- 例 1:查询 students 表中 name(姓名)等于’小乔’学生的 age(年龄)
select age from students where name = '小乔';

-- 例 2:查询 students 表中 30 岁以下的学生记录
SELECT * from students where age < 30;

-- 例 2:查询 students 表中 30 岁和30岁以下的学生记录
SELECT * from students where age <= 30;

-- 查询家乡不在'北京'的学生记录
select * from students where hometown != '北京';
select * from students where hometown <> '北京';

24.逻辑运算符

  • and与
    • 条件1 and 条件2
    • 两个条件必须都满足
  • or或
    • 条件1 or 条件2
    • 两个条件只要有一个满足即可
  • not非
    • not 条件
    • 条件成立,not以后就不成立,条件不成立,not以后就成立
-- 例 1:查询 age 年龄小于 30,并且 sex 性别为’女’的同学记录
SELECT * from students where age < 30 and sex = '女';

-- 例 2:查询 sex 性别为’女’或者 class 班级为'1 班'的学生记录
SELECT * from students where sex = '女' or class = '1班';

-- 例 3:查询 hometown 老家非’天津’的学生记录
SELECT * from students where not hometown = '天津';

-- 例 3:查询 hometown 老家’天津’的学生记录
SELECT * from students where not hometown != '天津';

25.模糊查询

  • like实现模糊查询
  • %代表任意多个字符
  • _代表任意一个字符
  • 字段名 like '字符%'
    • 指定字符开始,后面任意多个字符
-- 例 1:查询 name 姓名中以’孙’开头的学生记录
SELECT * from students where name like '孙%';

-- 例 2:查询 name 姓名以’孙’开头,且名只有一个字的学生记录
SELECT * from students where name like '孙_';
-- 例 3:查询 name 为任意姓,名叫’乔’的学生记录
SELECT * from students where name like '%乔';

-- 查询 name 姓名有’白’子的学生记录
SELECT * from students where name like '%白%';

26.范围查找

  • in (值, 值, 值)
    • 非连续范围查找
  • between 开始值 and 结束值
    • 连续范围查找,包含开始值 包含 结束值
-- 例 1:查询 hometown 家乡是’北京’或’上海’或’广东’的学生记录
SELECT * from students where hometown = '北京' or hometown = '上海' or hometown = '广东';
SELECT * from students where hometown in ('北京', '上海', '广东');

-- 例 2:查询 age 年龄为 25 至 30 的学生记录
SELECT * from students where age >= 25 and age <= 30; 
SELECT * from students where age BETWEEN 25 and 30;

27.空判断

  • null不是0,也不是'',null在SQL里面代表空,什么也没有
  • null不能用比较运算符的判断
  • is null ---是否为null
  • is not null ---是否不为null
    • 不能用 字段名 = null 字段名 != null这些都是错误的
-- 例 1:查询 card 身份证为 null 的学生记录
SELECT * from students where card is null;

-- 例 2:查询 card 身份证非 null 的学生记录
SELECT * from students where card is not null;

28.where子句可以用到update和delete语句后面

-- 例 1:修改 age 为 25,并且 name 为’孙尚香’的学生 class 为’2 班’
update students set class = '2班' where age = 25 and name = '孙尚香';

-- 例 2:删除 class 为’1 班’,并且 age 大于 30 的学生记录
DELETE from students where class = '1班' and age > 30;

29.order by排序

  • order by 字段名 [asc/desc]
    • asc代表从小到大,升序,asc可以省略
    • desc代表从大到小,不可以省略
-- 例 1:查询所有学生记录,按 age 年龄从小到大排序

select * from students order by age asc;
select * from students order by age;
-- 例 2:查询所有学生记录,按 age 年龄从大到小排序
select * from students order by age desc;
  • 两个字段排序的例子
-- 例 2:查询所有学生记录,按 age 年龄从大到小排序,
--  年龄相同时,再按 studentNo 学号从小到大排序
SELECT * from students ORDER BY age desc, studentNo;
  • 当一条select语句出现了where和order by
    • select * from 表名 where 条件 order by 字段1,字段2;
    • 一定要把where写在order by前面

30.聚合函数

count求select返回的记录总数

  • count(字段名)
-- 查询学生总数(查询stuents表有多少记录)
select count(*) from students;
select count(name) from students;
select count(DISTINCT class) from students;
select count(DISTINCT sex) from students;
-- 查询女同学数量
SELECT count(name) from students where sex = '女';
SELECT count(*) from students where sex = '女';
SELECT count(sex) from students where sex = '女';

max查询最大值

  • max(字段名)
  • 查询指定字段里的最大值
-- 查询students中的最大年龄
SELECT max(age) from students;

-- 查询students中的女生最大年龄
SELECT max(age) from students where sex = '女';


-- 查询students中的'1班'最大年龄
SELECT max(age) from students where class = '1班';

聚合函数不能用到where后面的条件里

min查询最小值

  • min(字段名)
  • 查询指定字段的最小值
-- 查询students中的最小年龄
SELECT min(age) from students;

-- 查询students中的女生最小年龄
SELECT min(age) from students where sex = '女';


-- 查询students中的'1班'最小年龄
SELECT min(age) from students where class = '1班';

sum求和

  • sum(字段名)
  • 指定字段的值求和
-- 查询students中的年龄总和
SELECT sum(age) from students;

-- 查询students中的女生年龄总和
SELECT sum(age) from students where sex = '女';


-- 查询students中的'1班'年龄总和
SELECT sum(age) from students where class = '1班';

avg求平均数

  • avg(字段名)
  • 指定字段的平均值
-- 查询students中的年龄总和
SELECT sum(age) from students;

-- 查询students中的女生年龄总和
SELECT sum(age) from students where sex = '女';

-- 查询students中的'1班'年龄总和
SELECT sum(age) from students where class = '1班';
  • avg的字段中如果有null,null不做为分母计算平均
create table aa (age int, name varchar(10));
insert into aa values (10, 'a'), (20, 'b'), (null, 'c');
select avg(age) from aa;-- 结果为15,而不是10

31.数据分组

  • group by 字段名
  • select 聚合函数 from 表名 where 条件 group by 字段
  • select 聚合函数 from 表名 group by 字段
  • group by就是配合聚合函数使用的
-- 分别查询男女同学的数量
SELECT count(*) from students where sex = '男';
SELECT count(*) from students where sex = '女';

select sex, count(*) from students group by sex;
  • group by的例子
-- 分别查询各个年龄段的同学数量

select age, count(*) from students group by age;
  • where与group by
-- 分别查询'1班'不同性别学生数量

select sex, count(*) from students where class = '1班' group by sex;

where和group by 和order by的顺序

  • select * from 表名 where 条件 group by 字段 order by 字段

32.分组聚合之后的数据筛选

  • having子句
  • 总是出现在group by之后
  • select * from 表名 group by 字段 having 条件
-- 用where查询男生总数
-- where先筛选复合条件的记录,然后在聚合统计
SELECT count(*) from students where sex = '男';

-- 用having查询男生总数
-- having先分组聚合统计,在统计的结果中筛选
SELECT count(*) from students GROUP BY sex HAVING sex = '男';

33.having配合聚合函数的使用

  • where后面条件不能使用聚合函数, having可以使用聚合函数
-- 求班级人数大于3人的班级名字
select class from students GROUP BY class HAVING count(*) > 3;

34.having与where筛选的区别

  • where是对标的原始数据进行筛选
  • having是对group by之后已经分过组的数据进行筛选
  • having可以使用聚合函数, where不能用聚合函数

35.limit显示指定的记录数

  • select * from 表名 where 条件 group by 字段 order by 字段 limit start, count
  • limit总是出现在select语句的最后,
  • start代表开始行号,行号从0开始编号
  • count代表要显示多少行
  • 省略start,默认从0开始,从第一行开始
-- 查询前三行记录
SELECT * from students limit 0, 3;
SELECT * from students limit 3;

-- 查询从第4条记录开始的三条记录
SELECT * from students limit 3, 3;
  • 当有where或者group by或者order by, limit总是出现在最后
-- 查询年龄最大同学的name
select name from students ORDER BY age desc limit 1;

-- 查询年龄最小的女同学信息
SELECT * from students where sex = '女' ORDER BY age LIMIT 1;

36.数据分页显示

  • m 每页显示多少条记录
  • n,第n页
  • (n - 1) * m, m
  • 把计算结果写到limit后面
-- 每页显示4条记录,第3页的结果
select * from students limit 8, 4;
-- 每页显示4条记录,第2页的结果
select * from students limit 4, 4;
  • 已知每页记录数,求一张表需要几页显示完
    • 求总页数
    • 总页数 / 每页的记录数
    • 如果结果是整数,那么就是总页数,如果结果有小数,那么就在结果的整数上+1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
宋红康MySQL高级课件是一套以MySQL数据库为主题的高级教学材料。本课件由宋红康编写,旨在提供深入理解和学习MySQL高级特性的知识。下面是对该课件的简介: 该课件分为多个章节,内容涵盖了MySQL的高级应用和技巧。首先,课程将回顾MySQL基础知识,包括数据库设计、表的创建和管理以及基本的SQL查询语句。然后,课程将深入剖析MySQL高级特性,包括存储过程、触发器、事务和并发控制等方面的知识。 对于存储过程,课程将介绍如何创建和使用存储过程,以及存储过程的参数和返回值的使用。同时,课程还会详细讲解存储过程的语法和常见应用场景,帮助学习者更好地掌握和应用存储过程。 在触发器的部分,课程将解释触发器的概念和工作原理,并且演示如何创建和使用触发器。同时,课程还将分享触发器在实际项目中的常见应用,帮助学习者理解并运用触发器。 针对事务和并发控制,课程将讲解事务的定义、特性和隔离级别,并演示如何使用事务保证数据的完整性。课程还将介绍并发控制的原理和技术,包括锁和多版本并发控制(MVCC)等,以提高数据库的并发性和性能。 此外,课程还将介绍MySQL的高级优化技巧,包括索引优化、查询优化和存储引擎选择等。学习者将在课程中学会如何通过合理设计和优化数据库结构来提升系统的性能和可扩展性。 总而言之,宋红康MySQL高级课件是一套系统、全面的MySQL高级教学材料,内容丰富、深入,将帮助学习者深入理解和应用MySQL高级特性,提升数据库应用开发和管理的能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值