Re: Newbie table definition question

From: Steven Klassen <sklassen(at)commandprompt(dot)com>
To: Ken Tozier <kentozier(at)comcast(dot)net>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Newbie table definition question
Date: 2004-10-17 00:42:17
Message-ID: 20041017004217.GA19149@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

* Ken Tozier <kentozier(at)comcast(dot)net> [2004-10-16 13:50:37 -0400]:

> I'm a C/Objective C programmer and am having a bit of difficulty
> figuring out how to define SQL table approximations to things that
> are very easy to do in C/Objective C

I sympathize; no matter how many languages you know, there's always a
learning curve involved trying to pick up the nuances of the next one.

> Basically what I'm confused about is how to simulate arrays of
> structs in Postgres. For example, if I define a C struct like so

You can track whatever information you need about the particular trip,
add rows to the cart associating the trip with the items being
purchased, and finally the grocery types and items.

CREATE TABLE trips (
id bigserial primary key NOT NULL,
created timestamp default now() NOT NULL
);

CREATE TABLE cart (
id bigserial primary key NOT NULL,
trips_id bigint NOT NULL,
grocery_items_id bigint NOT NULL
);

CREATE TABLE grocery_types (
id bigserial primary key NOT NULL,
name text NOT NULL
);

CREATE TABLE grocery_items (
id bigserial primary key NOT NULL,
grocery_types_id bigint NOT NULL,
name text NOT NULL,
price numeric(10,2) NOT NULL
);

ALTER TABLE cart ADD CONSTRAINT grocery_items_id_exists FOREIGN KEY (grocery_items_id) REFERENCES grocery_items(id);
ALTER TABLE grocery_items ADD CONSTRAINT grocery_types_id_exists FOREIGN KEY (grocery_types_id) REFERENCES grocery_types(id);

INSERT INTO grocery_types (name) VALUES ('fruit');
INSERT INTO grocery_types (name) VALUES ('vegatable');

INSERT INTO grocery_items (grocery_types_id, name, price) VALUES (1, 'Apple', '0.50');
INSERT INTO grocery_items (grocery_types_id, name, price) VALUES (1, 'Orange', '0.75');

INSERT INTO grocery_items (grocery_types_id, name, price) VALUES (1, 'Brocolli', '1.35');
INSERT INTO grocery_items (grocery_types_id, name, price) VALUES (1, 'Lettuce', '2.55');

HTH,

--
Steven Klassen - Lead Programmer
Command Prompt, Inc. - http://www.commandprompt.com/
PostgreSQL Replication & Support Services, (503) 667-4564

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Steven Klassen 2004-10-17 00:54:06 Re: Newbie table definition question
Previous Message Josh Close 2004-10-16 23:55:09 Re: plpgsql loop not returning value