insert

From: alan <alan(dot)miller3(at)gmail(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: insert
Date: 2011-07-23 16:23:48
Message-ID: 2e208bc6-1bb8-4170-809f-77ab63a15bf4@l18g2000yql.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

next question.

I have a product table with a 'category" column that I want to
maintain in a separate table.

CREATE TABLE products (
product_id INTEGER DEFAULT
nextval('product_id_seq'::regclass) NOT NULL,
name VARCHAR(60) NOT NULL,
category SMALLINT NOT NULL,
CONSTRAINT product_id PRIMARY KEY (product_id)
);
CREATE TABLE products (
category_id INTEGER DEFAULT
nextval('category_id_seq'::regclass) NOT NULL,
name VARCHAR(20) NOT NULL,
CONSTRAINT category_id PRIMARY KEY (category_id)
);

Every product must have a category,
Since many (but not all) products have the same category I only want 1
table with unique categories.

To do the insert into the products table I need to retrieve or insert
the category_id in categories first.
Which means more code on my client app (if ($cat_id =
get_cat_id($cat)) }else { $cat_id = insert_cat($cat)})

Can I write a BEFORE ROW trigger for the products table to runs on
INSERT or UPDATE to
1. insert a new category & return the new category_id OR
2. return the existing category_id for the (to be inserted row)

Alan
I donproducts.category to be a foreign key that points to the uniqie
category_id id in the want to keep I need to do get the cate

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Дмитрий Васильев 2011-07-24 14:06:40 Bad query plan
Previous Message alan 2011-07-23 15:58:30 Re: Trigger or Function