一、创建表结构

 

create table order_ipad(
id bigint(20) unsigned not null auto_increment comment '自增主键',
uuid bigint(20) unsigned not null default 0 comment '用户id',
order_id varchar(64) not null default '' comment '订单id',
name varchar(64) not null default '' comment '商品名称',
type tinyint(4) unsigned not null default 0 comment '状态 1:增加 2:减少',
price int(11) unsigned not null default 0 comment '',
title varchar(64) not null default '' comment '标题',
status tinyint(4) unsigned not null default 0 comment '状态 1:进行中 2:已完成 3:已退款 4:已失败 99:异常',
source tinyint(4) unsigned not null default 0 comment '来源 1:任务系统 2:金币系统 3:活动系统',
end_time bigint(20) unsigned not null default 0 comment '过期时间',
record_time bigint(20) unsigned not null default 0 comment '记录发生时间',
create_time timestamp not null default current_timestamp comment '创建时间',
refund_time bigint(20) unsigned not null default 0 comment '退款时间',
update_time timestamp not null default current_timestamp on update current_timestamp comment '更新时间',
primary key (id),
unique key uk_orderid (order_id) ,
key idx_uuid_name (uuid,name(10)),
key idx_uuid_title (uuid,title(10))
)engine=innodb auto_increment=1 default charset=utf8mb4 comment='用户小游戏金币收支记录表';  

############################################################################################
1、一张表必须有主键。一般都是与业务无关的unsigned自增主键。
2、字段一般都要有not null约束;
除主键外,一般还都有default约束,通常整型的默认值是0,字符串的默认值为
''
每个字段和表都有comment说明,用以说明每个字段的含义和表的用途。
3、通常需要在建表的时候就能预估下常用的查询,以便创建合适的索引来加快查询、删除、更新速度。
4、表的存储引擎一般都是innodb,表的默认字符集一般都是utf8mb4,自增列初始值。

5、尽量不要用外键约束!!!

 

 此外还需注意:

1、如果字段为int 或者varchar类型,那么要设置字段类型为 not null 并且设置default

2、如果字段为text,或者longtext,则既不需要设置not null,也不需要手动设置default 的值

3、关于text字段不能有默认值的问题,这个只针对于手动增加的default属性,如果创建字段不设置default的话,mysql会默认加上一个默认值Null

 

 

 

 

二、修改表结构

 

1、修改表的字符集和字符序

alter table table_name convert to character set utf8mb4 collate utf8mb4_general_ci;




 

创建相同结构的表:

((none)) > use google
Database changed
Fri Sep  4 15:10:25 2020(google) > create table user like apple.user;

 

 

建表反例:

 > show create table tasks\G
*************************** 1. row ***************************
       Table: tasks
Create Table: CREATE TABLE `tasks` (
  `id` varchar(255) NOT NULL,
  `created_date` varchar(255) NOT NULL,
  `datasource` varchar(255) NOT NULL,
  `payload` longblob NOT NULL,
  `pay` longblob NOT NULL,
  `active` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `idx_date` (`active`,`created_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.08 sec)


# 第一:主键是varchar类型,不及自增主键bigint;

# 第二:联合索引顺序不对,且active字段是地选择性,因此,只需要对created_date字段创建索引即可;

# 第三:字段没有注释,不知道这个表是干嘛的;

# 字段不给默认值,不过这个问题不算大;

 

 

 

 

 

#####################################