資料庫修改表結構

2021-10-04 01:38:01 字數 1188 閱讀 6995

--修改資料表

alter table 表名

sql**的書寫不考慮順序,但是批量執行**需要要考慮好先執行哪些,後執行哪些

在修改資料表結構時,必須要明確:修改的字段中是否存在資料,例如:如果需要更改乙個欄位的約束為非空約束,那麼首先要保證該字段中已有的資料沒有null值。

因此在做程式之前資料庫分析,設計是至關重要

alter table 表名

alter column 欄位名 目標資料型別

alter table 表名

add 欄位名 資料型別 [欄位的特徵]

sql中刪除語法使用drop關鍵字,使用drop關鍵字一定要謹慎,drop掉的資料無法恢復

alter table 表名

drop column 欄位名

alter table 表名

add constraint 主鍵名(pk_id) primary key (欄位名[id])

alter table 表名

add constraint 約束名(uq_tel) unique (欄位名[tel])

alter table 表名

add constraint 約束名(df_address) default (預設值['位址不詳']) for 欄位名[address]

alter table 表名

add constraint 約束名(ck_***) check (檢查的表示式[*** in ('男','女')])

--add constraint ck_*** check (***='男' or ***='女')

alter table 表名

alter column 欄位名(id) 字段資料型別(int) not null

注意:新增外來鍵時,必須先設定引用表中的引用字段為主鍵

alter table 表名

add constraint 外鍵名(fk_subid) foreign key (欄位名[subid]) references 引用表名[subject](引用欄位名[subid])

資料庫修改表結構SQL

資料庫修改欄位名 alter table t pda node template item rename column sequence number to sequence number1 新增字段 alter table t pda node template item add sequence...

Django修改資料庫表結構

django在資料庫中建立資料庫表的方法是 1,執行 python manage.py makemigrations 這一步在專案中建立migrations目錄,在目錄中建立了0001 initial.py檔案,這個檔案中包含了所有在資料庫表中建立表的資訊。2,執行 python manage.py...

MySQL資料庫表結構修改總結

建立表dept和stu mysql create table department dept id int primary key auto increment,dept name char 20 not null default mysql create table stu stu id int ...