ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC=1,5] >[success] # **语法** * * * * * >[info] ##### **基本语法** ``` create table 表名( 列名 类型 是否可以为空, 列名 类型 是否可以为空 )ENGINE=InnoDB DEFAULT CHARSET=utf8 ``` ![](https://box.kancloud.cn/8576711008f5f3934cad6a3f5e2e9849_628x178.png) <br> * * * * * >[info] ##### **设置字段是否为空-null** ``` 默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值 create table tb1( nid int null -可空 , num int not null -不可为空 ) ``` <br> * * * * * >[info] ##### **设置默认值-defalut** ``` 默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值 create table tb1( nid int not null defalut 2, num int not null ) ``` * * * * * <br> >[info] ##### **设置自增-auto_increment** ``` 自增,如果为某列设置自增列,插入数据时无需设置此列 默认将自增(表中只能有一个自增列) create table tb1( nid int not null auto_increment primary key, num int null ) 或 create table tb1( nid int not null auto_increment, num int null, index(nid) ) ``` **注意**: ``` 1、对于自增列,必须是索引(含主键)。 2、对于自增可以设置步长和起始值 3、自增时要用int 类型 ``` 图片对应第二条`CREATE TABLE 表名 ( ...) AUTO_INCREMENT=10000` ![](https://box.kancloud.cn/972d51c830b1fcecfe86dfbef98c6e79_662x69.png) <br> * * * * * >[info] ##### **主键 -primary key** ``` 主键,一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。 create table tb1( nid int not null auto_increment primary key, num int null ) 或 create table tb1( nid int not null, num int not null, primary key(nid,num) ) ```