SqlServer視圖(view)

--上節回顧
--1.什么是事務
--2.事務的特征
--原子性、一致性、隔離性、持久性
--3.與事務相關的t-sql命令
--開啟事務:begin transaction
--提交事務:commit transaction
--回滾事務:rollback transaction


----------------視圖------------------
--首先思考一個問題:查詢上機記錄信息。
--計算機名、會員名稱、上機開始時間、上機結束時間、金額


select? cp.chvComputerName, ci.chvUserName, ri.dtmStart, ri.dtmEnd, ri.mnyFee
from TblRecordInfo as ri--as表示為表取別名
inner join TblCardInfo as ci on ri.intCardId=ci.intCardId--內連接用inner join,同時應該為兩張表指定連接字段
inner join TblComputer as cp on ri.intComputerId=cp.intComputerId

--評價:上述書寫sql查詢語句的方式太繁瑣,每次書寫的時候都會感覺很復雜

--思考:到底有木有一種非常方便的操作方式,能夠實現一樣的查詢效果

--答案:肯定有:視圖來解決

--什么是視圖

--注意點:
--1.視圖中的數據并沒有保存在視圖中,它僅僅保留查詢的結果
--? 這些記錄是保存在表當中的

--如何創建視圖,創建視圖有相應的語法
create view view_RecordDetail
as
? select? cp.chvComputerName, ci.chvUserName, ri.dtmStart, ri.dtmEnd, ri.mnyFee
? from TblRecordInfo as ri--as表示為表取別名
? inner join TblCardInfo as ci on ri.intCardId=ci.intCardId--內連接用inner join,同時應該為兩張表指定連接字段
? inner join TblComputer as cp on ri.intComputerId=cp.intComputerId
?
?--如何使用視圖:使用方式和表一樣
?select * from view_recorddetail
?go
?
?--視圖是不是只能用來進行查詢操作???
?--不是的,視圖也可以用來進行新增\修改\刪除
?
?--利用學生信息表進行視圖的增刪改操作
?create table TblStudent
?(
?? intStudentId int primary key identity,
?? chvStudentName nvarchar(30) not null,
?? dtmBirthday datetime null,
?? chvCeilPhone nvarchar(11) not null
?)
?go
?
?--創建視圖:學生id、學生姓名、學生出生日期
?create view view_Student
?as
?? select intstudentid, chvstudentname,dtmbirthday from TblStudent
??
?go

--通過視圖完成對學生信息的新增
insert into view_student
(chvstudentname,dtmbirthday)
values
('zhangsan', '1989-9-9')
go
--上述sql會執行失敗,原因是:學生的手機號不能為空

--如果修改視圖
alter view view_student
as
?? select intstudentid, chvstudentname,dtmbirthday, chvCeilPhone from TblStudent
go??
--新增數據
insert into view_student
(chvstudentname,dtmbirthday, chvCeilPhone)
values
('zhangsan', '1989-9-9','13698766666')
go
--驗證,數據是在視圖中還是在表中
select * from TblStudent;
go
select * from view_student
?
--通過視圖進行數據修改

update view_student
? set chvceilphone = '13666666666'
where chvstudentname = 'zhangsan'
go

select * from view_student
go

--通過視圖刪除數據
delete from view_student
where chvstudentname='zhangsan'
go

select * from view_student;
select * from TblStudent;
go

--如何刪除視圖
--如何視圖存在,則刪除;否則不執行刪除操作
if exists (select 1 from sys.sysobjects where name='view_student')
begin
? print '視圖存在,即將刪除...'
? drop view view_student
end
else
begin
? print '視圖不存在'
end

alter view view_RecordDetail
as
? select cp.intComputerId, ci.intCardId,? cp.chvComputerName, ci.chvUserName, ri.dtmStart, ri.dtmEnd, ri.mnyFee
? from TblRecordInfo as ri--as表示為表取別名
? inner join TblCardInfo as ci on ri.intCardId=ci.intCardId--內連接用inner join,同時應該為兩張表指定連接字段
? inner join TblComputer as cp on ri.intComputerId=cp.intComputerId
?
select * from view_recorddetail
go

--如果執行下面的語句:
--A:失敗? B:tblcomputer中的記錄被刪除?
--c:recoredinfo表中的記錄被刪除? d:B+C
delete from view_recorddetail
where intComputerId=2
--答案:A??
--原因:視圖或函數 'view_recorddetail' 不可更新,因為修改會影響多個基表。


--是否是只要視圖由多張表組成,那么就不能執行刪除操作
--否定的,是可以刪除的,只是要滿足指定的條件

?

?


?

轉載于:https://www.cnblogs.com/changjiang/archive/2012/07/23/2605292.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377316.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377316.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377316.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android中的廣播Broadcast詳解

今天來看一下Android中的廣播機制,我們知道廣播Broadcast是Android中的四大組件之一,可見他的重要性了,當然它的用途也很大的,比如一些系統的廣播:電量低、開機、鎖屏等一些操作都會發送一個廣播,具體的And…

sql check約束_在SQL中使用CHECK約束

sql check約束Basically, CHECK constraint is used to LIMIT in columns for the range of values. We can use this constraint for single or multiple columns. 基本上, CHECK約束用于限制值范圍內的列 。 我們可以將此約束用于單列或多列。 In single column,…

.NET線程池

摘要 深度探索 Microsoft .NET提供的線程池, 揭示什么情況下你需要用線程池以及 .NET框架下的線程池是如何實現的,并告訴你如何去使用線程池。 內容 介紹 .NET中的線程池 線程池中執行的函數 使用定時器 同步對象的執行 異步I/O操作 監視線程池 死鎖 有關…

折線分割平面

Input輸入數據的第一行是一個整數C,表示測試實例的個數&#xff0c;然后是C 行數據&#xff0c;每行包含一個整數n(0<n<10000),表示折線的數量。Output對于每個測試實例&#xff0c;請輸出平面的最大分割數&#xff0c;每個實例的輸出占一行。Sample Input2 1 2Sample Ou…

《c++特性》

目錄多態構造函數和析構函數存在多態嗎&#xff1f;虛函數表虛析構函數純虛函數和抽象類運行時多態和編譯時多態的區別繼承設計實例指針對象和普通對象的區別正確初始化派生類方式繼承和賦值的兼容規則protected 和 private 繼承基類與派生類的指針強制轉換如何用C實現C的三大特…

Scala中的while循環

在Scala中的while循環 (while loop in Scala) while loop in Scala is used to run a block of code multiple numbers of time. The number of executions is defined by an entry condition. If this condition is TRUE the code will run otherwise it will not run. Scala中…

Linux操作系統啟動過程

在做開發的過程中&#xff0c;突然發現&#xff0c;要對系統做一些有意義的改變&#xff0c;必須要對操作系統的啟動過程有一定的了解&#xff0c;不然就是修改你都不知道從哪里下手啊&#xff0c;然后就是找來資料看&#xff0c;去網上看別人的博客&#xff0c;有了前一周一些…

方法命名的區別

GetDecimalFromString ExtractDecimal 這2個方法名那個比較好呢。上邊的明顯的是中式英語&#xff0c;單詞拼湊而成的。下邊的更加流暢一些。方法名稱取名還是很有要求的。要通俗易懂還要符合文法。從上邊的可以擴展出什么想法呢。 ExtractDecimalExtractDoubleExtractInt16Ext…

工作排序問題

Problem statement: 問題陳述&#xff1a; Given an array of jobs where every job has a deadline and a profit. Profit can be earned only if the job is finished before the deadline. It is also given that every job takes a single unit of time, so the minimum p…

牛客網與leetcode刷題(高頻題中簡單or中等的)

目錄1、反轉鏈表2、排序3、先序中序后序遍歷4、最小的k個數5、子數組的最大累加和6、 用兩個棧實現隊列7、142. 環形鏈表 II8、20. 有效的括號9、最長公共子串(動態規劃),磕磕絆絆10、二叉樹之字形層序遍歷11、重建二叉樹12、LRU緩存13、合并兩個有序鏈表15、大數加法16、一個二…

AMUL的完整形式是什么?

AMUL&#xff1a;阿南德牛奶聯盟有限公司 (AMUL: Anand Milk Union Limited) AMUL is an abbreviation of Anand Milk Union Limited. It is an Indian milk product cooperative dairy organization that is based in the small town of Anand in the state of Gujarat. AMUL …

mochiweb 源碼閱讀(十一)

大家好&#xff0c;今天周六&#xff0c;繼續接著上一篇&#xff0c;跟大家分享mochiweb源碼。上一篇&#xff0c;最后我們看到了mochiweb_socket_server:listen/3函數&#xff1a; listen(Port, Opts, State#mochiweb_socket_server{sslSsl, ssl_optsSslOpts}) ->case moch…

Android下拉刷新完全解析,教你如何一分鐘實現下拉刷新功能 (轉)

轉載請注明出處&#xff1a;http://blog.csdn.net/guolin_blog/article/details/9255575 最 近項目中需要用到ListView下拉刷新的功能&#xff0c;一開始想圖省事&#xff0c;在網上直接找一個現成的&#xff0c;可是嘗試了網上多個版本的下拉刷新之后發現效果都不怎么理 想。有…

Python中的append()和extend()

列出append()方法 (List append() method) append() method is used to insert an element or a list object to the list and length of the List increased by the 1. append()方法用于將元素或列表對象插入列表&#xff0c;并且列表長度增加1。 Syntax: 句法&#xff1a; …

紅黑樹的實現

目錄1、紅黑樹原理1、紅黑樹性質2、變換規則&#xff08;從插入結點的角度來講&#xff09;1.變色2.左旋3.右旋3、刪除結點需要注意的地方2、代碼1、定義結點以及構造函數2、定義紅黑樹類以及聲明它的方法3、左旋4、右旋5、插入操作6、修正操作7、刪除操作3、參考鏈接1、紅黑樹…

118 - ZOJ Monthly, July 2012

http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId339 都是賽后做的。。。弱爆了 A題是找由2和5組成的數字的個數 直接打個表就行了 只是比賽的時候不知道怎么打表啊。。 View Code #include<cstdio> #include<cstring> #include<algorith…

edp1.2和edp1.4_EDP??的完整形式是什么?

edp1.2和edp1.4EDP??&#xff1a;電子數據處理 (EDP: Electronic Data Processing) EDP is an abbreviation of Electronic Data Processing. It alludes to the functioning of operations of commercial data, documents processing of storing, with the use of a compute…

高效讀書心得

1.盡量閱讀中文版 雖然有人英文很強&#xff0c;有的翻譯很差&#xff0c;但AnyWay 中文閱讀與理解的時間&#xff0c;略讀與快速定位感興趣內容的速度還是要快一些。 2.即時批注、總結筆記與交流 雖然愛書&#xff0c;但發現最有效的讀書方式還是不斷的制造脂批本&…

《MySQL——增刪改查以及常用語法》

目錄登錄和退出MySQL服務器基本語法&#xff08;增刪改查&#xff09;登錄和退出MySQL服務器 # 登錄MySQL 密碼 $ mysql -u root -p12345612 # 退出MySQL數據庫服務器 exit;基本語法&#xff08;增刪改查&#xff09; -- 顯示所有數據庫 show databases;-- 創建數據庫 CREA…

WCF簡介

一、簡介 WCF是Windows Communication Foundation縮寫&#xff0c;是Microsoft為構建面向服務的應用提供的分布式通信編程框架&#xff0c;是.NET Framework 3.5的重要組成部分。使用該框架&#xff0c;開發人員可以構建跨平臺、安全、可靠和支持事務處理的企業級互聯應用解決方…