>Hello,
>
>I'm trying to figure out the syntax for updating a count column based on the child table count.
>Parent table has the columns columns pcode, childcount
>Child table has the columns pcode, childname
>
>I'm thinking I want to do something like the following, though I'm stuck
>figuring out how to finish the sql
>
>update parent
>set childcount =
> (select COUNT(item_code)
> from child
> where child.pcode = parent.pcode)
>
>How do I tell the subquery what the parent.pcode is?
>
>Any suggestions would be greatly appreciated.
>
>Thanks,
>-Isaac
Not that I am a relational theory purist, but maintain a count of child records in the parent is generally considered "not a good idea" :). You can always get this data with the following SQL statement:
SELECT Child.PCode, COUNT(*)
FROM Child
GROUP BY PCode
This solution works well for a couple of reasons:
1. Space is saved - although it may be minor, you do not need the count column in the parent table.
2. Speed - Every time a child record is added or deleted (which may be often), you don't have the added overhead of counting all the child records and updating the parent.
Chris McCandless
Red Sky Software