[toc]
1、創(chuàng)建表
create table 表名(
列名 類型 是否可以為空,
列名 類型 是否可以為空
)ENGINE=InnoDB DEFAULT CHARSET=utf8
- InnoDB 支持事務(wù)回滾,原子性操作
- myisam 不支持事務(wù),但速度快
是否可以為空
- 是否可空,null表示空,非字符串
- not null - 不可空
- null - 可空
默認(rèn)值
默認(rèn)值,創(chuàng)建列時可以指定默認(rèn)值,當(dāng)插入數(shù)據(jù)時如果未主動設(shè)置,則自動添加默認(rèn)值
create table 表名(
nid int not null defalut 2,
num int not null
)
2、設(shè)置自增和主鍵
自增
自增,如果為某列設(shè)置自增列,插入數(shù)據(jù)時無需設(shè)置此列,默認(rèn)將自增(表中只能有一個自增列)
注意:
- 對于自增列,必須是索引(含主鍵)
- 對于自增可以設(shè)置步長和起始值
create table 表名(
nid int not null auto_increment primary key,
num int null
)
或
create table 表名(
nid int not null auto_increment,
num int null,
index(nid)
)
auto_increment 表示:自增
primary key 表示: 主鍵約束(不能重復(fù)且不能為空),有加速查找的效果
主鍵
主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。
create table 表名(
nid int not null auto_increment primary key,
num int null
)
或
create table 表名(
nid int not null,
num int not null,
primary key(nid,num)
)
唯一索引
唯一索引(此列值不能重復(fù))
==unique 唯一索引名稱 (列名)==
create table t1(
id int ....,
num int,
xx int,
unique uq1 (num),
constraint ....
)
聯(lián)合唯一索引(此兩列排列組合值不能重復(fù))
==unique 唯一索引名稱 (列名,列名)==
create table t1(
id int ....,
num int,
xx int,
unique uq1 (num,xx),
constraint ....
)
外鍵
外鍵,一個特殊的索引,只能是指定內(nèi)容
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)
)
3、查看表設(shè)置
- 查看表設(shè)置
desc 表名; - 以sql語句的方式查看表設(shè)置
show create table 表名; - 以豎向的方式查看
show create table 表名 \G;
4、設(shè)置自增起始值
- 設(shè)置自增起始值
alter table 表名 AUTO_INCREMENT=20;
5、設(shè)置步長
MySQL: 設(shè)置自增步長
基于會話級別:
- 查看全局變量
show session variables like 'auto_inc%'; - 設(shè)置會話步長
set session auto_increment_increment=2; - 設(shè)置起始值(在表里可以設(shè)置起始值,不必在這里設(shè)置)
set session auto_increment_offset=10;
基于全局級別(基本上不用):
- 查看全局變量
show global variables like 'auto_inc%'; - 設(shè)置會話步長
set global auto_increment_increment=2; - 設(shè)置起始值(在表里可以設(shè)置起始值,不必在這里設(shè)置)
set global auto_increment_offset=10;
(參考)SqlServer:自增步長:
基礎(chǔ)表級別:
CREATE TABLE `t5` (
`nid` int(11) NOT NULL AUTO_INCREMENT,
`pid` int(11) NOT NULL,
`num` int(11) DEFAULT NULL,
PRIMARY KEY (`nid`,`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=4, 步長=2 DEFAULT CHARSET=utf8
CREATE TABLE `t6` (
`nid` int(11) NOT NULL AUTO_INCREMENT,
`pid` int(11) NOT NULL,
`num` int(11) DEFAULT NULL,
PRIMARY KEY (`nid`,`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=4, 步長=20 DEFAULT CHARSET=utf8
刪除及清空表
- 刪除表:
drop table 表名 - 清空表數(shù)據(jù):
delete from 表名 - 清空表數(shù)據(jù)和自增起始值:
truncate table 表名
修改表
- 添加列:
alter table 表名 add 列名 類型 - 刪除列:
alter table 表名 drop column 列名 - 修改列類型:
alter table 表名 modify column 列名 類型; - 修改列名,類型:
alter table 表名 change 原列名 新列名 類型; - 添加主鍵:
alter table 表名 add primary key(列名); - 刪除主鍵:
alter table 表名 drop primary key;</br>
alter table 表名 modify 列名 int, drop primary key; - 添加外鍵:
alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵字段) references 主表(主鍵字段); - 刪除外鍵:
alter table 表名 drop foreign key 外鍵名稱 - 修改默認(rèn)值:
ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000; - 刪除默認(rèn)值:
ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;