第三十六章mysql语句一

sry622 sry622     2022-12-19     168

关键词:

4.外键(一对多):
作用:1.约束 2.节省空间

create table department (
id int auto_increment primary key,
depart_name varchar(32) not null default ‘‘,
num int not null default 0
)engine=Innodb charset=utf8;

create table userinfo (
id int auto_increment primary key,
name varchar(32) not null default ‘‘,
depart_id int not null default 1,

# constraint 外键名(fk_userinfo_depart) foreign key (列名(depart_id)) references 表名(department)(关联的列名(id)),
# constraint fk_userinfo_depart foreign key (depart_id) references department(id)
)engine=Innodb charset=utf8;
注意:
1.不能将创建外键的语句单独拿出来
先将表创建好以后再加约束外键,和删除外键
alter table userinfo add constraint fk_userinfo_depart foreign key (depart_id) references department(id);
alter table userinfo drop foreign key 外键名称(fk_userinfo_depart ); #删除外键
2.外键关联的时候,必须关联的是表的主键ID
3.主键索引:加速查找+不能为空+不能重复

外键的变种:(重点)
1.唯一索引:unique
create table 表名( id int,
num int,
unique(num) #num成为唯一
)engine=Innodb charset=utf8;
作用:num列的值不能重复,加速查找
2.联合唯一索引:
create table 表名(id int,
num int,
unique(id,num)
)engine =Innodb charset=utf8;
作用: num 列和id列的值不能重复,加速查找
3.多重联合索引:
create table 表名(id int,
num int,
... ,
unique(num,int,...)
) engine = Innodb charset=utf8;
5.外键:一对一:
多对多:

高级查询:
增:insert into 表名(列名1,列名2,) values(值1,值2); 添加一条
insert into 表名(列名1,列名2) values (值1,值2),(值1,值2),(值1,值2); #添加多条
insert into 表名(列名1,列名2) select 列名1,列名2 from 表名; #从另外一个表中将数据复制到要操作的表中
删除:删除可以在where后面加条件
delete from 表名;
delete from 表名 where id >10
delete from 表名 where id <10
delete from 表名 where id<=10
delete from 表名 where id>=10
delete from 表名 where id!=10
delete from 表名 where id=10 and name=‘xxx‘;# and:并且,两个条件都必须要成立
delete from 表名 where id=10 or name=‘xxx‘; #or :或者 只要满足一个条件成立

修改:可以在where后面加条件
update 表名 set 列名=‘xxx‘,列名=23 where id >10;

查询:
基本:
select * from 表名;
select 列名,列名 from 表名;
高级:
1. where 条件查询:
select * from 表名 where id>10;
select * from 表名 where id >10 and id<15;
select * from 表名 where id > 10;
!= : 不等与
>= <=
between and :闭区间
select * from 表名 where id between 9 and 12:
in :在某一个集合中
select * from 表名 where id in (9,10,11,...);

select * from t4 where id in (select id from t3 where id between 2 and 4)
是可以这样使用的, 但是不建议大家使用;
2. 通配符: like (alex)
select * from 表 where name like ‘ale%‘ - ale开头的所有(多个字符串)
select * from 表 where name like ‘ale_‘ - ale开头的所有(一个字符)
3.限制取几条:
select * from 表名 limit 索引偏移量,去除多少数据;
select * from t3 limit 0, 10; 第一页
select * from t3 limit 10, 10; 第二页

page = input(‘page:‘)

page 索引偏移量 数据量(offset)
1 0 10
2 10 10
3 20 10
4 30 10

page (page-1)*offset offset

分页核心SQL:

select * from t3 limit (page-1)*offset, offset;
4.排序:
order by:
select * from 表名 order by 列名 desc; descending
select * from 表名 order by 列名 asc; ascending
多列:
create table t7(
id int auto_increment primary key,
num int not null default 0,
age int not null default 0
)charset=utf8;

insert into t7 (num, age) values (2, 12),(3,13),(4, 12);

select * from t4 order by num desc, name asc;

如果前一列的值相等的话, 会按照后一列的值进行进一步的排序.
5.分组:
select 列名,聚合函数(count(num)/sum(num)/max(num)/min(num)/avg(num)) from 表名 group by 列名;
select age, count(num) from t7 group by age;
select age, count(num) as cnt from t7 group by age; 显示别名 as

having 的二次删选:

select 列名,聚合函数 as 别名 from 表名 group by 列名 having 别名 > 条件;
select age, count(num) as cnt from t7 group by age having cnt>1;
where 和 having的区别:
1). having与where类似,可筛选数据
2). where针对表中的列发挥作用,查询数据
3). having针对查询结果中的列发挥作用,二次筛选数据, 和group by配合使用
6.链表操作:
select * from 表名,表名 where 表名.关联id=表名.关联id;
左连接:
select * from userinfo left join department on userinfo.depart_id=department.id;
左边的表全部显示, 右边没有用到不显示
右连接:

select * from userinfo right join department on userinfo.depart_id=department.id;
右边的表全部显示, 左边没关联的用null表示

内连接:
左右两边的数据都会显示

ps:
a.只需要记住左连接 left join

b.可以连接多张表 通过某一个特定的条件

注意查询的顺序:
select name,sum(score) from 表 where id > 10 group by score having age> 12 order by age desc limit 2, 10
select * from score where

wpf学习第三十六章样式基础(代码片段)

原文:【WPF学习】第三十六章样式基础  前面三章介绍了WPF资源系统,使用资源可在一个地方定义对象而在整个标记中重用他们。尽管可使用资源存储各种对象,但使用资源最常见的原因之一是通过他们的保存样式。  样式... 查看详情

正点原子fpga连载第三十六章双路高速da实验-摘自正点原子新起点之fpga开发指南_v2.1(代码片段)

1)实验平台:正点原子新起点V2开发板2)平台购买地址:https://detail.tmall.com/item.htm?id=6097589511132)全套实验源码+手册+视频下载地址:http://www.openedv.com/thread-300792-1-1.html3 查看详情

mysql实战第三十六讲-为什么临时表可以重名?(代码片段)

在上一篇文章中,我们在优化join查询的时候使用到了临时表。当时,我们是这么用的:createtemporarytabletemp_tliket1;altertabletemp_taddindex(b);insertintotemp_tselect*fromt2whereb>=1andb<=2000;select*fromt1j 查看详情

mysql实战第三十六讲-为什么临时表可以重名?(代码片段)

在上一篇文章中,我们在优化join查询的时候使用到了临时表。当时,我们是这么用的:createtemporarytabletemp_tliket1;altertabletemp_taddindex(b);insertintotemp_tselect*fromt2whereb>=1andb<=2000;select*fromt1j 查看详情

第三十五mysql语句

为什么使用数据库:   数据如果比较多时我们需要考虑使用数据库来经行存储数据库的分类:  关系型数据库:    1.有约束    2.基于硬盘的存储(即将数据永久存储到硬盘上,落地)    典型代表:Mysql&nb... 查看详情

第三十六篇hashlib模块hmac模块和logging模块(代码片段)

目录第三十七篇hashlib模块、hmac模块和logging模块一、hashlib模块1.hash是什么2.撞库破解hash算法加密二、hmac模块三、logging模块1.日志的五个级别2.V33.日志配置文件4.总结第三十七篇hashlib模块、hmac模块和logging模块一、hashlib模块1.hash... 查看详情

2018-08-24第三十六课(代码片段)

第三十六课非关系统型数据库-mangodb目录二十四mongodb介绍二十五mongodb安装二十六连接mongodb二十七mongodb用户管理二十八mongodb创建集合、数据管理二十九php的mongodb扩展三十php的mongo扩展三十一mongodb副本集介绍三十二mongodb副本集搭... 查看详情

“全栈2019”java多线程第三十五章:如何获取线程被等待的时间?

...境JDKv11IntelliJIDEAv2018.3文章原文链接“全栈2019”Java多线程第三十五章:如何获取线程被等待的时间?下一章“全栈2019”Java多线程第三十六章:如何设置线程的等待截止时间学习小组加入同步学习小组,共同交流与进步。方式一... 查看详情

-考研第三十周总结-

-考研第三十周总结-【本周完成】✅历年真题本周做了4年+前11年的错题手机背单词每天恋词默写单词,到unit13英语分析摘抄原文练字+翻译、3篇阅读和一篇新题型、背了一篇作文政治马原部分做完+近代史单选第六章&... 查看详情

-考研第三十周总结-

-考研第三十周总结-【本周完成】✅历年真题本周做了4年+前11年的错题手机背单词每天恋词默写单词,到unit13英语分析摘抄原文练字+翻译、3篇阅读和一篇新题型、背了一篇作文政治马原部分做完+近代史单选第六章&... 查看详情

第三十六节,目标检测之yolo源码解析(代码片段)

...析一下。关于yolo目标检测的原理请参考前面一篇文章:第三十五节,目标检测之YOLO算法详解在讲解源码之前,我们需要做一些准备工作:下载源码,本文所使用的yolo源码来源于网址:https://github.com/hizhang 查看详情

爱创课堂每日一题第三十六天-请你谈谈cookie的弊端?

cookie虽然在持久保存客户端数据提供了方便,分担了服务器存储的负担,但还是有很多局限性的。第一:每个特定的域名下最多生成20个cookie1.IE6或更低版本最多20个cookie2.IE7和之后的版本最后可以有50个cookie。3.Firefox最多50个cookie... 查看详情

第三十六天

 css组合选择器<!DOCTYPEhtml><html><head> <metacharset="UTF-8"> <title>组合选择器</title> <styletype="text/css">  /*组合选择器*/  /*特性 查看详情

第三十六课文本编辑器中的功能交互

一、判断未保存的数据1、QPlainTextEdit能够触发与编辑功能相关的信号2、解决方案(1)、定义槽函数voidonTextChanged()(2)、映射textChanged()到槽函数(3)、定义成员变量boolm_isTextChanged=false;(4)、当文本框内容发生改变时, m_i... 查看详情

一起talkandroid吧(第三百三十六回:android中的volley一)(代码片段)

各位看官们,大家好,上一回中咱们说的是Android中HTTP请求的例子,这一回中咱们说的例子是Android中的volley。闲话休提,言归正转。让我们一起TalkAndroid吧!什么是Volley看官们,我们在前面章回中介绍了HTTP的两... 查看详情

mysql实战第三十五讲-join语句怎么优化?(代码片段)

在上一篇文章中,我和你介绍了join语句的两种算法,分别是IndexNested-LoopJoin(NLJ)和BlockNested-LoopJoin(BNL)。我们发现在使用NLJ算法的时候,其实效果还是不错的,比通过应用层拆分成多个语句然后再拼接查询结果更方... 查看详情

mysql实战第三十二讲-为什么还有kill不掉的语句?

在MySQL中有两个kill命令:一个是killquery+线程id,表示终止这个线程中正在执行的语句;一个是killconnection+线程id,这里connection可缺省,表示断开这个线程的连接,当然如果这个线程有语句正在执行... 查看详情

[机缘参悟-65]:《兵者,诡道也》-7-三十六计解读-败战计

目录前言:第1章 《三十六计》概述第六套败战计第三十一计美人计第三十二计空城计第三十三计反间计第三十四计苦肉计第三十五计连环计第三十六计走为上计前言:兵者,诡道也,兵者,“道”的部分ÿ... 查看详情