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 2000 Forums
 Transact-SQL (2000)
 A simple Check constraint

Author  Topic 

drewsalem
Constraint Violating Yak Guru

304 Posts

Posted - 2006-07-04 : 05:29:02
Consider the following:

ShowRoom_Manager ShowRoom_ID CarModel ShowRoomLocation
'Pete',             '3',           'Audi',     'Showroom_London'
'John', '5', 'Ford', 'Showroom_Scotland'
'Richard', '2', 'Audi', 'Showroom_Ireland'


I would like it that for every show room, there is at least 1 Audi. The best way I can think of doing this is by putting a check in place:

add Check ((select count (*) from Showrooms where CarModel = 'Audi' ) > 1);

However my check does not take into account "for every showroom"

How do I achieve the required?

Thanks

The Drewster

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-07-04 : 06:04:38
[code]
SELECT ShowRoom_Manager,
ShowRoom_ID,
CarModel,
ShowRoomLocation
FROM MyTable
WHERE ShowRoom_ID IN (SELECT ShowRoomID FROM MyTable WHERE CarModel = 'Audi')[/code]


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-07-04 : 06:11:35
quote:
Originally posted by drewsalem

Consider the following:

ShowRoom_Manager ShowRoom_ID CarModel ShowRoomLocation
'Pete',             '3',           'Audi',     'Showroom_London'
'John', '5', 'Ford', 'Showroom_Scotland'
'Richard', '2', 'Audi', 'Showroom_Ireland'


I would like it that for every show room, there is at least 1 Audi. The best way I can think of doing this is by putting a check in place:

add Check ((select count (*) from Showrooms where CarModel = 'Audi' ) > 1);

However my check does not take into account "for every showroom"

How do I achieve the required?

Thanks

The Drewster



I am afraid that simple Check constraint would not be sufficient - since CHECK constraint does not support subqueries. Maybe you can write trigger for that. Folks, any other way out there ?


quote:
SELECT ShowRoom_Manager,
ShowRoom_ID,
CarModel,
ShowRoomLocation
FROM MyTable
WHERE ShowRoom_ID IN (SELECT ShowRoomID FROM MyTable WHERE CarModel = 'Audi')




How this query will be able to handle drewsalem's requirement?

Harsh Athalye
India.
"Nothing is Impossible"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-07-04 : 06:16:50
How about
quote:
Originally posted by drewsalem

1. I would like it that for every show room, there is at least 1 Audi.
2. add Check ((select count (*) from Showrooms where CarModel = 'Audi' ) > 1)
For us that not speak native english, it's hard to tell what he really wants.

My query can be put into a function, and the function can be set as CONSTRAINT, if you wonder.

Second, why would you write a contraint that checks for at least one Audi in a showroom? The millisecond the first Audi is inserted, the constraint has filled it's purpose. The only reason I can think of, is when you delete cars from showroom and it is vital that at least one Audi is still there. This might be a better job for a trigger.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

drewsalem
Constraint Violating Yak Guru

304 Posts

Posted - 2006-07-04 : 07:10:46
quote:
why would you write a contraint that checks for at least one Audi in a showroom?...the only reason I can think of, is when you delete cars from showroom and it is vital that at least one Audi is still there. This might be a better job for a trigger.


Yeh, that's pretty much the reason.


What's the trigger trigerring. I just want it to prevent me from deleting the last audi for a show room.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-07-04 : 07:20:51
quote:
Originally posted by drewsalem

Yeh, that's pretty much the reason.

What's the trigger trigerring. I just want it to prevent me from deleting the last audi for a show room.
For all the showrooms or for every shoowroom?

This T-SQL statement created a trigger that checks for at least one Audi in whole table
CREATE TRIGGER trgLastAudi ON MyTable
FOR DELETE
AS

IF (SELECT COUNT(*) FROM MyTable WHERE CarModel = 'Audi') < 1
BEGIN
ROLLBACK TRAN
RAISERROR(16, 1, 'No more Audis left in table.')
END
This is one way to check for at least one Audi left for every single showroom
CREATE TRIGGER trgLastAudi ON MyTable
FOR DELETE
AS

DECLARE @Name VARCHAR(100),
@ID INT

SELECT TOP 1 @ID = Showroom_ID,
@Name = Showroom_Name
FROM ShowroomTable
WHERE Showroom_ID NOT IN (SELECT Showroom_ID FROM InventoryTable WHERE CarModel = 'Audi')

IF @ID IS NOT NULL
ROLLBACK TRAN
RAISERROR(16, 1, 'No more Audis left for showroom %d in %s.', @ID, @Name)
END

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

drewsalem
Constraint Violating Yak Guru

304 Posts

Posted - 2006-07-04 : 07:29:17
Oh! Like that! That's pretty cool.

No it's for each show room. But that's fine, I get the idea.

Thanks
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2006-07-04 : 21:40:21
Interesting problem but one not really suited to a check constraint (as you've now discovered).
However from a data modelling perspective I think you're setting yourself up for a fall. You are trying to force your system to reject something that might actually happen in real life - e.g. the salesman gets special permission to sell the last Audi, Audi recalls all their cars due to a saftey issue, all the cars are stolen..... Your system will no longer be capable of reflecting reality. You need to decide if your database models the situation or drives it. I suspect in this case it models - you won't be able to stop a car salesman selling his own mother let alone the last car in the shop!
Go to Top of Page

drewsalem
Constraint Violating Yak Guru

304 Posts

Posted - 2006-07-05 : 04:51:07
I daren't say it in case people stop answering my posts, but most of my questions are not work related but self study, trying to figure out how things work. I don't use triggers and functions at work, so unless I play with them in my own time, I'll never get to grips with them. What do you think of that for dedication
Go to Top of Page
   

- Advertisement -