X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 0E1BE9DC9C4 for ; Wed, 14 Dec 2005 11:09:33 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 69859-05 for ; Wed, 14 Dec 2005 11:09:36 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from smtp1.libero.it (smtp1.libero.it [193.70.192.51]) by postgresql.org (Postfix) with ESMTP id A048E9DC830 for ; Wed, 14 Dec 2005 11:09:29 -0400 (AST) Received: from localhost (172.16.1.118) by smtp1.libero.it (7.0.027-DD01) id 439D8FC3003B5CE3 for pgsql-sql@postgresql.org; Wed, 14 Dec 2005 16:09:33 +0100 Received: from smtp2.libero.it ([172.16.1.97]) by localhost (asav14.libero.it [193.70.193.42]) (amavisd-new, port 10024) with ESMTP id 19924-09 for ; Wed, 14 Dec 2005 16:09:32 +0100 (CET) Received: from [172.16.0.5] (ppp-219-211.24-151.libero.it [151.24.211.219]) by smtp2.libero.it (Postfix) with ESMTP id 8E594A8F5C for ; Wed, 14 Dec 2005 16:06:16 +0100 (MET) Message-ID: <43A03555.6050007@moonwatcher.it> Date: Wed, 14 Dec 2005 16:08:05 +0100 From: Gianluca Riccardi User-Agent: Mozilla/5.0 (X11; U; Linux i686; it-IT; rv:1.7.8) Gecko/20050927 Debian/1.7.8-1sarge3 X-Accept-Language: it MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: DB design and foreign keys References: <439F0FE8.1050503@moonwatcher.it> In-Reply-To: <439F0FE8.1050503@moonwatcher.it> Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 8bit X-Scanned: with antispam and antivirus automated system at libero.it X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200512/118 X-Sequence-Number: 23660 so, after the needed modifications the SQL schema is the following -- SQL schema for business-test-db2 CREATE TABLE customers ( customer_code serial UNIQUE, alfa_customer_code varchar(6), customer_name character varying(250) NOT NULL, address character varying(250) NOT NULL, city character varying(250) NOT NULL, zip_code character varying(8) NOT NULL, prov character varying(30) NOT NULL, security character varying(15) NOT NULL, tel character varying(30), tel2 character varying(20) NOT NULL, fax character varying(250), url character varying(250), email1 character varying(250) NOT NULL, email2 character varying(250) NOT NULL, discount1 integer, discount2 integer, PRIMARY KEY (customer_code) ); CREATE TABLE users ( id smallint NOT NULL, login varchar(20) NOT NULL, pwd varchar(20) NOT NULL, name varchar(20) NOT NULL, customer_code int REFERENCES customers (customer_code), valid date, primary key (id) ); CREATE TABLE products ( code varchar(60) UNIQUE NOT NULL, description varchar(250) NOT NULL, dimensions varchar(250) NOT NULL, price numeric NOT NULL, state boolean, PRIMARY KEY (code) ); CREATE TABLE orders ( order_code serial UNIQUE NOT NULL, customer_code integer REFERENCES customers (customer_code) NOT NULL, order_date time without time zone NOT NULL, remote_ip inet NOT NULL, order_time timestamp with time zone NOT NULL, order_type varchar(10) NOT NULL, state varchar(10) NOT NULL, PRIMARY KEY (order_code) ); CREATE TABLE order_items ( order_code integer REFERENCES orders (order_code) NOT NULL, customer_code integer REFERENCES customers (customer_code) NOT NULL, product_code varchar(60) REFERENCES products (code) NOT NULL, qty int NOT NULL, price numeric NOT NULL, row_price numeric ); -- -- END OF FILE the order_items table is: business-test-db2=# \d order_items Tabella "public.order_items" Colonna | Tipo | Modificatori -------------------+-------------------------+-------------- order_code | integer | not null customer_code | integer | not null product_code | character varying(60) | not null qty | integer | not null price | numeric | not null row_price | numeric | Vincoli di integrità referenziale "$1" FOREIGN KEY (order_code) REFERENCES orders(order_code) "$2" FOREIGN KEY (customer_code) REFERENCES customers(customer_code) "$3" FOREIGN KEY (product_code) REFERENCES products(code) business-test-db2=# thanks a lot, you all pointed me out of misconceptualized position. best ragards all, Gianluca Riccardi p.s. Colonna = column Tipo = type Modificatori = modifiers Vincoli di integrita' referenziale = referential integrity constraints