-
数据类型 和 约束
-
DDL操作字段
-
DML操作表数据(增, 删, 改)
-
DQL操作表数据
-
简单查询
-
条件查询
-
分组查询
-
排序查询
-
聚合查询
-
分页查询
-
1. 数据类型介绍
-
概述/作用
用来对数据表中的数据做限定用的, 必须是某种类型的数据, 如果不合法, 则添加失败.
-
分类
-
整型: int, 例如: 10, 20, 30
-
浮点型: double, 例如: 10.3, 13.14
-
字符串型: varchar(长度), 长度的意思是: 最多只能放多少个字符.
-
日期: datetime, 即: 年月日 时分秒
-
枚举: enum, 即: 固定的几个值, 例如: enumerate(男, 女)
-
2. 约束
-
概述/作用
在数据类型的基础上, 对该列的值做额外限定的.
-
分类
-
单表约束
-
主键约束, primary key
特点: 非空, 唯一, 且一般结合 auto_increment 自动增长 一起使用.
-
唯一约束, unique
-
非空约束, not null
-
默认约束, default
-
-
多表约束
-
外键约束 foreign key
-
-
-
示例
# ---------------------- DDL语句之 操作数据表 约束详解 --------------------- # 1. 创建day02数据库. create database day02; # 2. 切库. use day02; show tables; # 3. 创建数据表. # 3.1 在建表的时候, 直接添加约束. 例如: 学生表student(id, name, age, phone, address) drop table student; create table student( id int primary key auto_increment, # id, 主键(唯一, 非空)约束 name varchar(20) not null, # 姓名, 非空约束 age int default 18, # 年龄, 默认约束 phone varchar(11) unique , # 手机号, 唯一约束 address varchar(20) # 地址 ); # 3.2 在建表之后, 添加约束. 例如: 老师表teacher(id, name, age, phone, address) # 3.2.1 建表 drop table teacher; create table teacher( id int, # id, 主键(唯一, 非空)约束 name varchar(20), # 姓名, 非空约束 age int, # 年龄, 默认约束 phone varchar(11), # 手机号, 唯一约束 address varchar(20) # 地址 ); # 3.2.2 添加约束. alter table teacher add primary key(id) ; # 添加主键约束 alter table teacher modify id int auto_increment; # 设置自增的扩展功能. alter table teacher modify name varchar(20) not null; # 添加非空约束 alter table teacher modify age int default 18; # 添加默认约束 alter table teacher modify phone varchar(11) unique ; # 添加默认约束 alter table teacher add unique (phone); # 效果同上. # 4. 查看表结构. desc student; desc teacher;
3.DDL之操作字段
# ------------------------------ DDL语句之 操作字段 ------------------------ # 1. 切库. use day02; show tables; # 2. 查看表结构, 即: 查看表字段. desc student; # 3. 给表 添加字段. 增 # 格式: alter table 表名 add 字段名 数据类型 [约束]; # []表示可选项 alter table student add email int not null; # 邮箱列, 整型, 非空约束. # 4. 给表 修改字段. 改 # 格式: alter table 表名 modify 字段名 数据类型 [约束]; # []表示可选项 alter table student modify email varchar(20); # 邮箱列, 字符型, 没有非空约束 # 格式: alter table 表名 change 旧字段名 新字段名 数据类型 [约束]; # []表示可选项 # 将email列的列名, 改为 desc, 注意: desc是关键字. alter table student change email `desc` varchar(25); # 5. 给表 删除字段. 删 # 格式: alter table 表名 drop 字段名; alter table student drop `desc`;
4.DML语句--更新语句
# DML语句也叫 数据操作语言, 主要是对 表数据 进行 更新操作的, 即: 增, 删, 改操作. # --------------------------------------- DML语句之 增 ---------------------------- # 1. 创建day02数据库, 然后切库. create database if not exists day02; use day02; # 2. 查看数据表. show tables; # 3. 建表, 查看表结构. create table if not exists student( id int primary key auto_increment, # id, 主键(唯一, 非空)约束 name varchar(20) not null, # 姓名, 非空约束 age int default 18, # 年龄, 默认约束 phone varchar(11) unique , # 手机号, 唯一约束 address varchar(20) # 地址 ); desc student; # 4. 添加数据到学生表中, 因为学生表我们已经加入了各种约束了, 所以一起测试. # 添加单条格式: insert into 表名(列1, 列2...) values(值1, 值2...); insert into student(id, name, age, phone, address) values(1, '乔峰', 33, '111', '契丹'); insert into student(id, name, age, phone, address) values(2, '乔峰', 33, '222', '契丹'); -- 正确, 主键id, phone不重复, name不为空 insert into student(id, name, age, phone, address) values(3, '虚竹', 33, null, '契丹'); -- 正确, name列不能为空. insert into student(id, name, age, phone, address) values(4, '', 33, null, '契丹'); -- 正确, '' 和 null 不一样. # 上述格式的变形写法: insert into 表名 values(值1, 值2...); # 不写列了, 默认是全列名, 即: 后边的值要全写, 和列的个数, 顺序一致. insert into student values(5, '段誉', 19, '555', '大理'); # 我们还可以给表只添加指定的列. insert into student(id, name, phone) values(6, '阿朱', '666'); -- 测试: age列有默认值18 # 最终写法, 因为主键已经结合自增一起使用了, 所以以后的主键列, 直接写 null 即可, 它(底层)会根据最大的那个id值, +1, 然后存储. insert into student(id, name, phone) values(null, '阿紫', '777'); -- 测试: age列有默认值18 insert into student(id, name, age, phone, address) values(1, '乔峰', 33, '111', '契丹'); -- 报错, 主键具有唯一性, 1已经存在. insert into student(id, name, age, phone, address) values(2, '乔峰', 33, '111', '契丹'); -- 报错, phone列有唯一约束, 111已经存在. insert into student(id, name, age, phone, address) values(3, null, 33, '333', '契丹'); -- 报错, name列不能为空. # 添加多条格式: insert into 表名(列1, 列2...) values(值1, 值2...), (值1, 值2...), (值1, 值2...)...; insert into student values (null, '李清露', 25, '888', '西夏'), # 数字(整数, 小数)可以直接写, 其它类型要用引号包裹, 例如: '张三', '131...', '郑州' (null, '木婉清', 29, '999', '宋朝'), (null, '钟灵儿', 23, '123', '恶人谷'), (null, '王语嫣', 26, '234', '桃花坞'); # 扩展, 如何查看表数据. select * from student; # ----------------------------DML语句之 改 ----------------------------- # 非常非常非常重要: 修改 或者 删除数据的时候一定一定一定要写where条件, 不写就是操作所有. 含泪忠告. # 格式: update 表名 set 列名=值, 列名=值 where 条件; # 需求: 修改id为2的学生信息, 姓名为张三, 年龄为11, 手机号为1111 update student set name='张三', age=11, phone='1111' where id = 2; # 需求: 修改id为3的学生, 住址为郑州, 故意忘了写where条件了, 看看会出现啥情况. update student set address='郑州'; # 如果不写where条件, 则将该列所有的值都会修改. update student set phone='111' where id = 2; -- 格式正确, 但是phone列具有唯一性, 111已经存在了, 所以修改失败. # ------------------------------ DML语句之 删 ------------------------------- # 格式: delete from 表名 where 条件; # 非常非常非常重要: 修改 或者 删除数据的时候一定一定一定要写where条件, 不写就是操作所有. 含泪忠告. delete from student where id > 5; # 故意不写where条件, 删除所有数据. delete from student; # truncate table: 清空表. truncate table student; # 上述格式的table可以省略不写. truncate student; # 细节: delete删完数据之后, 如果重新插入数据, id是几呢? insert into student values (null, '李清露', 25, '888', '西夏'); # 面试题: delete from 和 truncate table 之间的区别是什么? # delete from仅仅是删除数据的, 不会重置主键id, 它属于DML语句, 可以结合 事务 一起使用. # truncate table相当于把表摧毁了, 然后再创建一张和原表一模一样的表, 即: 会重置主键id, 它属于 DDL语句, 一般不会结合事务一起用. # -------------------------------- 扩展: DML语句之 备份表 ---------------------------- show tables; # 情况1: 备份表不存在. student(源表) => student_tmp(备份表) # 格式: create table 备份表名 select * from 源表 where 条件; create table student_tmp select * from student; # 细节: 备份表主要是备份 字段和数据的, 不是备份 约束的. desc student; desc student_tmp; # 情况2: 备份表存在. student(源表) => student_tmp(备份表) # 格式: insert into 备份表名 select * from 源表 where 条件; insert into student_tmp select * from student; # 清空备份表的数据. truncate student_tmp; # 查看备份后的效果. select * from student_tmp; # 模拟误删 源表. # 1. 删除源表数据. truncate student; # 2. 查看源表数据, 没了. select * from student; # 3. 从备份表导入数据到源表. insert into student select * from student_tmp; # 4. 查询源表, 数据来了. select * from student;
5.DQL语句--查询语句
# DQL语句介绍: 数据查询语句, 主要是对表数据进行 查询操作的. # -------------------------------- 准备源数据 ------------------------------ # 1. 建库, 切库. create database if not exists day02; use day02; # 2. 查看数据表. show tables; # 3. 创建数据表(商品表), 用于存储 源数据. 快捷键: Ctrl + shift + 字母U drop table product; create table product ( pid int primary key comment '商品id', # comment 相当于给列写的 注释, 给人看的. pname varchar(20) comment '商品名', price double comment '商品价格', category_id varchar(32) comment '分类id' ) comment '商品表'; # 4. 插入源数据到上述的表中, 至此, 准备动作完毕. INSERT INTO product(pid,pname,price,category_id) VALUES (1,'联想',5000,'c001'), (2,'海尔',3000,'c001'), (3,'雷神',5000,'c001'), (4,'杰克琼斯',800,'c002'), (5,'真维斯',200,'c002'), (6,'花花公子',440,null), (7,'劲霸',2000,'c002'), (8,'香奈儿',800,'c003'), (9,'相宜本草',200,'c003'), (10,'面霸',5,'c003'), (11,'好想你枣',56,'c004'), (12,'香飘飘奶茶',1,'c005'), (13,'海澜之家',1,'c002'); # 5. 查看表中所有的数据. select * from product; # -------------------------- DQL语句 简单查询 ----------------------------- # 需求1:获取全部商品信息 select * from product; # 需求2: 查询指定列的信息. select pid, pname, price from product; # 需求3: 别名查询, 注意: 只是在显示的时候, 有别名, 表中的字段名, 并没有发生任何的变化. select pid as '商品id', pname as '商品名', price as '商品价格' from product as p; # 细节: as 可以省略不写. select pid '商品id', pname '商品名', price '商品价格' from product p; # 需求4: 去重查询. 去重的意思是: 重复的数据保留1个, 不是重复的全部删除. select distinct price from product; # 只根据 price 去重 select distinct pname, price from product; # 把 pname 和 price当做1个整体, 进行去重. 即: 联想,5000 和 雷神,5000 不是相同数据. # 需求5: 获取所有商品的名称和电商价(电商价 = 价格 + 10) select pname, price + 10 as '商品价格' from product; # ctrl + Q: 查看解释说明的. # ---------------------------DQL语句 条件查询 ------------------------- # 条件查询指的是: where条件查询, 格式: SELECT * FROM 表名 WHERE 条件; # 场景1: 比较运算符, >, <, >=, <=, !=, <> # 需求1: 获取pid值为1的商品的全部信息 select * from product where pid = 1; # 需求2: 查询所有价格等于800的所有商品信息: select * from product where price=800; # 需求3: 查询所有价格不为800的所有商品信息: select * from product where price != 800; select * from product where price <> 800; # 需求4: 查询价格大于600元的所有商品信息: select * from product where price > 600; # 需求5: 查询价格小于2000元的所有商品的名称和价格. select pname, price from product where price < 2000; # 场景2: 逻辑运算符. /* and, 逻辑与, 并且的意思, 要求条件都要满足, 即: 有false(假)则整体为false or, 逻辑或, 或者的意思, 只要满足任意1个条件即可, 即: 有true(真)则整体为true not, 逻辑非, 取反的意思, 以前是true, 取反后是false, 以前是false取反后是true */ # 需求6: 获取所有商品中,价格在200-2000之间的所有商品 select * from product where price >= 200 and price <= 2000; # 并且 10 select * from product where price between 200 and 2000; # 包括200 和 2000, 即: 包左包右 # 需求7: 获取所有商品中,价格大于3000或价格小于600的所有商品信息: select * from product where price > 3000 or price < 600; # 并且 10 # 需求8: 获取所有商品中几个不在200-2000范围内的所有商品信息: select * from product where price > 2000 or price < 200; # 并且 10 # 场景3: 模糊查询. 格式: like '内容' _代表任意的1个字符, %代表任意的多个字符 # 需求9: 查询所有商品中,商品名以'斯'结尾的商品信息: select * from product where pname like '%斯'; # 需求10: 查询所有商品中,商品名以斯结尾,并且是三个字的商品信息: select * from product where pname like '__斯'; # 这里是两个下划线, 代表任意的2个字符 # 需求11: 询所有商品中,名字中带霸的商品信息: select * from product where pname like '%霸%'; # 需求12: 查找以香开头, 一共3个字符的. select * from product where pname like '香__'; # 场景4: 范围查询, 格式: in (....), not in (....), between ... and ... # 需求13: 查询价格在800-2000范围内的所有商品: select * from product where price >= 800 and price <= 2000; select * from product where price between 800 and 2000; # 需求14: 查询所有商品中价格是600 800 2000的商品信息. select * from product where price = 600 or price = 800 or price = 2000; select * from product where price in (600, 800, 2000); # 需求15: 查询商品名称是 劲霸 或者 香奈儿的商品信息(可以对于字符型数据使用in) select * from product where pname = '劲霸' or pname = '香奈儿'; select * from product where pname in ('劲霸', '香奈儿'); # 需求16: 查找商品名称不是 劲霸 或者 香奈儿的商品信息 select * from product where pname != '劲霸' and pname <> '香奈儿'; select * from product where pname not in ('劲霸', '香奈儿'); # 场景5: 非空查询. is null 为空, is not null 不为空. # 需求17: 查询所有商品中category_id的值为NULL的商品信息: # select * from product where category_id = 'null'; # 非空校验的 错误写法 # select * from product where category_id = null; # 非空校验的 错误写法 select * from product where category_id is null; # 空值校验. # 需求18:查询所有商品中category_id的值 不为NULL的 商品信息: select * from product where category_id is not null; # 非空校验.