2.2.3. Secondary Tables
The script for creating the INVOICE
table:
CREATE GENERATOR GEN_INVOICE_ID;
CREATE TABLE INVOICE (
INVOICE_ID INTEGER NOT NULL,
CUSTOMER_ID INTEGER NOT NULL,
INVOICE_DATE TIMESTAMP,
TOTAL_SALE D_MONEY,
PAID D_BOOLEAN DEFAULT 0 NOT NULL,
CONSTRAINT PK_INVOICE PRIMARY KEY (INVOICE_ID)
);
ALTER TABLE INVOICE ADD CONSTRAINT FK_INVOCE_CUSTOMER
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER (CUSTOMER_ID);
CREATE INDEX INVOICE_IDX_DATE ON INVOICE (INVOICE_DATE);
SET TERM ^;
CREATE OR ALTER TRIGGER INVOICE_BI FOR INVOICE
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.INVOICE_ID IS NULL) THEN
NEW.INVOICE_ID = GEN_ID(GEN_INVOICE_ID,1);
END
^
SET TERM ;^
COMMENT ON TABLE INVOICE IS
'Invoices';
COMMENT ON COLUMN INVOICE.INVOICE_ID IS
'Invoice number';
COMMENT ON COLUMN INVOICE.CUSTOMER_ID IS
'Customer Id';
COMMENT ON COLUMN INVOICE.INVOICE_DATE IS
'The date of issuance invoices';
COMMENT ON COLUMN INVOICE.TOTAL_SALE IS
'Total sum';
COMMENT ON COLUMN INVOICE.PAID IS
'Paid';
The INVOICE_DATE
column is indexed because we will be filtering invoices by date to enable the records to be selected by a work period that will be application-defined by a start date and an end date.
In Firebird 3.0, you need to add the command for granting the
|
The script for creating the INVOICE_LINE
table:
CREATE GENERATOR GEN_INVOICE_LINE_ID;
CREATE TABLE INVOICE_LINE (
INVOICE_LINE_ID INTEGER NOT NULL,
INVOICE_ID INTEGER NOT NULL,
PRODUCT_ID INTEGER NOT NULL,
QUANTITY NUMERIC(15,0) NOT NULL,
SALE_PRICE D_MONEY NOT NULL,
CONSTRAINT PK_INVOICE_LINE PRIMARY KEY (INVOICE_LINE_ID)
);
ALTER TABLE INVOICE_LINE ADD CONSTRAINT FK_INVOICE_LINE_INVOICE
FOREIGN KEY (INVOICE_ID) REFERENCES INVOICE (INVOICE_ID);
ALTER TABLE INVOICE_LINE ADD CONSTRAINT FK_INVOICE_LINE_PRODUCT
FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCT (PRODUCT_ID);
SET TERM ^;
CREATE OR ALTER TRIGGER INVOICE_LINE_BI FOR INVOICE_LINE
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.INVOICE_LINE_ID IS NULL) THEN
NEW.INVOICE_LINE_ID = NEXT VALUE FOR GEN_INVOICE_LINE_ID;
END
^
SET TERM ;^
COMMENT ON TABLE INVOICE_LINE IS
'Invoice lines';
COMMENT ON COLUMN INVOICE_LINE.INVOICE_LINE_ID IS
'Invoice line Id';
COMMENT ON COLUMN INVOICE_LINE.INVOICE_ID IS
'Invoice number';
COMMENT ON COLUMN INVOICE_LINE.PRODUCT_ID IS
'Product Id';
COMMENT ON COLUMN INVOICE_LINE.QUANTITY IS
'Quantity';
COMMENT ON COLUMN INVOICE_LINE.SALE_PRICE IS
'Price';
In Firebird 3.0, you need to add the command for granting the
|