触发器--再次出发

       关于触发器其实在机房重构中我们就已经涉及到了,有的用的是触发器有的使用存储过程。现在在牛腩中我们又遇到了使用触发器。让我们再次出发看看究竟怎么使用。                 

    在牛腩视频中要实现对删除类别的操作,我们使用一般的SQL语句去删除会出现这样的错误:

    { DELETE语句与REFERENCE约束“FK_news_category”冲突。该冲突发生于数据库“newssystem”,表“dbo.news” 语句终止;}

    出现这种错误的原因就是各表直接有主键外键约束 ,如数据库关系图:

                  

       首先我们知道SQL Server 包括三种常规类型的触发器:DML 触发器、DDL 触发器和登录触发器。                  

     要想完美删除类别表下的新闻以及评论就用到了触发器。当数据库中表中的数据发生变化时,包括insert,update,delete任意操作,我们对该表写对应的DML触发器,该触发器自动执行所以这里我们用的触发器应该叫DML触发器。

                 

<span style="font-family:KaiTi_GB2312;">-- =============================================
-- Author:		刘伟
-- Create date: <2014-9-22  11:28
-- Description:	删除类别触发器
-- =============================================
ALTER TRIGGER [dbo].[trigCategoryDelete]
   ON [dbo].[category]
   instead of DELETE
AS 
BEGIN
	declare @caId int
	select @caId=id from deleted
	--删除评论
	delete comment where newsId in(select newsId from news where caId=@caId )
	--删除新闻
	delete news where caId=@caId
	--删除类别
	delete category where id=@caId

END</span>


    小结:使用 DML触发器可通过数据库中的相关表实现级联更改,通过级联引用完整性约束可以更有效地执行这些更改。触发器与存储过程的唯一区别是触发器不能执行EXECUTE语句调用,而是在用户执行Transact-SQL语句时自动触发执行。





可以使用如下代码实现: ```python import carla # 定义汽车循环运动的起点和终点 start_point = carla.Location(x=100, y=0, z=0) end_point = carla.Location(x=200, y=0, z=0) # 定义创建触发器的函数 def create_trigger(world, start_point, end_point, npc_actor): # 创建触发器形状和位置 box = carla.BoundingBox(carla.Location(x=(start_point.x + end_point.x) / 2, y=(start_point.y + end_point.y) / 2, z=start_point.z), carla.Vector3D(abs(end_point.x - start_point.x), abs(end_point.y - start_point.y), 2)) transform = carla.Transform(carla.Location(x=0, y=0, z=0)) # 创建触发器并且绑定回调函数 blueprint = world.get_blueprint_library().find('sensor.other.obstacle') blueprint.set_attribute('hit_radius', '0.1') trigger = world.spawn_actor(blueprint, transform, attach_to=npc_actor) trigger.listen(lambda event: npc_actor.destroy()) # 绑定触发器形状和位置 trigger.set_box(box) trigger.set_attribute('is_npc', 'true') # 初始化carla模拟器 client = carla.Client('localhost', 2000) client.set_timeout(10.0) world = client.get_world() try: # 获取蓝图 blueprint_library = world.get_blueprint_library() # 创建NPC车辆 npc_bp = blueprint_library.find('vehicle.npc.standard') npc_transform = carla.Transform(start_point, carla.Rotation()) npc_actor = world.spawn_actor(npc_bp, npc_transform) # 创建触发器 create_trigger(world, start_point, end_point, npc_actor) # 循环运动 while True: # 计算NPC车辆当前位置到终点的距离 distance = npc_actor.get_location().distance(end_point) # 如果到达终点,则销毁NPC车辆并重新生成 if distance <= 5: npc_actor.destroy() npc_actor = world.spawn_actor(npc_bp, npc_transform) create_trigger(world, start_point, end_point, npc_actor) # 否则继续沿直线行驶 else: npc_actor.set_velocity(carla.Vector3D(x=50, y=0, z=0)) finally: # 销毁所有角色 for actor in world.get_actors().filter('vehicle.*'): actor.destroy() ``` 该代码首先定义了起点和终点的坐标,然后创建了一个create_trigger()函数用于创建触发器,该触发器的位置和形状会在每次循环中更新,以便于实现NPC车辆的循环运动。接着使用Carla Python API中的相关函数创建了一个NPC车辆,并且在车辆上绑定了触发器。最后,在一个while循环中,程序会不断计算NPC车辆当前位置到终点的距离,如果到达终点,则销毁NPC车辆并重新生成,否则继续沿直线行驶。
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值