单表约束
今天继续衔接上一篇博客的Mysql进行讲解,今天讲解的是上一张图中的红色的部分,DQL重点在于一个完整的查询包括哪几个部分,在有条件时是如何查询的,如何给一张表中某一列进行排序。
#——————— 案例1: 演示单表约束 ———————
/*
本节的重点是介绍上篇博客的最后约束
约束介绍:
概述:
约束可以理解为在数据类型的基础上,继续在某列数据值上做限定,例如不能重复,不能为空.....
分类:
单表约束:
主键约束: primary key
特点: 非空, 唯一, 一般结合 auto_increment(自动增长) 一起使用.
非空约束: not null
特点: 该列值不能为空, 但是可以: 重复.
唯一约束: unique
特点: 该列值不能重复, 但是可以: 为空.
默认约束: default 默认值
特点: 如果添加数据的时候没有给值, 就用默认值填充. 类似于Python中的 缺省参数(默认参数)
多表约束:
主外键约束: foreign key
*/
#删除数据库
drop database day02;
#新建数据库
create database day02 charset 'utf8';
#切换数据库
use day02;
#查看表数据
show tables;
#创建数据表
create table stu(
id int primary key auto_increment, # id列, 主键列(非空,唯一), 自增
name varchar(10) not null, # 姓名, 不能为空
phone varchar(11) unique, # 手机号, 唯一
gender varchar(2), # 性别, 没有限定.
address varchar(10) default '北京' # 默认约束
);
#往表中添加数据
insert into stu values(null, '杨过', '111', '男', '上海');
insert into stu values(null, '郭靖', '222', '男', '广州');
insert into stu values(null, '黄蓉', '333', '女', '深圳');
#往表中添加默认约束数据记得多少个键就匹配多少个值
insert into stu(id, name, phone, gender) values(null,'小龙女','555','女');
select * from stu;
desc stu;
简单查询
# ——————— 案例2: 演示单表查询 -> 简单查询 ———————
/*
本节重点在于完整了解一下完整的查询流程
单表查询, 完整查询格式如下:
select
[distinct] 列名1 as 别名, 列名2 as 别名, ...
from
数据表名
where
组前筛选
group by
分组字段
having
组后筛选
order by
排序字段 [asc | desc]
limit
起始索引, 数据条数;
*/
# 1. 创建商品表.
create table product
(
pid int primary key auto_increment, # 商品id, 主键
pname varchar(20), # 商品名
price double, # 商品单价
category_id varchar(32) # 商品的分类id
);
# 2. 添加表数据.
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'联想',5000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'海尔',3000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'雷神',5000,'c001');
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'杰克琼斯',800,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'真维斯',200, null);
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'花花公子',440,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'劲霸',2000,'c002');
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'香奈儿',800,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'相宜本草',200, null);
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'面霸',5,'c003');
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'好想你枣',56,'c004');
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'香飘飘奶茶',1,'c005');
INSERT INTO product(pid,pname,price,category_id) VALUES(null,'海澜之家',1,'c002');
#查询所有表目录,方式一
select * from product;
#查询所有表目录,方式二
select pid,pname,price,category_id from product;
#需求2:查看商品名和商品价格
select pname,price from product;
#扩展:起别名,列名,表名都可以起别名。
#格式:列名 as 别名 或者 表名 as 别名,其中as可以省略不写
#select 列名 as 别名 from 表名 as 别名;
select pname as 商品名,price 商品价格 from product;
#需求3:查看结果表达式,将所有的商品价格+10,进行展示
select pname,price+10 from product;
select pname,price as price from product;
条件查询
# ——————— 案例3: 演示单表查询 -> 条件查询 ———————
#本节的重点是添加条件时我们应该如何做,主要内容包括限定条件下某一列我们应该如何查询内容
#友情提示一下 select *代表着查询所有字段和字段包括的内容,用大白话来讲就是整个表被读入了
#格式:select 列名1,列名2……from 数据表名 where 条件;
#场景1:比较运算符>,<,>=,<=,=,<>(!=)
select * from product where pname='花花公子';
select * from product where price=800;
#查询价格不是800的
select * from product where price!=800;
select * from product where price<>800;
#查询商品价格大于60元的所有商品信息
select *from product where price>60;
#查询商品价格小于等于800元的所有商品信息
select *from product where price<=800;
#场景2:范围查询,between 值1 and 值2——>适用于连续区间, in(值1,值2,值3,……)
#场景3:逻辑运算符,and,or,not
#需求6:查询商品价格在200到800之间的所有商品,一下两种写法都可以的
select *from product where price>=200 and price<=800;
select * from product where price between 200 and 800;
#需求7:查询商品价格是200或者800的所有商品
select * from product where price in (200,800);
select * from product where price=200 or price=800;
#需求8:查询价格不是800的所有商品
select * from product where price!=800;
select * from product where not price=800;
#场景4:模糊查询,字段名 like '_内容%' _代表任意的1个字符,%代表任意的多个字符,至少0个,至多无所谓,注意区别和理解不要死记硬背
#需求9,查询以香开头的所有商品
select * from product where pname like '香%';
#需求10,查询第二个字为想的所有商品
select * from product where pname like '_想%';
#场景5 非空查询,is null,is not null, 不能用=来判断空
select * from product where product.category_id is null;
select * from product where category_id is not null;
排序查询
# ——————— 案例4: 演示单表查询 -> 排序查询 ———————
#本节的重点是了解数据表列中的升序降序排列关键字是asc和desc
#desc有两个意思在mysql语法中一个就是降序,一个就是展示表结构
#格式如下:select * from 表名 order by 排序字段1[asc|desc],排序字段2[asc|desc]
#解释:ascending 升序,descending 降序
#默认升序 所以asc可以省略不屑
#根据价格降序
select * from product order by price;
select * from product order by price desc ;
#需求2,根据价格降序排列,价格一样的情况根据分类进行排序
select * from product order by price desc ,category_id desc ;
