2.2.2. Primary Tables
Now let us proceed to the primary tables. The first will be the CUSTOMER
table. We will create a sequence (a generator) for its primary key and a corresponding trigger for implementing it as an auto-incrementing column. We will do the same for each of the tables.
CREATE GENERATOR GEN_CUSTOMER_ID;
CREATE TABLE CUSTOMER (
CUSTOMER_ID INTEGER NOT NULL,
NAME VARCHAR(60) NOT NULL,
ADDRESS VARCHAR(250),
ZIPCODE D_ZIPCODE,
PHONE VARCHAR(14),
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUSTOMER_ID)
);
SET TERM ^ ;
CREATE OR ALTER TRIGGER CUSTOMER_BI FOR CUSTOMER
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.CUSTOMER_ID IS NULL) THEN
NEW.CUSTOMER_ID = NEXT VALUE FOR GEN_CUSTOMER_ID;
END
^
SET TERM ; ^
COMMENT ON TABLE CUSTOMER IS
'Customers';
COMMENT ON COLUMN CUSTOMER.CUSTOMER_ID IS
'Customer Id';
COMMENT ON COLUMN CUSTOMER.NAME IS
'Name';
COMMENT ON COLUMN CUSTOMER.ADDRESS IS
'Address';
COMMENT ON COLUMN CUSTOMER.ZIPCODE IS
'Zip Code';
COMMENT ON COLUMN CUSTOMER.PHONE IS
'Phone';
|
Now we construct a script for creating the PRODUCT
table:
CREATE GENERATOR GEN_PRODUCT_ID;
CREATE TABLE PRODUCT (
PRODUCT_ID INTEGER NOT NULL,
NAME VARCHAR(100) NOT NULL,
PRICE D_MONEY NOT NULL,
DESCRIPTION BLOB SUB_TYPE 1 SEGMENT SIZE 80,
CONSTRAINT PK_PRODUCT PRIMARY KEY (PRODUCT_ID)
);
SET TERM ^;
CREATE OR ALTER TRIGGER PRODUCT_BI FOR PRODUCT
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
IF (NEW.PRODUCT_ID IS NULL) THEN
NEW.PRODUCT_ID = NEXT VALUE FOR GEN_PRODUCT_ID;
END
^
SET TERM ;^
COMMENT ON TABLE PRODUCT IS
'Goods';
COMMENT ON COLUMN PRODUCT.PRODUCT_ID IS
'Product Id';
COMMENT ON COLUMN PRODUCT.NAME IS
'Name';
COMMENT ON COLUMN PRODUCT.PRICE IS
'Price';
COMMENT ON COLUMN PRODUCT.DESCRIPTION IS
'Description';
In Firebird 3.0, you need to add the command for granting the
|