范文健康探索娱乐情感热点
热点动态
科技财经
情感日志
励志美文
娱乐时尚
游戏搞笑
探索旅游
历史星座
健康养生
美丽育儿
范文作文
教案论文

数据库系列你想要的sql全都有plus

  目录
  详细sql /** 数据准备   一、ddl(data define language)数据定义语言   1、库管理     1、库的创建(create)     2、库的修改(alter)     3、库的删除(drop)   2、表管理     1、表的创建(create)     2、表的修改(alter)     3、表的删除(drop)     4、表的查询(desc)     5、复制表数据     6、重命名表     7、清空表   二、dml(data manipulation language)数据操作语言   1、增(insert)   2、删(delete)   3、改(update)   三、dql(data query language)数据查询语言   1、基础查询(select)   2、条件查询(where)   3、分页查询(limit)   4、连接查询(join)   5、子查询(in)   6、排序查询(order by)   7、分组查询(group by)   8、时间查询   9、其它查询   四、dcl(data control language)数据控制语言   1、创建用户(create)   2、用户授权(grant)   3、撤销权限(revoke)   4、查看权限(show)   5、删除用户(drop)   6、修改用户(update)   五、索引操作   1、索引创建(create)   2、索引删除(drop)   3、索引查询(show)   六、存储过程   1、简单查询   2、批量插入数据   七、相关函数   1、时间函数   2、随机数   3、其它函数   八、其它内容   1、查看数据库大小   2、设置变量   3、系统变量   4、explain   5、批量删除表 **/     -- =============================================================================================== -- 数据准备(前提:先执行下方创库创表sql) INSERT INTO `dbname`.`t_table_info`(`id`, `name`, `age`, `sex`, `job`, `json_set`, `text`, `blob`, `create_time`, `update_time`) VALUES (1, "张三", 1, "男", "律师", "{"uuid":"123"}", "文本1", NULL, "2021-10-27 23:28:35", "2022-01-09 12:37:07"); INSERT INTO `dbname`.`t_table_info`(`id`, `name`, `age`, `sex`, `job`, `json_set`, `text`, `blob`, `create_time`, `update_time`) VALUES (2, "李四", 2, "男", "律师", "{"uuid":"123"}", "文本2", NULL, "2021-10-13 23:28:38", "2022-01-09 12:37:09"); INSERT INTO `dbname`.`t_table_info`(`id`, `name`, `age`, `sex`, `job`, `json_set`, `text`, `blob`, `create_time`, `update_time`) VALUES (3, "王五", 3, "男", "医生", "{"uuid":"123"}", "文本3", NULL, "2021-10-20 23:30:56", "2022-01-09 12:37:11"); INSERT INTO `dbname`.`t_table_info`(`id`, `name`, `age`, `sex`, `job`, `json_set`, `text`, `blob`, `create_time`, `update_time`) VALUES (4, "赵六", 4, "女", "医生", "{"uuid":"456"}", "文本4", NULL, "2021-10-27 23:31:28", "2022-01-09 12:37:12"); INSERT INTO `dbname`.`t_table_info`(`id`, `name`, `age`, `sex`, `job`, `json_set`, `text`, `blob`, `create_time`, `update_time`) VALUES (5, "嘎嘎大王", 5, "女", "教师", "{"uuid":"456"}", "文本5", NULL, "2021-11-21 21:58:17", "2022-01-09 12:37:14"); INSERT INTO `dbname`.`t_table_info`(`id`, `name`, `age`, `sex`, `job`, `json_set`, `text`, `blob`, `create_time`, `update_time`) VALUES (6, "呱呱大王", 6, "女", "教师", "{"uuid":"456"}", "文本6", NULL, "2021-11-21 21:59:32", "2022-01-09 12:37:16");           -- =============================================================================================== -- 一、ddl(data define language)数据定义语言 -- 1、库管理 -- 1、库的创建(create) create database if not exists dbname;   -- 2、库的修改(alter) alter database dbname character set utf8;   -- 3、库的删除(drop) drop database if exists dbname;     -- 2、表管理 -- 1、表的创建(create) drop table if exists `t_table_info`; create table `t_table_info`  (   `id` int(0) not null,   `name` varchar(255) character set utf8mb4 collate utf8mb4_0900_ai_ci null default null comment "姓名(varchar(m) 存放长度不固定的字符)",   `age` int(0) null default null comment "年龄",   `sex` varchar(255) character set utf8mb4 collate utf8mb4_0900_ai_ci null default null comment "性别",   `job` varchar(255) character set utf8mb4 collate utf8mb4_0900_ai_ci null default null comment "职业",   `json_set` varchar(100) character set utf8mb4 collate utf8mb4_0900_ai_ci null default null comment "json集合",   `text` text character set utf8mb4 collate utf8mb4_0900_ai_ci null comment "文本(text 存放长文本)",   `blob` blob null comment "不录歌(blob 存放较大的二进制,如图片等)",   `create_time` datetime(0) null default null comment "创建时间",   `update_time` datetime(0) null default null on update current_timestamp(0) comment "更新时间",   primary key (`id`) using btree,   unique index `index_id`(`id`) using btree,   index `index_name`(`name`) using btree,   index `index_age_sex`(`age`, `sex`) using btree ) engine = innodb character set = utf8mb4 collate = utf8mb4_0900_ai_ci row_format = dynamic;   -- 复制表(create) create table dbname.`t_table_info_v2` like dbname.`t_table_info`;   -- 2、表的修改(alter) -- 添加列 alter table dbname.`t_table_info` add new_col varchar(255) null comment "新增列";  -- 修改列 alter table dbname.`t_table_info` change new_col new_col_v2 varchar(50); -- 删除列 alter table dbname.`t_table_info` drop column new_col_v2;   -- 3、表的删除(drop) drop table dbname.`t_table_info_v2`;   -- 4、表的查询(desc) desc dbname.`t_table_info`; show columns from dbname.`t_table_info`;   -- 5、复制表数据 insert into dbname.`t_table_info_v2` select * from dbname.`t_table_info`;   -- 6、重命名表 rename table dbname.`t_table_info_v2` to dbname.`t_table_info_v3`;   -- 7、清空表 truncate table dbname.`t_table_info_v3`;         -- =============================================================================================== -- 二、dml(data manipulation language)数据操作语言 -- 1、增(insert) insert into `dbname`.`t_table_info`(`id`, `name`, `age`, `sex`, `job`, `json_set`, `text`, `blob`, `create_time`, `update_time`, `new_col_v2`)  values (23, "呱呱大王", 7, "女", "教师", "{"uuid":"456"}", null, null, now(), now(), null);   -- 2、删(delete) delete from dbname.`t_table_info` where id = "1";   -- 3、改(update) update `dbname`.`t_table_info` set `name` = "呱呱大王", `age` = 7, `sex` = "女", `job` = "教师", `json_set` = "{"uuid":"456"}", `text` = null, `blob` = null, `create_time` = now(), `update_time` = now(), `new_col_v2` = null where `id` = 1;         -- =============================================================================================== -- 三、dql(data query language)数据查询语言 -- 1、基础查询(select) select * from dbname.`t_table_info`;   -- 2、条件查询(where) select * from dbname.`t_table_info` where id = "1"; -- and和or查询 select * from dbname.`t_table_info` where job in ("律师") and (id in ("1") or age in ("5")); -- between筛选某个范围内的值 select * from dbname.`t_table_info` where age between "1" and "5"; -- 模糊查询 select * from dbname.`t_table_info` where name like "%大王%"; -- 多条件模糊查询  select * from dbname.`t_table_info` where name like "%大王" or job like "%律师%";   -- 3、分页查询(limit) -- 查询5条数据 select * from dbname.`t_table_info` limit 5; -- 查询最新一条数据 select * from dbname.`t_table_info` order by update_time desc limit 1;   -- 4、连接查询(join) -- 内连接-1:内连接查询操作列出与连接条件匹配的数据行 select * from t_table_info a, t_table_info_v2 b where a.id = b.id; -- 内连接-2:内连接查询操作列出与连接条件匹配的数据行 select * from t_table_info a inner join t_table_info_v2 b on a.id = b.id; -- 外连接-左连接:以左表为基准,右表的列为null select * from t_table_info a left join t_table_info_v2 b on a.id = b.id; -- 外连接-右连接:以右表为基准,左表的列为null select * from t_table_info a right join t_table_info_v2 b on a.id = b.id; -- 交叉连接:交叉联接返回左表中的所有行,左表中的每一行与右表中的所有行组合。交叉联接也称作笛卡尔积。 select * from t_table_info a cross join t_table_info_v2 as b on a.id = b.id;   -- 5、子查询(in) select * from dbname.`t_table_info` where id in ("1","5");   -- 6、排序查询(order by) select * from dbname.`t_table_info` order by update_time desc, id desc;   -- 7、分组查询(group by) select job, count(0) as "重复次数" from dbname.`t_table_info` group by job; select job, count(job) as "重复次数" from dbname.`t_table_info` group by job having count(job)>1;   -- 8、时间查询 select * from dbname.`t_table_info`; -- 查询今天的数据 select * from dbname.`t_table_info` where to_days(create_time) = to_days(now()); -- 查询昨天的数据 select * from dbname.`t_table_info` where datediff(create_time, now())=-1; -- 查询最近7天的数据 select * from dbname.`t_table_info` where date_sub(curdate(), interval 7 day) <= date(create_time); -- 查询本月的数据 select * from dbname.`t_table_info` where date_format(create_time, "%y%m") = date_format(curdate(), "%y%m"); -- 查询上月数据 select * from dbname.`t_table_info` where period_diff(date_format(now(),"%y%m"), date_format(create_time, "%y%m"))=1;   -- 9、其它查询 -- 查询json:json_unquote() 去除双引号; json_extract() 提取json的key select json_unquote(json_extract(json_set,"$.uuid")) from dbname.`t_table_info`;         -- =============================================================================================== -- 四、dcl(data control language)数据控制语言 -- 查看用户表 select * from mysql.`user`;   -- 1、创建用户(create) create user student identified by "student";   -- 2、用户号授权(grant) grant select, insert, update, delete on `dbname`.* to `student`@`%`; flush privileges;   -- 3、撤销权限(revoke) revoke delete on `dbname`.* from `student`@`%`;   -- 4、查看权限(show) show grants for "student"@"%";   -- 5、删除用户(drop) drop user "student"@"%";   -- 6、修改用户(update) update mysql.`user` set user = "student_v2" where user ="student" and host = "%";       -- =============================================================================================== -- 五、索引操作 -- 1、索引创建(create) -- 创建唯一索引 create unique index index_id on dbname.`t_table_info` (id); -- 创建普通索引,允许重复值,1个索引 create index index_name on dbname.`t_table_info` (name); -- 创建普通索引,允许重复值,2个索引 create index index_age_sex on dbname.`t_table_info` (age, sex);   -- 2、索引删除(drop) drop index index_name on dbname.`t_table_info`;   -- 3、索引查询(show) show index from dbname.`t_table_info`;     -- =============================================================================================== -- 六、存储过程 -- 简单查询 drop procedure if exists get_info; create procedure get_info() begin    select * from dbname.`t_table_info`; end; call get_info();     -- 批量插入数据 drop procedure if exists batch_data; create procedure batch_data() begin declare i int;   set i=7;   while(i<=8) do            insert into dbname.`t_table_info` (id, name, age, sex, job, json_set, create_time, update_time) values(i, concat("name",id), 1, "男", "律师", "{"uuid":"123"}", now(), now());     set i=i+1;    end while; end; call batch_data();     -- =============================================================================================== -- 七、相关函数 -- 1、时间函数 -- 获取当前时间 select now(), curdate(), curtime(3), current_date(), current_time(3), current_timestamp(2), current_date, current_time, current_timestamp, unix_timestamp(now());   -- 获取当前日期 select date(curdate());   -- 获取当前月份 select month(curdate());   -- 获取本月第一天 select date_add(curdate(), interval-day(curdate())+1 day);    -- 获取本月最后一天 select last_day(curdate());    -- 获取本月天数 select day(last_day(curdate()));   -- 获取当前年份 select year(curdate());   -- date_format(date, format)函数用于以不同的格式显示日期/时间数据,date是日期列,format是格式 select date_format(now(),"%y-%m-%d");   -- str_to_date(str,format) 将字符串转成日期 select str_to_date(now(),"%y-%m-%d") from dbname.`t_table_info`;   -- 查询当前日期是哪个季度的 select extract(quarter from str_to_date(now(),"%y-%m-%d"));   -- 时间区间 select datediff("2022-12-31",now());   -- 加减某个时间间隔函数date_add()与date_sub() -- date_add("某个日期时间",interval 1 时间种类名); quarter:季,week:周,day:天,hour:小时,minute:分钟,second:秒,microsecond:毫秒 -- 加1天 select date_add("2022-01-01", interval 1 day);  -- 加1月 select date_add(now(), interval 1 month);    -- 日期相减 select datediff("2022-12-31", "2022-01-01");   -- 时间相减 select timediff("2022-01-01 00:00:00", "2021-01-02 00:00:00"); select timediff("12:00:00","12:30:00");   -- 时间相减-转换为秒数 select time_to_sec(timediff("12:00:00", "12:02:00"));   -- 查询当年月份,前提:dbname.`t_table_info`大于等于12条数据 select  case    when length(mon)=1 then concat(left(current_date,5),"0",mon)    else concat(left(current_date,5),mon) end months    from (select @m:=@m+1 mon from dbname.`t_table_info`,(select @m:=0) a ) aa limit 12;   -- 2、随机数 -- 生成小于1的随机数 select rand();   -- 生成100以内的随机数 select floor(1 + (rand() * 100));   -- 生成3位随机数 select ceiling(rand()*900+100);   -- 生成4位随机数 select ceiling(rand()*9000+100);   -- 3、其它函数 -- 统计 select count(*) from dbname.`t_table_info`;   -- 查询平均值 select avg(id) from dbname.`t_table_info`;   -- 查询最大值 select max(id) from dbname.`t_table_info`;   -- 查询最小值 select min(id) from dbname.`t_table_info`;   -- 求和 select sum(id) from dbname.`t_table_info`;   -- concat:在字段值前加上前缀aaa- update dbname.`t_table_info` set name = concat("aaa-",name) where name in ("张三","李四");   -- replace:去掉字段值的某一部分 update dbname.`t_table_info` set name=replace(name,"aaa-","") where name in ("aaa-张三","aaa-李四");   -- case举例 select   name,   (case when name = "张三" then "张三呀"         when name = "李四" then "李四呀"         when name is null then "空值"         else "其它情况" end) as 备注 from dbname.`t_table_info`;         -- =============================================================================================== -- 八、其它内容 -- 1、查看数据库大小 -- 查询所有数据库的表大小 select table_name, concat(truncate(data_length/1024/1024,2)," mb") as data_size,concat(truncate(index_length/1024/1024,2)," mb") as index_size from information_schema.tables group by table_name order by data_length desc;   -- 查询某个数据库的表大小 select table_name, concat(truncate(data_length/1024/1024,2)," mb") as data_size,concat(truncate(index_length/1024/1024,2)," mb") as index_size from information_schema.tables where table_schema = "dbname" group by table_name order by data_length desc;   -- 2、设置变量 -- set设置变量 set @job = "教师"; set @age = (select age from dbname.`t_table_info` where id in ("6")); select * from dbname.`t_table_info` where job in (@job) and age in (@age);   -- select设置变量 select @job := "教师"; select * from dbname.`t_table_info` where job in (@job);   -- 查询结果并赋值 select age, job into @age, @job from dbname.`t_table_info` where id in ("6"); select * from dbname.`t_table_info` where age in (@age) and job in (@job);   -- 3、系统变量 -- 查看所有的会话变量 show session variables;   -- 查看所有的全局变量 show global variables;   -- 查看数据库隔离级别 show variables like "%isolation"; select @@global.tx_isolation;   -- 查看mysql版本 show variables like "version%";   -- 查看端口、目录、数据存放目录、服务器id select @@port, @@basedir, @@datadir, @@server_id;   -- 4、explain -- 分析查询的sql语句 explain select * from dbname.`t_table_info`;   -- 5、批量删除表(将结果复制出来并执行) select concat("drop table if exists ", table_name, ";") from information_schema.tables where table_schema = "dbname";
  关注【嘎嘎软件测试】
  搞测试,不迷路
  呱呱大王本呱带你飞!
  嘎嘎软件测试 将分享个人成长、团队管理、软件测试技能知识等内容,更新频率一周两篇,做到有思想、有观点、有深度,欢迎订阅。

杭州最懂生活的人,这两天都在极氪南山路店在今年最新的新一线城市排行榜中,成都与杭州并列一二。作为中国公认最具生活气息的两座城市,如果说成都是一座休闲之都,那杭州就是一座品质之城。东南形胜,三吴都会,钱塘自古繁华。从八千年欧洲杯到底假不假不出意外昨晚荷兰又是把一堆人推上了天台啊,那这个结果到底正不正常呢?争议点估计就在那张红牌吧,但是如果没有那张红牌荷兰就有把握拿下捷克吗?回到前天晚上意大利90分钟内逼平奥地利,昨飞书推出应用服务保险为用户提供更完善的安全保障随着产品生态体系的逐步壮大,以及中大型客户的快速增长,应用的安全性越来越受到重视。近日,字节跳动旗下企业协作与管理平台飞书正式面向平台上的ISV(独立软件开发商),推出了信息技术应开足马力,冲刺10月交付,极氪智慧工厂已完成量产准备日前,极氪宣布智慧工厂已完成量产准备,ZEEKR001将于10月中旬量产下线,开始交付时间已锁定在10月下旬。事实上,受全球供应链影响,各汽车企业都受到部分核心零部件供应状态不稳定上半年山东哪些企业赚的钱多?山东重工120亿,民企淄博黑马超牛文丛树来源鲁商儒风上半年,对于很多企业来说是相当忐忑的,教育培训房地产互联网等行业,有的纷纷遭遇黑天鹅,有的爆雷。相比而言,一向以工业尤其是制造业见长的山东,则显现出稳健的优势。从山东人创办了哪些千亿企业滨州聊城烟台菏泽各1家,新疆1家特殊文丛树来源鲁商儒风20年前,100亿元,对于企业来说是一个很遥远的目标,如今中国企业500强的门槛已经上升到了360多亿元,民企500强的门槛也超过了200亿元。1000亿,已经成美帝能源部将建造工业用重型燃料电池卡车美国能源部(D。O。E。)宣布,它打算投资1亿美元开发一种工业用重型燃料电池卡车。这些资金将用于帮助制定计划和建造H2汽车。这项为重型燃料电池卡车提供新资金的声明是在由国际氢燃料电巴拉德燃料电池电动汽车已经行驶了5000万公里,足以绕地球1250圈巴拉德动力系统公司宣布,该公司的质子交换膜(PEM)燃料电池技术和产品现已为商用重型和中型动力应用中的燃料电池电动汽车(FCEV)供电自2017年以来,全球领先的累计行驶里程超过5教育部部长怀进鹏听取树根互联产教融合新模式专题汇报9月24日下午,教育部党组书记部长怀进鹏在广东省人民政府副省长王曦教育部有关司局省教育厅党组书记朱孔军等领导的陪同下,到广东轻工职业技术学院(以下简称广轻工)调研职业教育发展情况。将太阳能从太空送到地球,变成电流,即将进入大规模商用阶段图片AFRL的空间太阳能增量和示范研究项目由几个小型飞行实验组成,这些实验将成熟建立太阳能配电系统原型所需的技术。在不久的将来,从太空收集并发射到地球上的太阳能可以为全球偏远地区的流浪大师沈巍化名沈书601,在头条自媒体视频。你有何看法?我感觉很快就会被淡忘尊敬的沈大师您好!您执着于您的理念,二十多年日日夜夜始终不渝,您辛苦了!和您虽相隔万里,却近在咫尺。您来了,来到头条开视频。风尘仆仆,带着满腹诗华和日夜践行,带
首发骁龙855Plus处理器花落ROG手机联手腾讯探索120Hz刷新率高通今晚正式发布骁龙855Plus处理器,华硕将会首发搭载骁龙855plus的ROG游戏手机2,成为首家使用骁龙855Plus芯片的厂商。经查,ROG玩家国度官微也确认,定于7月2小米MIX4发布,能否接力华为成国产高端智能机下一个王牌5G时代,万物互联,移动通信进入一个全新的阶段,华为荣耀小米苹果上演智能手机高端市场的大乱斗。美国三轮制裁,使华为芯片存量持续下滑,苹果趁势回归智能机市场的主导,而国产智能机的竞争三星S21U年度安卓机皇发布日前,三星正式发布了新一代旗舰机GalaxyS21系列,这也是近年来问世最早的S系列旗舰,共有GalaxyS21GalaxyS21Plus及GalaxyS21Ultra三款,不仅部小米11正式发布,3999起送充电器?20201228晚19点30分,小米11终于发布了,虽说充电和拍照方面没有太大提升,但是仅凭首发骁龙8883999RMB这一点,毫无疑问又会成为一款热门手机外观屏幕首先是外观方面,小米发布会全面总结,MIX4小米平板5都在这了话不多说,发布会太长,前言就不多说废话了小米MIX4屏幕方面,小米MIX4采用了一块6。67英寸OLED柔性屏,供应商依旧为合作伙伴华星光电。分辨率24001080,120Hz刷新华为P50系列正式发布,全系标配4G7月30日晚730,华为正式在线上发布了P50系列,这不仅是P系列的新旗舰,也是一款命运多舛的国产旗舰机。屏幕外观这次华为发布了两款机型华为P50和P50Pro。华为P50采用了6iPhone12系列发布会卖了一个月的关子,苹果终于把iPhone12系列抬出来了。虽然已经被曝光得底裤都不剩,四款型号外观颜色配置基本都能对上。不卖关子,直接进入今天的四款新iPhone,第一款iPhon房地产税重磅!全国人大常委会授权国务院在部分地区开展改革试点新华社10月23日消息,为积极稳妥推进房地产税立法与改革,引导住房合理消费和土地资源节约集约利用,促进房地产市场平稳健康发展,第十三届全国人民代表大会常务委员会第三十一次会议决定授5个让人惊艳的先进物品!深扒那些让人惊艳的宝藏好物现在都在追求精致生活,很多物品已经渐渐被淘汰了,特别是网上,有很多网红开始吹嘘各种网红用品,刚开始抱着试一试的态度,也没饱多大希望!5个让人惊艳的先进物品!深扒那些让人惊艳的宝藏好太惊艳了?古装剧嘉南传开播,这一次能够养养眼了古装剧嘉南传开播受关注,古风作品的魅力到底是有多大?在最近,下半年中备受观众期待的古装剧嘉南传终于可以和大家见面了。对于这部剧的粉丝而言,可谓是期待了许久,因为从之前官方放出的宣传V社砍掉的太空游戏StarsofBlood概念图最新曝光此前据说是V社参与开发的一款全新的太空海岛游戏被腰斩,最近这款被取消开发的游戏StarsofBlood曝光了最新的概念图,可以看到游戏中的人设和环境等要素内容,一起来看看详情吧!根