创建表
create table 表名(
列名 类型 是否可以为空,
列名 类型 是否可以为空
)ENGINE=InnoDB DEFAULT CHARSET=utf8
InnoDB支持事物,原子操作,回滚
是否可以为空
是否可空,null表示空,非字符串
not null - 不可空
null - 可空
默认值
默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
create table tb1(
nid int not null defalut 2,
num int not null
)
自增
自增,如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)
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;
自增列
主键
主键,一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。
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)
)
唯一键
可以为null,一张表可以有多个唯一列
约束
索引,加速查找
多列组成一个主键
create table student(
name varchar(10) not null,
age int,
gender int,
primary key(name,num)
)
约束
name num age
a 88 9
a 99 9
a 88 0
主键:
不能为null
不能重复
一张表只有一个主键(可以多列组成主键)
一般用法
nid int auto_increment primary key,
create table tb5(
nid int not null auto_increment primary key,
name varchar(16),
age int default 19
)engine=innodb default charset=utf8;
外键
外键,一个特殊的索引,只能是指定内容
creat table color(
nid int not null primary key,
name char(16) not null
)
create table fruit(
nid int not null primary key,
smt char(32) null ,
color_id int not null,
constraint fk_cc foreign key (color_id) references color(nid)
)
MariaDB [test]> create table tb3( -> nid int not null default 1, -> num int null)engine=innodb default charset=utf8; Query OK, 0 rows affected (0.02 sec) MariaDB [test]> desc tb3; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | nid | int(11) | NO | | 1 | | | num | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec) MariaDB [test]> insert into tb3(nid,num) values(11,22); Query OK, 1 row affected (0.01 sec) MariaDB [test]> select * from tb3; +-----+------+ | nid | num | +-----+------+ | 11 | 22 | +-----+------+ 1 row in set (0.00 sec) MariaDB [test]> insert into tb3(num) values(666); Query OK, 1 row affected (0.00 sec) MariaDB [test]> select * from tb3; +-----+------+ | nid | num | +-----+------+ | 11 | 22 | | 1 | 666 | +-----+------+ 2 rows in set (0.00 sec) MariaDB [test]> insert into tb3(num) values(999); Query OK, 1 row affected (0.00 sec) MariaDB [test]> select * from tb3; +-----+------+ | nid | num | +-----+------+ | 11 | 22 | | 1 | 666 | | 1 | 999 | +-----+------+ 3 rows in set (0.00 sec) MariaDB [test]> create table tb4( -> nid int not null auto_increment primary key, -> num int)engine=innodb default charset=utf8; Query OK, 0 rows affected (0.02 sec) MariaDB [test]> insert into tb4(num) values(999); Query OK, 1 row affected (0.01 sec) MariaDB [test]> insert into tb4(num) values(999); Query OK, 1 row affected (0.00 sec) MariaDB [test]> insert into tb4(num) values(999); Query OK, 1 row affected (0.00 sec) MariaDB [test]> insert into tb4(num) values(999); Query OK, 1 row affected (0.00 sec) MariaDB [test]> insert into tb4(num) values(999); Query OK, 1 row affected (0.00 sec) MariaDB [test]> insert into tb4(num) values(999); Query OK, 1 row affected (0.00 sec) MariaDB [test]> select * from tb4; +-----+------+ | nid | num | +-----+------+ | 1 | 999 | | 2 | 999 | | 3 | 999 | | 4 | 999 | | 5 | 999 | | 6 | 999 | +-----+------+ 6 rows in set (0.00 sec) MariaDB [test]> insert into tb4(num) values(999); Query OK, 1 row affected (0.00 sec) MariaDB [test]> insert into tb4(num) values(999); Query OK, 1 row affected (0.00 sec) MariaDB [test]> insert into tb4(num) values(999); Query OK, 1 row affected (0.00 sec) MariaDB [test]> select * from tb4; +-----+------+ | nid | num | +-----+------+ | 1 | 999 | | 2 | 999 | | 3 | 999 | | 4 | 999 | | 5 | 999 | | 6 | 999 | | 7 | 999 | | 8 | 999 | | 9 | 999 | +-----+------+ 9 rows in set (0.00 sec) MariaDB [test]> create table tb5( -> nid int not null auto_increment primary key, -> name varchar(16), -> age int default 19 -> )engine=innodb default charset=utf8; Query OK, 0 rows affected (0.03 sec)