Update 資料表 set 要改的 where 指定的欄位
1.在學生資料表指定s001欄位更改
update 學生 set 學分=4,名稱='計算機概論' where 學號='S001'
2.從員工提取資料來更新學生資料表
update 學生 set 姓名=(select 姓名 from 員工 where 編號='s001') where 學號='s003'
3.
update
新增
1.新增資料表裡的內容
insert into 員工(編號,姓名,性別)
values ('s001','王小苗','女')
2.複製整張資料表
select * into 員工2 from 員工
3.複製特定欄位到其他資料表 (複製通訊錄的到員工)
insert into 員工 select 姓名,住址,電話 from 通訊錄
merge 結尾要分號;
1.把新客戶的資料丟進客戶,有新的要insert 有舊的要update
merge 客戶 as c
using 新客戶 as nc
on c.客戶編號=nc.客戶編號
when matched then
update set 姓名=nc.姓名,電話=nc.電話
when not matched then
insert (客戶編號,姓名,電話) values (nc.客戶編號,nc.姓名,nc.電話)
;
create table 商品( 商品編號 char(5) not null primary key, 定價 money not null check(定價>=0), 庫存數量 int not null constraint check_庫存量不小於1 check(庫存數量>=0), )
create table 會員資料( 會員編號 char(8) primary key, 姓名 nvarchar(20) not null, 身分證字號 char(10) unique, 帳號 varchar(20) not null unique, 密碼 varchar(20) not null )
foreign key(學號) references 學生(學號) on delete no action on update no action, --不準刪除 不準修改
foreign key(課程編號) references 課程(課程編號) on delete cascade on update no cascade, --一起修改一起刪除
foreign key(教授編號) references 教授(教授編號) on delete cascade on update no action --一起刪除,不準修改
--增加一個foreign key,參考員工的
alter table 教授
add foreign key(身份證字號) references 員工(身份證字號)
--增加一個欄未
alter table 班級2
add 備註 varchar(8)
go
--修改欄未
alter table 班級2
alter column 教室 varchar(8)
----------Constraints (條件約束)---------- --primary key constraints create table 訂單明細( 訂單編號 char(10) not null, 商品編號 char(5) not null, 價格 money not null, 數量 int not null default 1, primary key(訂單編號,商品編號) --資料表層級的Constraints ) create table 學生( 學號 char(4), 姓名 nvarchar(16) not null, 性別 bit not null, 電話 varchar(16), 生日 datetime, primary key(學號) ) --check constraints create table 商品( 商品編號 char(5) not null primary key, 定價 money not null check(定價>=0), 庫存數量 int not null constraint check_庫存量不小於1 check(庫存數量>=0), ) --unique constraints create table 會員資料( 會員編號 char(8) primary key, 姓名 nvarchar(20) not null, 身分證字號 char(10) unique, 帳號 varchar(20) not null unique, 密碼 varchar(20) not null ) --foreign key constraints create table 班級( 學號 char(4), 課程編號 char(5), 教授編號 char(4), 上課時間 datetime, 教室 varchar(8), primary key(學號,課程編號,教授編號), foreign key(學號) references 學生(學號) on delete no action on update no action, foreign key(課程編號) references 課程(課程編號) on delete cascade on update cascade, foreign key(教授編號) references 教授(教授編號) on delete cascade on update no action )
--alter table --增加某資料表的foreign key alter table 教授 add foreign key(身分證字號) references 員工(身分證字號) --增加某資料表的foreign key create table 訂單( 訂單編號 char(10) not null primary key, 訂單日期 datetime ) alter table 訂單明細 add foreign key(訂單編號) references 訂單(訂單編號) go alter table 訂單明細 add foreign key(商品編號) references 商品(商品編號) go --增加一個欄位 alter table 課程 add 備註 nvarchar(max) not null go ------------------------ --修改欄位 alter table 課程 alter column 備註 nvarchar(50) -------------------------------------------- --增加訂單資料表的foreign key alter table 訂單 add 會員編號 char(8) not null go alter table 訂單 add foreign key(會員編號) references 會員資料(會員編號) go ----------------------------------------- --修改check constraint --先刪再定義 alter table 商品 drop constraint CK__商品__定價__31EC6D26 go alter table 商品 add constraint check_商品_定價 check(定價>=0) -------------------------------- --刪掉某個欄位 alter table 課程 drop column 備註 --刪掉資料表 drop table 訂單
留言列表