自增补充

这是查看怎么创建的表, \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 外键 表的查询的更多相关文章

  1. mysql 外键和子查询,视图

    1.mysql 外键约束 建表时生成外键   foreing key ('sid') references' student'('id'); 建表后添加外键  alter table' course ...

  2. day03 mysql外键 表的三种关系 单表查询 navicat

    day03 mysql navicat   一.完整性约束之     外键 foreign key     一个表(关联表: 是从表)设置了外键字段的值, 对应的是另一个表的一条记录(被关联表: 是主 ...

  3. MySQL外键和高级查询(连接查询、联合查询、子查询、去重查询)

    MySQL的外键 什么是外键,很简单保持数据一致性的一个约束键.如果你有两张表,第一张是学生表,第二张表是一个成绩表,我们来看看保持数据一致性,其实在Django等框架的模型中中也能做关联获取对象. ...

  4. MYSQL - 外键、约束、多表查询、子查询、视图、事务

    MYSQL - 外键.约束.多表查询.子查询.视图.事务 关系 创建成绩表scores,结构如下 id 学生 科目 成绩 思考:学生列应该存什么信息呢? 答:学生列的数据不是在这里新建的,而应该从学生 ...

  5. mysql外键是多个id组成的字符串,查询方法

    借鉴:mysql使用instr达到in(字符串)的效果 结论:select * from 表名where INSTR(CONCAT(字符串),CONCAT(表id)) 问题来源:一表中的某字段是另一表 ...

  6. Django ORM - 001 - 外键表查询主表信息

    开始用Django做web开发,我想大家都会遇到同样的问题,那就是如何高效快速的查询需要的数据,MVC都很简单,但是ORM折腾起来就有些费时间,我准备好好研究下Django ORM,所以会有一个系列的 ...

  7. 数据库内连接GROUP BY查询外键表数据行的总数

    最近看了看SQL,刚好遇到这个问题. INNER JOIN [外键表] ON [主键表] 内链接,用 GROUP BY 分组外键数据,COUNT(*)计算该外键数据总行数,最后用 ORDER BY 排 ...

  8. mysql 外键(FOREIGN KEY)

    最近有开始做一个实验室管理系统,因为分了几个表进行存储·所以要维护表间的关联··研究了一下MySQL的外键. (1)只有InnoDB类型的表才可以使用外键,mysql默认是MyISAM,这种类型不支持 ...

  9. MySQL外键约束On Delete、On Update各取值的含义

    主键.外键和索引的区别?   主键 外键 索引 定义: 唯一标识一条记录,不能有重复的,不允许为空 表的外键是另一表的主键, 外键可以有重复的, 可以是空值 主索引(由关键字PRIMARY定义的索引) ...

随机推荐

  1. 过滤asp.net页面每次发出请求之前访问

    public class PageFiltert : System.Web.UI.Page { public PageFiltert() { // //TODO: 在此处添加构造函数逻辑 // } p ...

  2. 在 WinForm/WPF 下制作 Google Material Design 风格程序

    国内社区没有,顺手转.WinForm: https://github.com/IgnaceMaes/MaterialSkin演示:https://www.youtube.com/watch?v=A8o ...

  3. JavaScript 原型的实际应用之实现一个 jQuery

    我们平时使用jQuery大概是这样: let $p = $('p'); $p.css('fontSize', '40px'); 我们生成jQuery实例对象后,就可以使用原型上的css(), html ...

  4. [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)

    题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...

  5. IT兄弟连 JavaWeb教程 过滤器与监听器经典面试题

    1.谈谈你对Servlet过滤器的理解 过滤器是Servlet2.3规范中定义的一种小型的.可插入的Web组件.用来拦截Servlet容器的请求和响应过程,以便查看.提取客户端和服务器之间正在交换的数 ...

  6. MyBatis二级缓存配置

    正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 Mybatis二级缓存是SessionFactory,如果两次查询基于同一个SessionFactory,那么就从二级缓存 ...

  7. A JAX-WS web service is by itself a Singleton

    http://stackoverflow.com/questions/11096310/singleton-object-in-java-web-service http://stackoverflo ...

  8. 测试REST Web服务

    EST Web服务的测试计划 线程组 HTTP请求 与任何Jmeter测试一样,我们首先需要创建一个线程组以及一个HTTP请求采样器. 如果您现在运行测试,则可能会收到错误,响应代码为415,响应消息 ...

  9. vuex初使用(写的当然是最简单的应用啦)

    关于vuex的简图 vuex文档:https://vuex.vuejs.org/zh-cn/installation.html 一:npm安装 npm install vuex --save 在mai ...

  10. Java怎么把一个.log文件,以text文件方式打开,显示在桌面

    总要有一个开始吧 群里面有一个哥们,问这个问题,索性记录下来, quextion: Java怎么把一个.log文件,以text文件方式打开,显示在桌面 anwser: 这里注意一个问题:拼接路径的时候 ...