A, Mysql data types:
1, Value Type:
整数类型:使用unsigned修饰时,只取正值,数值不够指定宽度时,在左边补空格
整数类型包括:tinyint,smallint,int等
浮点型: (5,2)总长度和小数点后的长度
包括:float,double,decimal(固定长度,不符合长度要求输入不进去)
2, character types:
char: 定长,255个字符,右边用空格补齐,效率高
varchar: 可变长,按数据实际大小分配空间,邮箱可用varchar
大文本类型:text、blob
3, the date and time type:
日期时间:datetime(8个字节) timestamp(4个字节)
日期: date 0001-01-01~9999-12-31
年份:year 1901-2155 当用两位表示时 :01-69 表示20XX 70-99 表示 19XX
时间:time HH:MM:SS,占用三个字节
4, a function of time
now() :获取调用此函数时的系统日期时间
month() ,date() time() :获取指定的月份,日期,时间
5, enumerated types: hobbies, gender, profession
字段名 enum(值1,值2,值3) 单选 可用数字来表示
字段名 set (值1,值2,值3) 多选
6, field constraints: how to limit the field assignment
null(为空) ,not null(非空),default(默认值)
Two, Mysql Index
Advantages and disadvantages: take up physical storage space, slow down the write speed; speed up queries
1, the general index: index
可以有多个索引字段,其值可以重复和空值,在表结构中,其key键的值显示为:mul
创建:create table user( id int(3), index(id)); //创建表时创建
create index id on user1(id); //在已存在的表里创建
删除:drop index id on user1; //在user1表里删除index为id的索引
2, a unique index: unique index key value in that column is expressed as: UNI
一个表中可以有多个唯一索引,对应字段的值不能重复,当不为空时,限制功能
And the same primary key.
创建:create unique index hz_id on s2(hz_id);
删除: drop index hz_id on s2;
Third, the primary key, composite primary key, foreign key
1, the primary key: primary key of a table can have only one primary key, which is expressed as: PRI
对应的字段值不能为空。
创建:alter table 表名 add primary key (字段名);
删除:alter table 表名 drop primary key;
primary key 与 auto_increment连用,字段的值自动增长
在创建主键时,在后面加上 auto_increment,删除主键时,
First of all you want to delete from increased property, that is, to modify the properties of the field
2, the composite primary key: a plurality of fields of the table with a primary key, to create together
创建:alter table s1 add primary key(stu_id,name);
删除: alter table s1 drop primary key;
3, foreign key rule: InnoDB storage engine, and the width of the same type, the reference table: preferably a master key
创建:foreign key(A表字段名) references B表名(字段名)
on update cascade(同步更新) on delete cascade;(同步删除)
删除外键:alter table 表名 drop foreign key 约束名;
外键名查询:show create table 表名\G,可以查询
Four, Mysql storage engine
何为存储引擎:负责为数据库执行实际的数据I/O操作,
Different storage engines, its way of storing data is not the same,
In mysql5.7, the default storage engine is innodb.
1, View: show create table sys_in; // view a table storage engine
show engines; //查看数据库支持的和默认的存储引擎
2, modify the default database storage engine: vim /etc/my.cnf
default-storage-engine=myisam/innodb
3, modify a table there is a default engine:
alter table 表名 engine=innodb;
4, the difference between innodb and myisam
myisam: supports table-level locking, suitable for small to read write; .frm (table structure), .MYI (index), .MYD (data)
innodb: supports row-level locking for read-write less, to support foreign keys, supports transactions, transaction rollback, .frm (table structure) .ibd (index + data)
5, locks and related definitions affairs
锁类型:读锁(共享锁),写锁(排它锁)
锁粒度:表锁,行锁,页锁(内存)
事务:一次sql操作从连接到断开连接的过程称为事务。要么成功,要么失败
事务日志文件:ib_logfile(sql语句存放位置);ibdata1(数据信息)