第八章 資料修改 3

2021-06-08 17:08:46 字數 4002 閱讀 8841

--8.3 更新資料

use tempdb;

goif object_id('dbo.orderdetails', 'u') is not null drop table dbo.orderdetails

if object_id('dbo.orders', 'u') is not null drop table dbo.orders

select * into dbo.orders from tsqlfundamentals2008.sales.orders;

select * into dbo.orderdetails from tsqlfundamentals2008.sales.orderdetails;

alter table dbo.orders add constraint pk_orders primary key(orderid)

alter table dbo.orderdetails add

constraint pk_orderdetails primary key(orderid, productid),

constraint fk_orderdetails_orders foreign key(orderid) references dbo.orders(orderid);

--8.3.1 update語句

update dbo.orderdetails

set discount = discount + 0.5

where productid = 51

update dbo.orderdetails

set discount += 0.5

where productid = 51

--在sql中所有的賦值表示式好像都是同時進行計算的

--8.3.2 基於聯接的update

update od

set discount += 0.5

from dbo.orderdetails as od

join dbo.orders as o

on od.orderid = o.orderid

where custid = 1;

update dbo.orderdetails

set discount = discount + 0.5

where exists

(select * from dbo.orders

where dbo.orders.orderid = dbo.orderdetails.orderid

and dbo.orders.custid = 1);

--在某些情況下,使用聯接比使用子查詢在效能上更具優勢.除了過濾作用,通過聯接

--還可以訪問其他表的屬性(列),並在set子句中使用這些屬性為列屬性。

update t1

set t1.col1 = t2.col1,

t1.col2 = t2.col2,

t1.col3 = t2.col3

from dbo.t1 join dbo.t2

on t2.keycol = t1.keycol

where t2.col4='abc'

--8.3.4 賦值update

--tsql支援特殊的update語法,可以在對錶中的資料進行更新的同時為變數賦值

--使用這種特殊的update語法是作為原子操作而進行的,因為它只須要訪問一次資料。

use tempdb;

if object_id('dbo.sequence','u') is not null drop table dbo.sequence;

create table dbo.sequence(val int not null);

insert into dbo.sequence values(10);

declare @nextval as int;

update sequence set @nextval = val = val+1;

select @nextval

--8.4 合併資料

--merge的語句能在一條語句中根據邏輯條件對資料進行不同的修改操作(insert,update和delete).

--用較少的**就可以表達需求,提高查詢效能,因為它可以更少地訪問查詢涉及表。

use tempdb;

if object_id('dbo.customers','u') is not null drop table dbo.customers;

gocreate table dbo.customers

(custid int not null,

companyname varchar(25) not null,

phone varchar(20) not null,

address varchar(50) not null,

constraint pk_customers primary key(custid)

);insert into dbo.customers(custid, companyname, phone, address)

values

(1, 'cust 1', '(111)111-1111', 'address 1'),

(2, 'cust 2', '(222)222-2222', 'address 2'),

(3, 'cust 3', '(333)333-3333', 'address 3'),

(4, 'cust 4', '(444)444-4444', 'address 4'),

(5, 'cust 5', '(555)555-5555', 'address 5');

if object_id('dbo.customersstage', 'u') is not null drop table dbo.customersstage;

gocreate table dbo.customersstage

(custid int not null,

companyname varchar(25) not null,

phone varchar(20) not null,

address varchar(50) not null,

constraint pk_customersstage primary key(custid)

);insert into dbo.customersstage(custid, companyname, phone, address)

values

(2, 'cust 1', '(111)111-1111', 'address 1'),

(3, 'cust 2', '(222)222-2222', 'address 2'),

(5, 'cust 3', '(333)333-3333', 'address 3'),

(6, 'cust 4', '(444)444-4444', 'address 4'),

(7, 'cust 5', '(555)555-5555', 'address 5');

select * from dbo.customers;

select * from dbo.customersstage;

merge into dbo.customers as tgt

using dbo.customersstage as src

on tgt.custid = src.custid

when matched then

update set

tgt.companyname=src.companyname,

tgt.phone=src.phone,

tgt.address=src.address

when not matched then

insert (custid, companyname, phone, address)

values(src.custid, src.companyname, src.phone, src.address);

select * from dbo.customers

第八章 資料修改 1

第8章 資料修改 8.1 插入資料 8.1.1 insert values 語句 use tempdb if object id dbo.orders u is not null drop table dbo.orders create table dbo.orders orderid int no...

第八章 指標 第八章 指標

1 什麼是位址 include using namespace std int main 11 在堆中建立對像 我們既然可以在堆中儲存變數,那麼也就可以儲存對像,我們可以將對像儲存堆中,然後通過指標來訪問它 include using namespace std class human 14 在建構...

第八章(筆記)

能在 中進行記憶體單元的定址的暫存器只有4個,分別是bx si di bp 其中bx bp 是基址,bx對應的段位址是ds,bp對應的段位址是ss si di 是變址,單獨使用時段位址是ds,組合使用段位址是跟隨組合的基址對應的段位址 中進行記憶體單元定址彙總 si di bx bp 常量 si 常...