Tuesday, January 31, 2006

Porting the EMPLOYEE database from Firebird 2.0 to MySQL 5.1 part 5

Having used InnoDB table handler in MySQL to be on par with Firebird capabilities I'm going to add foreign keys, unique constraints and check constraints and indexes to the tables migrated in MySQL.
Here is a screenshot of Flamerobin showing constraints for table CUSTOMER.

First of all I'll add the foreign key constraints, here is MySQL's syntax:
ALTER TABLE `employee`.`customer` ADD CONSTRAINT `INTEG_127` FOREIGN KEY `INTEG_127` (`country`)
REFERENCES `country` (`country`) ON DELETE RESTRICT ON UPDATE RESTRICT;

A nice graphical representation is in this screenshot of MySQLQueryBrowser showing the same constraint.


Another nice thing, foreign keys referencing the same table are supported in both databases, see this example:

ADD CONSTRAINT `INTEG_35` FOREIGN KEY `INTEG_35` (`head_dept`)
REFERENCES `department` (`dept_no`)
ON DELETE CASCADE
ON UPDATE RESTRICT;

Note that those constraints imply an index with the same name on the referencing field.
Of course unique constraints are supported, like:


ALTER TABLE `employee`.`department` ADD UNIQUE `INTEG_30`(`department`);

Multi-column constraints also work fine in both databases:

ALTER TABLE `employee`.`employee` ADD CONSTRAINT `INTEG_54` FOREIGN KEY `INTEG_54` (`job_code`, `job_grade`, `job_country`)
REFERENCES `job` (`job_code`, `job_grade`, `job_country`)
ON DELETE RESTRICT
ON UPDATE RESTRICT;

No comments: