I、表操作
一、创建表
基本语法如下:
create table 表名(
列名 类型 是否可以为空,
列名 类型 是否可以为空
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
下面这两个变量是:
ENGINE :存储引擎
charset:字符集
1、是否可空,null表示空,非字符串
not null - 不可空
null - 可空
2、默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
create table tb1(
nid int not null defalut 2,
num int not nul
)
3、自增,如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列
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)对于自增可以设置步长和起始值
show session variables like 'auto_inc%';
set session auto_increment_increment=2;
set session auto_increment_offset=10;
shwo global variables like 'auto_inc%';
set global auto_increment_increment=2;
set global auto_increment_offset=10;
4、主键,一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。
create table tb1(
nid int not null auto_increment prima