修改SQL SERVER資料庫表結構的SQL命令

2021-09-01 01:21:29 字數 1546 閱讀 2476

向表中增加乙個 varchar 列: 

alter table distributors add column address varchar(30);

從表中刪除乙個字段: 

alter table distributors drop column address restrict;

在乙個操作中修改兩個現有欄位的型別: 

alter table distributors

alter column address type varchar(80),

alter column name type varchar(100);

使用乙個 using 子句, 把乙個包含 unix 時間戳的 integer 字段轉化成 timestamp with time zone: 

alter table foo

alter column foo_timestamp type timestamp with time zone

using

timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';

對現存字段改名: 

alter table distributors rename column address to city;

更改現存表的名字:

alter table distributors rename to suppliers;

給乙個字段增加乙個非空約束: 

alter table distributors alter column street set not null;

從乙個欄位裡刪除乙個非空約束: 

alter table distributors alter column street drop not null;

給乙個表增加乙個檢查約束: 

alter table distributors add constraint zipchk check (char_length(zipcode) = 5);

刪除乙個表和它的所有子表的監查約束: 

alter table distributors drop constraint zipchk;

向表中增加乙個外來鍵約束: 

alter table distributors add constraint distfk foreign key (address) references addresses(address) match full;

給表增加乙個(多欄位)唯一約束: 

alter table distributors add constraint dist_id_zipcode_key unique (dist_id, zipcode);

給乙個表增加乙個自動命名的主鍵約束,要注意的是乙個表只能有乙個主鍵: 

alter table distributors add primary key (dist_id);

把錶移動到另外乙個表空間: 

alter table distributors set tablespace fasttablespace;

資料庫修改表中的資料(SqlServer)

dml update 語法 update 表名 set 欄位1 新的值,欄位2 新的值,where 記錄的匹配條件 說明 如果不寫where子句,預設是修改所有的行 準備資料 use worker go create table worker id int notnull primary key,n...

SQL Server資料庫 資料修改

use tt3 goinsert into tb2 學號,姓名,性別,出生日期,手機號碼,成績,備註 values 120101 王富貴 1 1994 8 13 13412132324 99 因為書寫不規範減1分 120102 王有才 1 1992 3 1 13411112222 90 null i...

SQL Server 修改資料庫

檢視資料庫資訊execute sp helpdb northwind刪除資料庫 包括其中所有資料檔案,在不使用本資料庫的狀態下才能執行。drop database northwind修改資料庫名alter database northwind modify name northwind 000增添資...