創建表
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)