15-07-20 数据库--索引视图编程

 

1.索引

添加索引,设计界面,在任何一列前右键--索引/键--点击进入添加某一列为索引

2.视图

视图就是我们查询出来的虚拟表

创建视图:create view 视图名

as

SQL查询语句 --分组,排序,in 等都不能写

视图的用法: select * from 视图名

3.SQL编程

定义变量:declare @变量名 数据类型

变量赋值:set @变量名 = 值

输出:print 变量或字符串

查汽车表中名称含有宝马两个字的

declare @name varchar(20)

set @name='宝马'

select * from car where Name like '%'+@name+'%'

查汽车表中所有汽车的平均值并输出

declare @price decimal(10,4)

select @price = AVG(Price) from Car

print '所有汽车的平均价格为:'+cast(@price as varchar(20))

 

if ... else 的用法,if后面没有小括号,花括号用begin end 替代

if 判断条件

begin 

要执行的语句

end

else

begin

要执行的语句

end

C#里的Switch case 变形到数据库里用法

declare @ccname varchar(20)

set @ccname = '宝马'

select * from Car where Name like 

case

when @ccname='宝马' then '%宝马%'

when @ccname='奥迪' then '%奥迪%'

else '%'

end

posted @ 2015-07-22 08:52  断肠人—断肠人  阅读(162)  评论(0编辑  收藏  举报