Subject | Re: [ib-support] Implementing referential integrity |
---|---|
Author | Arno Brinkman |
Post date | 2002-06-28T12:17:31Z |
Hi,
CREATE TABLE CUSTOMER
(
ID INTEGER NOT NULL CONSTRAINT PK_CUSTOMER PRIMARY KEY,
FULLNAME VARCHAR(35)
);
CREATE TABLE INVOICE
(
ID INTEGER NOT NULL CONSTRAINT PK_INVOICE PRIMARY KEY,
CUSTOMER_ID INTEGER CONSTRAINT FK_INVOICE_CUSTOMER REFERENCES
CUSTOMER(ID),
CALCTOTAL NUMERIC(12,3),
DESCRIPTION VARCHAR(50)
);
--------------------
or
--------------------
CREATE TABLE CUSTOMER
(
ID INTEGER NOT NULL,
FULLNAME VARCHAR(35)
);
ALTER TABLE CUSTOMER ADD CONSTRAINT PK_CUSTOMER PRIMARY KEY (ID);
CREATE TABLE INVOICE
(
ID INTEGER NOT NULL,
CUSTOMER_ID INTEGER,
CALCTOTAL NUMERIC(12,3),
DESCRIPTION VARCHAR(50)
);
ALTER TABLE INVOICE ADD CONSTRAINT PK_INVOICE PRIMARY KEY (ID);
ALTER TABLE INVOICE ADD CONSTRAINT FK_INVOICE_CUSTOMER FOREIGN KEY
(CUSTOMER_ID) REFERENCES CUSTOMER(ID);
--------------------
I would advice you to read an SQL-learning book.
Regards,
Arno
> I am really hoping someone can just--------------------
> post a simple example of the SQL they use to create a simple foreign key.
CREATE TABLE CUSTOMER
(
ID INTEGER NOT NULL CONSTRAINT PK_CUSTOMER PRIMARY KEY,
FULLNAME VARCHAR(35)
);
CREATE TABLE INVOICE
(
ID INTEGER NOT NULL CONSTRAINT PK_INVOICE PRIMARY KEY,
CUSTOMER_ID INTEGER CONSTRAINT FK_INVOICE_CUSTOMER REFERENCES
CUSTOMER(ID),
CALCTOTAL NUMERIC(12,3),
DESCRIPTION VARCHAR(50)
);
--------------------
or
--------------------
CREATE TABLE CUSTOMER
(
ID INTEGER NOT NULL,
FULLNAME VARCHAR(35)
);
ALTER TABLE CUSTOMER ADD CONSTRAINT PK_CUSTOMER PRIMARY KEY (ID);
CREATE TABLE INVOICE
(
ID INTEGER NOT NULL,
CUSTOMER_ID INTEGER,
CALCTOTAL NUMERIC(12,3),
DESCRIPTION VARCHAR(50)
);
ALTER TABLE INVOICE ADD CONSTRAINT PK_INVOICE PRIMARY KEY (ID);
ALTER TABLE INVOICE ADD CONSTRAINT FK_INVOICE_CUSTOMER FOREIGN KEY
(CUSTOMER_ID) REFERENCES CUSTOMER(ID);
--------------------
I would advice you to read an SQL-learning book.
Regards,
Arno