1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

anyone decent with ms-sql?

Discussion in 'BBS Hangout' started by Nikos, Oct 14, 2004.

Tags:
  1. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    Im supposed to test the trigger by inserting rows in the members table, deleting rows from the members table, updating a row by changing the groupid and then print out both the members table and the groups table. (Then Test putting in several rows by creating a new table, NewMem with the same head as the members table, putting some rows in it and then inserting all the rows of NewMem into members at once.)

    Im confused as to go about testing this trigger. How do I go about inserting the rows into the members table, and updating a row etc...?
     
  2. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    Oh also, about the update, you would only want to recalc NumMems if the groupid was changed. You can check if the groupid has changed using the follow commands

    IF NOT UPDATE(groupid)
    RETURN

    Put that in the update trigger before the recalc, so if groupid wasn'tt changed you exit out of the trigger.
     
  3. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    You can insert/update/delete from the members table like normal. A trigger won't affect that, only after the action took place (for an after trigger that is)
     
  4. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    CREATE TRIGGER MembersDelete ON members
    After Delete
    AS
    Update groups
    set NumMems = (Select count(*)
    from members
    where groupid= Deleted.groupid)
    from Deleted
    where groups.gid= Deleted.groupid
    IF NOT UPDATE(groupid)
    RETURN

    Does this account for all three INSERT, DELETE and UPDATING?

    How would I test these, just by trying to insert new members, delete some, and update them? What does that code above do exactly in this case (even though u have them seperately, the three Ins, delete and update)?
     
  5. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    the trigger should automatically update the groups table, affecting only
    those rows which had changes in the number of members in the group. This
    occurs when a member is inserted, deleted, or is updated to change
    group.

    Would this be the case?
     
  6. thegary

    thegary Contributing Member

    Joined:
    Jul 22, 2002
    Messages:
    10,357
    Likes Received:
    2,370
  7. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    How can I combine this trigger to include all of the inerseting, updating and deleting? Your mentioned trigger is only for delete right?
     
  8. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    No, that was only the delete trigger. Also, the if not update groupid line isn't in the right trigger, that is only for the update trigger.


    Delete trigger:

    CREATE TRIGGER MembersDelete ON members
    After Delete
    AS
    Update groups
    set NumMems = (Select count(*)
    from members
    where groupid= Deleted.groupid)
    from Deleted
    where groups.gid= Deleted.groupid

    ====================================

    Update Trigger

    CREATE TRIGGER MembersUpdate ON members
    After Update
    AS

    IF NOT UPDATE(groupid)
    RETURN

    Update groups
    set NumMems = (Select count(*)
    from members
    where groupid= Inserted.groupid)
    from Inserted
    where groups.gid= Inserted.groupid

    =====================================

    The insert trigger is the same as before, I just changed the name:

    CREATE TRIGGER MembersInsert ON members
    After Insert
    AS
    Update groups
    set NumMems = (Select count(*)
    from members
    where groupid= Inserted.groupid)
    from Inserted
    where groups.gid= Inserted.groupid

    ======================================

    Triggers are a set of commands that get performed when a specified action occurs on the table.

    So in your case, whenever a row gets inserted into member table, the insert trigger fires, which calculates the number of members for the groupid of the newly inserted row and then updates the groups table. Same thing happens when a delete occurs. The delete trigger fires and recalcutes the number of memebers for the groupid of the row that just go deleted. The same thing for when an update occurs, except the trigger checks to see if groupid was updated. If not, there isn't a point of recalculating, as the number of members hasn't changed.

    To just test perform a bunch of insert/deletes/updates to the members table. So if you insert 3 rows with groupid of 10 and there were already 2 rows with groupid of 10, the trigger should have fired and updated NumMems in the groups table with 5 where gid = 10
     
  9. ron413

    ron413 Contributing Member

    Joined:
    May 29, 2002
    Messages:
    3,913
    Likes Received:
    102
    I don't mean to change the subject, but this is indeed one of the funniest threads of the year.

    Good times:cool:
     
  10. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    The question is

    Write a trigger that maintains the correct values of NumMems on update (of the groupid column in the members table), or on delete or insert in the members table. Remember that one update, delete or insert command may affect more than one row. Test your trigger by inserting rows in the members table, deleting rows from the members table, updating a row by changing the groupid and then print out both the members table and the groups table.

    (Test putting in several rows by creating a new table, NewMem with the same head as the members table, putting some rows in it and then inserting all the rows of NewMem into members at once.)

    How would these three triggers be combined to form one trigger statement? Instead of three seperate ones? It wouldn't work if I had to fire them seperately right?
     
  11. Surfguy

    Surfguy Contributing Member

    Joined:
    Sep 23, 1999
    Messages:
    23,428
    Likes Received:
    11,715
    Dude...your weird. :p

    I hate threads like this. Hire a DBA...for crying out loud.

    Just kidding. :D
     
  12. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    Try this. It ain't pretty, but this should work for you.

    CREATE TRIGGER UpdateMemberCount ON [dbo].[members]
    FOR Insert, Delete, Update
    AS

    if update(groupid)
    Begin
    Update groups
    set count = (Select count(*)
    from members
    where groupid= Inserted.groupid)
    from Inserted
    where groups.gid= Inserted.groupid
    End

    Update groups
    set count = (Select count(*)
    from members
    where groupid= Deleted.groupid)
    from Deleted
    where groups.gid= Deleted.groupid


    Its hard writing code in this little box.
     
  13. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    To think, I came to the Hangout to get away from work :D
     
  14. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    How would I create the NewMem

    "(Test putting in several rows by creating a new table, NewMem with the same head as the members table, putting some rows in it and then inserting all the rows of NewMem into members at once.)"
     
  15. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    The same way you created the members table
     
  16. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    Im starring to think maybe those 3 seperate statements would be a good way of doing it.

    But how would I create the new table

    (Test putting in several rows by creating a new table, NewMem with the same head as the members table, putting some rows in it and then inserting all the rows of NewMem into members at once.)
     
  17. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    There was a flaw in the original update trigger. It didn't take into account recalculating the count of the groupid that got replaced.
     
  18. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    So is what you have above basically 2 pieces of code or three?

    The three pieces you wrote before this one has that error you mention?
     
  19. tolne57

    tolne57 Contributing Member

    Joined:
    Nov 7, 2002
    Messages:
    624
    Likes Received:
    4
    yeah, there is a logic flaw in the individual update trigger. It isn't there in the combined trigger.
     
  20. Nikos

    Nikos Member

    Joined:
    Feb 25, 2002
    Messages:
    381
    Likes Received:
    0
    To create the NewMems table would I just do something like

    Create Table
    NewMems......

    The thing is I was given the members table, so I don't understand how to create it from scratch into a new table (even though it has the same headings etc...)
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now