跳到主要内容

创建表

2024年06月12日
柏拉文
越努力,越幸运

一、认识


二、表语法


2.1 create table [tableName]

create table [tableName] (
column1 datatype,
column2 datatype,
...
);

2.2 create table if not exists [tableName]

create table if not exists [tableName](
column1 datatype,
column2 datatype,
...
)

2.3 create table [tableName] engine=innodb default charset=utf8

create table [tableName] (
column1 datatype,
column2 datatype,
...
)engine=innodb default charset=utf8;

2.4 create table [tableName] auto_increment=100

create table [tableName]()auto_increment=100 通过 auto_increment 来指定自增开始值。

create table `user` (
id int not null auto_increment,
name varchar(255),
age int,
birthdate date,
)auto_increment=100;

id 列从 100 开始自增。

三、列语法


3.1 基础

create table `user` (
id int,
name varchar(255),
age int,
birthdate date
)engine=innodb default charset=utf8;

3.2 非空

通过 not null 将该列设置为不允许为空

create table `user` (
id int not null,
name varchar(255) not null,
age int,
birthdate date
)engine=innodb default charset=utf8;

3.3 主键

通过 primary key 设置该列为主键。

方式一: column dataType primary key

create table `user` (
id int not null primary key,
name varchar(255) not null primary key,
age int,
birthdate date
)engine=innodb default charset=utf8;

方式二: primary key ([tableName1], [tableName2], ……)

create table `user` (
id int not null,
name varchar(255) not null,
age int,
birthdate date,
primary key (`id`, `name`)
)engine=innodb default charset=utf8;

3.4 默认值

默认值为数值

create table `user` (
id int default 1,
name varchar(255),
age int,
birthdate date,
)engine=innodb default charset=utf8;

默认值为字符串

create table `user` (
id int,
name varchar(255) default "嘻嘻哈哈",
age int,
birthdate date,
)engine=innodb default charset=utf8;

默认值为指定时间

create table `user` (
id int,
name varchar(255),
age int,
birthdate date default "2024-05-20",
)engine=innodb default charset=utf8;

默认值为当前时间戳

create table `user` (
id int,
name varchar(255),
age int,
birthdate date,
create_time timestamp not null default current_timestamp,
update_time timestamp not null default current_timestamp on update current_timestamp
)engine=innodb default charset=utf8;
  • default current_timestamp: 表示当插入数据的时候,该字段默认值为当前时间戳

  • default current_timestamp on update current_timestamp: 表示每次更新这条数据的时候,该字段都会更新成当前时间戳

3.5 自增序列

通过 AUTO_INCREMENTauto_increment 来指定表中某一列的自增性。

create table `user` (
id int not null auto_increment,
name varchar(255),
age int,
birthdate date,
)engine=innodb default charset=utf8;

如果需要设置自增初始值,有如下两种方法:

  • alter table [tableName] auto_increment=100

  • create table [tableName]()auto_increment=100

    create table `user` (
    id int not null auto_increment,
    name varchar(255),
    age int,
    birthdate date,
    )engine=innodb default charset=utf8; auto_increment=100;

如果需要获取刚刚插入的行的自增值, 可以通过 last_insert_id()

四、表列用法


4.1 用户表

create table user(
user_id int not null auto_increment primary key comment "主键 id",
user_name varchar(255) default "柏拉文" comment "用户名",
user_password varchar(128) default "123456" comment "用户密码",
user_age int default 18 comment "用户年龄",
user_email varchar(128) default "" comment "用户邮箱",
user_sex int default 1 comment "用户性别",
user_roles varchar(128) default "" comment "用户角色,多个角色用逗号隔开",
user_birthday date default "2024-01-01 00:00:00" comment "用户生日",
user_create_time timestamp not null default current_timestamp comment "创建时间",
user_is_deleted int(2) default 0 comment "用户删除状态, 0 表示未删除, 1 表示已删除",
user_update_time timestamp not null default current_timestamp on update current_timestamp comment "更新时间"
)engine=innodb default charset=utf8 comment="用户表";

4.2 商品表

create table `good`(
good_id int not null auto_increment,
good_name varchar(255) not null comment "名称",
good_desc varchar(255) not null comment "描述",
good_price int not null comment "价格",
good_create_time timestamp not null default current_timestamp comment "创建时间",
good_update_time timestamp not null default current_timestamp on update current_timestamp comment "更新时间"
primary key(good_id)
)engine=innodb default charset=utf8 comment="商品表";

4.3 订单表

4.4 用户行为表

用户行为表: 包括 点赞(like收藏(favorite

create table `user_actions`(
`user_actions_id` int not null auto_increment primary key comment "用户行为主键 id",
`user_actions_user_id` int not null comment "用户主键 id",
`user_actions_target_id` int not null comment "用户操作对象主键 id",
`user_actions_action_type` enum('like','favorite') not null comment "用户操作对象类型,点赞、收藏",
`user_actions_create_time` timestamp not null default current_timestamp comment "创建时间",
`user_actions_update_time` timestamp not null default current_timestamp on update current_timestamp comment "更新时间",

unique key `unique_user_action` (`user_actions_user_id`, `user_actions_target_id`, `user_actions_action_type`) comment "用户+用户操作对象+用户操作对象类型的组合必须是唯一的,不能重复,并使用 unique_user_action 约束名字,有助于理解和维护代码",
index `user_actions_action_type_index` (`user_actions_action_type`) use btree comment "用户操作对象类型索引",
index `user_actions_user_actions_id_target_id_index` (`user_actions_user_id`, `user_actions_target_id`) use btree comment "用户主键和用户操作对象主键联合索引,因为用户和用户操作对象组合查询十分频繁"
)engine=innodb default charset=utf8 comment="用户行为表";

4.5 国际化语言表

create table `language`(
`language_id` int(10) unsigned not null auto_increment primary key comment "主键 id",
`language_lang` varchar(128) not null comment "语言标识, cn-cn en-us en-th",
`language_desc` varchar(128) default "" comment "语言说明",
`language_is_deleted` int(2) not null default 0 comment "语言删除状态, 0 表示已删除 1 表示未删除",
`language_creator_id` int(10) comment "创建者 id",
`language_create_time` timestamp not null default current_timestamp comment "创建时间",
`language_update_time` timestamp not null default current_timestamp on update current_timestamp comment "更新时间",
index `language_lang_index` (`language_lang`) using btree comment "语言 lang 索引"
)engine=innodb default charset=utf8 comment="语言表"

4.6 国际化单词表

create table `word`(
`word_id` int(10) unsigned not null auto_increment primary key comment "单词主键 id",
`word_key` varchar(1024) not null comment "单词唯一标识",
`word_desc` varchar(128) default "" comment "单词标识说明",
`word_value` varchar(1024) not null comment "单词翻译文案",
`word_status` int(2) not null comment "单词状态 0=stable 1=非stable",
`word_is_deleted` int(2) not null comment "单词删除状态, 0=未删除 1=已删除",
`word_creator_id` int(10) comment "单词创建者 id",
`word_create_time` timestamp not null default current_timestamp comment "创建时间",
`word_update_time` timestamp not null default current_timestamp on update current_timestamp comment "更新时间",
index `word_key_index` (`word_key`) using btree comment "单词 key 索引"
)engine=innodb default charset=utf8 comment="单词表"

4.7 国际化翻译表

create table `translate`(
`translate_id` int(10) unsigned not null auto_increment comment "翻译主键 id",
`word_id` int(10) not null comment "单词主键 id",
`translate_lang` varchar(128) default null comment "翻译语言",
`translate_value` varchar(1028) default null comment "翻译结果",
`translate_is_deleted` int(2) not null default 0 comment "翻译删除状态,0=未删除,1=已删除",
`translate_creator_id` int(10) comment "创建者 id",
`translate_create_time` timestamp not null default current_timestamp comment "创建时间",
`translate_update_time` timestamp not null default current_timestamp on update current_timestamp comment "更新时间",
index `translate_word_id_translate_lang_index` (`word_id`, `translate_lang`) using btree comment "单词 id 和翻译语言的组合索引"
)engine=innodb default charset=utf8 comment="翻译表"

4.8 国际化模块表

create table `module`(
`module_id` int(10) unsigned not null auto_increment primary key comment "模块主键 id",
`module_key` varchar(128) not null comment "模块名称",
`module_desc` varchar(128) default null comment "模块说明",
`module_status` int(2) not null comment "状态, 0=stable, 1=非stable",
`module_is_deleted` int(2) not null default 0 comment "翻译删除状态,0=未删除,1=已删除",
`module_creator_id` int(10) comment "创建者 id",
`source_module_id` int(10) default null comment "源 module_id, 用于合并场景",
`module_create_time` timestamp not null default current_timestamp comment "创建时间",
`module_update_time` timestamp not null default current_timestamp on update current_timestamp comment "更新时间",
)engine=innodb default charset=utf8 comment="模块表"

4.9 国际化项目表

create table `project`(
`project_id` int(10) unsigned not null auto_increment comment "项目主键 id",
`project_name` varchar(1024) not null comment "项目名称",
`project_module_ids` varchar(4096) not null comment "模块列表,多个模块通过逗号分割",
`project_status` int(2) not null comment "状态, 0=stable, 1=非stable",
`project_source_project_id` int(10) default 0 comment "源 project_id, 用于合并场景, 默认为 0",
`project_source_module_ids` varchar(4096) not null comment "源 project_module_ids, 方便做 Diff"
`project_creator_id` int(10) comment "创建者 id",
`project_create_time` timestamp not null default current_timestamp comment "创建时间",
`project_update_time` timestamp not null default current_timestamp on update current_timestamp comment "更新时间",
)engine=innodb default charset=utf8 comment="项目表"

4.10 国际化模块单词表

create table `module_word`(
`module_word_id` int(10) unsigned not null auto_increment primary key comment "模块单词主键 id",
`module_word_module_id` int(10) not null comment "模块主键 id",
`module_word_word_id` int(10) default null comment "单词主键 id,当 type = word 时存在",
`module_word_type` varchar(128) not null comment "类型,值为 word 或 node",
`module_word_name` varchar(128) default null comment "模块单词名称, 当 type = node 时存在",
`module_word_parent_id` int(10) default null comment "模块单词父级目录,值为 module_word_id, 可以表示嵌套关系",
`module_word_is_deleted` int(2) not null default 0 comment "模块单词删除状态,0=未删除,1=已删除",
`module_word_creator_id` int(10) comment "创建者 id",
)engine=innodb default charset=utf8 comment="模块单词表"