1.存储引擎
- 存储引擎是用于根据不同的机制处理不同的数据。
- 查看mysql中所有引擎:
- show engines;
- myisam: 5.5以前老的版本使用的存储引擎 - blackhole: 类似于队列中的数据,存进去消失 - innodb: 默认使用存储引擎 - 支持事务 - 行锁 - 外键 - memory: 通电数据存在,断电丢失 create table t1(id int)engine=innodb; create table t2(id int)engine=myisam; create table t3(id int)engine=blackhole; create table t4(id int)engine=memory; - 提前学习 redis: 面试问的比较多; - 插入数据验证引擎的作用: insert into t1 values(1); insert into t2 values(2); insert into t3 values(3); ##结果隐藏了 insert into t4 values(4); 2.创建表完整的语法 # 约束条件: 可有可无 # 宽度: 限制某些数据类型的存储大小 create table 表名( 字段名1 字段类型(宽度) 约束条件, 字段名2 字段类型(宽度) 约束条件 ); # 初始约束条件: not null create table teacher( id int not null, # 约束插入记录时id不能为空 name varchar(16), age int ); insert into teacher values(null, 'tank', 17); insert into teacher values(1, 'tank', 17); 注意: 1.创建表的字段名不能重复; create table test( id int, id int ); 2.最后一个字段不能在末尾加 , 号 create table test( id int, age int, ); 3.字段名必须要有字段类型与宽度 create table test( id int, name char ); insert into test values(1, 'tank'); alter table test modify name char(4); insert into test values(2, 'sean'); 2.字段类型 1) 确定表结构 2) 字段与字段类型 - 整型: - tinyint: 默认范围 -128, 127 create table t5( id tinyint, name varchar(16) ); 5.6.40 insert into t5 values(-128, 'tank'), (127, 'jason'); insert into t5 values(-129, 'tank'); insert into t5 values(128, 'jason'); insert into t5 values(12, 'sean'); - int: 默认范围是(-2147483648, 2147483647) 应用场景: id号、年龄... create table t6( id int ); # int 默认宽度11---> 默认展示宽度 insert into t6 values(-2147483649); insert into t6 78values(2147483648); insert into t6 values(100); create table t7( id int(3) ); # 若插入超过设定宽度,则正常显示 insert into t7 values(123456); # 若插入不足够4位,则以空格补全 insert into t7 values(1); - bigint - 浮点型: 应用场景: 工资、身高、体重... - float - double - decimal # 范围255是最大长度(不包括.小数), 30代表是小数的位数 ##255代表的是数字的总长度,不包括小数点 整数最多是225位 create table t8(x float(255, 30)); create table t9(x double(255, 30)); create table t10(x decimal(65, 30)); # 插入数据 # 三种浮点型: 区别在于精确度不一样 insert into t8 values(1.111111111111111111111111111111); insert into t9 values(1.1111111111111111111111111111); insert into t10 values(1.1111111111111111111111111111); >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>打印的结果 +----------------------------------+ | x | +----------------------------------+ | 1.111111164093017600000000000000 | +----------------------------------+ 1.111111111111111200000000000000 1.111111111111111111111111111111 以上可以看出 精确度 :decimal > double > float
- 字符类型
- char(16): 定长字符
char: 手机号、身份证号、银行卡号等... - 插入16个字符: create table t11( id int, name char(4) # 4 ); insert into t11 values(1, 'tank'); # utf8 中文3个bytes gbk 中文2个bytes insert into t11 values(2, '大鸡哥大鸡哥'); 优点: 存取速度快 缺点: 浪费空间。 insert into t11 values(1, 't'); # t+三个空格 egon + sean + tank - varchar(16): 不定长字符 - 存几个字符,就是几个字符的大小,每个字符前都要+1bytes ##这个1个的bytes是另外加的 不在设定的字符长度内 - 插入16个字符 ---> 1bytes+ 优点: 节省空间。 create table t12(id int, name varchar(4)); insert into t12 values(1, 'egon'); # 1bytes + egon insert into t12 values(2, 'tank'); # 1bytes + tank insert into t12 values(3, 'sean'); # 1bytes + sean insert into t12 values(4, 't'); # 1bytes + t 1bytes + egon 、 1bytes + tank、 1bytes + sean - 日期类型 - date: 2019-12-11 - datetime: 2019-12-11 11:11:11 - time: 11:11:11 - year: 2019 - timestamp: 时间戳 create table student( id int, name varchar(4), birth date, register datetime, work_time year, t_time time, update_time timestamp ); insert into student values(1, '张全蛋', '2019-11-11', '2019-11-11 11:11:11','2019', '11:11:11', null); insert into student values(2, 'HCY', '1000-11-11', '1980-11-11 11:11:11','2019', '11:11:11', null); update student set name='HCY2号' where id=2; python 插入时间数据时,转成str类型。 - 枚举与集合 - enum: 可以 多选一 create table t13( id int, name varchar(4), gender enum('male', 'female', 'others') ); # insert into 表名(字段名) values(字段名对应的值); insert into t13(id, name, gender) values(1, 'tank', 'male'); # 严格模式下,选择枚举以外的值会报错 insert into t13(id, name, gender) values(2, 'gd', '人Y'); - set: 可 多选一 或 多选多 create table t14( id int, name varchar(4), gender enum('male', 'female', 'others'), hobbies set('read', 'sing', '生蚝', 'HSNM', '架子鼓') ); # 多选一 insert into t14 values(1, '大鸡J', 'others', 'HSNM'); # 多选多 insert into t14 values(2, 'tank', 'male', 'read,架子鼓,sing,生蚝'); # 多选多的顺序可不一 insert into t14 values(2, 'tank', 'male', 'read,架子鼓,sing,生蚝'); 3.约束条件 - not null + unique: create table user1( id int not null, name varchar(4) ); insert into user1(id, name) values(null, 'tank'); insert into user1(id, name) values(1, 'tank'); - unique 将某个字段设置为唯一的值 # not null + unique create table user2( id int not null unique, name varchar(4) ); insert into user2(id, name) values(1, 'tank'), (2, 'sean'); - primary key + auto_increment: 主键+自增 - primary key -----》 not null + unique - pk就是表中的索引: 可以通过索引快速查找某些数据。 - 提高查询效率 # 将id设置为主键,非空切唯一 create table user3( id int primary key, name varchar(4) ); insert into user3(id, name) values(1, 'tank'); insert into user3(id, name) values(2, 'tank'); - auto_increment: # 将id设置为自增 create table user4( id int primary key auto_increment, name varchar(4) ); # 自增默认从0开始 insert into user4(name) values('tank'); insert into user4(name) values('sean'); insert into user4(name) values('egon'); insert into user4(name) values('大鸡哥'); # 若想自增从指定值开始,可插入第一条数据时先指定id的值; insert into user4(id, name) values(10, 'tank'); insert into user4(name) values('sean'); # 11 insert into user4(name) values('egon'); # 12 insert into user4(name) values('大鸡哥'); # 13 +----+-----------+ | id | name | +----+-----------+ | 1 | tank | | 2 | sean | | 3 | egon | | 4 | 大鸡哥 | | 10 | tank | | 11 | sean | | 12 | egon | | 13 | 大鸡哥 | +----+-----------+ - unsigned - 无符号 create table user5( id int unsigned ); # 报错 insert into user5 values(-100); insert into user5 values(0); insert into user5 values(100); - 有符号 create table user6( id int ); insert into user6 values(-100); - zerofill 使用0填充空格 create table user7( id int zerofill ); insert into user7 values(100); -default 默认 create table user_1(pwd varchar(20) default'1000000') insert into user_1 values('1000000') - 删除记录 create table user8( id int primary key auto_increment, name varchar(4) ); insert into user8(name) values('tank'); insert into user8(name) values('大大大'), ('egon'); - delete: # 清空user8表中的所有记录 delete from user8; - truncate: # 清空user8表中的所以记录,并且id重置为0 truncate table user8;
来源:https://www.cnblogs.com/bs2019/p/12037300.html