### part1

时间类型

date YYYY-MM-DD 年月日 (出现日期) time HH:MM:SS 时分秒 (竞赛时间) year YYYY 年份值 (红酒年份 82年矿泉水) datetime YYYY-MM-DD HH:MM:SS 年月日时分秒 (登录时间,下单时间)

create table t5(d date,t time,y year,dt datetime);

insert into t5 values("2019-7-30","09:08:55","2019","2019-10-01 17:13:33");

insert into t5 values(now(),now(),now(),now());

timestamp YYYYMMDDHHMMSS 自动更新时间戳,不需要手动写入(在修改数据的时候,自动更新最后一次修改时间) create table t6(dt datetime,ts timestamp); insert into t6 values(null,null); insert into t6 values(20190730091420,20190730091420); insert into t6 values(20190730091420,20390730091420); error 超过了时间戳的范围;

mysql 内置函数

now() 获取当前时间

### part2

约束: 对插入数据库的值进行限制,不满足条件的不让操作

unsigned 无符号整型 not null 不能为空 default 设置默认值 unique 唯一约束,数据唯一不重复. primary key 主键,唯一不为空的值,用于表达数据的唯一性 auto_increment 自增加1 [一般是对primary key 或者 unique 进行设置] foreign key 外键,把多张表通过一个字段关联在一起 zerofill 零填充,int(10),位数不够的时候前面补0,前导零

unsigned 无符号整型

create table t66(id int unsigned);

insert into t66 values(3);

insert into t66 values(-3); error

not null 不能为空

create table t7(id int not null,name varchar(255));

insert into t7 values(1,"333")

insert into t7 values(null,"2") error

default 设置默认值

create table t8(id int not null ,name varchar(255) default "李毅");

insert into t8 values(1,null)

insert into t8(id) values(2);

unique 唯一约束(索引),数据唯一不重复. UNI

"""索引是为了加快查询速度的,相当于字典当中的目录""" create table t9(id int unique,name char(12) default '曾文'); insert into t9(id) values(1); insert into t9(id) values(1); error 不能插入重复值 insert into t9(id) values(null); insert into t9(id) values(null); success 可以连续插入空值

primary key 主键,唯一不为空,用于表达数据的唯一性 PRI

# 原型:

create table t10(id int not null unique,name char(6) default '陈学斌');

# 主键

create table t11(id int primary key ,name char(6) default '陈学斌');

insert into t11 values(1,"力争")

insert into t11 values(null,"力争") error 不能为空

auto_increment 自增加1 [一般是对primary key 或者 unique 进行设置]

create table t12(id int primary key auto_increment, name varchar(11) default "王伟")

insert into t12 values(null,"李杰"); # 通用写法

insert into t12 values(5,"李杰2");

insert into t12(name) values("重汽彩");

删除:

# (1)delete from 表 where 条件

delete from t12 where id = 6

delete from t12

# (2)truncate table 表名 所有数据全部删除,id号重置(速度更快)

truncate table t12

### part3

1.联合唯一约束 unique(字段1,字段2,...)

(1) 联合唯一主键

"""单独的ip 或者端口都可以重复,但是联合在一起不能重复,针对于多个字段来讲的"""

create table t1_server(id int,server_name char(10) not null , ip char(15) not null , port int not null , unique(ip,port));

insert into t1_server(id,server_name,ip,port) values(4,"aa","192.168.75.128",3306);

insert into t1_server(id,server_name,ip,port) values(4,"aa","192.168.75.128",3307);

insert into t1_server(id,server_name,ip,port) values(4,"aa","192.168.75.128",3306); error

(2) 联合唯一索引 MUL 代表普通索引

'''如果要创建联合字段约束,最好设置非空,否则的话连续插入null 是默认允许的;'''

create table t2_server(id int,server_name char(10) not null ,ip char(15),port int ,unique(ip,port))

insert into t2_server(id,server_name,ip,port) values(4,"aa","192.168.75.128",3306);

insert into t2_server(id,server_name,ip,port) values(4,"aa","192.168.75.129",3306);

insert into t2_server(id,server_name,ip,port) values(4,"aa",null,null);

insert into t2_server(id,server_name,ip,port) values(4,"aa",null,null);

(3) 如果两种类型(主键,联合主键)如果都在同一个表里,如何显示? 优先显示主键为PRI ip显示MUL 普通索引

alter table t1_server add primary key(id);

# (了解) primary key(ip,port) 与 unique 写法用法相似,区别在于不能再继续添加主键了.

zerofill 零填充,int(10),位数不够的时候前面补0,前导零

create table ceshi01(id int(5) zerofill);

insert into ceshi01 values(1)

foreign key 外键,把多张表通过一个字段关联在一起

"""外键要求: 主动关联的表字段用foreign key , 被关联的字段必须唯一 (unique 或 primary key,一般设置为主键)"""

student1:

id name age classname

1 wangwen 18 python6期

2 liyi 38 python6期

3 zengwen 99 python6期

4 chenyu 80 python6期

5 xuebin 87 python7期

# 为了避免过多的出现冗余数据,开始进行分表操作,利用外键关联不同的表

student1:

id name age classid

1 wangwen 18 1

2 liyi 38 1

3 zengwen 99 1

4 chenyu 80 1

5 xuebin 87 2

class1:

id classname

1 python6期

2 python7期

# 创建class1表

create table class1(id int,classname varchar(255));

# 创建学生表 foreign key(字段) references class1(字段)

create table student1(id int primary key auto_increment,name varchar(255) not null,age int not null,classid int,foreign key(classid) references class1(id) );

# 设置id为unique

alter table class1 add unique(id);

# 插入数据

insert into class1 values(1,"python6期");

insert into class1 values(2,"python7期");

insert into student1 values(null,"liyi",90,1);

insert into student1 values(null,"xuebin",90,2);

insert into student1 values(null,"wangwen",18,2);

# 删除class1中的数据

delete from class1 where id = 1 # 删不掉的,因为被动关联其他表

delete from student1 where id = 1 # 先删除学生表中所有跟这个班级关联的数据,然后在删除class1表中的对应班级

# 外键的联级操作

"""

on update cascade 联级更新

on delete cascade 联级删除

"""

create table class2(id int unique,classname varchar(255));

create table student2(id int primary key auto_increment,name varchar(255) not null,age int not null,classid int, foreign key(classid) references class2(id) on update cascade on delete cascade );

insert into class2 values(1,"python6");

insert into class2 values(2,"python7");

insert into student2 values(null,'zengwen',100,1);

insert into student2 values(null,'lizeng',101,2);

insert into student2 values(null,'xuebin',103,2);

# 删除class2表 对应的学生表中与他相关的所有数据都会被删除

delete from class2 where id = 2;

# 更新class2表的 id号 , student2中的classid 也会被更新. 操作的是被关联的那张表. 1对多的那个1

update class2 set id = 10 where classname = "python6";

# 表与表之间的关系:

(1) 一对一: 一个人对应一个身份证号 在外键上要添加2个约束 unique + foreign key

(2) 一对多或者多对一: 一个班级对应多个学生,在多个学生的那个表里设置外键,被关联的另外一张表设置unique 或者 primary key 表达唯一.

(3) 多对多:一个学生对应多个学科,一个学科可以被多个学生学生,一本书可以对应多个作者,一个作者可以出版多本书.

把xid 和 sid 设置成外键,关联xueke 的id 和 student 的id 这两个id设置成主键

xueke (表1)

id xueke_name

1 math

2 huaxue

3 english

4 wuli

student (表2)

id name

1 王文

2 李杰

3 王伟

relation (关系表3)

xid sid

1 1

1 2

1 3

2 1

2 2

2 3

### part4

1.添加/删除 约束 not null

# alter table 表名 modify 字段名 类型 ...

alter table t1 modify id int not null

alter table t1 modify id int

2.添加/删除 unique 唯一索引

# alter table 表名 add unique(id)

alter table t1 add unique(id)

alter table t1 drop index id

3.添加/删除 primary key

# alter table 表名 add primary key(id)

alter table t1 add primary key(id);

alter table t1 drop primary key;

4.添加/删除 foreign key 外键

# student1 删除它的外键 第一步先用show create table student1 看一下外键名字

alter table student1 drop foreign key student1_ibfk_1 # 删除

alter table student1 add foreign key(classid) references class1(id) # 添加

### part5

事务: 在操作一些列sql语句的时,只有都执行成功才算最终成功,但凡有一个失败,就回滚,恢复到最初的数据状态;

begin 开始事务处理 commit 提交数据 rollback 回滚

存储引擎:

""" show engines\G """

概念理解:

行级锁: 有一个人再修改这张表中的一条记录,这条记录就会上锁,其他人在上锁期间改不了,保证数据的安全性.(允许更大的并发和更快的速度) 表级锁: 有一个人在修改这张表,就会上锁,其他人修改不了 外键foreign key : 把多张表通过一个字段关联在一起

存储引擎的种类

InnoDB : 5.6版本之后 默认的存储引擎 特点: 支持事务, 行级锁, 外键 , 内存开销大

MyISAM : 5.6版本之前 默认的存储引擎 特点: 表级锁,不支持并发.内存开销小

MEMORY : 把数据存储在内存当中,也可以把这种存储形式叫做缓存. 特点: 速度快,但是不能进行持久化存储.

BLACKHOLE : 黑洞 ,用作同步数据的存储引擎方法(数据库的主从复制) 特点: 所有数据都不会真正写入,但是都会提示成功.

create table innodb2(id int ,name char(2)) engine = innodb;

innodb2.frm 表结构

innodb2.ibd 表数据

create table myisam(id int , name char(3)) engine = myisam;

myisam.frm 表结构

myisam.MYD 表数据

myisam.MYI 表索引

create table memory1(id int , name char(3)) engine = memory;

memory.frm 表结构,只是单纯的存储结构,数据都放在内存中

create table blackhole1(id int , name char(3)) engine = blackhole;

blackhole1.frm 表结构 负责生产binlog日志,舍弃数据.

mysql 引擎 外键_mysql的事物,外键,与常用引擎相关推荐

  1. mysql 所有外键_mysql中的外键

    mysql中的外键 1.默认的外键存在之后,会对数据进行约束. 1)约束1:如果子表中添加的数据,外键字段对应的数据如果在父表中不存在,那么添加失败. 有数据之后: 修改:可以修改跟外键不相关的任何字 ...

  2. mysql 主外键_mysql中主外键关系

    一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复的,不允许为空,用来保证数据完整性 外键:是另一表的主键, ...

  3. mysql建表 外键_mysql建表外键怎么设

    mysql建表外键怎么设 mysql建表时设置外键的方法:在"CREATE TABLE"语句中,通过"[CONSTRAINT ] FOREIGN KEY 字段名 [,字段 ...

  4. mysql建表外键_mysql建表外键怎么设?

    mysql建表时设置外键的方法:在"CREATE TABLE"语句中,通过"[CONSTRAINT ] FOREIGN KEY 字段名 [,字段名2,-] REFEREN ...

  5. mysql设置外键_mysql怎么增加外键

    mysql增加外键的方法:1.在CREATE TABLE语句中,通过FOREIGN KEY关键字来添加外键:2.在ALTER TABLE语句中,通过ADD和FOREIGN KEY关键字来添加外键. M ...

  6. mysql 联合主键_Mysql 创建联合主键

    Mysql 创建联合主键 2008年01月11日 星期五 下午 5:21 使用primary key (fieldlist) 比如: create table mytable ( aa int, bb ...

  7. mysql foreign key 设置_mysql foreign key(外键) 说明与实例

    一,什么是foreign key,及其完整性 个人觉得,foreign key就是表与表之间的某种约定的关系,由于这种关系的存在,我们能够让表与表之间的数据,更加的完整,关连性更强.关于完整性,关连性 ...

  8. mysql foreign key 用法_mysql foreign key(外键) 说明与实例

    一,什么是foreign key,及其完整性 个人觉得,foreign key就是表与表之间的某种约定的关系,由于这种关系的存在,我们能够让表与表之间的数据,更加的完整,关连性更强.关于完整性,关连性 ...

  9. mysql的外键_mysql如何查看外键

    展开全部 查看mysql外键方式主要是通过第三方工具或者62616964757a686964616fe4b893e5b19e31333431373233是sql语句,主要有以下三种方式 1.使用Nav ...

最新文章

  1. saccharomyces_cerevisiae 酿酒酵母
  2. java web运行的快慢_WebAssembly执行速度真的很强悍吗?对微软Edge很无语
  3. UbuntuKylin技巧
  4. String.Format in javascript
  5. python中plot的plt.text_用Python进行数据可视化的第一步,全面详解matplotlib中样式属性...
  6. 批处理-文件比较生成
  7. spring ref historydesign philosophy
  8. 添加错误debug信息
  9. Hive 导数据到本地(2种方式)
  10. 边开车边唱K?特斯拉汽车卡拉OK功能即将推出
  11. 使用k8s安装minio
  12. USB-IF 再度为 USB 3 改名,这次更难辨别了
  13. Mat类具体解释(二)
  14. 问题一:CodeBlocks环境搭建及创建第一个C++程序
  15. android 大众点评,Android 大众点评的接入
  16. php tp5生成条形码,TP5条形码
  17. PC端 VUE 官网项目 前端开发 响应式布局(宽+高 等比例缩放)
  18. OSG使用OpenGL(以及glad库)绘制自定义图形
  19. 初识-Python-day03
  20. C1.Win.C1GanttView.C1GanttView 甘特图使用经验:子任务

热门文章

  1. Python中的decorator装饰器使用方法
  2. PTA寒假基础题训练(含解题思路)(中)
  3. 「镁客早报」恒大健康称首款电动汽车拟6月投产;产业链透露苹果正悄悄准备Apple Car...
  4. Android自动化的一般方法
  5. mysql只读事务不分配事务id_只读事务是否做无用功?
  6. 科技云报道:车云协同,云计算下一个主战场?
  7. win7 vs2013 编译错误
  8. (软件工程视频总结)之软件测试
  9. Python爬虫入门(一)火车票余票实时提醒
  10. 6、幻灯管理 - 后端功能开发 - 微擎小程序模块应用开发