sql server,sql

來源:互聯網
上載者:User

sql server,sql
經過一段時間的學習,也對資料庫有了一些認識。
  資料庫基本是由表,關係,操作組成;對於初學者首先要學的:

   1.資料庫是如何儲存資料的
    表,約束,觸發器
   2.資料庫是如何操作資料的
    insert,update,delete T-sql 函數 預存程序 觸發器
   3.資料庫是如何顯示資料的
    select

SQLServer資料庫學習總結

1.SQL基礎

SQL Server2000安裝、配置,伺服器啟動、停止,企業管理器、查詢分析器
第一代資料庫--網狀資料庫和層次資料庫;第二代資料庫--關聯式資料庫
資料庫(DB);資料庫管理系統(DBMS);資料庫系統(DBS)
SQL Server 2000 提供了不同版本:企業版、標準版、個人版、開發版

SQL Server中的資料類型:整數:int,smallint,tinyint,bigint;浮點數:real,float,decimal;二進位:binary,varbinary;邏輯:bit;字元:char,nchar,varchar,nvarchar;文本和圖形:text,ntext,image;日期和時間:datetime,smalldatetime;貨幣:money,smallmoney

資料庫的建立和刪除;資料庫表的建立、修改和刪除

資料完整性:實體完整性:Primary Key,Unique Key,Unique Index,Identity Column;值域完整性:Default,Check,Foreign Key,Data type,Rule;參照完整性:Foreign Key,Check,Triggers,Procedure;使用者定義完整性:Rule,Triggers,Procedure;Create Table中得全部列級和表級約束

SQL Server中有5種約束:主鍵約束(Primary Key Constraint)、預設約束(Default Constraint)、檢查約束(Check Constraint)、唯一性限制式(Unique Constraint)、外鍵約束(Foreign Key Constraint).

關係圖

資料庫設計的步驟:需求分析、概念結構設計、邏輯結構設計、資料庫實體設計、資料庫實施、資料庫運行和維護

兩個實體之間的聯絡:一對一(1:1)、一對多(1:n)、多對多(m:n)

實體關聯模型 -- E-R圖

資料庫正常化:將資料庫的結構精簡為最簡單的形式;從表中刪除冗餘列;標識所有依賴於其他資料庫的資料。

資料庫三範式:第一範式就是無重複的列;第二範式就是非主屬性非部分依賴於主關鍵字;第三範式就是屬性不依賴於其他非主屬性

2.SQL語句
SQL全稱是“結構化查詢語言 (SQL)(Structured Query Language)”

SQL的4個部分:
資料定義語言 (Data Definition Language)DDL(Data Definition Language)用來定義資料的結構:create、alter、drop。
資料控制語言DCL(Data Control Language)用來控制資料庫組件的存取許可、存取許可權等得命令:grant、revoke。
資料操縱語言DML(Data Manipulation Language)用來操縱資料庫中得資料的命令:insert、update、delete。
資料查詢語言DQL(Data Query Language)用來查詢資料庫中得資料的命令:select。

SQL中得運算子:算術運算子、位元運算符、比較子、邏輯運算子、通配運算子、字串串連符、賦值運算子

3.查詢
簡單查詢,使用TOP子句
查詢結果排序order by
帶條件的查詢where,使用算術運算式,使用邏輯運算式,使用between關鍵字,使用in關鍵字,
模糊查詢like
在查詢中使用彙總函式:sum(x),avg(x),min(x),max(x),count(x),count(*)
使用分組查詢group by,having子句
distinct關鍵字
列別名
select top 6 * from sales order by qty desc
select au_id,au_fname,au_lname from authors where state in('ks','ca','mi')
select au_fname,au_lname,phone from authors where au_id like '72[234]-%'
select type,sum(price),avg(price),count(*) from titles group by type having type in('business','psycheology')


簡單子查詢:嵌套子查詢、相互關聯的子查詢;子查詢的select語句中不能使用order by子句,roder by子句只能對最終查詢結果排序。
嵌套子查詢:執行過程,先執行子查詢,子查詢得到的結果不被顯示,而是傳給外層查詢,作為外層查詢的條件,然後執行外層查詢,並顯示結果。
嵌套子查詢的執行不依賴於外層查詢,子查詢只執行一次。
帶有比較子的子查詢,帶有in和not in的子查詢,帶有any或all的子查詢
相互關聯的子查詢:子查詢為外層查詢的每一行執行一次,外層查詢將子查詢引用的列的值傳給了子查詢。
相互關聯的子查詢的執行依賴於外層查詢,子查詢需要重複的執行。
帶有exists和not exists的相互關聯的子查詢。
多表聯結查詢:內聯結(inner join)、外聯結((left、right、full)outer join)、自我聯結(self join)和交叉聯結(cross join)
在查詢上建立新表:select into語句首先建立一個新表,然後用查詢的結果填充新表。
表別名
select coursename from course where courseid in(select distinct courseid from grade where grade>10)
select studname from student where sudbirthday > any (select studbirthday from student where class = '資訊系') and class<>'資訊系'
select studname from student where exists (select * from grade where studid = student.studid and courseid = '01')
select stud1.* from student as stud1 join student as stud2 on stud2.studname = 'mm' and stud1.studsex = stud2.studsex
select * into girls from student where studsex='m'

4.視圖、索引和事務
視圖是由一個或多個資料表(基本表)匯出的虛擬表或者查詢表,是關聯式資料庫系統提供給使用者以多種角度觀察資料庫中資料的重要機制。
視圖的好處:能夠簡化使用者的操作;視圖能夠對機密資料提供安全保護。
建立視圖時,視圖的名稱存在sysobjects表中。有關視圖中所定義列的資訊添加到syscolumns表中,而有關視圖相關性的資訊添加到sysdepends表中。另外,create view語句的文本添加到syscomments表中。
在通過視圖向表中插入資料時,如果insert語句列表中包含有視圖中沒有選擇的列和不允許為空白值的列,這種操作是不允許的。
建立視圖:create view view_employee as select emp_id,fname,lname from employee
使用視圖:select * from view_employee
修改視圖:alter view view_employee as select emp_id,fname,job_id from employee where job_id>10
刪除視圖:drop veiw view_employee
查看視圖結構:exec sp_help view_employee
查看視圖定義資訊:exec sp_helptext 'view_employee'

索引提供了一種基於一列或多列的值對錶的資料行進行快速存取的方法。索引提供的是表中得邏輯順序。
叢集索引基於資料行的鍵值在表內排序和儲存這些資料行。當資料表以某列為關鍵字建立叢集索引時,表中得資料行就以該列(叢集索引鍵)的排序次序進行儲存。每個表只能有一個叢集索引。
非叢集索引具有完全獨立於資料行的結構,一個表可以建立多個非叢集索引。
建立叢集索引:create clustered index studid_ind on stud(studid)
建立非叢集索引:create unique index studfullname_ind on stud(fname desc,lname)
刪除索引:drop index stud.studid_ind
查看stud表上得索引:exec sp_helpindex stud

事務是一種機制,是一個操作序列,它包含了一組資料庫操作命令,並且所有的命令作為一個整體一起向系統提交或撤銷操作請求。
事務的特性:原子性(Atomicity)、一致性(Consistenty)、隔離性(Isolation)、永久性(Durability)。
事務分類:顯示事務、隱性事務、自動認可事務。

視圖、索引和事務的建立、使用、修改和刪除

5.Transact—SQL編程
全域變數:由系統定義和維護,其名稱以@@字元開頭
局部變數:由使用者定義和賦值,其名稱以@字元開頭
輸出語句:print
邏輯控制語句:begin...end ;break ;case ;continue ; goto ; if...else ;return ; while
常用函數:行集合函式,彙總函式,純量涵式
轉換函式:convert(dt,e,s),cast()
數學函數:絕對值abs(n),向上取整ceiling(n),向下取整floor(n),指定次冪power(n,y),四捨五入round(n,length),求符號sign(n),平方根sqrt(n)
日期和時間函數:dateadd(datepart,num,date),datediff(datepart,date1,date2),datename(datepart,date),datepart(datepart,date),getdate(),year(date),month(date),day(date)
字串函數:lower(e),upper(e),left(e,i),right(e,i),replace(s1,s2,s3)用3替換1中的2,replicate(e,i)重複指定次數,stuff(s1,start,length,s2)用2替換1中指定位置,substring(expression,start,length)
中繼資料函數:db_id('database_name'),db_name(datebase_id),object_id('obj_name'),object_name(obj_id),col_length('table','column'),col_name(table_id,col_id)
彙總函式:avg(expr),count(expr),count(*),max(expr),min(expr),sum(expr)
select au_lname,au_fname,contory =
case state
when 'ut' then 'utah'
when 'ca' then 'california'
else 'world'
end,city from authors order by state desc

while(select avg(price) from titles)<30
begin
update titles set price = price * 2
if(select max(price) from titles)>50 break
else continue
end
print '價格太高'

begin
insert into jobs values('a',80,234)
if @@error<>0 print '資料插入失敗'
else goto M
end
M:print '資料插入成功'

6.遊標
遊標是一種能從包含多條資料記錄的結果集中每次提取一條記錄的機制。將批操作變成行操作,對結果集中得某行進行操作。
declare author_csr cursor read_only for --定義唯讀遊標
select au_fname,au_lname from authors where state = 'ca' order by au_fname,au_lname
declare @lname varchar(20),@fname varchar(20) --定義變數
open author_csr --開啟遊標
fetch next from author_csr into @lname,@fname --執行一次資料讀取操作
while @@fetch_status=0 --迴圈遊標讀取資料
begin
print 'author name:'+@lname+''+@fname
fetch next from author_csr into @lname,@fname
end
close author_csr --關閉遊標
deallocate author_csr --釋放遊標

7.預存程序
預存程序(stored procedure)類似c語言中的函數,是一組為了完成特定功能的SQL語句集,經編譯後儲存在資料庫中。使用者通過指定預存程序的名字餅給出參數來執行它。
常用的系統預存程序:sp_database,sp_helpdb,sp_renamedb,sp_tables,sp_column,sp_help,sp_helpconstraint,sp_helpindex,sp_stored_procedure,sp_password
建立預存程序:
create procedure book_num (@book_name varchar(26),@starttime datetime,@endtime datetime,@total int output)
as
select @total=count(jy.askbookid) from book,jyls jy where bookname like @book_name and book.isbn=jy.isbn and jy.starttime>=@starttime and endtime<=@endtime
使用預存程序:
declare @book_name char(26),@total int
set @book_name='物件導向分析和設計'
exec book_num @book_name,'2007-01-01','2007-11-01',@total output
select @book_name as bookname,@total as num

8.觸發器
觸發器是一種特殊類型的預存程序,主要是通過實踐進行觸發而被執行。
觸發器的主要作用就是能夠實現由主鍵和外鍵所不能保證的複雜的參照完整性和資料的一致性。其他功能:強化約束,跟蹤變化,級聯運行,預存程序調用。
SQL Server 2000支援兩種類型觸發器:
after觸發器:要求只有執行某一操作之後,觸發器才被執行,且只能在表上定義。
instead of觸發器:表示並不執行其所定義的操作,而僅是執行觸發器本身。既可以在表上定義,也可以在視圖上定義,但對同一操作只能定義一個instead of觸發器。

工作原理:
當觸發insert觸發器時,新的資料行就會被插入到觸發器表和inserted表中。觸發器通過檢查inserted表來確定是否執行觸發器動作或如何執行。
當在定義有觸發器的表上執行update語句時,原始行被移入到deleted表,更新行被移入inserted表。觸發器檢查deleted表和inserted表以及被更新的表,來確定是否更新了多行以及如何執行觸發器動作。
當觸發deleted觸發器後,從受影響的表中刪除的行將被放置到一個特殊的deleted表中。

create trigger update_smoke_t_sale on smoke_t_sale for update
as
declare @newsalenum int,@smokeproductname varchar(40)
select @newsalenum= salenum from inserted
select @smokeproductname=smokeproductname from inserted
if update(salenum) --判斷是否更新
begin
update smoke_t_sale set saletotalprice=@newsalenum * saleprice where smokeproductname=@smokeproductname
insert into smoke_log(logContent) values('更新成功')
end
else
print '未更新'

9.資料庫進階管理
SQL Server安全體繫結構,4個等級:客戶機作業系統的安全性,SQL Server的登入安全性,資料庫的使用安全性,資料對象的使用安全性
SQL Server驗證模式:windows身分識別驗證模式和混合模式(windows身分識別驗證和SQL Server身分識別驗證)
登入賬戶:使用者登入(串連)SQL Server伺服器的賬戶和密碼。
角色管理:伺服器角色(負責管理和維護SQL Server的組);資料庫角色(是對某個資料庫具有相同存取權限的使用者賬戶和組的集合)
資料庫使用者:對於每個要求訪問資料庫的登入賬戶,必須在要訪問的資料庫中建立該資料庫的訪問賬戶,且與其登入賬戶連結關聯,才可進入該資料庫訪問。
許可權管理:是指使用者是否能進行訪問資料庫資源的相應操作。許可權包括:語句許可權、對象許可權和暗示許可權。

授予許可權:
grant 語句 [...] to 安全賬戶[...]
grant 許可權 [...] on 表或視圖[(列[,...])]|on 預存程序|on使用者自訂函數 to 安全賬戶[,...]
拒絕許可權:
deny 語句 [...] to 安全賬戶[...]
deny 許可權 [...] on 表或視圖[(列[,...])]|on 預存程序|on使用者自訂函數 to 安全賬戶[,...]
撤銷許可權:
revoke 語句 [...] from 安全賬戶[...]
revoke 許可權 [...] on 表或視圖[(列[,...])]|on 預存程序|on使用者自訂函數 from 安全賬戶[,...]

備份和恢複:
Database Backup裝置,在進行Database Backup之前,首先要建立備份裝置。包括:磁碟、磁帶和具名管道
SQL Server 備份策略:只備份資料庫、備份資料庫和交易記錄、差異備份。
backup database medicaldb to disk='medical_bk1' with name='medicaldb backup' description='medicaldb fullbackup' init
restore database medicaldb from medical_bk1

匯入匯出:
DTS(Data Transformation Service)是SQL Server提供的Data Transmission Service。使使用者可以將來自完全不同資料來源的資料析取、轉換併合併到單個或多個目的。

sql server 2008 的密鑰是好多?可以用得起的

SQL Server 2008 序號:
Developer: PTTFM-X467G-P7RH2-3Q6CG-4DMYB
Enterprise: JD8Y6-HQG69-P9H84-XDTPG-34MBB
 
sql server 2008 的密鑰是好多?可以用得起的

SQL Server 2008 序號:
Developer: PTTFM-X467G-P7RH2-3Q6CG-4DMYB
Enterprise: JD8Y6-HQG69-P9H84-XDTPG-34MBB
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.