mysql中的表操作

2022-08-02 12:51:13 字數 1435 閱讀 1878

------------恢復內容開始------------

create database 資料庫名

use 資料庫名

create table 表名 (

欄位名1,型別,約束

欄位名2,型別,約束

...)

1)直接在建表時字段型別後加 primary key

2)在表最後加 constraint 約束名 primary key(欄位名)

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

1)直接在建表型別後加 check(約束條件)

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

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

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

1)直接在建立表的型別後加 not null

2) 在表最後加入 constraint 約束名 check(欄位名 is not null)

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

1)直接在建立表的型別後加 unique

2) 在表的最後加入 constraint 約束名 unqiue(欄位名)

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

1)直接在建立表的型別後加 references 父表名(父表主鍵名)

2)在表的最後加入 constraint 約束名 foreign key(欄位名) references 父表名(父表主鍵名)

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

1)直接在建立表的型別後加 default 預設值

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

alter table 表名 drop constraint 約束名

1)新增字段

alter table 表名 add 欄位名 字段型別 注:在表中已經有值時,不能加非空約束

2)刪除字段

alter table 表名 drop 欄位名

3)修改字段型別

alter table 表名 modify 欄位名 新字段型別

4)修改欄位名

alter table 表名 change 欄位名 新欄位名 字段型別

5)修改表名

alter table 表名 rename as 新錶名

6)刪除表

drop table 表名

show tables

mysql 表的操作 mysql 表的操作

建立表 檢視表結構 修改表 刪除表 1.建立表 建立表之前選定資料庫 use testx create table table2 屬性名 資料型別 約束 屬性名 資料型別 約束 約束 primary key 該屬性 欄位設為此表主鍵 foreign key 該屬性 欄位為該表外來鍵,即另乙個表的主鍵...

MySQL中對於表的操作

建立表 create table table name field1 datatype,field2 datatype,field3 datatype character set 字符集 collate 校驗規則 engine 儲存引擎 不同的儲存引擎,建立表的檔案不一樣。例如儲存引擎是myisam...

mysql操作表 MySQL表的操作(一)

在建立表之前,首先要指明表在哪個資料庫中建立,也就是要指明命令所要操作的資料庫 用use語句選擇資料庫,一般格式 use 資料庫名 建立表的語法格式如下 例如選擇在linda資料庫中建立乙個use1表 use linda create table use1 id int,name varchar 2...