创建表
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;