MySQL database storage engine, creating a table full syntax, field types, constraints

1. Storage Engine
- storage engine is different depending on the mechanism used to process different data.
- View mysql in all engines:
  - Show Engines;

- myisam: 5.5 storage engine previously used the old version
- blackhole: similar to the data queue, deposit into the disappearance

- innodb: default storage engine
  - Support Services
  - row lock
  - foreign key

- memory: Memory engine (powered data exists, power loss)

Create 4 different storage Engine Table
  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;

  - Early learning redis: interview to ask more;

  - inserting data validation engine role:
    INSERT INTO T1 values (. 1);
    INSERT INTO T2 values (2);
    INSERT INTO T3 values (. 3);
    INSERT INTO T4 values (. 4);  (Close mysql server, memory data will disappear)

 

2. Create the full syntax table
# constraints: dispensable
# Width: limit certain types of data storage size
create table table name (
  field name field type 1 (width) constraints,
  Field Name Field Type 2 (width) constraints
);

# Initial constraints: Not null
Create Table Teacher (
  id int Not null, # constraint inserted record id can not be empty
  name VARCHAR (16),
  Age int
);

insert into teacher values(null, 'tank', 17);
insert into teacher values(1, 'tank', 17);

Note:
1. Create a table of field names can not be repeated;
Create Table Test (
  ID int,
  ID int
);

2. At the end of the last field is not applied, No.
Create Table Test (
  ID int,
  Age int,
);

3. The field name field type must have a width
Create Table Test (
  ID int,
  name char  (default character 1)
);
INSERT INTO Test values (1, 'Tank');

alter table test modify name char(4);
insert into test values(2, 'sean');

 

3. Field Type

1) determining the structure of Table
2) field and field type

 

 

- 整型:
- tinyint: 默认范围 -128, 127
create table t5(
  id tinyint,
  name varchar(16)
);

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: default range is (-2147483648, 2147483647)

Application scenarios: id number, age ...

create table t6(
  id int
);

# int 默认宽度11---> 默认展示宽度
insert into t6 values(-2147483649);
insert into t6 values(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代表是小数的位数
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);

 

- 字符类型
- 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
- 插入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);
(timestamp时间戳:会记录创建的时间)
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;

(timestamp时间戳:会变为修改的时间)
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,生蚝');


4.约束条件

- 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 将某个字段设置为唯一的值(单单unique,多个字段null不算重)

# 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:(必须和unique或primary key一起用。会自动排序)
# 将id设置为自增
create table user4(
id int primary key auto_increment,
name varchar(4)
);

# 自增默认从1开始
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);


- 删除记录
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; (如果再添加项,如下还继续id序号,因为能回滚)

- truncate:
# 清空user8表中的所以记录,并且id重置为0
truncate table user8;(如果再添加项,id序号重新开始)

 

5.设置严格模式

# 查看数据库配置中变量名包含mode的配置参数:
show variables like "%mode%";

# 修改安全模式:
set session;  # 局部有效,只在你当前操作的窗口有效
set global session;  # 全局有效,永久有效

# 修改完之后退出当前客户端重新登录即可
set global sql_mode = 'STRICT_TRANS_TABLES';

Guess you like

Origin www.cnblogs.com/ludingchao/p/12024514.html