




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、1.sql創(chuàng)建數(shù)據(jù)庫的語法; 【語法】 create database database-name 【舉例】 create database my_rsgl (創(chuàng)建:人事管理庫) 【說明】 本代碼用于創(chuàng)建數(shù)據(jù)庫,可在“企業(yè)管理器”中執(zhí)行,也可以在“查詢分析器”中執(zhí)行,也可以在delphi等開發(fā)工具中執(zhí)行。 以下將要講到的語法都有多種執(zhí)行方式,下文不再復(fù)述。 2.sql刪除數(shù)據(jù)庫的語法; 【語法】 drop database&
2、#160;database-name 【舉例】 drop database my_rsgl (刪除:人事管理庫) 【說明】 刪除數(shù)據(jù)庫之前,必須確認(rèn)數(shù)據(jù)庫是否真的要刪除,如果數(shù)據(jù)庫中還包含著重要的數(shù)據(jù),刪除之后就后悔莫及,所以不要隨意操作這個命令。3.sql備份、恢復(fù)數(shù)據(jù)庫的語法; a、怎樣備份? 【語法】 backup database database-name to disk=路徑 【舉例】 backup databas
3、e my_rsgl to disk= 'f:back1.bak ' 【說明】 備份數(shù)據(jù)庫時,除指定有效的硬盤路徑以外,注意還要取一個文件名。 B、怎樣恢復(fù)? 【語法】 use master restore database database-name from disk=路徑 with replace 【舉例】 use master restore d
4、atabase my_rsgl from disk='f:back1.bak ' with replace 【說明】 恢復(fù)數(shù)據(jù)庫時,在后面加上:with replace,是表示按“完全恢復(fù)”的方式進(jìn)行恢復(fù)。 4.sql創(chuàng)建表的語法; 【語法】 create table tabname(col1 type1 not null primary key,col2
5、type2 not null,.) 【舉例】 create table 人員表 (工號 nvarchar (50) collate chinese_prc_ci_as null,姓名 nvarchar (50) collate chinese_prc_ci_as null,性別 nvarchar (2) collate chinese_prc_ci_as null)
6、on primary 【說明】 代碼中,表名、字段名和數(shù)據(jù)類型都要寫在“”里面,如果字段的類型是字符型,都必須在“()”里面指定長度。 5.sql刪除表的語法; 【語法】 drop table table-name 【舉例】 drop table 人員表 (刪除:人員表) 【說明】 刪除表以及表中的全部數(shù)據(jù),注意在sql server里面,刪除表是真正的刪除,不是偽刪除。 6.sql向表中增加一個“列”
7、的語法; 【語法】 alter table table-name add column col type 【舉例】 alter table 人員表 add 年齡 nvarchar(10) null【說明】 增加的字段必須指定數(shù)據(jù)類型,如果是字符型的,必須指定長度。 附:刪除列的語法: alter table 人員表 drop column 性別&
8、#160;附:修改某列的數(shù)據(jù)類型的語法: alter table 工資表 alter column 工號 varchar(100) 7.sql給表中的字段增加“主鍵”的語法; a、怎樣增加主鍵? 【語法】 alter table-name add primary key (col) 【舉例】 alter table 人員表 add primary
9、0;key (工號) 【說明】 不能在可以為“空”的字段上增加主鍵,否則會報錯。 B、怎樣刪除主鍵?【語法】 alter table-name drop 主鍵約束 【舉例】 alter table 人員表 drop pk_人員表 8.sql給表中的字段創(chuàng)建“索引”的語法; 【語法】 create unique index idxname on tabname(co
10、l.) 【舉例】 create unique index index_gh on 人員表(工號) 【說明】 跟字段創(chuàng)建索引后,可用于查詢語句中,提高檢索速度。 9.sql刪除表中某個“索引”的語法; 【語法】 drop index idxname 【舉例】 drop index 人員表.index_gh 【說明】 注意索引一旦建立后,不能
11、修改,只能先刪除了重新建立。 10.sql創(chuàng)建視圖的語法; 【語法】 create view viewname as select statement 【舉例】 create view v_ryb as select * from 人員表 【說明】 不引用變量的查詢語句,都可以用代碼創(chuàng)建視圖。11.sql刪除視圖的語法; 【語法】 drop
12、view viewname 【舉例】 drop view v_ryb 12.sql向表中插入數(shù)據(jù)的語法; 【語法】 insert into table1(field1,field2) values(value1,value2) 【舉例】 insert into 人員表(工號,姓名,性別) values ('gh006','黃剛',
13、9;男') 【說明】 只有給選定字段插入數(shù)據(jù)時,才需要在表名的后面用“()”來排列字段名。 13.sql修改表中某條數(shù)據(jù)的語法; 【語法】 update 表名 set field1=value1 where 范圍 【舉例】 update 人員表 set 姓名= '洪七公' where 工號= 'gh001' 【
14、說明】 寫修改語句時,注意一般要指定where條件語句,否則就是對整個表的所有記錄作修改,可能產(chǎn)生預(yù)期之外的嚴(yán)重后果。 14.sql刪除表中某條數(shù)據(jù)的語法; 【語法】 delete from 表名 where 范圍 【舉例】 delete from 人員表 where 工號= 'gh002' 【說明】 如果沒寫where條件語句,那么刪除的就是整個表的全部記錄。&
15、#160; 15.sql查詢表中數(shù)據(jù)的語法; 【語法】 select * from 表名 where 范圍 【舉例】 查出全部字段:select * from 人員表 where 工號= 'gh001' 只查出指定字段:select 工號,姓名 from 人員表 where 工號= 'gh001' 【說明】
16、0;“*”是通配符,表示查出全部字段。 16.sql排序、求總、求和、求平均、取最大、取最小的語法; a、排序 【語法】select * from 表名 order by field1,field2 desc 【舉例】 select * from 人員表 order by gh,xm desc (降序) select * from&
17、#160;人員表 order by gh,xm asc (升序) b、求總 【語法】 select count as totalcount from 表名 【舉例】 select count(*) as 記錄數(shù) from 人員表 c、求和 【語法】 select sum(字段) as sumvalue&
18、#160;from 表名 【舉例】 select sum(工資) as 工資合計 from 人員表 d、求平均 【語法】 select avg(字段) as avgvalue from 表名 【舉例】 select avg(工資) as 平均工資 from 人員表 e、取最大 【語法】 select ma
19、x(字段) as maxvalue from 表名 【舉例】 select max(工資) as 最大工資 from 人員表 f、取最小 【語法】 select min(字段) as minvalue from 表名 【舉例】 select min(工資) as 最小工資 from 人員表 17.sql u
20、nion運(yùn)算符的用法; 【作用】 union 運(yùn)算符通過組合其他兩個結(jié)果表(例如 table1 和 table2)并消去表中任何重復(fù)行而組合生出一個結(jié)果表。當(dāng) all 隨 union 一起使用時(即 union all),不消除重復(fù)行。兩種情況下,組合表的每一行不是來自 table1 就是來自 table2。 【舉例】 不消除重復(fù)行的寫法: select gh,xm,xb
21、from 人員表1 union all select gh,xm,xb from 人員表2消除重復(fù)行的寫法: select gh,xm,xb from 人員表1 union select gh,xm,xb from 人員表2【說明】 用來組合的所有表的字段必須一致、或參與組合的表的字段必須一致。18.sql except運(yùn)算符的用法; 【作用】 except 運(yùn)算符通過包括所有在
22、0;table1 中但不在 table2 中的行、并消除所有重復(fù)行而派生出一個結(jié)果表。當(dāng) all 隨 except 一起使用時 (except all),不消除重復(fù)行。 【舉例】 select gh,xm,xb from 人員表1 except all select gh,xm,xb from 人員表2 【說明】 本運(yùn)算符在sql
23、server 2000中不支持,sql server 2005版本及后續(xù)版中支持。 19.sql intersect運(yùn)算符的用法; 【作用】 intersect 運(yùn)算符通過只包括 table1 和 table2 中都有的行、并消除所有重復(fù)行而派生出一個結(jié)果表。當(dāng) all 隨 intersect 一起使用時 (intersect all),不消除重復(fù)行。 【舉例】
24、;select gh,xm,xb from 人員表1 intersect all select gh,xm,xb from 人員表2 【說明】 本運(yùn)算符在sql server 2000中不支持,sql server 2005版本及后續(xù)版中支持。 20.sql left (outer) join 語法; 【作用】 左外連接(左連接):結(jié)果集既包
25、括連接表的匹配行,也包括左連接表的所有行。 【舉例】 假設(shè)有這樣兩個表: 表一: - 姓名 備注 Aa 1111111 Bb 2222222 Cc
26、0;3333333 表二: - 姓名 年齡 性別Aa 22 男 Tt 33 女 Cc 44
27、60;男 Gg 55 女 對于左外連接: Select a.*,b.* From 表一 as a left join 表二 as b On a.姓名=b.姓名 則查詢的結(jié)果為: - 姓名
28、; 備注 年齡 性別 Aa 1111111 22 男 Bb 2222222 null null
29、 Cc 333333 44 男 21.sql right(outer) join語法; 【作用】 右外連接(右連接):結(jié)果集既包括連接表的匹配連接行,也包括右連接表的所有行。 【舉例】 假設(shè)有這樣兩個表: 表一: - 姓
30、名 備注Aa 1111111 Bb 2222222 Cc 3333333 表二: - 姓名 年齡 性別 Aa
31、 22 男 Tt 33 女 Cc 44 男 Gg 55 女
32、 對于右外連接: Select a.*,b.* From 表一 as a right join 表二 as b On a.姓名=b.姓名 則查詢的結(jié)果為: - 姓名 備注 年齡 性別 Aa
33、; 1111111 22 男 Tt null 33 女 Cc 333333
34、160; 44 男 Gg null 55 女 22.sql full/cross (outer) join語法; 【作用】 全外連接:不僅包括符號連接表的匹配行,還包括兩個連接表中的所有記錄。
35、 【舉例】select a.*,b.* From 表一 as a full outer join 表二 as b On a.姓名=b.姓名 23.sql group by分組的語法; 【語法】 select 字段,sum(字段) as 字段名 from 表名 group
36、0;by 字段 【舉例】 select 月份,sum(工資) as 月工資合計 from 人員表 group by 月份 【說明】 分組字段不能是:text,ntext,image類型的字段。 24.sql分離數(shù)據(jù)庫的語法; 【語法】 exec sp_detach_db '數(shù)據(jù)庫名', 'true' 【舉例】 exec sp_deta
37、ch_db '人事管理庫', 'true' 25.sql附加數(shù)據(jù)庫的語法; 【語法】 exec sp_attach_db dbname='數(shù)據(jù)庫名',filename1=n'c:program filessql server路徑名+.mdf',filename2=n'.填寫路徑.+.ldf' 【舉例】 例1: exec sp_attach_db dbname=n
38、39;人事管理庫' filename1=n'c:人事管理庫.mdf', filename2=n'c:人事管理庫.ldf' 例2: exec sp_attach_db dbname=n'my_rsgl', filename1=n'c:program filesmicrosoft sql servermssqldatamy_rsgl.mdf', filename2=n'c:program filesm
39、icrosoft sql servermssqldatamy_rsgl_log.ldf' 26.sql修改數(shù)據(jù)庫名稱的語法; 【語法】 exec sp_renamedb 'old_name', 'new_name' 【舉例】 exec sp_renamedb '人事管理庫', '人員管理庫' 【說明】 注意此修改只是改變了數(shù)據(jù)庫的名稱,而不是它在硬盤上的文件名。2
40、7.sql between的用法; 【作用】 between限制查詢數(shù)據(jù)范圍時包括了邊界值,not between不包括 【語法】 select * from table1 where time between time1 and time2 select a,b,c from table1 where a not between 數(shù)值1 and&
41、#160;數(shù)值2 【舉例】 select * from 人員表 where (日期 between '1997-12-01' and '2012-06-01') select * from 人員表 where (年齡 not between 30 and 40) 【說明】 用來比較的字段要求是數(shù)值類型的
42、字段或日期型字段。 28.sql in的用法; 【作用】 查找“在”或“不在”指定范圍的數(shù)據(jù)。 【語法】 select * from table1 where a not in ('值1','值2','值4','值6') 【舉例】 select * from 人員表 where (年齡 no
43、t in ('20', '30', '50', '60') select * from 人員表 where (年齡 in ('20', '30', '50', '60') 【延伸】 select * from 人員表 where
44、工號 in (select 工號 from 工資表) select * from 人員表 where 工號 not in (select 工號 from 工資表) 【說明】 in語句的作用很大,但是用于比較的數(shù)據(jù)越多,得出結(jié)果的速度就越慢。 29.sql top的用法; 【作用】 查找指定范圍的數(shù)據(jù)的前幾條,默認(rèn)從首條開始返回。 【語法】
45、0;select top n * from table1 (n 表示條數(shù),用整數(shù)) 【舉例】 select top 3 * from 人員表 【延伸】 select top 2 * from 人員表 where (性別 = '女') 【說明】 top語句適合用在提取少量數(shù)據(jù)用于顯示、或需要提取指定條數(shù)的記錄。30
46、.sql exists的用法; 【作用】 查找當(dāng)前表在別的表中“存在”或“不存在”的記錄。 【語法】 select * from table1 where not exists ( select * from table2 where table1.field1=table2.field1 ) 【舉例】 select * from 人員表 wher
47、e not exists ( select * from 工資表 where 人員表.工號=工資表.工號 ) 31.sql truncate table的用法; 【作用】 刪除表中的全部數(shù)據(jù),而且不寫日志。 【語法】 truncate table table1 【舉例】 truncate table 工資表 【說明】 這個命
48、令與delete的作用相同,但delete要寫日志,而且delete 語句后面可以使用where條件語句。 32.sql 收縮數(shù)據(jù)庫的語法; 【語法】 dbcc shrinkdatabase (database_name, target_percent) 【舉例】 Dbcc shrinkdatabase (userdb, 10) go 【說明】 上面的代
49、碼是將 userdb 用戶數(shù)據(jù)庫中的文件減小,以使 userdb 中的文件有 10% 的可用空間。 33.sql檢查備份集的語法; 【語法】 restore verifyonly from disk=路徑 【舉例】 restore verifyonly from disk='f:back1.bak' 【說明】 驗(yàn)證備份但不還原備份。檢查備份集是否完整以及所有卷是否都可
50、讀。但是,restore verifyonly 不嘗試驗(yàn)證備份卷中的數(shù)據(jù)結(jié)構(gòu)。如果備份有效,則 microsoft sql server 2000 返回此消息:"備份集有效"。34.sql修復(fù)數(shù)據(jù)庫的語法; 【語法】 alter database database-name set single_user go dbcc checkdb(database-name,repair_allow_data_loss)
51、60;with tablock go alter database database-name set multi_user go 【舉例】 alter database 人事管理庫 set single_user go dbcc checkdb('人事管理庫',repair_allow_data_loss) with tablock go alter data
52、base人事管理庫 set multi_user go 【說明】 檢查指定數(shù)據(jù)庫中的所有對象的分配和結(jié)構(gòu)完整性、并修復(fù)錯誤,alter database 提供了更改數(shù)據(jù)庫名稱、文件組名稱以及數(shù)據(jù)文件和日志文件的邏輯名稱的能力。 35.sql清除日志的寫法; 【舉例】 if exists (select * from sysobjects where id = object
53、_id(n'dbo.truncate_log_file') and objectproperty(id, n'isprocedure') = 1) drop procedure dbo.truncate_log_file go set quoted_identifier off set ansi_nulls on go
54、160; create procedure truncate_log_file as set nocount on declare logicalfilename sysname, maxminutes int,
55、160; newsize int select logicalfilename = 'xxxx_log', - 這里的xxxx_log替換成泥自己數(shù)據(jù)庫的log名 maxminutes = 10, &
56、#160; newsize = 10 declare originalsize intselect originalsize = size from sysfiles where name =
57、160;logicalfilename declare counter int, starttime datetime, trunclog varchar(255) while
58、0;originalsize*8/1024>newsize begin create table dummytrans (dummycolumn char (8000) not null) select starttime = getdate(),
59、 trunclog = 'backup log '+ db_name() + ' with truncate_only' dbcc shrinkfile (logicalfilename, newsize) exec (trunclog) &
60、#160;while maxminutes > datediff (mi, starttime, getdate() and originalsize = (select size from sysfiles where name = logicalfilename)
61、 and (originalsize * 8 /1024) > newsize begin select counter = 0
62、;while (counter < originalsize / 16) and (counter < 5000) begin insert dummytrans values ('fil
63、l log') delete dummytrans select counter = counter + 1 end
64、160; exec (trunclog) end select originalsize=size from sysfiles where name = logicalfilen
65、ame drop table dummytrans end set nocount off go set quoted_identifier off set ansi_nulls on go 【說明】 這段代碼包含著多種語法,在使用的時候把數(shù)據(jù)庫換成你要清除日志的數(shù)據(jù)庫,也就是說,這段代碼是通用的。
66、;36.sql循環(huán)語句的寫法; 【舉例】 declare i intset i=1 while i<30 begin insert into test (userid) values(i) set i=i+1 end 【說明】 while 就是循環(huán)指令,sql的循環(huán)語句一般寫在存儲過程當(dāng)中;也可以在查詢分析
67、器中直接執(zhí)行。 37.sql比較a表和b表是否相等的寫法; 【舉例】 if (select checksum_agg(binary_checksum(*) from a表) = (select checksum_agg(binary_checksum(*) from b表) print '相等' else print '不相等' 【說明】 兩個表的“記錄數(shù)”和“內(nèi)
68、容”完全一致,才相等,內(nèi)容有不同則不相等。 38.sql將本地表數(shù)據(jù)導(dǎo)入到遠(yuǎn)程表的寫法; 【語法】 insert opendatasource('sqloledb', 'data source=遠(yuǎn)程ip;user id=sa;password=密碼' ).庫名.dbo.表名 (列名1,列名2) select 列名1,列名2 from 本地表名 【舉例】 insert opendat
69、asource('sqloledb', 'Data source=;user id=sa;password=888' ).my_rsgl.dbo.人員表 (工號,姓名) select 工號,姓名 from 人員表 39.sql將遠(yuǎn)程表數(shù)據(jù)導(dǎo)入到本地表的寫法; 【舉例】 exec sp_addlinkedserver server = &
70、#39;' exec sp_addlinkedsrvlogin rmtsrvname = '', useself = false , locallogin = 'sa', rmtuser = 'sa' , rmtpassword ='sql' select&
71、#160;* into xxx01 from .人事管理庫.dbo.人員表 【說明】 前面創(chuàng)建的鏈接服務(wù)器,如果不需要了,刪除的語法是: exec sp_dropserver '', 'droplogins' 1. 演示用代碼建立數(shù)據(jù)庫、表和字段的過程; 【代碼】 1)建數(shù)據(jù)庫:create database my_gzgl 2)建數(shù)據(jù)表: creat
72、e table 人員表 (工號 nvarchar (50) collate chinese_prc_ci_as null,姓名 nvarchar (50) collate chinese_prc_ci_as null,性別 nvarchar (2) collate chinese_prc_ci_as null) on primary 2. 演示用代碼建立視圖的過程; 【代碼】 create view v_ryb as select 工號,姓名 from 人員表 3. 演示用代碼建立存儲過程、觸發(fā)器的過程; 【代碼】 1)建存儲過程: create procedure myccgc
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 高中語文情感美文藝藝
- 音樂類視頻產(chǎn)品的創(chuàng)新制作與推廣
- 初中語文文摘職場阿蘇的小雪
- 四年級數(shù)學(xué)上冊可能性教案蘇教版
- 高中語文課外古詩文歸莊送顧寧人北游序原文及翻譯
- 部編版四年級下冊道德與法治全冊教案
- 跨國企業(yè)知識產(chǎn)權(quán)糾紛解決與司法管轄探討
- 海南2025年01月??谑协偵絽^(qū)事業(yè)單位(綜合類)2025年度公開招考65名工作人員(第一號)筆試歷年典型考題(歷年真題考點(diǎn))解題思路附帶答案詳解
- 跨境B2B電商平臺運(yùn)營模式探討
- 養(yǎng)殖灘涂合同范本
- 2025年上半年永春縣農(nóng)文旅發(fā)展集團(tuán)限公司公開招聘若干名工作人員易考易錯模擬試題(共500題)試卷后附參考答案
- 家庭康復(fù)服務(wù)的商業(yè)價值與發(fā)展趨勢
- 2025年?;髽I(yè)安全教育培訓(xùn)計劃
- 《HR的成長之路》課件
- 2025年山東浪潮集團(tuán)有限公司招聘筆試參考題庫含答案解析
- 2018NFPA10便攜式滅火器標(biāo)準(zhǔn)
- 裝修完成情況報告范文
- 2024-2024年上海市高考英語試題及答案
- 雙線性濾波器與圖像去噪-洞察分析
- 酒店住宿服務(wù)合同三篇
- 衛(wèi)生監(jiān)督協(xié)管員培訓(xùn)課件
評論
0/150
提交評論