3、mysql數(shù)據(jù)庫表操作

[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)將自增(表中只能有一個自增列)

注意:

  1. 對于自增列,必須是索引(含主鍵)
  2. 對于自增可以設(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;
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 手動不易,轉(zhuǎn)發(fā)請注明出處 --Trance 數(shù)據(jù)庫系統(tǒng)命令: (1).查看存儲過程狀態(tài):show pro...
    Trance_b54c閱讀 1,827評論 0 8
  • Ubuntu下安裝mysqlapt updatesudo apt-get install mysql-server...
    恬恬i阿萌妹O_o閱讀 351評論 0 0
  • ORA-00001: 違反唯一約束條件 (.) 錯誤說明:當(dāng)在唯一索引所對應(yīng)的列上鍵入重復(fù)值時,會觸發(fā)此異常。 O...
    我想起個好名字閱讀 5,943評論 0 9
  • MYSQL 基礎(chǔ)知識 1 MySQL數(shù)據(jù)庫概要 2 簡單MySQL環(huán)境 3 數(shù)據(jù)的存儲和獲取 4 MySQL基本操...
    Kingtester閱讀 8,056評論 5 115
  • 產(chǎn)品介紹:無糖易減重原理:是碳水化合物的阻擋者,迫使身體消耗脂肪,從而達到纖體減重的目的。為較大限度的預(yù)防生化酶的...
    心碎的寶貝閱讀 16,927評論 0 1

友情鏈接更多精彩內(nèi)容