mysql数据库查找数据的方法。
今日内容
1.外键的变种
唯一索引:关键字 unique(num)
作用:使指定的列,中的属性不能重复,并且加速查找
案例:
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;
上述案例,idl列与num列都不能重复,可以加快查找速度,理论上是可以添加N个唯一索引,根据实际情况来定
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)
)engine=Innodb charset=utf8;
关联公式
constraint 外键名 foreign key (列名) references 表名(关联的列名),
1. 不能将创建外键的语句单独拿出来,
只能后续使用add添加
alter table 需要关联的表名 add constraint 外键名称 foreign key (关联的列名) references 外键表名(外键列名);
使用drop删除外键
alter table 需要处理的表名 drop foreign key 外键名称;
2. 外键关联的时候, 必须关联的是表的主键ID
3. 练习的时候, 将语句写在文本中, 然后考过去执行
4. 主键索引 : 加速查找 + 不能为空 + 不能重复
3.一对一
在创建外键的时候,加上unique属性,使关联的值变得唯一性
4.多对多
第一个表中的一个行与第二个表中的一个或多个行相关。第二个表中的一个行也可以与第一个表中的一个或多个行相关
比如说有一个用户表,和一个主机表,他们之间的数据需要相互关联,比如说一个用户拥有多台主机
那么我们就需要创建第三张表来存放用户id与主机id的表格,然后相互关联,创建的时候,用户id与主机id必须是外键,
然后联合唯一索引unique(用户id,主机id)
5.数据行的操作
增:
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
mysql数据库查找数据的方法。的更多相关文章
- 将Excel数据导入mysql数据库的几种方法
将Excel数据导入mysql数据库的几种方法 “我的面试感悟”有奖征文大赛结果揭晓! 前几天需要将Excel表格中的数据导入到mysql数据库中,在网上查了半天,研究了半天,总结出以下几种方法,下面 ...
- MySQL数据库mysqlcheck的使用方法
MySQL数据库mysqlcheck的使用方法的相关知识是本文我们主要要介绍的内容,我们知道,mysqlcheck,是mysql自带的可以检查和修复MyISAM表,并且它还可以优化和分析表,mysql ...
- 使用python将mysql数据库的数据转换为json数据
由于产品运营部需要采用第三方个推平台,来推送消息.如果手动一个个键入字段和字段值,容易出错,且非常繁琐,需要将mysql的数据转换为json数据,直接复制即可. 本文将涉及到如何使用Python访问M ...
- 读取mysql数据库的数据,转为json格式
# coding=utf-8 ''' Created on 2016-10-26 @author: Jennifer Project:读取mysql数据库的数据,转为json格式 ''' import ...
- Loadrunner脚本优化-参数化之关联MySQL数据库获取数据
脚本优化-参数化之关联MySQL数据库获取数据 by:授客 QQ:1033553122 测试环境: Loadrunner 11 Win7 64位 实操: 1. 安装MySQL ODBC驱动程序 O ...
- 将mysql数据库的数据导出做成excl表格通过邮件发送附件发给指定人
mysql数据库的数据导出成excl表 方法一: mysql -uroot -p123456 -e “select *.* from 表 into outfile ‘文件路径.xls’into out ...
- MySQL数据库插入数据出现 ERROR 1526 (HY000): Table has no partition for value xxx
MySQL数据库插入数据出现ERROR 1526 (HY000): Table has no partition for value xxx工作的时候发现无法插入数据,报错:ERROR 1526 (H ...
- 如何在MyEclipse中通过hibernate使用jtds驱动连接数据库,并向数据库添加数据的方法
最近学习了下如何在MyEclipse中通过hibernate使用jtds驱动连接数据库,并向数据库添加数据的方法,虽然MyEclipse中自带了连接数据库的方法,我也尝试了下其他方法,如有不当之处请指 ...
- CentOS6 更改Mysql数据库的数据存放位置
mysql使用yum安装时,默认的数据是存储在/var/lib/mysql下.一般情况下,为了数据的安全性,建议将mysql数据库的数据文件存储在系统的第二块磁盘上的目录下可以按照以下步骤进行操作: ...
随机推荐
- .configurable:可配执行 .enumerble:枚举性 .writable:可读写性 .value:数据值
configurable:控制属性能否被删除,只有当属性的configurable特性的值为true时,该属性才能够被删除. 默认值为false,即不可删除) var person = {}; Obj ...
- C# ado.net 操作存储过程(二)
调用存储过程 sql IF OBJECT_ID('RegionInsert') IS NULL EXEC (' -- -- Procedure which inserts a region recor ...
- BZOJ 3895 3895: 取石子 / Luogu SP9934 ALICE - Alice and Bob (博弈 记忆化搜索)
转自PoPoQQQ大佬博客 题目大意:给定n堆石子,两人轮流操作,每个人可以合并两堆石子或拿走一个石子,不能操作者输,问是否先手必胜 直接想很难搞,我们不妨来考虑一个特殊情况 假设每堆石子的数量都&g ...
- learning armbian steps(8) ----- armbian 源码分析(三)
在lib/main.sh当中 ) == main.sh ]]; then echo "Please use compile.sh to start the build process&quo ...
- [python之ipython] jupyter notebook在云端服务器上开启,本地访问
本地ssh到云端: ssh username@xxx.xxx.xxx.xxx -L127.0.0.1:7777:127.0.0.1:8888 把云端的8888端口映射到本地的7777端口 云端运行指令 ...
- poj1737
Connected Graph POJ - 1737 An undirected graph is a set V of vertices and a set of E∈{V*V} edges.An ...
- OpsManage安装过程中遇到的问题和解决方案
系统地址:https://github.com/welliamcao/OpsManage 系统:ubuntu ubuntu使用apt-get进行自动化安装 自带python2.7,不需要再次安装 1. ...
- Java基础线程系列大纲
## Java 多线程之 线程创建 ## Java 多线程之 Sleep ## Java 多线程之 Join ## Java 多线程之 生命周期 ## Java 多线程之 wait, notify a ...
- 深入解析 composer 的自动加载原理
PHP 自5.3的版本之后,已经重焕新生,命名空间.性状(trait).闭包.接口.PSR 规范.以及 composer 的出现已经让 PHP 变成了一门现代化的脚本语言.PHP 的生态系统也一直在演 ...
- Nginx之 Location 的生成
1. Location 的生成 location 的生成大致有三种: 由 location 指令直接生成 命令 location:仅用于 server 内部跳转,如 rewrite 就是命名 loca ...