SQL培训大纲Word下载.docx

上传人:b****2 文档编号:14938031 上传时间:2022-10-26 格式:DOCX 页数:12 大小:22.08KB
下载 相关 举报
SQL培训大纲Word下载.docx_第1页
第1页 / 共12页
SQL培训大纲Word下载.docx_第2页
第2页 / 共12页
SQL培训大纲Word下载.docx_第3页
第3页 / 共12页
SQL培训大纲Word下载.docx_第4页
第4页 / 共12页
SQL培训大纲Word下载.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

SQL培训大纲Word下载.docx

《SQL培训大纲Word下载.docx》由会员分享,可在线阅读,更多相关《SQL培训大纲Word下载.docx(12页珍藏版)》请在冰豆网上搜索。

SQL培训大纲Word下载.docx

3.SQL事件探查器

跟踪监视数据库

三、数据定义语言(DDL),数据操纵语言(DML)

1.DDL

CREATE,ALTER,DROP

2.DML

SELECT,INSERT,UPDATE,DELETE

四、视图,自定义函数,存储过程,触发器

1.视图createviewviewsheet:

视点集中、简化操作、定制数据、合并分割数据、安全性

ifexists(select*fromsysobjectswhereid=object_id('

viewsheet'

)andtype='

v'

dropviewviewsheet

go

createviewviewsheet

as

selecta.id,a.[name],b.[bname],b.[price]fromstudentsa,booksb

2.自定义函数createfunctionmyfun(@iint):

自定义函数主要用于数据的计算,功能性很强,不能在函数体内对数据库进行insert,update,delete操作

alterfunctionmy_function(@ivarchar)

returnsint

Begin

Declare@varmyvarchar(50)

Set@varmy='

12345'

+@i

return@varmy

End

调用自定义函数

selectdbo.my_function

(1)asa,*fromstudents

3.存储过程:

允许标准组件式编程、能够实现较快的执行速度、能够减少网络流量、可被作为一种安全机制来充分利用

创建存储过程

createprocliyue

@idint,

@bunamevarchar(40)

AS

BEGIN

insertintostudents(id,name)values(@id,@buname)

select*fromstudents

updatestudentssetname='

14'

wherename='

gdf'

deletefromstudentswherename='

sss'

END

调用存储过程

execliyue@id=11,@buname='

1.触发器:

必须清楚inserted,deleted的意义

inserted表和deleted表用于存放对表中数据行的修改信息。

他们是触发器执行时自动创建的,放在内存中,是临时表。

当触发器工作完成,它们也被删除。

它们是只读表,不能向它们写入内容。

触发器里面的两个临时的表:

Deleted,Inserted。

注意Deleted与Inserted分别表

示触发事件的表“旧的一条记录”和“新的一条记录”。

五、工作中常用的知识点

1.innerjoin,leftjoin,rightjoin

2.selectintofrom,insertintoselectfrom

INSERTINTOSELECT语句复制表数据

InsertintoTable2(a,c,d)selecta,c,5fromTable1

InsertintoTable2(field1,field2,...)selectvalue1,value2,...fromTable1

 

要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。

SELECTINTOFROM语句创建表Table2并复制数据

selecta,cintoTable2fromTable1

selectvale1,value2intoTable2fromTable1

要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。

3.IDENTITY

标识列,这一列将自动编号:

只能在新建表,或者修改列时才能设置

createtabletb(idintidentity(1,1))

alerttabletbaddkidintidentity(1,1)

例如identity(1,1)

表示这一列将自动从1开始编号,每插入一行,这一列就增1,并且插入数据时不能手动为这列插入,这列的值是系统自动插入的

@@rowcount(全局变量,统计受影响的行数;

只对它上一个操作有影响)

4.updateaseta.col1=b.col1 

frombwherea.col2=b.col2(将那些满足在a表中a.col2=b.col2条件的数据修改为满足在a表中a.col1=b.col1条件的数据从b表中读取)

5.利用临时表来做循环运算,exec('

动态SQL语句'

利用临时表来做循环运算

createtable#temp01(idintidentity(1,1),cntint)

begin

declare@iint

set@i=1

while(@i<

100)

begin

insertinto#temp01(cnt)values(@i)

set@i=@i+1

end

end

exec('

1:

普通SQL语句可以用Exec执行

eg:

Select*fromtableName

Exec('

select*fromtableName'

Execsp_executesqlN'

--请注意字符串前一定要加N

2:

字段名,表名,数据库名之类作为变量时,必须用动态SQL

declare@fnamevarchar(20)

set@fname='

'

Select@fnamefromtableName 

--错误,不会提示错误,但结果为固定值,并非所要。

Exec('

select'

+@fname+'

fromtableName'

) 

--请注意加号前后的单引号的边上加空格

当然将字符串改成变量的形式也可

--设置字段名

declare@svarchar(1000)

set@s='

Exec(@s) 

--成功

execsp_executesql@s 

--此句会报错,@s不是nvarchar类型

declare@sNvarchar(1000) 

--注意此处改为nvarchar(1000)

--成功 

--此句正确

3.输出参数

declare@numint,

@sqlsnvarchar(4000)

set@sqls='

selectcount(*)fromtableName'

exec(@sqls)

--如何将exec执行结果放入变量中?

declare@numint,@sqlsnvarchar(4000)

select@a=count(*)fromtableName'

execsp_executesql@sqls,N'

@aintoutput'

@numoutput--定义@a为输出变量,@num获取变量

select@num

6.Exists,noexists,

7.Begintrans,commit,rollback

8.Xact_abort,数据类型转换convertconvert(varchar

(2),datepart(hh,getdate()))+convert(varchar

(2),datepart(mi,getdate()))+convert(varchar

(2),datepart(ss,getdate()))convert(varchar(100),getdate(),23)

selectconvert(varchar(20),getdate(),120)

selectconvert(decimal(7,2),'

111.00'

castcast('

123'

asint),while,if(if@age>

12begin…endelsebegin…end),likelike‘%M%’

9.Altertablealtercolumn,altertableadd,altertabledropcolumn

Altertablealtercolumn修改表的某列的定义altertableUsersaltercolumnbnchar

(2)

altertableadd为表添加新的列altertableUsersaddaint

altertabledropcolumn删除表的某列altertableUsersdropcolumna

10.dateadd,datediff,datename,datepart,day,getdate,monty,year

dateadd()函数计算一个日期通过给时间间隔加减来获得一个新的日期

dateadd(yy,1,'

2008-10-1'

年yy,yyyy;

季度qq,q;

月mm,m;

年中的日dy,y;

日dd,d;

周wk,ww;

星期dw,w;

小时hh;

分钟mi,n;

秒ss,s;

毫秒ms;

微妙mcs;

纳秒ns。

Datediff()函数计算两个日期之间的小时、天、周、月、年等时间间隔总数。

datediff(datepart,startdate,end

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 科学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1