国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

Transact-SQL速查常用手冊

2024-07-21 02:12:19
字體:
供稿:網(wǎng)友

sql基礎(chǔ)知識
1.數(shù)據(jù)庫查詢:select用法
select [top(數(shù)值)] 字段列表 from 數(shù)據(jù)表 [where 條件] [order by 字段] [asc或desc]

2.添加數(shù)據(jù):insert into用法
insert into 數(shù)據(jù)表(字段1,字段2,字段3,…) values(字段1的值,字段2的值,字段3的值,…)

3.刪除數(shù)據(jù):delete用法
delete from 數(shù)據(jù)表 [where 條件]

4.更新數(shù)據(jù):update用法
update 數(shù)據(jù)表 set 字段1=字段值1, 字段2=字段值2, … [where 條件]

5.建數(shù)據(jù)表:create table用法
create table 數(shù)據(jù)表名(字段1名稱,字段1類型,字段2名稱 字段2類型, …)

access數(shù)據(jù)表常用數(shù)據(jù)類型:text,char(number),memo,number,int,date/time,logical,oleobject

自動編號字段添加例子:create table aaa(id int identity (1, 1) not null,abc varchar(25) null)

6.改數(shù)據(jù)表:alter table用法
添加字段:alter table 數(shù)據(jù)表名 add column 字段名 字段類型

刪除字段:alter table 數(shù)據(jù)表名 drop column 字段名

7.刪數(shù)據(jù)表:drop table用法

drop table 數(shù)據(jù)表名


*******************transact_sql********************

alter table [表] alter [id] counter constraint [表_p] primary key 把id列改為自動編號類型,并且設(shè)置為主鍵

--語 句 功 能
--數(shù)據(jù)操作
select --從數(shù)據(jù)庫表中檢索數(shù)據(jù)行和列
insert --向數(shù)據(jù)庫表添加新數(shù)據(jù)行
delete --從數(shù)據(jù)庫表中刪除數(shù)據(jù)行
update --更新數(shù)據(jù)庫表中的數(shù)據(jù)
--數(shù)據(jù)定義
create table --創(chuàng)建一個數(shù)據(jù)庫表
drop table --從數(shù)據(jù)庫中刪除表
alter table --修改數(shù)據(jù)庫表結(jié)構(gòu)
create view --創(chuàng)建一個視圖
drop view --從數(shù)據(jù)庫中刪除視圖
create index --為數(shù)據(jù)庫表創(chuàng)建一個索引
drop index --從數(shù)據(jù)庫中刪除索引
create procedure --創(chuàng)建一個存儲過程
drop procedure --從數(shù)據(jù)庫中刪除存儲過程
create trigger --創(chuàng)建一個觸發(fā)器
drop trigger --從數(shù)據(jù)庫中刪除觸發(fā)器
create schema --向數(shù)據(jù)庫添加一個新模式
drop schema --從數(shù)據(jù)庫中刪除一個模式
create domain --創(chuàng)建一個數(shù)據(jù)值域
alter domain --改變域定義
drop domain --從數(shù)據(jù)庫中刪除一個域
--數(shù)據(jù)控制
grant --授予用戶訪問權(quán)限
deny --拒絕用戶訪問
revoke --解除用戶訪問權(quán)限
--事務(wù)控制
commit --結(jié)束當(dāng)前事務(wù)
rollback --中止當(dāng)前事務(wù)
set transaction --定義當(dāng)前事務(wù)數(shù)據(jù)訪問特征
--程序化sql
declare --為查詢設(shè)定游標(biāo)
explan --為查詢描述數(shù)據(jù)訪問計劃
open --檢索查詢結(jié)果打開一個游標(biāo)
fetch --檢索一行查詢結(jié)果
close --關(guān)閉游標(biāo)
prepare --為動態(tài)執(zhí)行準(zhǔn)備sql 語句
execute --動態(tài)地執(zhí)行sql 語句
describe --描述準(zhǔn)備好的查詢

---局部變量
declare @id char(10)
--set @id = '10010001'
select @id = '10010001'

---全局變量
---必須以@@開頭

--if else
declare @x int @y int @z int
select @x = 1 @y = 2 @z=3
if @x > @y
print 'x > y' --打印字符串'x > y'
else if @y > @z
print 'y > z'
else print 'z > y'

--case
use pangu
update employee
set e_wage =
case
when job_level = ’1’ then e_wage*1.08
when job_level = ’2’ then e_wage*1.07
when job_level = ’3’ then e_wage*1.06
else e_wage*1.05
end

--while continue break
declare @x int @y int @c int
select @x = 1 @y=1
while @x < 3
begin
print @x --打印變量x 的值
while @y < 3
begin
select @c = 100*@x + @y
print @c --打印變量c 的值
select @y = @y + 1
end
select @x = @x + 1
select @y = 1
end

--waitfor
--例 等待1 小時2 分零3 秒后才執(zhí)行select 語句
waitfor delay ’01:02:03’
select * from employee
--例 等到晚上11 點零8 分后才執(zhí)行select 語句
waitfor time ’23:08:00’
select * from employee

***select***

select *(列名) from table_name(表名) where column_name operator value
ex:(宿主)
select * from stock_information where stockid = str(nid)
stockname = 'str_name'
stockname like '% find this %'
stockname like '[a-za-z]%' --------- ([]指定值的范圍)
stockname like '[^f-m]%' --------- (^排除指定范圍)
--------- 只能在使用like關(guān)鍵字的where子句中使用通配符)
or stockpath = 'stock_path'
or stocknumber < 1000
and stockindex = 24
not stocksex = 'man'
stocknumber between 20 and 100
stocknumber in(10,20,30)
order by stockid desc(asc) --------- 排序,desc-降序,asc-升序
order by 1,2 --------- by列號
stockname = (select stockname from stock_information where stockid = 4)
--------- 子查詢
--------- 除非能確保內(nèi)層select只返回一個行的值,
--------- 否則應(yīng)在外層where子句中用一個in限定符
select distinct column_name form table_name --------- distinct指定檢索獨有的列值,不重復(fù)
select stocknumber ,"stocknumber + 10" = stocknumber + 10 from table_name
select stockname , "stocknumber" = count(*) from table_name group by stockname
--------- group by 將表按行分組,指定列中有相同的值
having count(*) = 2 --------- having選定指定的組

select *
from table1, table2
where table1.id *= table2.id -------- 左外部連接,table1中有的而table2中沒有得以null表示
table1.id =* table2.id -------- 右外部連接

select stockname from table1
union [all] ----- union合并查詢結(jié)果集,all-保留重復(fù)行
select stockname from table2

***insert***

insert into table_name (stock_name,stock_number) value ("xxx","xxxx")
value (select stockname , stocknumber from stock_table2)---value為select語句

***update***

update table_name set stockname = "xxx" [where stockid = 3]
stockname = default
stockname = null
stocknumber = stockname + 4

***delete***

delete from table_name where stockid = 3
truncate table_name ----------- 刪除表中所有行,仍保持表的完整性
drop table table_name --------------- 完全刪除表

***alter table*** --- 修改數(shù)據(jù)庫表結(jié)構(gòu)

alter table database.owner.table_name add column_name char(2) null .....
sp_help table_name ---- 顯示表已有特征
create table table_name (name char(20), age smallint, lname varchar(30))
insert into table_name select ......... ----- 實現(xiàn)刪除列的方法(創(chuàng)建新表)
alter table table_name drop constraint stockname_default ---- 刪除stockname的default約束

***function(/*常用函數(shù)*/)***

----統(tǒng)計函數(shù)----
avg --求平均值
count --統(tǒng)計數(shù)目
max --求最大值
min --求最小值
sum --求和

--avg
use pangu
select avg(e_wage) as dept_avgwage
from employee
group by dept_id

--max
--求工資最高的員工姓名
use pangu
select e_name
from employee
where e_wage =
(select max(e_wage)
from employee)

--stdev()
--stdev()函數(shù)返回表達式中所有數(shù)據(jù)的標(biāo)準(zhǔn)差

--stdevp()
--stdevp()函數(shù)返回總體標(biāo)準(zhǔn)差

--var()
--var()函數(shù)返回表達式中所有值的統(tǒng)計變異數(shù)

--varp()
--varp()函數(shù)返回總體變異數(shù)

----算術(shù)函數(shù)----

/***三角函數(shù)***/
sin(float_expression) --返回以弧度表示的角的正弦
cos(float_expression) --返回以弧度表示的角的余弦
tan(float_expression) --返回以弧度表示的角的正切
cot(float_expression) --返回以弧度表示的角的余切
/***反三角函數(shù)***/
asin(float_expression) --返回正弦是float 值的以弧度表示的角
acos(float_expression) --返回余弦是float 值的以弧度表示的角
atan(float_expression) --返回正切是float 值的以弧度表示的角
atan2(float_expression1,float_expression2)
--返回正切是float_expression1 /float_expres-sion2的以弧度表示的角
degrees(numeric_expression)
--把弧度轉(zhuǎn)換為角度返回與表達式相同的數(shù)據(jù)類型可為
--integer/money/real/float 類型
radians(numeric_expression) --把角度轉(zhuǎn)換為弧度返回與表達式相同的數(shù)據(jù)類型可為
--integer/money/real/float 類型
exp(float_expression) --返回表達式的指數(shù)值
log(float_expression) --返回表達式的自然對數(shù)值
log10(float_expression)--返回表達式的以10 為底的對數(shù)值
sqrt(float_expression) --返回表達式的平方根
/***取近似值函數(shù)***/
ceiling(numeric_expression) --返回>=表達式的最小整數(shù)返回的數(shù)據(jù)類型與表達式相同可為
--integer/money/real/float 類型
floor(numeric_expression) --返回<=表達式的最小整數(shù)返回的數(shù)據(jù)類型與表達式相同可為
--integer/money/real/float 類型
round(numeric_expression) --返回以integer_expression 為精度的四舍五入值返回的數(shù)據(jù)
--類型與表達式相同可為integer/money/real/float 類型
abs(numeric_expression) --返回表達式的絕對值返回的數(shù)據(jù)類型與表達式相同可為
--integer/money/real/float 類型
sign(numeric_expression) --測試參數(shù)的正負號返回0 零值1 正數(shù)或-1 負數(shù)返回的數(shù)據(jù)類型
--與表達式相同可為integer/money/real/float 類型
pi() --返回值為π 即3.1415926535897936
rand([integer_expression]) --用任選的[integer_expression]做種子值得出0-1 間的隨機浮點數(shù)


----字符串函數(shù)----
ascii() --函數(shù)返回字符表達式最左端字符的ascii 碼值
char() --函數(shù)用于將ascii 碼轉(zhuǎn)換為字符
--如果沒有輸入0 ~ 255 之間的ascii 碼值char 函數(shù)會返回一個null 值
lower() --函數(shù)把字符串全部轉(zhuǎn)換為小寫
upper() --函數(shù)把字符串全部轉(zhuǎn)換為大寫
str() --函數(shù)把數(shù)值型數(shù)據(jù)轉(zhuǎn)換為字符型數(shù)據(jù)
ltrim() --函數(shù)把字符串頭部的空格去掉
rtrim() --函數(shù)把字符串尾部的空格去掉
left(),right(),substring() --函數(shù)返回部分字符串
charindex(),patindex() --函數(shù)返回字符串中某個指定的子串出現(xiàn)的開始位置
soundex() --函數(shù)返回一個四位字符碼
--soundex函數(shù)可用來查找聲音相似的字符串但soundex函數(shù)對數(shù)字和漢字均只返回0 值
difference() --函數(shù)返回由soundex 函數(shù)返回的兩個字符表達式的值的差異
--0 兩個soundex 函數(shù)返回值的第一個字符不同
--1 兩個soundex 函數(shù)返回值的第一個字符相同
--2 兩個soundex 函數(shù)返回值的第一二個字符相同
--3 兩個soundex 函數(shù)返回值的第一二三個字符相同
--4 兩個soundex 函數(shù)返回值完全相同

quotename() --函數(shù)返回被特定字符括起來的字符串
/*select quotename('abc', '{') quotename('abc')
運行結(jié)果如下
----------------------------------{
{abc} [abc]*/

replicate() --函數(shù)返回一個重復(fù)character_expression 指定次數(shù)的字符串
/*select replicate('abc', 3) replicate( 'abc', -2)
運行結(jié)果如下
----------- -----------
abcabcabc null*/

reverse() --函數(shù)將指定的字符串的字符排列順序顛倒
replace() --函數(shù)返回被替換了指定子串的字符串
/*select replace('abc123g', '123', 'def')
運行結(jié)果如下
----------- -----------
abcdefg*/

space() --函數(shù)返回一個有指定長度的空白字符串
stuff() --函數(shù)用另一子串替換字符串指定位置長度的子串


----數(shù)據(jù)類型轉(zhuǎn)換函數(shù)----
cast() 函數(shù)語法如下
cast() ( as [ length ])
convert() 函數(shù)語法如下
convert() ([ length ], [, style])

select cast(100+99 as char) convert(varchar(12), getdate())
運行結(jié)果如下
------------------------------ ------------
199 jan 15 2000

----日期函數(shù)----
day() --函數(shù)返回date_expression 中的日期值
month() --函數(shù)返回date_expression 中的月份值
year() --函數(shù)返回date_expression 中的年份值
dateadd( , ,)
--函數(shù)返回指定日期date 加上指定的額外日期間隔number 產(chǎn)生的新日期
datediff( , ,)
--函數(shù)返回兩個指定日期在datepart 方面的不同之處
datename( , ) --函數(shù)以字符串的形式返回日期的指定部分
datepart( , ) --函數(shù)以整數(shù)值的形式返回日期的指定部分
getdate() --函數(shù)以datetime 的缺省格式返回系統(tǒng)當(dāng)前的日期和時間

----系統(tǒng)函數(shù)----
app_name() --函數(shù)返回當(dāng)前執(zhí)行的應(yīng)用程序的名稱
coalesce() --函數(shù)返回眾多表達式中第一個非null 表達式的值
col_length(<'table_name'>, <'column_name'>) --函數(shù)返回表中指定字段的長度值
col_name(, ) --函數(shù)返回表中指定字段的名稱即列名
datalength() --函數(shù)返回數(shù)據(jù)表達式的數(shù)據(jù)的實際長度
db_id(['database_name']) --函數(shù)返回數(shù)據(jù)庫的編號
db_name(database_id) --函數(shù)返回數(shù)據(jù)庫的名稱
host_id() --函數(shù)返回服務(wù)器端計算機的名稱
host_name() --函數(shù)返回服務(wù)器端計算機的名稱
identity([, seed increment]) [as column_name])
--identity() 函數(shù)只在select into 語句中使用用于插入一個identity column列到新表中
/*select identity(int, 1, 1) as column_name
into newtable
from oldtable*/
isdate() --函數(shù)判斷所給定的表達式是否為合理日期
isnull(, ) --函數(shù)將表達式中的null 值用指定值替換
isnumeric() --函數(shù)判斷所給定的表達式是否為合理的數(shù)值
newid() --函數(shù)返回一個uniqueidentifier 類型的數(shù)值
nullif(, )
--nullif 函數(shù)在expression1 與expression2 相等時返回null 值若不相等時則返回expression1 的值

  • 本文來源于網(wǎng)頁設(shè)計愛好者web開發(fā)社區(qū)http://www.html.org.cn收集整理,歡迎訪問。
  • 發(fā)表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發(fā)表
    主站蜘蛛池模板: 井陉县| 普定县| 博野县| 肥乡县| 宁城县| 阳新县| 杭锦旗| 弋阳县| 达孜县| 浏阳市| 夏河县| 疏附县| 江山市| 开化县| 枣强县| 盈江县| 东莞市| 广水市| 吉安市| 利川市| 朝阳市| 改则县| 吉安市| 凤庆县| 松溪县| 渝北区| 怀仁县| 安乡县| 景洪市| 平昌县| 石楼县| 织金县| 蒙阴县| 宁安市| 牡丹江市| 潮州市| 伽师县| 宜兴市| 溆浦县| 肃北| 阿荣旗|