MySQL 外键 表的查询
自增补充
这是查看怎么创建的表, \G示旋转90度显示表的内容
表的自增的关键是** AUTO_INCREMENT=3**,在表中添加数据后,这个会自动改变,通过alert可以改变这个默认值
mysql> show create table t1 \G;
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
下一次添加的内容的id会从20处添加
alter table t10 AUTO_INCREMENT=20;
自增步长
mysql是的默认步长是基于会话session的,sqlserver是基于表的。
查看全局变量,其中默认是1
mysql> show session variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
2 rows in set (0.00 sec)
设置步长基于会话步长,只能自该自己的会话
set session auto_increment_increment=2; 设置会话步长
set session auto_increment_offset=10; 设置开始的位置
基于全局的级别的,可以修改全部的会话
show global variables like 'auto_inc%'; 查看全局变量
set global auto_increment_increment=2; 设置会话步长
set global auto_increment_offset=10;
唯一索引
unique
create table t1(
id int ....,
num int,
xx int,
unique 唯一索引名称 (列名,列名),
constraint ....
)
这了唯一的意思是:
- 约束不能重复(可以为空)
- 主键不能重复(不能为空)
作用是加速查找
外键的变种
单列
联合
关联一对多
create table userinfo(
id int auto_increment primary key,
username varchar,
usertype int,
)engine=innodb default charset=utf8;
create table admin(
id int auto_increment primary key,
user_id int,
passwprd varchar,
unique index(user_id),
constraint fk_key1 foreign key (user_id) references userinfo(id)
)engine=innodb default charset=utf8;
-- 外键关联多个列
create table t2(
nid int not null auto_increment,
pid int not null,
num int,
primary key(nid,pid)-- 这里的关联两个列的主键
)engine=innodb default charset=utf8;
create table t3(
id int auto_increment primary key,
name char,
id1 int,
id2 int,
constraint fk_t3_t2 foreign key (id1,id2) references t2(nid,pid)
)engine=innodb default charset=utf8;
多对多
-- 多对多
-- 用户表
create table userinfo(
id int auto_increment primary key,
username varchar,
gender int,
)engine=innodb default charset=utf8;
-- 主机表
create table computer(
id int auto_increment primary key,
name varchar,
)engine=innodb default charset=utf8;
-- 用户主机关系表
create table userandcom(
id int auto_increment primary key,
user_id int,
host_id int,
unique index(user_id,host_id),
constraint fk_key2 foreign key (user_id) references userinfo(id),
constraint fk_key3 foreign key (host_id) references computer(id)
)engine=innodb default charset=utf8;
SQL语句数据行操作补充
增
-- 增加单条数据
insert into t1 (name) values('ddd');
增加多条数据
-- insert into t1 (name) values('ddd'),('eee');
-- 从一个表中添加另一个内容
insert into t4(name) select name from t1;
+------+------+
| id | name |
+------+------+
| NULL | aaa |
| NULL | aaa |
| NULL | ccc |
| NULL | ddd |
| NULL | eee |
+------+------+
这里出现null的原因是在创建表的时候没有添加自增和主键
在调试中发现char后面不加长度,默认的长度是1,所以要添加一个长度。这个是根据需求
删
delete from tb12;
delete from tb12 where id !=2
delete from tb12 where id =2
delete from tb12 where id > 2
delete from tb12 where id >=2
delete from tb12 where id >=2 or name='a'
改
update tb12 set name='a' where id>12 and name='xx'
update tb12 set name='a',age=19 where id>12 and name='xx'
查
select * from tb12;
select id,name from tb12;
select id,name from tb12 where id > 10 or name ='xxx';
select id,name as cname from tb12 where id > 10 or name ='xxx';
select name,age,11 from tb12;
select * from tb12 where id != 1
select * from tb12 where id in (1,5,12);
select * from tb12 where id not in (1,5,12);
select * from tb12 where id in (select id from tb11)
select * from tb12 where id between 5 and 12;
- 通配符
通配符的意识替换的意思
%能够替换多个字符
_只能替换一个字符
select * from tb12 where name like "a%"
select * from tb12 where name like "aa_"
- 分页
select * from tb12 limit 10;
select * from tb12 limit 0,10;
select * from tb12 limit 10,10;
select * from tb12 limit 20,10;
后期的Python应用
# page = input('请输入要查看的页码')
# page = int(page)
# (page-1) * 10
# select * from tb12 limit 0,10; 第一页1
# select * from tb12 limit 10,10;第二页2
- 排序
select * from tb12 order by id desc; 大到小
select * from tb12 order by id asc; 小到大
select * from tb12 order by age desc,id desc; # 这是优先级 先按照age倒序,后按照id排序(ID中有相同的)
取后10条数据:先倒序后去取
select * from tb12 order by id desc limit 10;
mysql> select * from t5 order by name desc,id desc;
+----+------+
| id | name |
+----+------+
| 5 | eee |
| 4 | ddd |
| 3 | ccc |
| 2 | aaa |
| 1 | aaa |
+----+------+
5 rows in set (0.00 sec)
mysql> select * from t5 order by name desc,id asc;
+----+------+
| id | name |
+----+------+
| 5 | eee |
| 4 | ddd |
| 3 | ccc |
| 1 | aaa |
| 2 | aaa |
+----+------+
- 分组
select count(id),max(id),part_id from userinfo5 group by part_id;
- count
- max
- min
- sum
- avg
如果对于聚合函数结果进行二次筛选时?必须使用having ,不能使用where
select count(id),part_id from userinfo5 group by part_id having count(id) > 1;
- 连表操作
连表操作主要是把两张表显示在一张表上,主要用过join
select * from userinfo5,department5 -- 这种是笛卡尔积的形式 即所有的乘积
select * from userinfo5,department5 where userinfo5.part_id = department5.id;
左边全部显示
select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
右边全部显示
select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
select * from department5 left join userinfo5 on userinfo5.part_id = department5.id;这种就是变相的right
如果一张表显示全部,但是另一张表还有多的内容的时候,就会出现空null
inner join 将出现null时一行隐藏
select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
mysql> select * from t1 left join t5 on t1.id=t5.id;
+----+-------+------+------+
| id | name | id | name |
+----+-------+------+------+
| 1 | aaa | 1 | aaa |
| 2 | aaa | 2 | aaa |
| 3 | ccc | 3 | ccc |
| 4 | ddd | 4 | ddd |
| 5 | eee | 5 | eee |
| 6 | hahah | NULL | NULL |
+----+-------+------+------+
隐藏空行
mysql> select * from t1 inner join t5 on t1.id=t5.id;
+----+------+----+------+
| id | name | id | name |
+----+------+----+------+
| 1 | aaa | 1 | aaa |
| 2 | aaa | 2 | aaa |
| 3 | ccc | 3 | ccc |
| 4 | ddd | 4 | ddd |
| 5 | eee | 5 | eee |
+----+------+----+------+
5 rows in set (0.00 sec)
数据库的备份
数据库导出
mysqldump -u用户名 -p密码 数据库名称 >导出文件路径 # 结构+数据(导入的时候会自动穿件表并把表的内容插入)
mysqldump -u用户名 -p密码 -d 数据库名称 >导出文件路径 # 仅仅结构
导入现有数据库数据:mysql -uroot -p密码 数据库名称 < 文件路径
注意的是导入的时候不能用dump
MySQL 外键 表的查询的更多相关文章
- mysql 外键和子查询,视图
1.mysql 外键约束 建表时生成外键 foreing key ('sid') references' student'('id'); 建表后添加外键 alter table' course ...
- day03 mysql外键 表的三种关系 单表查询 navicat
day03 mysql navicat 一.完整性约束之 外键 foreign key 一个表(关联表: 是从表)设置了外键字段的值, 对应的是另一个表的一条记录(被关联表: 是主 ...
- MySQL外键和高级查询(连接查询、联合查询、子查询、去重查询)
MySQL的外键 什么是外键,很简单保持数据一致性的一个约束键.如果你有两张表,第一张是学生表,第二张表是一个成绩表,我们来看看保持数据一致性,其实在Django等框架的模型中中也能做关联获取对象. ...
- MYSQL - 外键、约束、多表查询、子查询、视图、事务
MYSQL - 外键.约束.多表查询.子查询.视图.事务 关系 创建成绩表scores,结构如下 id 学生 科目 成绩 思考:学生列应该存什么信息呢? 答:学生列的数据不是在这里新建的,而应该从学生 ...
- mysql外键是多个id组成的字符串,查询方法
借鉴:mysql使用instr达到in(字符串)的效果 结论:select * from 表名where INSTR(CONCAT(字符串),CONCAT(表id)) 问题来源:一表中的某字段是另一表 ...
- Django ORM - 001 - 外键表查询主表信息
开始用Django做web开发,我想大家都会遇到同样的问题,那就是如何高效快速的查询需要的数据,MVC都很简单,但是ORM折腾起来就有些费时间,我准备好好研究下Django ORM,所以会有一个系列的 ...
- 数据库内连接GROUP BY查询外键表数据行的总数
最近看了看SQL,刚好遇到这个问题. INNER JOIN [外键表] ON [主键表] 内链接,用 GROUP BY 分组外键数据,COUNT(*)计算该外键数据总行数,最后用 ORDER BY 排 ...
- mysql 外键(FOREIGN KEY)
最近有开始做一个实验室管理系统,因为分了几个表进行存储·所以要维护表间的关联··研究了一下MySQL的外键. (1)只有InnoDB类型的表才可以使用外键,mysql默认是MyISAM,这种类型不支持 ...
- MySQL外键约束On Delete、On Update各取值的含义
主键.外键和索引的区别? 主键 外键 索引 定义: 唯一标识一条记录,不能有重复的,不允许为空 表的外键是另一表的主键, 外键可以有重复的, 可以是空值 主索引(由关键字PRIMARY定义的索引) ...
随机推荐
- JAVA企业级开发-JavaScript(02)
一.JavaScript介绍 Javascript语言诞生主要是完成页面的数据验证.因此它运行在客户端,需要运行浏览器来解析执行JavaScript代码. 特点: 交互性(它可以做的就是信息的动态交互 ...
- 火狐restclient安装和使用
RESTClient是一款用于测试各种Web服务的插件,它可以向服务器发送各种HTTP请求(用户也可以自定义请求方式),并显示服务器响应.使用RESTClient您可以方便的测试各种Web服务,为您的 ...
- 给出每个员工每年薪水涨幅超过5000的员工编号emp_no、薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列。
题目描述 给出每个员工每年薪水涨幅超过5000的员工编号emp_no.薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列. 提示:在s ...
- [UE4]用C++如何创建Box Collision
http://www.dawnarc.com/2016/08/ue4%E7%94%A8c--%E5%A6%82%E4%BD%95%E5%88%9B%E5%BB%BAbox-collision/ 在蓝图 ...
- codevs1229 数字游戏
1229 数字游戏 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解
- Maven入门 项目的生命周期&pom.xml配置&仓库
- java.exe is valid, but is for a machine type other than the current machine
java.exe is valid, but is for a machine type other than the current machine jdk版本不一致问题,在32位机器上使用64位的 ...
- JSPs only permit GET POST or HEAD的解决方案(REST风格)
问题:原文链接 https://blog.csdn.net/tiberroot/article/details/76615727 看到很多人解决办法使用 @ResponseBody注解 这个意思是按照 ...
- 牛客假日团队赛2 C.修围栏
链接: https://ac.nowcoder.com/acm/contest/924/C 题意: 农民 John 希望修复围绕农场的一小段围栏.他测量了一下,发现需要N (1 <= N < ...
- [FACT_采购信息]增加了延期天数
[延期天数]是指的采购单上的货品交货日期 减 [厂家来货]单据货品第一次到货日期. [FACT_采购信息] SELECT p.[Purchase_ID] [采购单号ID], p.[Supply_No] ...