Royman.Chen

My DBA + Programmer 成长路 ...

统计

常用链接

Contact Me

阅读排行榜

评论排行榜

2006年1月15日 #

SQL Server 存储过程的分页

摘要: SQLServer存储过程的分页方案比拼SQLServer存储过程的分页,这个问题已经讨论过几年了,很多朋友在问我,所以在此发表一下我的观点建立表:CREATETABLE[TestTable]([ID][int]IDENTITY(1,1)NOTNULL,[FirstName][nvarchar](100)COLLATEChinese_PRC_CI_ASNULL,[LastName][nvarcha...阅读全文

posted @ 2006-01-15 10:24 Royman.Chen 阅读(267) 评论(1) 编辑

触发器 Trigger

Sql-Server 主要提供了两种机制来强制业务规则和数据完整性:约束constrain 和 触发器 trigger ..
触发器在制定的表中发生变化时被调用以响应insert,update,delete事件。
trigger可以查询其他表,包含复杂的T-sql语句,并支持回滚.

每个触发器都会创建两个逻辑专用表inserted 和 deleted表,表结构与被触发作用的表结构相同 执行完毕后 与触发器相关的两个表都会被删除。
当执行insert语句时,inserted表存放要向表中插入的所有行
当执行delete语句时,deleted表存放要向表中删除的所有行
当执行update语句时,相当于执行一个delete操作,再执行一个insert操作,旧的行先被移动到deleted表,然后再在新值插入到inserted表和目标表。


一般的创建trigger : (for / after 语句 触发)

use DB_name
go
--
create trigger trigger_name
on table_Name   // On 关键字
[with Encryption] //加密
for [/ after] {[insert [,]/ update [,] /delete]} --指定触发器只有在触发SQL语句中制定的所有操作都已经成功执行后才激发。(包括所有的级联和约束都检查成功后才能激发)
AS
[SQL-statements]

go 

利用触发器instead of 语句代替其他语句操作 (就不执行instead of 后面指定的SQL指令而执行Trigger体 [sql-statements])

use Db_Name
go
create trigger Trigger_Name
on Table_name
instead 
of [insert,update,delete]
as 
[sql_statements]
go

例如在系统查询分析器执行 update t1 set column_1='aaa' where id=123 可以看到t1下123号的column_1值没有预期改变 而是按照Trigger中 [sql-statements]中的格式走了

如何利用inserted 和 deleted表:
例: 创建一个触发器 要求当插入 删除 更新 T1表数据时 能更新 T2表中相应的C2字段值 (C2是统筹t1的字段)

use DB_Name
go
create trigger T_name
on T1
for update,insert,delete
as 
update T2 set C2=C2+1 where id=(select id from inserted)
update T2 set C2=C2-1 where id=(select id from deleted)
go
//

if update(column)语句 
use DB_Name
go
Alter Trigger Test1
on T1
for update
as
if update(PWD) print 'password has changed..'
go


用来检测修改该字段的操作

ps:允许多个事件insert delete update创建多个触发器 ,且相互不影响



-----
系统操作

改名trigger :    sp_remane Old_Trigger_name,NewTrigger_Name

禁用: Alter Table T1 DISABLE trigger Trigger_Name

查询: select * from sysObjects where Type='TR'



posted @ 2006-01-15 07:58 Royman.Chen 阅读(542) 评论(0) 编辑

2006年1月14日 #

New Msn Messenger 8.0 Beta invitation :)

哇哈哈 收到了MSN 8.0版本的beta邀请函 下载体验了一下 帅啊。。。 


--------------------------------------
Hello!

We'd like you to be among the first to try the beta version of Windows LiveTM Messenger.

If you use MSN?Messenger now, we think you're really going to enjoy Windows Live Messenger. It's as fun and reliable as the Messenger you already know, and you won't lose your contact list or any other personal information when you try this new version. Check it out to experience new ways to connect to your friends via text, voice, video, and more.

                                                                                                 Download now

And in case you were wondering about the name change, Windows Live Messenger is part of a new family of services from Microsoft?that includes Windows Live Mail (now a simpler, safer, faster mail), and Live.com (a new personalized homepage experience), to name just a few.

Thanks,

The Windows Live Messenger team



You are receiving this message from Microsoft because you are a preferred Microsoft customer. We value our relationship with you, however, if you would prefer to no longer receive future research e-mails from Microsoft please follow this link: http://privacy.msn.com/choice/default.asp

Once your request is received, we will take prompt action to ensure you do not receive future research e-mails from Microsoft. Keep in mind that opting out of Microsoft "special offer" or research e-mails does not affect any newsletters you may have requested nor restrict important customer communications concerning your Microsoft products. If you have questions about Microsoft privacy policies, please read our privacy statement at: http://privacy.msn.com/.

Microsoft Corporation, One Microsoft Way, Redmond, WA 98052 ?2005 Microsoft Corporation. All Rights Reserved.


-------------------
来张贴图:
-------------------
MSN8Beta 

界面比较Vista,可以更换背景色 还有QQ共享文件夹的功能  新一代的msn 感觉不错 哈哈。
PS: Beta版本要邀请函激活才能登陆你的id号 听说过两天我就能邀请别人体验,给别人发邀请函了 谁要的可以留言给我。。
过把时髦隐 :-)

posted @ 2006-01-14 02:12 Royman.Chen 阅读(998) 评论(52) 编辑

2006年1月10日 #

*** Procedure 存储过程 ***

无疑,Procedure是一重大课题 。。
对于自定义Procedure,之前没有操作SQL-Server的经验 ,但是写Procedure却是比较熟手的,也是拜高老师所赐吧..
下面是一个之前程序用过的prodecure, 里面包括传入、传出的参数设置。

/****** Object:  Stored Procedure dbo.pro_CheckAdminLogin    Script Date: 2004-6-4 16:37:25 ******/
CREATE PROCEDURE  pro_CheckAdminLogin 
(
@username nvarchar(20),
@password char(32),
@lastloginip char(15),
@output int output
)
 
AS

if exists(Select id from admin where username=@username and password=@password)
    
begin
        
        
update admin set lastLoginIP=@lastloginip,lastlogintime=getdate() where username=@username
        
set @output=0  --验证通过
    end
else
begin
    
set @output=-1  --帐号密码不正确
end

GO
-----------
//
//Alter

Alter procedure Pro_procedureName 
as 
[SQL statements]

//Drop
Drop procedure pro_ProcedureName

通过阅读,了解到MS-SQLserver 的procedure 是可以加密的

Create procedure P_XXX
with encryption
as
[SQL statements]
go


Procedure的好处:
系统有预编译 即编译1次,大大提高效益
某些业务规则可在procedure里面完成, 方便修改(不需改源前后台程序)
当然,还有我才知道的加密--安全性提高 Encryption

SQL-server还提供系统Procedure(sp_)和扩展Procedure(xp_)

利用系统存储过程新添用户的例子:


EXEC sp_addlogin 'LALA_ConnectName','PWD','DB_Name'
go

----

相对也明白了,具有SA权限后 利用扩展Procedure可以操作windows命令行解释器,

Use Master [<--Master数据库是 SQL-server存放系统procedure的数据库]
go
EXEC xp_cmdShell 'dir C:\*.exe'

这个XP_CmdShell就是在SQLserver系统执行命令行的方法

估计前段时间Hack最多的SQL injected,多半是成功注入后利用这里进行下一步权限探讨..

大概的 , Procedure掌握的就这些 需要反复练习 :)

posted @ 2006-01-10 03:59 Royman.Chen 阅读(92) 评论(0) 编辑

2006年1月9日 #

About 视图 (View)

一直对视图都没有好好总结过 , 之前跟高老师牛Oracle时候用得是比较上手了。只是时间流逝 自己也忘得差不多了
回顾了一下:
作用: 其实视图最大的作用无非就是集中多张主表的数据 包括统筹、运算等 方便客户端或前台开发人员直接调用。

use DB_name
go

//Create
//
create view v_ViewName
as
select Column_Name_1 '别名_1', Column_Name_2 '别名_2' from t1,t2
where t1.XXid=t2.XXid and t2.yyId=t1.yyId
Group by Column_nameX

//Alter

Alter view v_ViewName
as 
[select _statements ]

//Drop

Drop view v_ViewName

//查看指定视图sql语句信息

Exec sp_helptext 'v_ViewName'

//查看指定视图的基表信息等

Exec sp_depends 'v_ViewName'


PS:  View不支持Order by ,but Group by ...


posted @ 2006-01-09 08:28 Royman.Chen 阅读(62) 评论(0) 编辑

PetShop 3.0 Database structure

PetShop 3.0 Database structure

1. MSpetshop

MSPetShop_1 

 

2. petshop oders

MSPetshop_2 

download 1.5 pdf

27/12/05 by Royman


posted @ 2006-01-09 07:23 Royman.Chen 阅读(92) 评论(0) 编辑

Structure of "Net Tradding" by Kane

DB structure of Net Tradding by Kane

NetTrading.

27/12/05 by Royman


posted @ 2006-01-09 07:21 Royman.Chen 阅读(47) 评论(0) 编辑

Muti-Tier application design


Muti-Tier application design

Miti-Tier 

Presentation : like HTML / XML , CSS or JS for web

Business Tier : security (like SQL injured), business logic control , validation (like regular expressions) ...

Data Access Tier : supply some interfaces between BT and DB (like  decide to use procedure or SQL connecting string in the programme,use ado.net code to reat/write DB, to control the BT table object or class, CRUD-- create,read,update,delete )

DB Tier : Manage the database, write the procedure in order to improve the performance.

by Royman 29/12/05


posted @ 2006-01-09 07:18 Royman.Chen 阅读(62) 评论(0) 编辑

Wifi Royman!!

Cool!

288RMB = = D-Link DI-624+A  802.11b/g 2.4GHz Wifi Router

wifi 

Of course need the password to access my apartment Wifi service!!!

posted @ 2006-01-09 07:16 Royman.Chen 阅读(148) 评论(0) 编辑

2006年1月8日 #

规则与函数 (Rule & function)

规则与check不同于:
可使用于多列,但每列只允许定义一次
check只用于一列 但一列可定义多个check

可用系统控制面板建立规则 用@x 来定义规则文本

 Function:


//定义相减的两列值函数 求Remain
CREATE FUNCTION CalcRemain (@x decimal(6,0),@y decimal(6,0))
RETURNS decimal (6,0AS  
BEGIN 
 
return (@x-@y)
END

 规则和函数都必须要绑定列才能生效

Function 绑定:

Alter table Table_name
ADD RemainNum [<-This is the Column name I make] 
AS 
 dbo.CalcRemain(Column_1,Column_2)
[数据类型应保持一致] 

go

-----
测试
-----
Select * from Table_name
--返回RemainNum的结果就是Column_1 - Column_2
    ---
    也可直接调系统自定义函数
    Select *,dbo.CalcRemain(Column_1,Column_2)  as RemainNum from Table_name

  总觉得定义规则不如定义Check. 没有具体实战
Function应该是比较常用的 :)

posted @ 2006-01-08 07:45 Royman.Chen 阅读(71) 评论(0) 编辑