一. 外键的变种: (*********************************************************)

1. 唯一索引:

create table t5(
id int,
num int,
unique(num)
)engine=Innodb charset=utf8;

作用:
num列的值不能重复
加速查找

create table t6(
id int,
num int,
unique(id, num)
)engine=Innodb charset=utf8;

联合唯一索引作用:
num列和id列的值不能重复
加速查找

create table t6(
id int,
num int,
unique(id, num......)
)engine=Innodb charset=utf8;

2. 一对多:

部门表:
id depart_name
1 公关部
2 公共部
3 保安部

员工表:
id name age depart_id(外键)
1 lxxx 12 2
2 xxxx 13 1
3 xxxx 13 2

3. 一对一:

用户表:
id name age
1 zekai 23
2 eagon 34
3 lxxx 45
4 owen 83

博客表:
id url user_id (外键 + 唯一约束)
1 /linhaifeng 2
2 /zekai 1
3 /lxxx 3
4 /lxxx 4

4. 多对多:

用户表:
id name phone
1 root1 1234
2 root2 1235
3 root3 1236
4 root4 1237
5 root5 1238
6 root6 1239
7 root7 1240
8 root8 1241

主机表:

id hostname
1 c1.com
2 c2.com
3 c3.com
4 c4.com
5 c5.com

为了方便查询, 用户下面有多少台主机以及某一个主机上有多少个用户, 我们需要新建第三张表:
user2host:

id userid hostid
1 1 1
2 1 2
3 1 3
4 2 4
5 2 5
6 3 2
7 3 4
创建的时候, userid 和 hostid 必须是外键, 然后联合唯一索引 unique(userid, hostid)

Django orm 也会设计

二. 数据行的操作:

增:
insert into 表名 (列名1, 列名2,) values(值1, 值2);
insert into 表名 (列名1, 列名2,) values(值1, 值2),(值1,值2),(值n,值n);

insert into 表名 (列名1, 列名2,) select 列名1, 列名2 from 表名;

删除:
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 : 或者 只要满足一个条件成立
修改:
update 表名 set name='zekai', age=23 where id > 10;

查询:

基本:
select * from 表名;
select name , age from 表名;

高级:

a. where 条件查询:
select * from 表名 where id=10;
select * from 表名 where id >10 and id<15;
select * from 表名 where id > 10;
!= : 不等与
>= <=

between and: 闭区间
select * from t4 where id between 9 and 12;

in: 在某一个集合中
select * from t4 where id in (9,10,11....);

select * from t4 where id in (select id from t3 where id between 2 and 4)

是可以这样使用的, 但是不建议大家使用;

b. 通配符:
alex

select * from 表 where name like 'ale%' - ale开头的所有(多个字符串)
select * from 表 where name like 'ale_' - ale开头的所有(一个字符)

c. 限制取几条:

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;

d. 排序:

order by

降序:
select * from t4 order by 列名 desc; descending

升序:
select * from t4 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;

如果前一列的值相等的话, 会按照后一列的值进行进一步的排序.

e. 分组

select age, 聚合函数(count(num)/sum(num)/max(num)/min(num)/avg(num)) from 表名 group by 列名;

select age, avg(num) from t7 group by age;

select age, count(num) from t7 group by age;

select age, count(num) as cnt from t7 group by age; 显示别名 as

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配合使用

f. 连表操作
select * from userinfo, department; (笛卡尔积)

select * from userinfo, department where userinfo.depart_id=department.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

转载于:https://www.cnblogs.com/xinfan1/p/11020213.html

Python Day42相关推荐

  1. 【Python百日基础系列】Day42 - Dash_DAQ 开关、拾色器、仪表盘、进度条

    文章目录 一.Dash DAQ 安装 二.布尔开关 daq.BooleanSwitch() 2.1 默认开关 2.2 开关颜色 2.3 带标签的开关 2.4 垂直开关 2.5 禁止点击的开关 2.6 ...

  2. Python九十天学习框架,从1到90,从0基础到IQ100

    每个人的基础以及学习进度都不一样,不管最后是90天学会,还是三个月,或是更久,自学按照这个来也能相对系统的学习知识,而不是零散细碎的知识最后无法整合,所以不管怎么样,学习得有个自己的框架,下面一起来看 ...

  3. python大神-国内某Python大神自创完整版,系统性学习Python

    很多小伙伴纠结于这个一百天的时间,我觉得完全没有必要,也违背了我最初放这个大纲上来的初衷,我是觉得这个学习大纲还不错,自学按照这个来也能相对系统的学习知识,而不是零散细碎的知识最后无法整合,每个人的基 ...

  4. python 100days github_GitHub - rghwer/Python-100-Days: Python - 100天从新手到大师

    Python - 100天从新手到大师 作者:骆昊 最近有很多想学习Python的小伙伴陆陆续续加入我们的交流群,目前我们的交流群人数已经超过一万人,光靠我自己已经无法及时处理小伙伴们的入群申请,更无 ...

  5. github100天python_GitHub - 664028812/Python-100-Days: Python - 100天从新手到大师

    Python - 100天从新手到大师 作者:骆昊 Python应用领域和就业形势分析 简单的说,Python是一个"优雅"."明确"."简单&quo ...

  6. python自学-Python 应该怎么学?

    0714更新: 之前说过骆昊 (jackfrued) 的"从新手到大师"的百天之路!今天给大家分享一个主学习路线的干货图谱,话不多说,直接上图! 这个图谱是按照[专业基础][数据分 ...

  7. Python高级全栈开发实战 老男孩课程S16+路飞学城项目+女神串讲 Python全栈直通车课程

    python高级全栈开发实战 老男孩课程S16+路飞学城项目+女神串讲 Python全栈直通车课程 Python高级全栈开发实战老男孩课程,是可以帮助同学们从零基础开始到项目开发实战的全栈课程,内容非 ...

  8. 路飞学城python全栈开发_[Python] 老男孩路飞学城Python全栈开发重点班 骑士计划最新100G...

    简介 老男孩&路飞学城Python全栈开发重点班课程,作为Python全栈教学系列的重头戏,投入了全新的课程研发和教学精力,也是Python骑士计划的核心教学,由ALEX老师开班镇守,一线技术 ...

  9. python100天-如何系统地学习 Python,100天从新手到大师

    如果你还是迷茫到底如何系统的学习Python,没关系! 为大家整理了Python100天从新手到大师的系统学习教程,让你不用再纠结,一定能帮助到你的问题. 包括从基础的Python脚本到web开发.爬 ...

最新文章

  1. zabbix服务器性能监控工具的安装二
  2. 【LeetCode从零单排】No22.Generate Parentheses
  3. 小程序真的能帮商家挣到钱吗
  4. linux 关闭端口_手把手教你在Linux中快速检测端口的 3 个小技巧
  5. 【COCOS2D-HTML5 开发之一】新建HTML5项目及简单阐述与COCOS2D/X引擎关系
  6. Android--从相册中选取照片并返回结果
  7. fastDFS引入jar包后日志冲突
  8. android looper介绍
  9. java8编程入门 pdf_Java8编程入门官方教程(第6版).pdf
  10. JAVA三大框架入门
  11. Java毕设项目——大学生社团管理系统(java+SSM+Maven+Mysql+Jsp)
  12. JavaWeb——JavaScript精讲之DOM、BOM对象与案例实战(动态添加删除表格)
  13. Postman安装与简单使用
  14. 实验一:基于HMM的拼音转汉字程序|自然语言
  15. 闲谈IPv6-我们在技术思维上需要作出改变(1)
  16. 【PAT B1015】德才论 (c语言)//答案正确
  17. 已知销售额怎么计算成本_知道销售总额怎么计算成本价?
  18. Mac缓解或关闭鼠标加速
  19. MySQL - binlog 图文详解
  20. LeetCode:1219.黄金矿工(Java语言)

热门文章

  1. u盘解密软件_企业都使用哪些数据防泄密软件
  2. 将图片储存在dataset_最新15-16方联体垃圾箱价格图片
  3. go语言服务器连接mysql,golang中连接mysql数据库
  4. python alter_GitHub - sealter/LearnPython: 以撸代码的形式学习Python
  5. 表格图片预览_Mac预览工具使用技巧,Mac预览功能实用技巧大全
  6. centos 计算器_Linux学习之CentOS(十五)--Linux常用命令之bc、man、shutdown...
  7. windows编译libevent
  8. mysql 删除hash分区_MySQL-如何删除hash表分区
  9. 数木桩的c语言编程,Vijos P1007 绕钉子的长绳子 C语言版
  10. 10kv开关柜价格_常用变压器、开关柜介绍、厂家联系方式、报价单分享