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

1.索引

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

2.视图

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

创建视图: 

              

创建视图:

create view 视图名

as  

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

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

3.SQL编程

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

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

C、输出:print 变量或字符串

例如:1、查汽车表中名称含有宝马两个字的

declare @name varchar(20) set @name='宝马' select * from car where Name like '%'+@name+'%'

例如:2、查汽车表中所有汽车的平均值并输出

declare @price decimal(10,4) select @price = AVG(Price) from Car print '所有汽车的平均价格为:'+cast(@price as varchar(20))

D、if ... else 的用法(与C#用法相同,只是 if判断条件不需要小括号,花括号变成了begin ...end...else...bigin...end)

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

注意:语句结束之后不要写分号或逗号

原文地址:https://www.cnblogs.com/SJP666/p/4665619.html