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.startt |