Subject RE: [firebird-support] Creating constraints in FireBird
Author Alan McDonald
> Another question I am asking, it might seem very trivial and easy
> but being a new user of FireBird (the latest release) installed and
> connected, I do suffer some headaches regarding this. I have created
> a database (successfully), added the tables I required and got stuck
> when trying to do the following.
>
> CREATE TABLE contacts(
> CONTACT_CODE VARCHAR(10) NOT NULL,
> CONTACT_NAME CHAR(50) NOT NULL,
> CONTACT_GENDER VARCHAR(1) NOT NULL,
> CONTACT_NATIONALITY VARCHAR(5) NOT NULL,
> CONTACT_PHONE VARCHAR(15)[3]
> PRIMARY KEY(CONTACT_CODE)
> );
> CREATE INDEX contactname ON contacts(CONTACT_NAME);
> ALTER TABLE contacts
> ADD CONSTRAINT fk_nationality
> FOREIGN KEY (CONTACT_NATIONALITY)
> REFERENCE countries(COUNTRY_CODE)
> ON UPDATE CASCADE;
>
> Now my first problem is not creating the table but when trying to
> create another constraint where the CONTACT_GENDER must either be M
> or F. I have created a constraint with INTEGER where I could specify
> a range between two numbers but I am stuck with forcing either one
> of two characters.

REATE DOMAIN T_YESNO AS
CHAR(1) CHARACTER SET NONE
DEFAULT 'N'
CHECK ((VALUE IS NULL) OR (VALUE IN ('N','Y')))
COLLATE NONE

>
> The second problem, when having an array of values (CONTACT_PHONE) I
> wish to have the first value of the array mandatory where the two
> remaining values are optional. How do I define that in the table? If
> I declare the field as NOT NULL will this not cause all three the
> values to be mandatory? Is the only solution to have this validation
> done in code or is it possible to define it within the table
> definition?
>
> Many thanks,
> Antz

NOT NULL on the first value

Alan