小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

mysql中的表操作

 Coder編程 2022-04-20

------------恢復(fù)內(nèi)容開始------------

創(chuàng)建數(shù)據(jù)庫

  create database 數(shù)據(jù)庫名

切換數(shù)據(jù)庫

  use 數(shù)據(jù)庫名

建表:

  create table 表名 (

    字段名1,類型,約束

    字段名2,類型,約束

    ...

  )

約束:

 1.主鍵約束

  1)直接在建表時字段類型后加 primary key

  2)在表最后加 constraint 約束名 primary key(字段名)

  3)表外修改 alter table 表名 add constraint 約束名 primary key(字段名)

 2.檢查約束

  1)直接在建表類型后加 check(約束條件)

  2)在表最后加 constraint 約束名 check(約束條件)

  3)表外修改 alter table 表名 add constraint 約束名 check(約束條件)

  注:mysql不支持檢查約束,但是寫上檢查約束不會報錯

 3.非空約束

  1)直接在創(chuàng)建表的類型后加 not null

  2) 在表最后加入 constraint 約束名 check(字段名 is not null)

  3)在表外修改 alter table 表名 modify 字段名 字段類型 not null

 4.唯一約束

  1)直接在創(chuàng)建表的類型后加 unique

  2) 在表的最后加入 constraint 約束名 unqiue(字段名)

  3) 在表外修改 alter table 表名 add constraint 約束名 unique(字段名)

 5.外鍵約束

  1)直接在創(chuàng)建表的類型后加 references 父表名(父表主鍵名)

  2)在表的最后加入 constraint 約束名 foreign key(字段名) references 父表名(父表主鍵名)

  3)在表外修改 alter table 表名 add constraint 約束名 foreign key(字段名) references 父表名(父表主鍵名)on delete set null on updata cascade

 6.默認(rèn)約束

  1)直接在創(chuàng)建表的類型后加 default 默認(rèn)值

  2)在表外修改 alter table 表名 add constraint 約束名 

刪除約束

  alter table 表名 drop constraint 約束名

 

表的修改

  1)添加字段

    alter table 表名 add 字段名 字段類型 注:在表中已經(jīng)有值時,不能加非空約束

  2)刪除字段

    alter table 表名 drop 字段名

  3)修改字段類型

    alter table 表名 modify 字段名 新字段類型

  4)修改字段名

    alter table 表名 change 字段名 新字段名 字段類型

  5)修改表名

    alter table 表名 rename as 新表名

  6)刪除表

    drop table 表名

 

 查看當(dāng)前數(shù)據(jù)庫中所有表

  show tables

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多