700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Mysql存储过程-新增数据时多条件判定是否已经存在某值

Mysql存储过程-新增数据时多条件判定是否已经存在某值

时间:2024-04-26 08:14:09

相关推荐

Mysql存储过程-新增数据时多条件判定是否已经存在某值

用户表

CREATE TABLE IF NOT Exists tb_user (user_id CHAR(16) NOT NULL COMMENT '用户id',user_name CHAR(10) COMMENT '姓名',email CHAR(30) unique COMMENT '邮箱',phone_number CHAR(11) unique NOT NULL COMMENT '手机号',balance INT(9) NOT NULL COMMENT '书币',briefs CHAR(50) COMMENT '简介',birthday DATE COMMENT '出生日期',grade INT(2) COMMENT '等级',password CHAR(20) NOT NULL COMMENT '密码',avatar VARCHAR(100) COMMENT '头像url',signature VARCHAR(100) COMMENT '签名',PRIMARY KEY(user_id));

replace into tb_user(user_id,user_name,email,phone_number,balance,briefs,birthday,grade,password,avatar,signature) values('324123125','llx','1753552134@','17665621006',0,'风一样的少年',CURDATE(),25,'123456','default.png','我就是我,不一样的烟火'),('324223125','user2345','2341235@','13722211122',0,'风一样的少年',CURDATE(),25,'123456','default.png','不顾别人感受是自私,太顾别人感受是自虐。'),('324423125','ling','234125@','13351234112',0,'风一样的少年',CURDATE(),25,'123456','default.png','年龄永远不是衡量一个人的刻度,只有责任的叠加才会让人逐渐成长。'),('324623125','xz','2341235@','15534562345',0,'风一样的少年',CURDATE(),25,'123456','default.png','昨天再好,也走不回去;明天再难,也要抬脚继续。');select * from tb_user

图书表

create table if not exists tb_book (book_id char(10) not null comment 'book主键',book_name char(30) not null comment '书名',book_type char(10) not null comment '类别',book_brief varchar(500) not null comment '简介',book_online_time timestamp not null comment '上线时间',book_author char(20) not null comment '作者',book_grade int(2) not null comment '评分',book_word_number int(10) not null comment '售量',book_small_picture char(100) not null comment '书小图',book_big_picture char(100) comment '书大图',book_recommend int(10) comment '总推荐',book_hits int(10) comment '总点击量',book_week_recommend int(10) comment '周推荐',book_status char(10) comment '连载或者完结',book_scale char(100) comment '轻小说···标识',primary key (book_id));

insert into tb_book(book_id, book_name, book_type, book_brief, book_online_time, book_author, book_grade,book_word_number, book_small_picture, book_big_picture, book_recommend,book_hits,book_week_recommend,book_status,book_scale)values ('0000000001', '天降鬼才', '玄幻','我不会奇门遁甲,也不懂风水八卦,但江湖人都称我绝世鬼才。为什么?因为我脑子有坑!装满来至新世纪的现代学识!讲道理,其实我是个很纯洁的斯文人,不管你们信不信,反正我是信了。', now(), '武异', 10, 451234,'0000000001.png', '0000000001.png',12421523,4521342,12431,'连载中','轻小说,升级,搞笑,扮猪吃虎,爽文'),('0000000002', '暴君的穿越小邪后', '古代言情','一名现代A帮千金,机缘巧合下穿越到大齐——一个历史上完全不存在的朝代,在这个波诡云谲的时代,看她如何见招拆招,并和那个冷面帝王谈一场千古绝恋。带着皇帝在现代吃喝玩乐,在古代又玩起了帝后和鸣,好不快活!',now(), '非墨千泷', 10, 1412342, '0000000002.png', '0000000002.png',12421523,4521342,12431,'连载中','轻小说,升级,搞笑,扮猪吃虎,爽文'),('0000000003', '汗灵昭烈', '武侠仙侠','打虎上山,行侠入世,一代英雄聚念而生。单臂挚天,双剑仗义,千年华夏因义流芳。武松是谁?他为何而来?他又将为谁而战?一代汉灵入世,沧海下暗流涌动,乾坤里谁是英雄!',now(), '千秋一梦', 10, 151241, '0000000003.png', '0000000003.png',12421523,4521342,12431,'连载中','轻小说,穿越'),('0000000004', '大明烟火', '历史军事', '朱元璋:“一群贪赃枉法之辈,朕要杀光他们。”韩度:“陛下息怒,与其杀了,不如废物利用,压榨干净他们的剩余价值才好。”朱元璋:......', now(),'阳光下写字', 10, 1123424, '0000000004.png', '0000000004.png',12421523,4521342,12431,'连载中','升级,爽文,穿越'),('0000000005', '重生八零甜妻有点狂', '都市言情','眼睛一闭一睁,乐薇重回十六岁。白手起家打拼出的跨国集团没了,上亿身家也没了,只有家徒四壁和兜里的几毛钱,还有她的小学学历。重活一回,除了赚钱,她还要走进知识殿堂,成为学霸,走向巅峰,出任CEO,迎娶白富美!一身戎装又帅又飒的小柳同志疑惑:谁是白富美?又白又富又美的乐大CEO理直气壮:你。',now(), '林间雪', 10, 112341, '0000000005.png', '0000000005.png',12421523,4521342,12431,'连载中','搞笑,扮猪吃虎,爽文'),('0000000006', '小道姑成仙记', '玄幻','斩恶鬼,斗尸煞,收妖邪!杀魔人,寻仙草,炼仙丹!初出茅庐的郑小雅,为了救出师尊,重振师门,踏上了一条危险曲折,惊心动魄的道路一个又一一个不可思议的经历,在她的生命中演绎,。最后她发现,自己的命运原来一直都不掌握在自己的手中,且看她如何打破命运的枷锁,成就仙道之躯。',now(), '羊小乐', 10, 112342, '0000000006.png', '0000000006.png',12421523,4521342,12431,'连载中','轻小说,升级'),('0000000007', '满级游戏宅在兽世惹桃花', '幻想时空','游戏玩家顾晓黎因为系统bug携带满级技能穿越到游戏中的兽人副本,踏上漫漫归家之路,却被各色美男拐骗,只得留下,发展城邦,与兽夫共同对抗恶势力。甜宠玛丽苏。',now(), '无摆', 10, 15234, '0000000007.png', '0000000007.png',12421523,4521342,12431,'连载中','轻小说,爽文')

收藏夹表

create table if not exists tb_collection(collection_id char(11) not null comment '收藏id',book_id char(10) not null comment 'book主键',user_id CHAR(16) NOT NULL COMMENT '用户id',collection_time timestamp not null comment '收藏时间',primary key (collection_id),constraint `collection_book` foreign key (`book_id`) references `tb_book`(`book_id`),constraint `collection_user` foreign key (`user_id`) references `tb_user`(`user_id`))

set foreign_key_checks = 0;insert into tb_collection(collection_id, book_id, user_id, collection_time)values ('00000000001', '0000000001', '324123125', now()),('00000000002', '0000000002', '324123125', now()),('00000000003', '0000000003', '324123125', now()),('00000000004', '0000000004', '324123125', now());set foreign_key_checks = 1;

创建存储过程

## delimiter 定义语句结束符号delimiter //drop procedure if exists proce;## 判定是否已经存在存储过程,存在则删除## 创建存储过程,传入5个参数,根据前两个值查询是否已存在,不存在则以后面三个值向表新增数据create procedure proce(in uid char(16),in bid char(10),in in_cid char(11),in in_uid char(16),in in_bid char(10))## 存储过程的开始begin ## 定义一个变量declare num int;## 根据传进来的前两个值进行查询返回数量select count(*) into num from tb_collection where user_id = uid and book_id = bid;## 假如查询结果为0则新增if(num=0) theninsert into tb_collection(collection_id, book_id, user_id, collection_time)values (in_cid, in_bid, in_uid, now());end if;## 存储过程的结束end//

使用存储过程

## 解除外键约束set foreign_key_checks =0;## 调用存储过程call proce('324123125','0000000003','41235125','0000000001','324123125');## 绑定外键约束set foreign_key_checks =1;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。