织梦写php查询语句,织梦dedecms中常用的php查询语句(2)

—————————- 存储过程 ———————————-

1、创建无参数存储过程

语法: create procedure 过程名

as

sql语句体

例题:create procedure p_student

as

select * from student as s join class as c

on s.cl_id=c.cl_id where s.stu_id>2

2、创建带输入参数的存储过程

语法:create procedure 过程名

@参数1 数据类型(=默认值),

@参数2 数据类型(=默认值)

as

select 语句体

例题:create procedure p_student

@name nvarchar(10)

as

select * from student as s join class as c

on s.cl_id=c.cl_id where s.stu_name=@name

3、创建带输入、输出参数的存储过程

语法:create procedure 过程名

@参数1 数据类型 output,

@参数2 数据类型(=默认值)

as

sql 语句体

return

例题:create procedure p_stu_cla

@cname nvarchar(10) output,

@sname nvarchar(10)

as

select @cname=c.cl_name from student as s join class as c

on s.cl_id=c.cl_id where s.stu_name=@sname

return

调用:declare @cname nvarchar(10)

exec p_stu_cla @cname output , ‘王二’

select @cname

4、存储过程的管理

查看存储过程的定义

exec sp_helptext 过程名

查看存储过程的信息

exec sp_help 过程名

删除存储过程

drop procedure 过程名

修改存储过程

alter procedure 过程名

@参数名 数据类型=默认值 output

as

sql 语句

return

————————————- 函数 ————————————–

Sql server2005支持三种用户定义函数:标量函数、内嵌表值函数、多语句表值函数

1、标量函数

标量函数是根据输入参数值的不同来获得不同的函数值,标量函数可以有多个输入参数,但是只能有一个返回值;标量函数体包括一条或多条sql语句,由begin开始,以end 结束;用returns字句定义函数返回值的数据类型,并返回函数值

语法: create function 函数名(标量参数 标量数据类型)

returns 函数返回值的类型

as

begin

函数体

return 变量/标量表达式

end

例题: create function f_count( @sname nvarchar(10) )

returns nvarchar(10)

as

begin

declare @cname nvarchar(10)

select @cname=cl_name from student as s jion class as c

on s.cl_id=c.cl_id where s.stu_name=@sname

return @cname

end

调用函数: declare @name nvarchar(10)

select @name=架构名.f_count(‘王二’)

print @name

2、内嵌表值函数

内嵌表值型函数以返回的都不是一个标量数据,而是一个表,返回的表值函数还可以提供参数化视图功能。

语法: create function 架构.函数名(标量参数 数据类型)

returns table

as

return (select语句)

调用函数:select * from 架构.函数名(参数)

——————————– 约束 ————————————-

SQL server2005 中,用于实现数据完整性的机制有这几种:数据类型、规则和默认值、约束、触发器、XML架构

约束的种类:主键(primary key)约束、外键(foreign key)约束、唯一(unique)约束、核对(check)约束、默认(default)约束

1、主键约束 primary key

直接创建表时创建约束:

create table student

(

sid int identity not null,

sname nvarchar(10),

constraint 主键名 primary key (sid)

)

在已创建表中添加约束:

alter table 表名

add constraint 主键名 primary key (列名)

例如:add constraint pk_id primary key (sid)

删除主键:

alter table 表名

drop constraint 主键名

2、外键约束 foreign key

直接创建表时创建:

create table student

(

id int identity not null,

sname nvarchar(10),

class_id int ,

constraint 外键名 foreign key (class_id) references 其它表(列名)

)

在已创建表中添加:

alter table 表名

add constraint 外键名 foreign key (列名) references 其它表(列名)

例如:add constraint fk_cid foreign key (class_id) references class(class_id)

删除:

alter table 表名

drop constraint 外键名

例如:drop constraint fk_cid

3、唯一约束 unique

直接创建表时创建:

create table student

(

id int identity not null,

sname nvarchar(10) ,

class_id int ,

constraint 唯一约束名 unique (sname)

)

在已创建表中添加:

alter table 表名

add constraint 唯一约束名 unique (列名)

例如:add constraint uni_name unique (sname)

删除:

alter table 表名

drop constraint 唯一约束名

例如:drop constraint uni_name

4、核对约束 check

直接创建表时创建:

create table student

(

id int identity not null,

sname nvarchar(10) ,

class_id int ,

constraint 核对约束名 check (class_id>0 and class_id<4)

)

在已创建表中添加:

alter table 表名

add constraint 核对约束名 check (列名的约束条件)

例如:add constraint che_id unique (class_id>0 and class_id<4)

删除:

alter table 表名

drop constraint 核对约束名

例如:drop constraint che_id

5、默认约束 default

直接创建表时创建:

create table student

(

id int identity not null,

sname nvarchar(10) ,

class_id int constraint 默认约束名 default(默认值)

)

在已创建表中添加:

alter table 表名

add constraint 默认约束名 default (默认值) for 列名

例如:add constraint df_id default (1002) for class_id

删除:

alter table 表名

drop constraint 默认约束名

例如:drop constraint df_id

—————————————- 触发器 ——————————————–

在sql server里面也就是对某个表的一定操作,触发某种条件,从而执行的一段程序。触发器是一个特殊的存储过程。触发器常用于强制业务规则,它是一种高级约束,通过事件进行触发而被执行

常见的触发器有三种:分别应用于insert,update,delete事件

例如两个表:student学生表(id编号,stuid学号,stu_name学生名字)

library借书表(id编号,stu_id学号,book_name书名)

1、update型触发器

create trigger tri_student on student

after update

as

if update(stu_id)

begin

update library set stu_id=i.stuid from library l ,deleted d, inserted i

where l.stu_id=d.stuid

end

2、delete型触发器

create trigger trg_student on student

after delete

as

delete library from library l,deleted d

where l.stu_id=d.stuid

———————————– 级联更新、删除 ————————————-

级联更新、删除是对主键进行的,外键却不能

1、创建级联更新、删除

create table class

(

cid int identity not null,

cname nvarchar(10),

constraint pk_cid primary key(cid)

)

create table student

(

sid int identity not null,

sname nvarchar(10),

cid int ,

constraint fk_cid foreign key (cid) references class (cid)

on delete cascad / on update cascade

)

注:只能对主表class表进行更新、删除时才能实现级联

———————————- 索引 —————————————

索引是的指表中的数据和其相应存储位置的列表。它类似于书中目录,可以快速地查找表中的数据而不必扫描整个数据表。

1、创建聚集索引

create clustered index 索引名

on 表(列名)

2、创建非聚集索引

create nonclustered index 索引名

on 表(列名)

3、创建唯一、非聚集索引

create unique nonclustered index 索引名

on 表(列名)

4、删除索引

drop index 表名.索引名

注意:删除索引时要注意,如果索引是在create table语句中创建的,只能用alter table语句删除。

如果索引是用create index创建的,可用drop index

5、修改索引的名称

sp_rename ‘表名.旧索引名’,‘新索引名’

注意:尽量不要修改索引的名字,容易破坏脚本和存储过程

(责任编辑:最模板)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值