If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > function on trigger

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-31-11, 17:23
marcos_Brz marcos_Brz is offline
Registered User
 
Join Date: Aug 2011
Posts: 3
function on trigger

Hi guys

I'm newbie in Psql and I'm trying to build one functions 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.
Reply With Quote
  #2 (permalink)  
Old 09-01-11, 07:23
marcos_Brz marcos_Brz is offline
Registered User
 
Join Date: Aug 2011
Posts: 3
sorry,

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


Idpublisher, name, city, Books, amount_Books_
Reply With Quote
  #3 (permalink)  
Old 09-01-11, 07:39
marc_ marc_ is offline
Registered User
 
Join Date: Aug 2011
Location: Glasgow, UK
Posts: 36
Cool

Hello marcos_Brz!

Have you considered using a view? It's probably better suited to your example than a trigger.

Using your example:
Code:
--Create the Tables:
create table books
(bookid  int, title varchar(50), price decimal(14,2), idPublisher int);

create table publisher
(Idpublisher int , name varchar(50), city varchar(50));

-- Popluate Tables:
insert into books values(501, 'Zombie Survival Guide', 5.99, 5);
insert into books values(502, 'World War Z', 6.99, 5);
insert into books values(102, 'The Return of Sherlock Holmes', 3.99, 2);
insert into books values(102, 'The Hound of the Baskervilles', 3.50, 2);
insert into books values(102, 'The Adventures of Sherlock Holmes', 3.99, 2):
insert into publisher values(5, 'Random House', 'New York');
insert into publisher values(2, 'Penguin', 'London');

Compile the view
Code:
create view publisher_books as
select a.Idpublisher, a.name, a.city, count(1) total_books
from publisher a
inner join books b
on a.idPublisher=b.idPublisher
group by a.Idpublisher, a.name, a.city;
Select from the view
Code:
postgres=# select * from publisher_books;
 idpublisher |     name     |   city   | total_books
-------------+--------------+----------+-------------
           2 | Penguin      | London   |           3
           5 | Random House | New York |           2
(2 rows)
Reply With Quote
  #4 (permalink)  
Old 09-01-11, 11:12
marc_ marc_ is offline
Registered User
 
Join Date: Aug 2011
Location: Glasgow, UK
Posts: 36
Afterthought...

If you really just want to learn functions and triggers , I'd suggest having a look at:
Practical PostgreSQL

Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On