function on trigger

Поиск
Список
Период
Сортировка
Искать

function on trigger

От:
Marcos Hercules Santos <mhercs@gmail.com>
Дата:
hi guys

I'm newbie in Psql and I'm trying to build one function  in order to
count the products for each supplier. So i'm gonna put it quite simply
though this example


Please, consider a table called books with the following fields

bookid, title, price, idPublisher



and one another table called publisher

Idpublisher, name, city, Books


Being in that last field from Publisher, called book, I gotta have the
amount of published books for each publisher.

Re: function on trigger

От:
David Johnston <polobo@yahoo.com>
Дата:
On Aug 31, 2011, at 18:39, Marcos Hercules Santos  wrote:

> hi guys
> 
> I'm newbie in Psql and I'm trying to build one function  in order to
> count the products for each supplier. So i'm gonna put it quite simply
> though this example
> 
> 
> Please, consider a table called books with the following fields
> 
> bookid, title, price, idPublisher
> 
> 
> 
> and one another table called publisher
> 
> Idpublisher, name, city, Books
> 
> 
> Being in that last field from Publisher, called book, I gotta have the
> amount of published books for each publisher.

Is there a question somewhere?


> 
Do not be tempted by the dark side Marcos.  Create a view that uses a join and a count.  Only if you have significant performance issues would you then consider materializing that view.

If you insist, or simply want to learn, read the sections on UPDATE, CREATE TRIGGER, and CREATE FUNCTION.  Try doing it following those examples.  Ask more specific questions if something specific stumps you.

Do it incrementally.  Get the raw SQL UPDATE working then create the trigger and function and get those working.  Then combine the two.

David J.

Re: function on trigger

От:
John R Pierce <pierce@hogranch.com>
Дата:
On 08/31/11 3:39 PM, Marcos Hercules Santos wrote:
> I'm newbie in Psql and I'm trying to build one function  in order to
> count the products for each supplier. So i'm gonna put it quite simply
> though this example
>
>
> Please, consider a table called books with the following fields
>
> bookid, title, price, idPublisher
>
>
>
> and one another table called publisher
>
> Idpublisher, name, city, Books
>
>
> Being in that last field from Publisher, called book, I gotta have the
> amount of published books for each publisher.

get rid of the books field on your publisher table, thats dynamic and 
changes as you add/remove books from the book table.  to get that data, 
try...

     SELECT p.idPublisher, p.name, p.city, COUNT(b.bookid) AS books FROM 
publisher p JOIN books b USING idPublisher GROUP BY p.idPublisher;

you could make this a view if its too cumbersome.

         CREATE VIEW publisher_books SELECT p.idPublisher, p.name, 
p.city, COUNT(b.bookid) AS books FROM publisher p JOIN books b USING 
idPublisher GROUP BY p.idPublisher;

     SELECT * from publisher_books;

and of course, add other WHERE conditions...

         SELECT books FROM publisher_books WHERE name=?;



-- 
john r pierce                            N 37, W 122
santa cruz ca                         mid-left coast

Re: function on trigger

От:
Sim Zacks <sim@compulab.co.il>
Дата:
 On 09/01/2011 01:39 AM, Marcos Hercules Santos wrote: 
hi guys

I'm newbie in Psql and I'm trying to build one function  in order to
count the products for each supplier. So i'm gonna put it quite simply
though this example


Please, consider a table called books with the following fields

bookid, title, price, idPublisher



and one another table called publisher

Idpublisher, name, city, Books


Being in that last field from Publisher, called book, I gotta have the
amount of published books for each publisher.

One way to do this would be with a view, so you don't need the trigger. The other one would need to be after update, insert and delete. This would include code such as:
update publisher set books=books[+|-]1 where idpublisher=[new|old].idpublisher;

Sim

Re: function on trigger

От:
Marcos Hercules Santos <mhercs@gmail.com>
Дата:
On Aug 31, 7:39 pm, Marcos Hercules Santos  wrote:
> hi guys
>
> I'm newbie in Psql and I'm trying to build one function  in order to
> count the products for each supplier. So i'm gonna put it quite simply
> though this example
>
> Please, consider a table called books with the following fields
>
> bookid, title, price, idPublisher
>
> and one another table called publisher
>
> Idpublisher, name, city, Books
>
> Being in that last field from Publisher, called book, I gotta have the
> amount of published books for each publisher.


sorry guys,

the last field in the publisher table is for the amount of books
published by each Publisher


Idpublisher, name, city, Books, amount_Books_

Guys, I'm discarding creating any view.

I already have the query to count the data, but my problem is how to
insert this data in the amount_books_   using  function...


thank goodness for the reply
FAQ