Newbie table definition question

From: Ken Tozier <kentozier(at)comcast(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Newbie table definition question
Date: 2004-10-16 17:50:37
Message-ID: E32FC6A2-1F9B-11D9-A78B-003065F300E2@comcast.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

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

typedef struct Fruit
{
char name[32];
float price;
}Fruit;

typedef struct Veggies
{
char name[32];
float price;
} Veggies;

typedef struct GroceryBill
{
unsigned long date;
long veggieCount;
Veggies *veggies;
long fruitCount;
Fruit *fruits;
} GroceryBill;

The nice thing in C is that I can assign an arbitrary array of
identically structured "fruits" or "veggies" to a single field in a
parent data structure. Is this possible in SQL?

My best guess would be to do something like this:

CREATE TABLE veggies (
name varchar(32) primary key,
price real
);

CREATE TABLE fruit (
name varchar(32) primary key,
price real
);

CREATE TABLE groceries (
date date;
veggies varchar(32) references veggies,
fruit varchar(32) references fruit,
);

But it seems like the "veggies" and "fruit" fields within the
"groceries" table would only reference a single entry in the "fruits"
and "veggies" tables. Is there a way to have a field reference multiple
entries in the fruits or veggies tables? Is there a better way to
handle the whole problem?

Thanks for any help.

Ken

Responses

Browse pgsql-general by date

  From Date Subject
Next Message ryan 2004-10-16 18:18:48 reusing column labels in select
Previous Message Joe Conway 2004-10-16 16:48:47 Re: [HACKERS] Networking feature for postgresql...