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.
| 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?ThanksThe Drewster |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-07-04 : 06:04:38
|
| [code]SELECT ShowRoom_Manager, ShowRoom_ID, CarModel, ShowRoomLocationFROM MyTableWHERE ShowRoom_ID IN (SELECT ShowRoomID FROM MyTable WHERE CarModel = 'Audi')[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
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?ThanksThe 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, ShowRoomLocationFROM MyTableWHERE ShowRoom_ID IN (SELECT ShowRoomID FROM MyTable WHERE CarModel = 'Audi')
How this query will be able to handle drewsalem's requirement?Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-07-04 : 06:16:50
|
How aboutquote: 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 LarssonHelsingborg, Sweden |
 |
|
|
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. |
 |
|
|
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 tableCREATE TRIGGER trgLastAudi ON MyTableFOR DELETEASIF (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 showroomCREATE TRIGGER trgLastAudi ON MyTableFOR DELETEASDECLARE @Name VARCHAR(100), @ID INTSELECT TOP 1 @ID = Showroom_ID, @Name = Showroom_NameFROM ShowroomTableWHERE 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 LarssonHelsingborg, Sweden |
 |
|
|
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 |
 |
|
|
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! |
 |
|
|
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 |
 |
|
|
|
|
|
|
|