Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 TRIGGER or Check Constraint

Author  Topic 

RovenaN
Starting Member

4 Posts

Posted - 2010-11-08 : 13:49:43
I am new into SQL so I hope I explained the Question OK.

I have a table named Exchange where people exchange books or CD. With col:LenderID, BorrowerID, ItemID, DateID. How can I write a trigger or check constraint (I am not sure which one to use) for this scenario:
A person should only be able to borrow another object, if the number of objects currently borrowed by this person does not exceed the number of objects that person has lent to others by more than 10.
Thank you,
Rovena

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-08 : 14:09:30
I think your approach isn't right here.
Assuming you are using an application/any kind of front end/maybe a web page...

If I didn't misunderstood then the way should be to do a select for that person first to determine if he actually is allowed to borrow another object. If not then you can display a message box to him 'no more objects to borrow for you...' or something else.
So there is no need to check it later when inserting the data.
This is an advantage because no person is first looking at what he/she wants to borrow and then making decisions, typing in any data and so on only to get a message later by a constraint or trigger.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

RovenaN
Starting Member

4 Posts

Posted - 2010-11-08 : 14:16:58
Thank you for the reply. How can I write the select for that person to determine if he actually is allowed to borrow another object? Can you help me with that select statement please?
Thank you,
Rovena
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-11-08 : 14:22:56
Sure.
If you can give some sample data and the expected result.

Best like this if you are able to do so:
declare @sample table (LenderID int, BorrowerID int, ...)
insert @sample
select 1, 7, ... UNION ALL
select 1, 5, ...

So we can copy and paste the test data to work it out for you.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-11-08 : 14:28:12
Not sure I agree with Fred's point: "...there is no need to check it later when inserting the data...".
It's always good to pre-validate but it's also good practice to constrain the database to support the business rules.

Could be done with either a check constraint or a trigger.

How does your model support when a borrower returns an item to the lender?

Be One with the Optimizer
TG
Go to Top of Page

RovenaN
Starting Member

4 Posts

Posted - 2010-11-08 : 14:40:48
quote:
Originally posted by TG

Not sure I agree with Fred's point: "...there is no need to check it later when inserting the data...".
It's always good to pre-validate but it's also good practice to constrain the database to support the business rules.

Could be done with either a check constraint or a trigger.

How does your model support when a borrower returns an item to the lender?

Be One with the Optimizer
TG



Good question I'm pretty sure its up to us how we want to treat that. Since checking if the borrower is giving back to his lender or not makes it even more complicated, we can keep it simple and consider it a new lend where borrower is now a lender and the original lender is now a borrower. Everytime an exchange happens lender and borrower are recorded, we're not really dealing with original owner, so I hope that will make it simpler for you.

Thank you.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-11-08 : 14:48:50
Here is one possible way to do this with a constraint:

use tempdb
create table b (LenderID int, BorrowerID int, ItemID int)
go

create function dbo.fn_BorrowDifferential(@id int)
returns int
as
begin
declare @d int
select @d =
sum(case when borrowerid = 1 then 1 else 0 end)
-
sum(case when lenderid = 1 then 1 else 0 end)
from b
where lenderid = @id or BorrowerID = @id
return @d
end
go
alter table b add constraint ck_borrowerDifferential check (dbo.fn_BorrowDifferential(borrowerid) <= 5)
go

insert b values( 11, 1, 100)
insert b values( 12, 1, 100)
insert b values( 13, 1, 100)
insert b values( 14, 1, 100)
insert b values( 15, 1, 100)

--check differential
select dbo.fn_BorrowDifferential(1)

--these will fail
insert b values( 16, 1, 100)
insert b values( 17, 1, 100)

--return two items
insert b values( 1, 11, 100)
insert b values( 1, 12, 100)

--check differential again
select dbo.fn_BorrowDifferential(1)

--now these will succeed
insert b values( 16, 1, 100)
insert b values( 17, 1, 100)

select dbo.fn_BorrowDifferential(1)
go

drop table b
drop function dbo.fn_BorrowDifferential


Be One with the Optimizer
TG
Go to Top of Page

RovenaN
Starting Member

4 Posts

Posted - 2010-11-08 : 20:34:49
Thanks a lot. Is there any way you can make a simple trigger instead of a function? I have not learned functions yet and am not comfortable with them yet. Its a check/trigger constraint-based assignment so I was trying to practice creation of triggers.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-11-09 : 08:46:24
Oh, I see, it's a school assignment. Then look at CREATE TRIGGER in Sql Server's Books Online. There is a good explanation and plenty of examples. If you want you can adapt the code I put in the function to use for your trigger.

If you still need help then post what you've tried so far...

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -