Re: DB design and foreign keys

From: Gianluca Riccardi <ml-reader(at)moonwatcher(dot)it>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: DB design and foreign keys
Date: 2005-12-14 15:08:05
Message-ID: 43A03555.6050007@moonwatcher.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jost Degenhardt 2005-12-14 17:26:23 Finding out to which table a specific row belongs
Previous Message Gianluca Riccardi 2005-12-14 10:27:47 Re: DB design and foreign keys