學習數據庫Sql Server(1)

     一.數據庫字符串的特性:sql

  

(1).---char 能夠存儲字符串,若是存儲的字符串走出了指定的長度就報錯,若是少於就會:char類型長度一旦肯定就不會再更改
select LEN(char) from ChatTest  --LEN獲得存儲的字符的個數,與中英文無關
select DATALENGTH(char) from ChatTest  --DATALENGTH獲得佔據的字節長度shell

(2).--varchar 能夠存儲字符串,若是存儲的字符串走出了指定的長度就報錯,若是少於就會:varchar類型長度能夠自動收縮
select LEN(varchar) from ChatTest  --LEN獲得存儲的字符的個數,與中英文無關
select DATALENGTH(varchar) from ChatTest  --DATALENGTH獲得佔據的字節長度數據庫


(3).---當有中文的時候才須要考慮判斷編碼----也就意味着若是值中可能含有中文,那麼就應該使用nexpress

--nchar 能夠存儲字符串,若是存儲的字符串走出了指定的長度就報錯,若是少於就會:ncharr類型長度一旦肯定就不會再更改  n---unicode:無論什麼字符都須要佔據兩個字節
select LEN(nchar) from ChatTest  --LEN獲得存儲的字符的個數,與中英文無關
select DATALENGTH(nchar) from ChatTest  --DATALENGTH獲得佔據的字節長度c#

(4).--nvarchar 能夠存儲字符串,若是存儲的字符串走出了指定的長度就報錯,若是少於就會:nvarchar類型長度能夠自動收縮  n---unicode:無論什麼字符都須要佔據兩個字節
select LEN(nvarchar) from ChatTest  --LEN獲得存儲的字符的個數,與中英文無關
select DATALENGTH(nvarchar) from ChatTest  --DATALENGTH獲得佔據的字節長度服務器

 

 

     二.建立數據庫ide

use master --切換當前使用的數據庫
if exists( select * from sysdatabases where name='MyTest')
 drop database MyTest --刪除數據庫
go 
create database MyTest
on primary
(
name='MyTest_data', --邏輯名稱,數據文件通常會添加data作爲後綴
size=3mb,--初始大小,08版本最小是3MB
fileGrowth=5%, --文件增加,每次遞增上一次大小的5%
fileName='D:\Classes\九期 .net增強+sql server增長\2013-10-15 sql server(1)\案例\MyTest_data.mdf', --文件的全路徑,須要添加擴展名
maxSize=100mb
),
(
name='MyTest_data1', --邏輯名稱,數據文件通常會添加data作爲後綴
size=3mb,--初始大小,08版本最小是3MB
fileGrowth=5%, --文件增加,每次遞增上一次大小的5%
fileName='D:\Classes\九期 .net增強+sql server增長\2013-10-15 sql server(1)\案例\MyTest_data1.ndf', --文件的全路徑,須要添加擴展名
maxSize=100mb
)
log on
(
name='MyTest_log', --邏輯名稱,數據文件通常會添加data作爲後綴
size=1mb,--初始大小,08版本最小是3MB
fileGrowth=5%, --文件增加,每次遞增上一次大小的5%
fileName='D:\Classes\九期 .net增強+sql server增長\2013-10-15 sql server(1)\案例\MyTest_log.ldf', --文件的全路徑,須要添加擴展名
maxSize=100mb
),
(
name='MyTest_log1', --邏輯名稱,數據文件通常會添加data作爲後綴
size=1mb,--初始大小,08版本最小是3MB
fileGrowth=5%, --文件增加,每次遞增上一次大小的5%
fileName='D:\Classes\九期 .net增強+sql server增長\2013-10-15 sql server(1)\案例\MyTest_log1.ldf', --文件的全路徑,須要添加擴展名
maxSize=100mb
)模塊化

    三.數據庫建立表:函數

 use MSJDevice
if exists (select * from sysobjects where name='[DevSORetDetail]')
drop table [DevSORetDetail]
create table [DevSORetDetail]
(
    [SORDtlID] [varchar](50) NOT NULL,
 [SORID] [varchar](50) NULL,
 [TraceNo] [varchar](30) NULL,
 [DevKey] [int] NULL,
 [RecQty] [numeric](22, 2) NULL,
 [Qty] [numeric](22, 2) NULL,
 [Price] [numeric](22, 2) NULL,
 [SaleRate] [numeric](22, 2) NULL,
 [RateAmount] [numeric](22, 2) NULL,
 [SaleAmount] [numeric](22, 2) NULL,
 [MoneyKey] [int] NULL,
 [Exchange] [numeric](22, 4) NULL,
 [ResonDescs] [nvarchar](200) NULL,
 [CreateOrgNo] [varchar](20) NULL,
 [CreateDeptNo] [varchar](20) NULL,
 [CreateUserNo] [varchar](20) NULL,
 [CreateTime] [datetime] NULL,
 [EditOrgNo] [varchar](20) NULL,
 [EditDeptNo] [varchar](20) NULL,
 [EditUserNo] [varchar](20) NULL,
 [EditTime] [datetime] NULL,
)測試

  四.插入數據(insert)

----語法: insert into 表名 (列名列表) values( 值列表)   列名列表至關於形參,值列表至關於實參,三個對應(類型對應,數量對應,順序對應)在這裏同樣存在 
use MyTest
--1.插入全部列的值            
insert  teacher (name,gender,age,salary,birthday) values('aa',1,20,3000,'1990-9-9')
--若是沒有指定列名那麼就會默認須要爲全部的列添加值  :列名或所提供值的數目與表定義不匹配。
insert  teacher values('aa',1,20,3000)
--2.標識列不能人爲添加值   "當 IDENTITY_INSERT 設置爲 OFF 時,不能爲表 'Teacher' 中的標識列插入顯式值。"
insert  teacher (id,name,gender,age,salary,birthday) values(100,'aa',1,20,3000,'1990-9-9')

--3. 插入的值數量須要和指定的列的數量一致:INSERT 語句中列的數目小於 VALUES 子句中指定的值的數目。VALUES 子句中值的數目必須與 INSERT 語句中指定的列的數目匹配。

--4.若是列的屬性是非空,那麼就必須插入一個值  :不能將值 NULL 插入列 'Birthday',表 'MyTest.dbo.Teacher';列不容許有 Null 值。INSERT 失敗。
insert  teacher (name,gender,age,salary) values('aa',1,20,3000)

--5.若是列的屬性標記爲能夠爲null,那麼就能夠不插入值:若是不想爲能夠爲null的列添加值:要麼不寫,要麼寫null
insert  teacher (name,gender,age,salary,Birthday) values('aa',1,20,null,'1900-8-8')

--6.若是列有默認值:要麼不寫這一列,那麼就會添加默認值,要麼能夠在值列表中寫default
insert  teacher (name,gender,age,salary,Birthday) values('aa',1,-100,null,'1900-8-8')

--7.插入的值須要知足表的約束   INSERT 語句與 CHECK 約束"CK_Age"衝突。該衝突發生於數據庫"MyTest",表"dbo.Teacher", column 'Age'。
insert  teacher (name,gender,age,salary,Birthday) values('aa',1,-100,null,'1900-8-8')

--8.當插入的值裏面有日期的時候:若是沒有添加'',那麼獲得的值是系統計算出來的默認值
insert  teacher (name,gender,age,salary,Birthday) values('aa',1,20,null,'1990-8-8')

--9.在sql語言中,類型其實不那麼嚴格,任何類型都能添加'',沒有錯誤,除了null,default以外,可是若是是字符串沒有添加''就會報錯
--由於系統會將這個值轉換爲目標類型,若是能夠轉換則進行賦值,若是不能夠轉換就報錯
insert  teacher (name,gender,age,salary,Birthday) values('aa','1','20',null,'1990-8-8')

 

   五.刪除數據(delete和truncate)

---刪除操做也是不可逆的,因此必定須要詢問用戶是否真的須要刪除,同時每每並不是真正的刪除,而只是作一個刪除事後的標記
--可以作爲條件的通常都是主鍵或者是標識列或者是惟一鍵值
---語法:delete 【from】 表 where 條件 
delete  from Teacher where Id=14  ---delete 後面不能接字段,是由於刪除是一行一行刪除的

delete from Teacher where Gender=1 and Salary>5000

---truncate:一次性刪除表中的全部記錄
--語法:truncate table 表名
truncate table teacher

---delete 是一條一條刪除,它每次刪除都會將操做寫入到日誌文件中。 而truncate不會
---delete能夠寫條件,可是truncate不會
--delete是一條一條刪除,效率低下
---delete刪除後的表的標識列不會從新從1開始計算,而truncate會重置標識列到初始值
delete from Teacher

 

   六.更新操做

----更新操做   必定要注意添加條件,多字段之間用,  多條件使用not and or
---語法 :update 表 set 字段=新值, 字段2=新值2。。。 where 條件
update Teacher set Age=30 where Id='14'
--多字段修改
update  Teacher set Name='張三' ,Age=18 ,Salary='6000' where Id=17

--修改性別是男,同時工資在3500如下,爲其加1000塊工資
update Teacher set Salary=Salary+1000 where Gender=1 and Salary<3500

update Teacher set Birthday=2000-2-2 where Id=15

  七.查詢操做

---語法說明 :select  *代碼全部的列/指定某一些列名   from 表(能夠多張表),查詢獲得虛擬表
use MySchoolMoreData
--查詢全部學員信息
select * from Student
--查詢某一些列
select StudentNo,StudentName,sex,borndate from Student
--查詢男學員的信息-
--語法:select  列 from 表 where 條件
select * from Student where Sex='男'  and ClassId=6 ---where是來作數據源的篩選,獲得過濾掉不知足條件的記錄,而獲得知足條件的結果集
---獲得6期班全部女生的姓名和學號,及電子郵箱
select StudentNo,StudentName,Email from Student where ClassId=6 and Sex='女'

--select 的做用:1.查詢獲得結果集  2.輸出   除了select into  是來作數據的遷移的
select '1'+'a'  --+在數據庫裏面本質來講是一個運算符,除非兩邊都是字符類型,那麼+就是鏈接符,若是有一邊是數值那麼系統會將另外的值也強制轉換爲數值,若是能夠轉換就相加,若是不能夠轉換就報錯

  八.as 、top 、Distinct關鍵字

 

---修改列的中文顯示方式 as
select StudentNo as 學號,StudentName 姓名,電子郵箱=Email from Student where ClassId=6 and Sex='女'
--能夠在結果集中添加常量列
select StudentNo as 學號,StudentName 姓名,電子郵箱=Email,學校='傳智播客' from Student where ClassId=6 and Sex='女'

---經過top關鍵字來獲取指定數量的記錄,1.能夠指定具體的條數
select top 5 StudentNo as 學號,StudentName 姓名,電子郵箱=Email,學校='傳智播客' from Student where ClassId=6 and Sex='女'
--2.也能夠指定百分比  ceiling:獲得比當前數大的全部整數中的最小值  天花板函數
select top 90 percent StudentNo as 學號,StudentName 姓名,電子郵箱=Email,學校='傳智播客' from Student where ClassId=6 and Sex='女'

---Distinct  能夠去除重複記錄:什麼叫重複記錄:在你指定的列列表中沒有任何一列是重複的值
select distinct Sex,Address,Email from Student order by sex

 

   九.聚合函數

--1.獲得班級總人數
select COUNT(0) from Student  --count裏面不須要指定列名,是由於它是對整條記錄而言的,它是獲得知足條件的記錄數
--2.計算6期 班的總人數
select COUNT(0) from Student where ClassId=6
---獲得6期 班年齡最大的學員信息   
select MIN(BornDate) from Student where ClassId=6  --若是對於非數值列會按照它們的拼音來排序,再取最大值  最小的是空格 ,而後數字,再是英文字符,最後是中文
select max(BornDate) from Student where ClassId=6
---獲得C#考試的平均分
select AVG(StudentResult) from Result where SubjectId=3  ---只能對數值類型的列來作計算 
select SUM(studentresult) from Result where SubjectId=3 ---只能對數值類型的列來作計算 
select sum(BornDate) from  Student   --日期類型的值不能作sum和avg聚合運算
--警告: 聚合或其餘 SET 操做消除了 Null 值。  說明聚合函數不能計算null.  判斷null須要使用is null   is not null
select SUM(StudentResult) from Result where StudentResult is not  null

 十.帶條件查詢

---帶條件的查詢   > >=  <  <= = <> !=     + - * / %
select * from Result where StudentResult>60 and StudentResult<70
select * from Result where StudentResult between 60  and 70   --至關於大於等於前面一個值小於等於後面這個值
---儘可能使用between...and,作過優化
select * from Result where StudentResult between 80  and 70    --若是查詢不到數據,就返回一個空的結果集
--select * from Result where  60<StudentResult<70

--Regex(str,@"[深圳]")
---in  能夠指定一個具體的範圍
---查詢班級號爲1 ,4 ,6的學員信息
select * from Student where ClassId=1 or ClassId=4 or ClassId=6
select * from Student where ClassId in(1,4,6)

--查詢地址是 「廣州」或者 「深圳」的學員信息
select * from Student where Address ='廣州' or Address='深圳'
--in裏面的值須要能夠相互轉換
select * from Student where Address in('123','廣州')

  十一.建立約束

---約束名稱   約束類型(check,默認值(default),主鍵 ,惟一鍵,外鍵)  約束說明(字段或者表達式)
--check--CK  --表達式
--primary key --PK --鍵
--Unique--UQ--字段名
--Default--DF --爲某個字段添加默認值
--foreign key--FK --引用

---1,密碼的長度大於6位   刪除約束和添加約束都須要去指定表名
if exists(select * from sysobjects where name='CK_LoginPwd')
alter table student drop constraint CK_LoginPwd
alter table student with nocheck
add constraint CK_LoginPwd check(len(loginPwd)>=6),
constraint CK_Phone check(Phone is not null)  --能夠一次建立多條約束
--默認約束
alter table student
add constraint DF_Addres default(N'沒有填寫') for address --爲某全字段來添加默認值
--主鍵約束
alter table student
add constraint PK_StudentNo primary key(StudentNo)
--惟一鍵約束
alter table student
add constraint  UQ_Email  unique(Email)
--主外鍵約束
alter table student
add constraint FK_student_grade_gradeid foreign key(gradeId) references Grade(GradeId)
--知道能夠級聯,可是通常狀況下不要這麼處理,由於太危險
on delete set default
--[ ON DELETE { NO ACTION  --不作任何處理,該報錯就報錯| CASCADE  級聯,刪除主表也會刪除從表的記錄 | SET NULL 將從表的對應列的值設置爲null | SET DEFAULT 爲從表的列設置默認值 } ]
--[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]

  十二.分組統計信息

--查詢學員表中男女生的人數
select COUNT(*) from Student where Sex='男'
select COUNT(*) from Student where Sex='女'

select * from Student  ---先獲得整個數據源
select * from Student where Sex='男' --使用where 對數據源進行篩選
select COUNT(*) from  (select * from Student where Sex='男' ) as temp   --再對篩選後的結果集作統計

select * from Student order by sex
select sex, COUNT(*) from Student  group by sex
select MAX(StudentResult),MIN(StudentResult) from Result


--須要統計每一個班級的學員數量
select ClassId,COUNT(*) from Student group by ClassId
--請從學生表中查詢出每一個班的班級Id和班級中男同窗的人數--

---與聚合函數一塊兒出如今select查詢中的列,要和麼被聚合,要麼被分組
select ClassId,Sex,COUNT(*) from Student group by ClassId,Sex

---查詢出6期班男女生的數量
select * from Student where ClassId=6

select COUNT(*) from (select * from Student where ClassId=6) as  temp group by Sex
--4 查詢6期班的男女生的數量      1      2      3
select sex,COUNT(*) from Student where ClassId=6 group by Sex 
---5.查詢6期班的男女生的數量,只顯示人數超過6人的信息
--不能使用where 來判斷聚合函數的條件,緣由有二:1.先where 再聚合,因此使用where 來作條件判斷的時候,聚合的值尚未出來   2.根據語法規則,聚合函數的條件不能在where 子句中寫
--5          1      2       3     4         6
select  sex,COUNT(*) As cnt from Student where ClassId=6  group by Sex  having COUNT(*)>=5 order by  cnt desc
---看看剛剛的sql語句具體是如何來執行的:
1.select * from Student
2.select * from Student where ClassId=6
3.分組獲得兩個子結果集----分組
  select * from Student where ClassId=6 and Sex='男'
  select * from Student where ClassId=6 and Sex='女'
4.having--是對分組統計的結果集來作篩選的
5.顯示結果
6.order by 是對獲得告終果集再作顯示的控制 ,它不可能去改變結果集的數據


select  sex,COUNT(*) As cnt from Student where ClassId=6  group by Sex  having COUNT(*)>=5 and Sex='男' order by  cnt desc

--查詢班級人數超過三我的的班級
select ClassId,COUNT(0) from Student group by ClassId having COUNT(*) in (3,4)

--查詢每一個參加考試的學員的平均分
select StudentNo,AVG(StudentResult) from Result where StudentResult is not null group by StudentNo order by AVG(StudentResult)

  十三.模糊查詢

---模糊查詢:是對字符串類型的值而言---
--=是徹底的嚴格的匹配
select * from Student where StudentName ='張莉'
--- %:代碼任意個任意字符
---  _表明單個字符
---[]:表明單個字符
select * from Student where StudentName like  '張__'   --  like:喜歡    像。。。。同樣
--總結:1.模糊查詢必須使用關鍵字:like   2.不能使用=,不然就是徹底的匹配

select * from Student where StudentNo not like '1[369]'  --若是有多個範圍也能夠一塊兒來指定囉
select * from Student where StudentName like '張[^麗12345]'

---獲得沒有填寫電子郵箱的學員信息
select * from Student where Email='null'
select * from Student where Email is null

---isnull 函數:若是查詢的字段值是null值,那麼就可使用指定的值來替代,注意指定的值的類型須要和原始字段的類型一致
select StudentName,ISNULL(Email,'她沒有寫,打電話吧') from Student where ClassId=4

select StudentNo,ISNULL(StudentResult,'0') from Result where SubjectId=2


----排序:能夠選擇升充或者降序排序   若是有多個關鍵字,那麼就先按第一個關鍵字排序,相同的記錄再按第二個關鍵字排序,排序必須寫在最後:由於排序是對結果集再進行數據的從新顯示 ,排序默認就是升序排序   asc--升序  desc  降序
select * from Result  order by StudentResult Desc,StudentNo asc

--1.查詢6期 班全部姓  陳  的學員
 select * from Student where ClassId=4 and StudentName like '張%'
--2.查詢全部科目中包含 c 字符的科目信息
  select * from Subject where SubjectName like '%c%'
--3.查詢office最逝一次考試時間
select top 1 ExamDate from Result where SubjectId=1 order by ExamDate desc  --升序
select SubjectId from Subject where SubjectName='office'
select MAX(ExamDate) from Result where SubjectId=(select SubjectId from Subject where SubjectName='office')

十四.Union

----聯合查詢:將多個結果集組成一個結果集:每個select操做都會生成一個新的結果集(除了select into from )
---列數,與列的類型須要對應一致,若是類型不一致,也需字段的值能夠相互的隱式轉換,最終的列名由第一個結果集來決定
--不能在最後一個結果集之外的任何結果集查詢語句中添加order by 排序
select '  '+cast(StudentNo as CHAR(3)) as 學號,StudentName 姓名 from Student 
union  --去除重複,效率不高,由於須要作是否重複的判斷操做
select cast(ClassId as CHAR(2)) as 班級ID,ClassName as 班級名稱      from Grade   ---order by ClassName 爲何報錯:由於結果集由第一個結果集的列來決定,因此只有學號和姓名,同時order by是獲得結果集以後再對結果集的數據作重排的,因此它不能使用第一個結果集之外的列

select * from Student where ClassId=3
union all
select * from Student where ClassId=3

select MAX(StudentResult),MIN(StudentResult),AVG(StudentResult)  from Result --能夠在同一行顯示 

select '最高分',MAX(StudentResult) from Result --分多行顯示,可是在是同一個結果集中
union
select '最低分',min(StudentResult) from Result
union
select '平均分',avg(StudentResult) from Result

---查詢每一個學員的學號和成績同時在最後一行顯示最高分和最低分
select '  '+cast (StudentNo as CHAR(4)),cast (StudentResult as CHAR(3)) from Result  --空格是最小的,在排序的時候若是是升序排序,那麼就會排在最前面
union
select ' 最高分','最低分'
union
select cast (MAX(StudentResult) as CHAR(3)),cast (MIN(StudentResult) as CHAR(3)) from Result


----一次插入多條數據
--1.select 字段列表 into  新的目標表中  from  源表     目標表是系統自動建立的,不能先存在 ,若是已經存在 就會給你報錯
--說明:經過這種方式能夠建立一個表結構,同時複製表數據,可是須要注意的是:表結構僅僅有標識列屬性,其它的,如(主鍵,惟一鍵,check約束,關係。。)都沒複製,須要本身再從新建立
select LoginPwd,StudentName,Sex,ClassId,Phone,Address,BornDate,Email,isDel  into newstudent from Student

--select LoginPwd,StudentName,Sex,ClassId,Phone,Address,BornDate,Email,isDel  into student from newStudent
--將數據複製到一個已經存在 的表結構中。注意類型須要對應,列的數量也須要對應,還須要遵照目標表的約束及相應的規範
--insert into 目標表中 select  字段列表 from 源表  ---目標表須要先存在 ,且與數據源(字段數量,字段的類型)可以對應
insert into Student select * from newstudent where email is not null

truncate table admin
---沒有數據源也能夠一次性插入多條記錄 ---歷來不用的
insert into Admin
select 'a','a' union all  ---union all不會去除重複的記錄  union會去除重複項
select 'b','b' union 
select 'a','a' union 
select 'a','a' union 
select 'a','a' union all
select 'a','a'  --最後一句不須要添加union

 

十五.類型轉換

--CAST ( expression AS data_type)  將指定的表達式expression強制轉換爲data_type所規定的類型,若是能夠轉換就轉換,若是不能夠轉換就報錯
select '個人成績是:'+cast(90 as CHAR(2))
--CONVERT ( data_type, expression,[style])   第一個參數是目標類型,第二個參數是須要轉換的表達式,第三個參數是:日期類型
select '個人成績是:'+CONVERT(char(2),90)

 

十六.日期類型

select GETDATE()
--DATEADD 能夠來添加一個指定的值  DATEADD (datepart , number, date ):第一個參數指定你須要中的值的單位(年,月日,時分秒。。。),第二個參數是你須要添加的具體的值,第三個參數是對那個日期值來作運算
select DATEADD(MM,-1,GETDATE())
--查詢年齡超過18歲的學員信息
select * from Student where BornDate<DATEADD(yyyy,-18,getdate())

---須要查詢學員的年齡
--DATEDIFF 獲得兩個日期的差別值
select DATEDIFF(yy,'1990-1-1',getdate())
--查詢年齡超過18歲的學員信息
select * from Student where DATEDIFF(yyyy,borndate,getdate())>=25

--DATENAME能夠獲得日期的字符串表現形式
select DATENAME(w,getdate())
--DATEPART 獲得具體的日期部分  2013-10-18
select cast(DATEPART(yyyy,getdate()) as CHAR(4))+'-'+cast(DATEPART(mm,getdate()) as CHAR(2))+'-'+cast(DATEPART(dd,getdate()) as char(2))

 

十七.數字函數

--RAND 能夠生成隨機數   永遠只能生成一個0~1之間的隨機數,理論上包含0可是不包含1
select RAND()
--ABS取絕對值  
select ABS(-100)
--CEILING  能夠獲取比當前指定的數大的全部整數中的最小值
select CEILING(0.01)
--floor:地板:能夠獲得比當前指定的數小的全部整數中的最大值
select FLOOR(99.9)
--POWER 冪
select POWER(4,2)
--SQRT 求 開平方--根
select SQRT(10)
---ROUND 四捨五入
select ROUND(5.557245678,2)  --多餘的位數會補0
--SIGN 若是是正值,就是1,若是是負值就是-1.若是是0就是0
select SIGN(0)

 

 

select CONVERT(char(20),GETDATE(),1000)   --style是對日期或者日間格式的值而言的。

 

十八.case ..and..

--case---end  至關於switch...case
--1.作等值判斷 ,,它會生成一個列   2.then後面的數據類型須要對應(互相轉換)  3.若是判斷範圍,那麼when後面的表達式不必定判斷同一列的值  4.不能捕獲到null值
select StudentName,
case ClassId  --若是在case後面添加了表達式,那麼這個case就只能進行等值判斷
  when 1 then '一期班'
  when 2 then '2期班'
  when 3 then '3期班'
  when 4 then '4期班'
  when 5 then '5期班'
  when 6 then '6期班'
  --when null then 'dsafs'
  else '不知道'
end as 班級名稱
 from Student 
---2.也能夠判斷範圍
select StudentName,sex,
case --若是case沒有接表達式,那麼也能夠進行範圍的判斷,至關於if
 when BornDate>'2000-1-1' then '小姑娘'
 when BornDate>'1990-1-1' and BornDate<'2000-1-1' then '少女' 
 when BornDate>'1980-1-1' and BornDate<'1990-1-1' then '×××' 
 when sex='女' then '1sdfsfsdafasd00'
end as 級別
 from Student where Sex ='女'

--案例:百分制--轉素質教育
select StudentNo,
case
 when StudentResult >=90 then 'A'
 when StudentResult between 80 and 89 then 'B' 
 when StudentResult between 70 and 789 then 'C' 
 when StudentResult between 60 and 69 then 'D' 
 when StudentResult <60 then 'E' 
 when StudentResult IS null then '沒有參加考試'   ---只有這種形式才能判斷空值
end,
ExamDate from Result

--表中有A B C三列,用SQL語句實現:當A列大於B列時選擇A列不然選擇B列,當B列大於C列時選擇B列不然選擇C列
select 
case
 when StudentNo>SubjectId then StudentNo
else SubjectId 
end,
case
 when SubjectId>StudentResult then SubjectId
else StudentResult 
end
 from Result

select eName,
case
  when eSalary>=10000 then '王牌'
  when eSalary> 8000 and eSalary<10000 then '鐵牌'
  else  '紙牌'
end
 from employee 

十九.連表查詢

---錶鏈接的傳統實現方式
--select 字段 from 表列表
--查詢學員基本信息,班級須要顯示爲班級名稱
--若是從多個表中取得數據,那麼必定會有兩個表的鏈接表達式,不然會取出兩個表記錄數乘積條記錄,而可以創建鏈接的字段,通常就是主外鍵
select * from grade ,Student where Student.ClassId=Grade.ClassId

----查詢學員參加的考試信息---學號,姓名,考試科目,考試成績和考試日期,以及他所屬班級名稱
select Student.StudentNo,Student.StudentName,  Grade.ClassId,      Subject.SubjectName,        Result.StudentResult,Result.ExamDate
from Student,Result,Subject,Grade --排名不分前後
where Student.StudentNo=Result.StudentNo 
and Result.SubjectId=Subject.SubjectId 
and Student.ClassId=Grade.ClassId

---查詢每一個學員須要參加的考試科目名稱
select Student.StudentNo,Student.StudentName,Subject.SubjectName
from Student,Subject,Result
where Student.StudentNo=Result.StudentNo and Result.SubjectId=Subject.SubjectId

select Student.StudentNo,Student.StudentName,Subject.SubjectName
from Student,Subject
where Student.ClassId=Subject.ClassId
--多表鏈接
--查詢學員基本信息,班級須要顯示爲班級名稱
--inner join :至關於from多表,只會取出創建關聯的字段相等的記錄
select Student.StudentNo,Student.StudentName,Grade.ClassName
from Student
inner join Grade on Student.ClassId=Grade.ClassId --on是說明在那個字段上面創建關聯,同樣的原則,通常是主外鍵,只有兩個classId值相等,這條記錄纔會被取出
----查詢學員參加的考試信息---學號,姓名,考試科目,考試成績和考試日期,以及他所屬班級名稱
select Student.StudentNo,Student.StudentName,  Grade.ClassId,      Subject.SubjectName,        Result.StudentResult,Result.ExamDate
from Student 
inner join Result on Student.StudentNo=Result.StudentNo
inner join Grade on Student.ClassId=Grade.ClassId
inner join Subject on Grade.ClassId=Subject.ClassId
where Student.Sex='男'

---查詢全部學生的姓名、年齡及所在班級
select Student.StudentName,DATEDIFF(yyyy,borndate,getdate()) 年齡,Grade.ClassName
from Student
inner join  Grade on Student.ClassId=Grade.ClassId
--查詢年齡超過20歲的學生的姓名、年齡及所在班級
select Student.StudentName,DATEDIFF(yyyy,borndate,getdate()) 年齡,Grade.ClassName
from Student
inner join  Grade on Student.ClassId=Grade.ClassId where DATEDIFF(yyyy,borndate,getdate())>=20
--案例3:查詢學生姓名、年齡、班級及成績
select Student.StudentName,DATEDIFF(yyyy,borndate,getdate()),Grade.ClassName,Result.StudentResult
from Student
inner join Grade on Student.ClassId=Grade.ClassId
inner join Result on Student.StudentNo=Result.StudentNo
--案例4:查詢全部學生(參加及未參加考試的都算)及成績
select Student.StudentNo,Student.StudentName,Result.StudentResult
from Student left  join Result on Student.StudentNo=Result.StudentNo
--案例5:請查詢出全部沒有參加考試(在成績表中不存在的學生)
select Student.StudentNo,Student.StudentName,Result.StudentResult
from Student left  join Result on Student.StudentNo=Result.StudentNo
where Result.StudentResult is null and Result.StudentNo is null

---左鏈接和右鏈接
use MyTest
--查詢參加了考試的學員信息
select * from Student inner join StuScore on Student.StuNo=StuScore.StuId
--查詢學員考試信息,參加了和沒有參加的都要顯示
select * from Student right join StuScore on Student.StuNo=StuScore.StuId

---交叉鏈接
select * from Student cross join grade
use MySchoolMoreData
--查詢全部學生(參加和未參加考試)的學生姓名、年齡、成績,若是沒有參加考試顯示缺考,若是小於60分顯示不及格
select Student.StudentName,DATEDIFF(YYYY,borndate,GETDATE()),

case
 when StudentResult>=60 then cast(StudentResult AS CHAR(3))
when StudentResult<60 then '不及格'
else '缺考' 
end

from Student
left join Result on Student.StudentNo=Result.StudentNo

order by StudentResult Desc

二十.子查詢

--子查詢有兩種:
--獨立子查詢:子查詢自己也是一個完整的查詢,能夠獨立執行的
--相關子查詢:子查詢中使用了父查詢中定義的列
--查詢參加了考試的學員信息--  
select distinct studentno from Result
select * from Student where StudentNo in(select distinct studentno from Result)
--查詢和刪除delete同樣,都是一條一條遍歷
select * from Student where StudentNo=(select distinct StudentNo from Result where StudentNo=student.StudentNo)
--沒有參加考試的學員信息
select * from Student where StudentNo not in(select distinct studentno from Result)

---子查詢作爲虛擬表--必須添加別名
---獲得不一樣班級的男學員的信息
select * from Student where Sex='男'
select COUNT(*) from Student where Sex='男' group by ClassId
select COUNT(*) from (select * from Student where Sex='男') temp group by ClassId
--要求在一個表格中查詢出學生的英語最高成績、最低成績、平均成績
select MAX(StudentResult),MIN(StudentResult),AVG(StudentResult) from Result
select MAX(StudentResult) from Result
select MIN(StudentResult)  from Result
select AVG(StudentResult) from Result
--子查詢作爲查詢的輸出列
select (select MAX(StudentResult) from Result),(select MIN(StudentResult)  from Result),(select AVG(StudentResult) from Result)

---子查詢如何實現分頁
select top 5 * from Student --第一頁
select top 5 * from Student where StudentNo not  in (select top 5  StudentNo from Student order by StudentNo)  --第二頁
select top 5 * from Student where StudentNo not  in (select top 10  StudentNo from Student order by StudentNo)  --第二頁
--由於記錄有可能刪除過,而標識列不會自動更新到合理的值
select * from Student where StudentNo between 1 and 5
select * from Student where StudentNo between 6 and 10

--row _number  over(order by)
select *,ROW_NUMBER() over(order by studentno) as id from Student
---它並無真正修改表的結構,只是臨時生成一個不間斷的數據列,相似於沒有間斷的標識列值
select * from (select *,ROW_NUMBER() over(order by studentno) as id from Student) as temp where temp.id between 1 and 5
select * from (select *,ROW_NUMBER() over(order by studentno) as id from Student) as temp where temp.id between 6 and 10
select * from (select *,ROW_NUMBER() over(order by studentno) as id from Student) as temp where temp.id between 11 and 15

二十一.視圖

----視圖能夠認爲就是一個查詢語句,
select * from Student where sex='女'
select StudentNo,StudentName from Student
---家長鬚要學員姓名,考試科目及考試成績
select  * from 媽媽要看的
--視圖就至關於一個虛擬表,咱們能夠像操做表同樣操做視圖,可是通常不要去對其進行修改和刪除的操做,由於有可能影響到其它的視圖
--1.對視圖進行刪除:視圖或函數 '媽媽要看的' 不可更新,由於修改會影響多個基表。這是由於你的數據來自於多和表,刪除視圖會影響到多張表的結果
delete from 媽媽要看的 where studentname='張3'
select * from 測試是否能夠刪除
delete from 測試是否能夠刪除 where studentno=25

---使用代碼建立視圖  視圖中是隻是select查詢操做, 不能update,delete.insert,視圖只能獲得一個結果集,因此不能建立多個select語句
if exists(select * from sysobjects where name='vw_getinfo')
 drop view vw_getinfo
go
create view vw_getinfo
as
 select * from Student
 --select * from Student 
go 

二十二.sql的設值

----變量的使用
--語法:declare   @變量的名稱  變量的類型
declare @name nvarchar(20)='沒有名字'
--賦值--共同點:1.均可以直接賦值一個肯定的值  2.若是使用select查詢語句賦值,若是=號右邊是一個完整的子查詢,那麼它們兩個賦值方式同樣
--1.若是賦值默認值
 --declare @name nvarchar(20)='沒有名字' --默認值是寫在類型的後面哦
--使用set賦值
set @name='aa' 
--set @name=(select StudentName from Student where StudentNo=7)
--set @name=(select StudentName from Student)  -子查詢返回的值不止一個,這種狀況是不容許的。
--使用select 能夠賦值
select @name='bb'
select @name=(select StudentName from Student)
print @name --文本的形式來輸出
select @name --結果集的方式來輸出
go

---這兩種方式賦值的區別:
--1.set只能一次爲一個變量賦值,而select能夠一次爲多個變量賦值
declare @name nvarchar(20)='aa',@age int=100 --一次能夠定義多個變量
--set @name='aa',@age=20 --set不能一次爲多個變量賦值
--select @name='aa',@age=20 --select能夠同時爲多個變量賦值
--print @name
--print @age
--2.若是=號右邊的sql語句(不是完整的子查詢)返回多個值,那麼set會報錯(語法有錯誤),而select會獲得最後一次值-,set後面必須接完整的sql語句
--select @name=StudentName from Student  --會依次取出每個獲得的值,賦值給當前變量,至關於覆蓋,因此會獲得最後一個值
----set @name=StudentName from Student  --錯誤,只能寫完整的子查詢語句,且放在()之內
--print @name
--print @age
--3.使用select賦值的時候,若是查詢語句沒有返回值,那麼變量也會保留默認值,而set會獲得null值
--select @name=StudentName from Student where StudentNo=123456
set @name=(select StudentName from Student where StudentNo=123456) ---/默認值會被清除
print @name
print @age
go
--查詢學號是"7「的學生參加」最近一次「的「office」課程考試的成績,要求輸出學生姓名和成績
declare @subjectname varchar(20)='office' --科目名稱
declare @studentName nvarchar(20) --學員姓名
set @studentName=(select studentname from Student where StudentNo=7)
declare @subjectId int ---科目ID
select @subjectId=SubjectId from Subject where SubjectName=@subjectname
declare @date datetime
set @date=(select MAX(examdate) from Result where StudentNo=7 and SubjectId=@subjectId)
--下面是查詢語句
select @studentName,Result.StudentResult from  Result 
where StudentNo='7' and SubjectId=@subjectId and ExamDate=@date

二十三.While循環

declare @num int=0
while(1=1) --在sql裏面,實際上是沒有bool這個類型的,bit是二進制數據1和0,因此,它不支持所謂的true和false,在sql裏面,若是須要獲得true或者false,須要使用關係運算符 > < >= <= = != <>
begin
  set @num=@num+1
  if(@num=10)
   break
 print 'a'
end
go
--計算1-100之間全部奇數的和
declare @sum int=0,@num int=0
while(@num<100)
 begin
 set @num=@num+1  
  if(@num%2=0) 
 continue
  set @sum=@sum+@num 
 end 
print @sum 
go
-- 學校沒有錢用了,想收office補考費,若是及格人數超過一半,就減分,每次減2分,直到須要補考的人超過一半。
--須要獲得總人數,再計算經過考試的人數,作出判斷,若是知足條件,就減分,再判斷是否須要減分(循環)
declare @subjectname varchar(20)='office' ---科目名稱
declare @subjectid int =(select subjectid from Subject where SubjectName=@subjectname) --科目ID
declare @totalNum int=(select COUNT(*) from Result where SubjectId=@subjectid) ---須要考試這一科考試的總人數
declare @passNum int=(select COUNT(*) from Result where SubjectId=@subjectid and StudentResult>=60) --這一科的經過人數
--if(@passNum>@totalNum/2)
-- begin
print '減分以前'
select * from Result where SubjectId=@subjectid
while(@passNum>@totalNum/2)
begin
  update Result set StudentResult-=2 where SubjectId=@subjectid and StudentResult>2
  set @passNum=(select COUNT(*) from Result where SubjectId=@subjectid and StudentResult>=60) --這一科的經過人數
end  
 --end 
print '減分以後'
select * from Result where SubjectId=@subjectid
go
--使用另一種方式來實現
declare @subjectname varchar(20)='office' ---科目名稱
declare @subjectid int =(select subjectid from Subject where SubjectName=@subjectname) --科目ID
declare @totalNum int=(select COUNT(*) from Result where SubjectId=@subjectid) ---須要考試這一科考試的總人數
declare @passNum int=(select COUNT(*) from Result where SubjectId=@subjectid and StudentResult>=60) --這一科的經過人數
--if(@passNum>@totalNum/2)
-- begin
print '減分以前'
select * from Result where SubjectId=@subjectid
while(1=1)
begin
  if(@passNum>@totalNum/2)
      update Result set StudentResult-=2 where SubjectId=@subjectid and StudentResult>2
  else
     break;
  set @passNum=(select COUNT(*) from Result where SubjectId=@subjectid and StudentResult>=60) --這一科的經過人數
end  
 --end 
print '減分以後'
select * from Result where SubjectId=@subjectid

--檢查學生「office」課最近一次考試是否有不及格(60分及格)的學生。若有,每人加2分,高於95分的學生再也不加分,直至全部學生此次考試成績均及格(注意加分過程不要違反約束)
go
declare @subjectName varchar(20)='office' --科目名稱
declare @subjectid int=(select subjectid from Subject where SubjectName=@subjectName) --科目ID
declare @time datetime=(select MAX(examdate) from Result where SubjectId=@subjectid) --這一科的最近一次考試時間
--有沒有不及格的人呢?
declare @count int=(select COUNT(*) from Result where SubjectId=@subjectid and ExamDate=@time and StudentResult<60)
while(@count>0) --說明有不及格的人
  begin
 update Result set StudentResult+=2 where SubjectId=@subjectid and ExamDate=@time and StudentResult<=95
 set @count=(select COUNT(*) from Result where SubjectId=@subjectid and ExamDate=@time and StudentResult<60)
  end

二十四.If ...Else

---if  else  也有多重和嵌套  {}----begin..and
--計算平均分數並輸出,若是平均分數超過60分輸出成績最高的三個學生的成績,不然輸出後三名的學生
declare @avg int 
declare @subjectname varchar(20)='office'
declare @subjectid int=(select subjectid from Subject where SubjectName=@subjectname) --也可使用一個sql語句來賦值默認值
select @avg=AVG(studentresult) from Result where SubjectId=@subjectid
print @avg
if @avg>60
  begin 
    select top 3 * from Result where SubjectId=@subjectid order by StudentResult desc
  end
 else
  begin
    select top 3 * from Result where SubjectId=@subjectid order by StudentResult asc
  end 

二十五.全局變量

update Result set StudentResult=80 where StudentNo=7
print @@error --獲得當前執行的語句的錯誤號,若是沒有錯誤,就返回0,若是有錯誤,就是一個非0的值

print @@SERVERNAME --服務器名稱
print @@servicename---服務名稱

update Result set StudentResult=80 where StudentNo=7
select @@ROWCOUNT

select * from Student
select @@ROWCOUNT --全部sql語句都會返回一個受影響的行數

print @@version

---sql卻沒有要求變量的名稱不能以@開頭,因此全局變量的樣子,可是本質仍是一個局部變量
declare @@num int=100
print @@num

 

二十六.事務

-在sql中,每一條語句默認都是一條單獨的批處理語句
update bank set cmoney-=1000 where name='aa'
update bank set cmoney+=1000 where name='bb'
go

--事務:將多和可能修改表數據的操做語句作爲一個統一的總體單元來執行,這個語句塊,要麼都執行,要麼都不執行
---事務是對有可能修改表數據的操做而言:新增,刪除,修改操做。對查詢沒有用
--1.原子性:事務必須是原子工做單元;對於其數據修改,要麼全都執行,要麼全都不執行。--
--2.一致性:事務處理先後,數據必須對應
--3.隔離性:一個事務與另一個事務是相隔離了,二者之間沒有任何的關聯
--4.持久性:事務完成以後,它對於系統的影響是永久性的。該修改即便出現系統故障也將一直保持


--語法步驟:  若是須要使用事務,就只須要將須要執行的sql命令包含在事務在開始和提交之間或者開始和回滾之間
--開始事務:BEGIN TRANSACTION       開啓事務
--事務提交:COMMIT TRANSACTION   --提交操做
--事務回滾:ROLLBACK TRANSACTION --取消操做
go
declare @error int=0
begin transaction
update bank set cmoney-=1000 where name='aa'
set @error=@error+@@ERROR --不去作判斷,只是記住有沒有相應的錯誤號,若是有,就作累加
--if(@@ERROR<>0)  ---不能這樣處理:由於若是再回滾了,那麼其它的語句就沒有包含在事務之內,就是受事務的控制了,那麼就達不到使用事務的初衷
-- rollback transaction
update bank set cmoney+=1000 where name='bb'
set @error=@error+@@ERROR 
--執行完全部的語句後再統一作出判斷 ---秋後算總賬
select * from bank
if(@error<>0) --說明其中一句有錯誤
  rollback transaction
else --沒有錯誤
  commit transaction
select * from bank

二十七.存儲過程

--存儲過程:至關於C#中的方法,能夠傳遞參數,也能夠返回值
--1.存儲過程的分類:
--excute xp_cmdshell  Extends procedure  系統擴充存儲過程
--sp---system procedure   系統存儲過程
--usp:user system procedure  用戶自定義的存儲過程

--2.系統存儲過程:
execute sp_databases
execute sp_renamedb 'mytest','newDB'

 

public void Show(int age)
{
string name;
}

自定義存儲過程:

--usp_getAllStudentInfo 若是存儲過程是sql語句的第一句,那麼也不須要使用exec,直接寫名稱就能夠了
---自定義的存儲過程
--語法:
--go
--create proc 存儲過程的名稱
----參數列表 (輸入參數,輸出參數)
--as
-- begin
--   --模塊化sql語句塊 
-- end 
--go 
--1.沒有返回值,沒有參數,沒有多邏輯結構的存儲過程
if exists(select * from sysobjects where name='usp_getAllStudentInfo')
drop proc usp_getAllStudentInfo
go
create proc usp_getAllStudentInfo
as
 select * from Student
go 
--調用存儲過程獲得全部學員信息
execute usp_getAllStudentInfo

--public void Show(int age)
--{
--string name;
--}
--2.建立帶一個參數的存儲過程
if exists(select * from sysobjects where name='usp_getAllStudentInfobySex')
drop proc usp_getAllStudentInfobySex
go
create proc usp_getAllStudentInfobySex
@sex char(2) ---至關於方法()裏面的參數,能夠由外部傳入值,由存儲過程來使用,不須要使用declare 來聲明 
as
 declare @age int ---局部變量,只有存儲過程內部可使用,至關於方法體內聲明的變量
 select * from Student where Sex=@sex
go
--獲取指定性別的學員信息
exec usp_getAllStudentInfobySex '男'
--建立帶多個參數的存儲過程
if exists(select * from sysobjects where name='usp_getAllStudentInfobySexandclassid')
drop proc usp_getAllStudentInfobySexandclassid
go
create proc usp_getAllStudentInfobySexandclassid
@sex char(2),
@classId int
as
 select * from Student where Sex=@sex and ClassId=@classId
go

exec usp_getAllStudentInfobySexandclassid '女',6

--建立帶多個參數的存儲過程,其中一個參數有默認值
if exists(select * from sysobjects where name='usp_getAllStudentInfobySexandclassid')
drop proc usp_getAllStudentInfobySexandclassid
go
create proc usp_getAllStudentInfobySexandclassid
@classId int,
@sex char(2)='男'--1.當參數有默認值的時候,最好可以將有默認值的參數與在參數列表的最後,這樣作的好處就是你能夠不爲其賦值 
as
 select * from Student where Sex=@sex and ClassId=@classId
go
--1.調用存儲過程的時候,參數是一一對應的,類型對象(至少須要能夠相互轉換)  個數須要對應(若是有默認值,就能夠不傳入實參值),順序對應(第一個實參值永遠爲賦值給第一個形參,無論類型是否OK,若是爲OK,報錯),這裏須要注意是 特別是順序
--2.若是不想考慮順序,那麼就可使用參數=值的方式來調用存儲過程,可是須要注意的是,若是有一個使用了參數=值的方式,那麼建議全部的調用都使用參數=值的方式 
--3.必定須要注意的是:在調用的時候,參數的名稱必定須要和存儲過程當中定義的同樣,若是不同,會給出錯誤的結果
exec usp_getAllStudentInfobySexandclassid  @classid='6',@sex='女'

--建立存儲過程獲得結果集同時返回數量--返回值參數
if exists(select * from sysobjects where name='usp_getAllStudentcountbySexandclassid')
drop proc usp_getAllStudentcountbySexandclassid
go
create proc usp_getAllStudentcountbySexandclassid
--通常狀況下:先寫輸出參數,再寫沒有默認的輸入參數再建立有默認值的輸入參數
@count int output,  ---output至關於c#裏面有out,它會返回一個值,什麼值都沒有所謂
@classId int ,
@sex char(2)='男'
as
select * from Student where Sex=@sex and ClassId=@classId
set @count=(select COUNT(*) from Student where Sex=@sex and ClassId=@classId)
go
--補充:
--1.若是接收返回值的變量有默認值,同時在存儲過程當中沒有對輸出參數從新賦值那麼就終就獲得默認值
--2.可是若是是在存儲過程當中爲輸出參數給默認值,那麼這個值是無效的
declare @cnt int
--返回值必須使用變量去作接收,同時接收的時候必須聲明接收變量也是output,否則不會將值正確的傳遞回來
--exec usp_getAllStudentcountbySexandclassid @classid=6,@count=@cnt output
exec usp_getAllStudentcountbySexandclassid @cnt output, 6, default--沒有指定名稱就是一一對應
print @cnt
print 'aaaaaaaaaaaa'

----建立經過return返回值的存儲過程   return只能reutrn整型類型的數據
if exists(select * from sysobjects where name='usp_getNum')
drop proc usp_getNum
go
create proc usp_getNum
@maleCount int output,
@femaleCount int output
as
declare @totalCount int
select @maleCount=(select COUNT(*) from Student where Sex='男')
select @femaleCount=(select COUNT(*) from Student where Sex='女')
select @totalCount=(select COUNT(*) from Student)
return @totalCount
go

declare @maleNum int,@femaleNum int,@TotalNum int
--set @TotalNum=(exec usp_getNum @maleNum output,@femaleNum output)
exec @TotalNum=usp_getNum @maleNum output,@femaleNum output ---接收return的返回值
print @maleNum
print @femaleNum
print @TotalNum

 

二十八.觸發器

--在grade表是面建立了個觸發器,在你對grade表進行插入操做後觸發,觸發的操做是查詢grade表的數據
if exists(select * from sysobjects where name='tr__insert')
drop trigger tr__insert
go
create trigger tr__insert on grade
after insert 
as 
select * from grade
go

--能夠爲同個操做建立多個after觸發器,可是在觸發的時候只會觸發第一個建立的,同時須要注意:instead of 觸發器每種操做只有一個
if exists(select * from sysobjects where name='tr__insert1')
drop trigger tr__insert1
go
create trigger tr__insert1 on grade
after insert 
as 
select * from student
go
---
insert into Grade values ('觸發器有沒有觸發?')

--inserted表與deleted表
select * from inserted

if exists(select * from sysobjects where name='tr_insert')
drop trigger tr_insert
go
create trigger tr_insert
on grade after insert
as
select * from inserted --操做以後的表--系統會將數據插入到表中,同時也將須要插入的數據存儲到這個內部表中
select * from deleted --操做以前,插入數據以前這個表是空的
go

insert into Grade values ('觸發器有沒有觸發?')

if exists(select * from sysobjects where name='tr_update')
drop trigger tr_update
go
create trigger tr_update on grade after update
as
select * from inserted --操做以後的表--系統會將數據插入到表中,同時也將須要插入的數據存儲到這個內部表中
select * from deleted --操做以前,插入數據以前這個表是空的
go

update Grade set ClassName='abccbaabcdefg' where ClassId=65


if exists(select * from sysobjects where name='tr_delete')
drop trigger tr_delete
go
create trigger tr_delete on grade after delete
as
select * from inserted --操做以後的表--系統會將數據插入到表中,同時也將須要插入的數據存儲到這個內部表中
select * from deleted --操做以前,插入數據以前這個表是空的
go

delete from Grade where ClassId=65


delete from Grade where ClassId>8

--使用觸發器實現惟一鍵約束
go
create trigger tr_unique
on grade after insert
as
declare @id int,@name varchar(50)
select @id=(select classid from inserted),@name=(select classname from inserted)
if((select count(*) from grade where classname=@name)>1)
  --將已經插入的記錄刪除
   begin
  print '已經插入成功了,可是我會將它刪除掉,不要心痛'
  delete from grade where classid=@id
  end
 else
 print 'ok' 
go

insert into Grade values('10期班')

 

 

  三十一.臨時表

--只在當前會話中可使用,離開這個會話就失效
create table #aa
(
 id int not null,
name varchar(50) not null 
)
select * into #aa from grade
insert into #aa select * from grade
select * from #aa

truncate table grade
insert into grade select name from #aa

create table ##aaa
(
 id int not null,
name varchar(50) not null 
)
insert into ##aaa select * from grade
select * from ##aaa

三十二.索引

if exists(select * from sysindexes where name='IX_StudentName')
 drop index student. IX_StudentName
go
create clustered index IX_StudentName
on student(studentname) 

三十三.分頁的存儲過程

if exists(select * from sysobjects where name='usp_getPageData')
drop proc usp_getPageData
go
create proc usp_getPageData
@totalPage int output,
@pageIndex int=1,
@pageCount int=5
as
 select * from (select *,ROW_NUMBER() over(order by studentno) as id from Student) temp where id between (@pageIndex-1)*@pageCount+1 and @pageCount*@pageIndex
 set @totalPage=CEILING((select COUNT(*) from Student)*1.0/@pageCount)
go

exec usp_getPageData 3

 

j_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gifj_0026.gif

相關文章
相關標籤/搜索