Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 49ED0475D3F for ; Fri, 10 Jan 2003 12:32:44 -0500 (EST) Received: from davinci.ethosmedia.com (unknown [209.10.40.251]) by postgresql.org (Postfix) with ESMTP id 8CADE475AFF for ; Fri, 10 Jan 2003 12:32:43 -0500 (EST) Received: from [63.195.55.98] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 4.0.2) with HTTP id 2309906; Fri, 10 Jan 2003 09:32:52 -0800 From: "Josh Berkus" Subject: Re: Table Design Questions To: Chad L , pgsql-sql@postgresql.org X-Mailer: CommuniGate Pro Web Mailer v.4.0.2 Date: Fri, 10 Jan 2003 09:32:52 -0800 Message-ID: In-Reply-To: <20030110090815.29853.qmail@web11607.mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/155 X-Sequence-Number: 11020 Chad, > I am trying to come up with an efficient table design > that describes a fantasy character that meets the > following criteria: Believe it or not, this is the first "D&D" question I've seen on this list. > CREATE TABLE ATTRIBUTES ( > CHAR_ID INT PRIMARY KEY NOT NULL, > ATTRIB_TYPE_ID INT NOT NULL, > ATTRIB_VALUE INT, > CONSTRAINT ATTRIB_TYPE_ID_FK FOREIGN KEY > (ATTRIB_TYPE_ID) REFERENCES ATTRIB_TYPES > (ATTRIB_TYPE_ID) > ); > > CREATE TABLE ATTRIB_TYPES ( > ATTRIB_TYPE_ID INT PRIMARY KEY NOT NULL, > ATTRIB_TYPE VARCHAR(20) NOT NULL, > ATTRIB_NAME VARCHAR(20) UNIQUE NOT NULL, > ); I do something similar a lot with User Defined Fields. Generally for UDFs I use a TEXT field to hold the data, setting up something like this: CREATE TABLE udfs ( udf_id SERIAL PRIMARY KEY, udf_format VARCHAR(30), udf_validate TEXT, udf_list INT FOREIGN KEY udf_lists (list_id) ); Where udf_format is a builtin or custom data type (INT, BOOLEAN, money, NUMERIC, TEXT, phone, e-mail, etc.), and udf_validate is a regexp to additionally validate the value. Based on the information on this table, you can write a custom function which formats each attribute as it comes out of the table based on the reference table. Hope that helps, half-elf! -Josh