创建用户并授予权限
create user chengyejing@localhost identified by 'chengyejing';
create user liuyingjie@localhost identified by 'liuyingjie';
grant all privileges on taste_explosion.* to liuyingjie@'localhost';
grant all privileges on taste_explosion.* to chengyejing@'localhost' ;
建表
user
表
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`user_id` int(0) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
`username` varchar(30) NOT NULL COMMENT '登录账号',
`password` varchar(255) NOT NULL COMMENT '登录密码',
`avatar` varchar(255) NULL DEFAULT 'https://lionkliu-typore.oss-cn-shanghai.aliyuncs.com/2022/06/01/4f4649906edd418bbf0c6684f929d5f5MofTqghgaoEQ6477495682f214bd4c7510b41e8c6b5a.jpg' COMMENT '用户头像地址',
`nickname` varchar(255) NULL DEFAULT NULL COMMENT '用户名',
`email` varchar(255) NULL DEFAULT NULL COMMENT '邮箱',
`address` varchar(255) NULL DEFAULT NULL COMMENT '居住地址',
`type` tinyint(0) NULL DEFAULT 1 COMMENT '管理员:0,普通用户:1',
`status` tinyint(0) NULL DEFAULT 1 COMMENT '禁用状态:0,启用状态:1',
PRIMARY KEY (`user_id`) USING BTREE
)ENGINE = INNODB AUTO_INCREMENT = 1 CHARACTER SET = utf8;
食谱表
DROP TABLE IF EXISTS `recipe`;
CREATE TABLE `recipe` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`img` varchar(255) NOT NULL COMMENT '简略图',
`title` varchar(255) NOT NULL COMMENT '标题',
`create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`detail` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '文章详情',
`user_id` int(0) NOT NULL COMMENT '对应发表该食谱的用户id',
`likes` int(0) NULL DEFAULT 0 COMMENT '点赞数',
`is_swiper` int(0) NOT NULL DEFAULT 0 COMMENT '是否为轮播图,0假1真',
`category_id` int(0) NULL DEFAULT NULL COMMENT '食谱分类',
`views` int(0) NULL DEFAULT 1 COMMENT '浏览量',
`collections` int(0) NULL DEFAULT 0 COMMENT '收藏数',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
评论表
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`recipe_id` int(0) NOT NULL COMMENT '对应是哪个食谱下的评论',
`root_id` int(0) NOT NULL DEFAULT -1 COMMENT '根评论-1',
`content` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`to_comment_user_id` int(0) NOT NULL DEFAULT -1 COMMENT '所回复的目标评论的userid',
`to_comment_id` int(0) NOT NULL DEFAULT -1 COMMENT '回复目标评论id',
`created_by` int(0) NOT NULL COMMENT '发表评论的用户id',
`create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '评论发布时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
分类表
DROP TABLE IF EXISTS `category`;
CREATE TABLE `category` (
`id` int(0) NOT NULL AUTO_INCREMENT,
`category_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`category_img` varchar(455) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`parent_id` int(0) NOT NULL DEFAULT 0 COMMENT '父级分类id,根评论为0',
PRIMARY KEY (`id`) USING BTREE
)
视图
-- 1、 创建show_index_card_view视图,用于展示首页食谱及发布人的信息
CREATE VIEW show_index_card_view AS SELECT
a.id,
a.img,
a.title,
a.create_time,
b.nickname,
b.avatar,
a.likes
FROM
recipe AS a,
`user` AS b
WHERE
a.user_id = b.user_id;
-- 2、用于展示首页食谱及发布人的信息,并通过发布时间降序排序
CREATE VIEW show_index_card_by_create_time_desc_view AS SELECT
a.id,
a.img,
a.title,
a.create_time,
b.nickname,
b.avatar,
a.likes
FROM
recipe AS a,
`user` AS b
WHERE
a.user_id = b.user_id
ORDER BY
a.create_time DESC;
-- 3、用于展示首页食谱及发布人的信息,并通过浏览量降序排序
CREATE VIEW show_index_card_by_views_desc_view AS SELECT
a.id,
a.img,
a.title,
a.create_time,
b.nickname,
b.avatar,
a.likes,
a.views
FROM
recipe AS a,
`user` AS b
WHERE
a.user_id = b.user_id
ORDER BY
a.views DESC;
-- 4、用于展示首页食谱及发布人的信息,并通过点赞量降序排序
CREATE VIEW show_index_card_by_likes_desc_view AS SELECT
a.id,
a.img,
a.title,
a.create_time,
b.nickname,
b.avatar,
a.likes
FROM
recipe AS a,
`user` AS b
WHERE
a.user_id = b.user_id
ORDER BY
a.likes DESC;
-- 通过视图来查询
SELECT
*
FROM
show_index_card_view;
SELECT
*
FROM
show_index_card_by_likes_desc_view;
SELECT
*
FROM
show_index_card_by_create_time_desc_view;
-- 查看创建的视图结构
DESCRIBE show_index_card_view;
SHOW CREATE VIEW show_index_card_view;
-- 查看创建的所有视图
show table status where comment='view';
-- 删除视图
DROP VIEW show_index_card;
存储过程
CREATE DEFINER=`root`@`%` PROCEDURE `pro_insert_into_star`( IN in_recipe_id INT, IN in_user_id INT )
BEGIN
INSERT INTO star ( recipe_id, user_id )
VALUES
( in_recipe_id, in_user_id );
END
CREATE DEFINER=`root`@`%` PROCEDURE `pro_getWorksById`(in uid int,out workId int ,out workImg VARCHAR(255),out workTitle VARCHAR(50),out userId int)
begin
SELECT id,img,title,user_id into workId,workImg,workTitle,userId from recipe where user_id=uid;
END
CREATE DEFINER=`root`@`%` PROCEDURE `pro_count_works`(out out_count_work INT)
BEGIN
select count(*) into out_count_work from recipe;
END
触发器
1、定义deleted_user触发器,实现删除自动备份功能
create table deleted_user as select * from `user` where 1=2;
delimiter $
CREATE TRIGGER delete_user_trigger BEFORE DELETE ON `user` FOR EACH ROW
BEGIN
INSERT INTO deleted_user VALUES (
old.user_id,
old.username,
old.password,
old.avatar,
old.nickname,
old.email,
old.address,
old.type
);
END $
delimiter;
-- 查看所有触发器
show TRIGGERS from `taste_explosion`;
SHOW TRIGGERS;
-- 测试触发deleted_user这个触发器
delete from `user` where user_id=10;
SELECT * from `user`;
SELECT * from deleted_user;
-- 删除触发器
drop trigger delete_user_trigger;
-- 当collection表添加一条数据时,recipe表中likes字段自动 +1
delimiter $
CREATE TRIGGER add_collection_trigger BEFORE INSERT ON collection FOR EACH ROW
BEGIN
UPDATE recipe set collections=collections+1 where id=new.recipe_id;
END $
delimiter;
-- 当collection表中删除一条数据时,recipe表中likes字段自动 -1
delimiter $
CREATE TRIGGER del_collection_trigger BEFORE DELETE ON collection FOR EACH ROW
BEGIN
UPDATE recipe set collections=collections-1 where id=old.recipe_id;
END $
delimiter;
drop TRIGGER del_collection_trigger;
drop TRIGGER add_collection_trigger;
show TRIGGERS;
INSERT into collection(recipe_id,user_id) VALUES(5,5);
DELETE from collection where user_id=5 and recipe_id=5;
-- 当star表添加一条数据时,recipe表中likes字段自动 +1
delimiter $
CREATE TRIGGER add_star_trigger BEFORE INSERT ON star FOR EACH ROW
BEGIN
UPDATE recipe set likes=likes+1 where id=new.recipe_id;
END $
delimiter;
-- 当star表中删除一条数据时,recipe表中likes字段自动 -1
delimiter $
CREATE TRIGGER del_star_trigger BEFORE DELETE ON star FOR EACH ROW
BEGIN
UPDATE recipe set likes=likes-1 where id=old.recipe_id;
END $
delimiter;
show TRIGGERS;