数据库SQL
1. 准备工作
代码语言:javascript复制-- ----------------------------
-- Table structure for wm_channel
-- ----------------------------
DROP TABLE IF EXISTS `wm_channel`;
CREATE TABLE `wm_channel` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '频道名称',
`description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '频道描述',
`is_default` tinyint(1) UNSIGNED NULL DEFAULT NULL COMMENT '是否默认频道',
`status` tinyint(1) UNSIGNED NULL DEFAULT NULL COMMENT '状态 0-停用,1-启用',
`ord` tinyint(3) UNSIGNED NULL DEFAULT NULL COMMENT '序号',
`created_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '频道信息表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Table structure for wm_news
-- ----------------------------
DROP TABLE IF EXISTS `wm_news`;
CREATE TABLE `wm_news` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_id` int(11) UNSIGNED NULL DEFAULT NULL COMMENT '自媒体用户ID',
`title` varchar(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '标题',
`content` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT '图文内容',
`type` tinyint(1) UNSIGNED NULL DEFAULT NULL COMMENT '文章布局rn 0 无图文章rn 1 单图文章rn 3 多图文章',
`channel_id` int(11) UNSIGNED NULL DEFAULT NULL COMMENT '图文频道ID',
`labels` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
`created_time` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
`submited_time` datetime(0) NULL DEFAULT NULL COMMENT '提交时间',
`status` tinyint(2) UNSIGNED NULL DEFAULT NULL COMMENT '当前状态rn 0 草稿rn 1 提交(待自动审核)rn 2 审核失败rn(自动审核失败或人工审核失败)rn 3 待人工审核rn 4 人工审核通过rn 8 自动审核通过(待发布)rn 9 已发布',
`publish_time` datetime(0) NULL DEFAULT NULL COMMENT '文章定时发布时间',
`reason` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '拒绝理由',
`article_id` bigint(20) UNSIGNED NULL DEFAULT NULL COMMENT 'APP端文章ID',
`images` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT '图片地址,多个图片用逗号分隔',
`enable` tinyint(1) UNSIGNED NULL DEFAULT 1 COMMENT '0-未上架n1-已上架',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6301 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '自媒体图文内容信息表' ROW_FORMAT = Dynamic;
2. 专业术语
RDBMS(Relaction DataBase Mangement System):MySQL、SQL Server、IBM DB2、Oracle
SQL(Struted Query Language): 结构化查询语言,是用来连接和操作RDBMS的标准计算机语言
ER(Entity RelationShip):实体关系图,用来描述业务实体数据之间的关系。
三个图形:矩形(实体类型)、椭圆形(属性名)、菱形(关系)、连线(几对几的关系)
SQL语言分类:
DDL:数据定义语言,一般用来对数据库表进行结构调整的,比如Create、Drop、Alter
DML:数据操作语言,对数据进行增删改查操作,比如Insert、Delete、Update、Select
DCL: 数据控制语言, grant、commit、rollback
ACID:
A(Atomictiy): 原子性
C(Consistency): 一致性
I (ISolation): 隔离性, 由事务隔离级别决定隔离性影响的大小
D(Durability): 持久性
PRIMARY KEY: 主键ID
FOREIGN KEY:极少用物理外键,用逻辑外键
INDEX:索引 (主键索引、普通索引、唯一索引)
3. SQL之DML语句
3.1 增删改
插入一条: 插入一条频道
代码语言:javascript复制insert into wm_channel values (9,"Scala","新型编程语言",0,1,9,"2022-06-27 12:00:00");
插入多条: 插入多条频道
代码语言:javascript复制insert into wm_channel values (10,".NET","微软件编程语言",0,0,10,"2022-06-27 13:00:00"),(11,"C ","很好的编程语言",0,1,11,"2022-06-27 14:00:00");
删除ID为11的频道
代码语言:javascript复制delete from wm_channel where id=11;
修改ID为10的频道名称为c
,描述改为不错的编程语言
update wm_channel set name="c ",description="不错的编程语言" where id=10;
3.2 单表查询
条件查询:查询频道名为java的频道
代码语言:javascript复制select * from wm_channel where name = "java";
逻辑查询
与查询:查询名称为java且状态为1的频道
代码语言:javascript复制 select * from wm_channel where name = "java" and status = 1;
或查询:查询名称为java或状态为0的频道
代码语言:javascript复制select * from wm_channel where name = "java" or status = 0;
非查询:查询状态不为1的频道
代码语言:javascript复制select * from wm_channel where status != 1;
模糊查询:查询描述包含框架
的频道
select * from wm_channel where wm_channel.description like "%框架%";
区间查询
区间查询1:查询序号为6和7的频道
代码语言:javascript复制select * from wm_channel where ord in (6,7);
区间查询2:查询序号大于5的频道
代码语言:javascript复制select * from wm_channel where ord > 5;
区间查询3:查询时间大于2022-06-24 00:00:00
的频道
select * from wm_channel where created_time > "2022-06-24 00:00:00";
区间查询4:查询时间大于等于2022-06-22 00:00:00
且小于等于2022-06-24 00:00:00
的频道
select * from wm_channel where created_time between "2021-06-22 00:00:00" and "2021-06-24 00:00:00";
3.3 多表关联查询
查询文章标题、所属频道名、文章状态、文章创建时间
写法一- 直接FROM两张表
代码语言:javascript复制select wn.title,wc.name channelName,wn.status,wn.created_time from wm_news wn,wm_channel wc where wn.channel_id = wc.id;
写法二- JOIN两张表
代码语言:javascript复制select wn.title,wc.name channelName,wn.status,wn.created_time from wm_news wn left join wm_channel wc on wn.channel_id = wc.id;
3.4 子查询
查询频道名称为java和Python的所有文章(仅需查询文章标题、频道id、创建时间)
代码语言:javascript复制select title,channel_id,created_time from wm_news where channel_id in (select id from wm_channel where name in ( 'java','Python'))
3.5 分页排序
分页查询
分页查询1:从第2条开始查询3条频道
代码语言:javascript复制select * from wm_channel limit 1,3;
分页查询2:查询前4条频道
代码语言:javascript复制select * from wm_channel limit 4;
排序查询:查询文章标题、发布时间按照发布时间倒排序
代码语言:javascript复制select title,publish_time from wm_news order by publish_time desc ;
3.6 聚合查询
查询频道序号最小值
代码语言:javascript复制select min(ord) from wm_channel;
查询频道序号最大值
代码语言:javascript复制select max(ord) from wm_channel;
查询频道序号平均值
代码语言:javascript复制select avg(ord) from wm_channel;
查询频道序号之和
代码语言:javascript复制select sum(ord) from wm_channel;
查询每天对应的已发布的文章数量,只查询发布数量大于2的记录,按数量倒排序,取前5条
代码语言:javascript复制select date_format(publish_time,"%Y-%m-%d") dateStr,count(*) dateCount from wm_news group by dateStr having dateCount>2 order by dateCount desc limit 5;