温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SQL Server数据库新手入门学习总结(三)

发布时间:2020-08-03 17:58:13 来源:网络 阅读:298 作者:imtcf 栏目:MySQL数据库

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 '数据插入成功'
本文转载,若有疑问请与我们联系https://3pomelos.1688.com/

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI