我为自己创建了这个小怪物 php 脚本,它从一些速记 txt 文件生成 SQL DDL,现在它可以生成历史表及其CREATE TRIGGER
语句。
http://simpleddl.coolpage.biz/
示例来源:
// ZZZ means "Create History Table + Triggers"
// you can try editing it live at the link above
!DROP
= ID id P AI
person ZZZ
ID
name _
N mother_id -> person
N father_id -> person
!FK mother_id, father_id -> family
family ZZZ
P female_id -> person
P male_id -> person
生成的 SQL DDL:
DROP TABLE IF EXISTS person;
CREATE TABLE IF NOT EXISTS person (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
mother_id INT NULL,
father_id INT NULL,
PRIMARY KEY ( id ),
FOREIGN KEY ( mother_id ) REFERENCES person( id ),
FOREIGN KEY ( father_id ) REFERENCES person( id ),
FOREIGN KEY ( mother_id, father_id ) REFERENCES family( female_id, male_id )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
DROP TABLE IF EXISTS family;
CREATE TABLE IF NOT EXISTS family (
female_id INT NOT NULL,
male_id INT NOT NULL,
PRIMARY KEY ( female_id, male_id ),
FOREIGN KEY ( female_id ) REFERENCES person( id ),
FOREIGN KEY ( male_id ) REFERENCES person( id )
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS zz_person;
CREATE TABLE IF NOT EXISTS zz_person (
id INT NOT NULL,
name VARCHAR(255) NOT NULL,
mother_id INT NULL,
father_id INT NULL,
_zz_id INT NOT NULL AUTO_INCREMENT,
_zz_op CHAR(1) NOT NULL,
_zz_date datetime NOT NULL,
PRIMARY KEY ( _zz_id ),
INDEX P ( id ),
INDEX ( father_id, mother_id ),
INDEX ( _zz_op ),
INDEX ( _zz_date )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
DROP TABLE IF EXISTS zz_family;
CREATE TABLE IF NOT EXISTS zz_family (
female_id INT NOT NULL,
male_id INT NOT NULL,
_zz_id INT NOT NULL AUTO_INCREMENT,
_zz_op CHAR(1) NOT NULL,
_zz_date datetime NOT NULL,
PRIMARY KEY ( _zz_id ),
INDEX P ( male_id, female_id ),
INDEX ( _zz_op ),
INDEX ( _zz_date )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
DROP TRIGGER IF EXISTS INSERT_ON_person;
CREATE TRIGGER INSERT_ON_person
AFTER INSERT ON person
FOR EACH ROW INSERT INTO
zz_person ( id, name, mother_id, father_id, _zz_date, _zz_op )
VALUES ( NEW.id, NEW.name, NEW.mother_id, NEW.father_id, NOW() , 'i' );
DROP TRIGGER IF EXISTS UPDATE_ON_person;
CREATE TRIGGER UPDATE_ON_person
AFTER UPDATE ON person
FOR EACH ROW INSERT INTO
zz_person ( id, name, mother_id, father_id, _zz_date, _zz_op )
VALUES ( NEW.id, NEW.name, NEW.mother_id, NEW.father_id, NOW() , 'u' );
DROP TRIGGER IF EXISTS DELETE_ON_person;
CREATE TRIGGER DELETE_ON_person
AFTER DELETE ON person
FOR EACH ROW INSERT INTO
zz_person ( id, name, mother_id, father_id, _zz_date, _zz_op )
VALUES ( OLD.id, OLD.name, OLD.mother_id, OLD.father_id, NOW() , 'd' );
DROP TRIGGER IF EXISTS INSERT_ON_family;
CREATE TRIGGER INSERT_ON_family
AFTER INSERT ON family
FOR EACH ROW INSERT INTO
zz_family ( female_id, male_id, _zz_date, _zz_op )
VALUES ( NEW.female_id, NEW.male_id, NOW() , 'i' );
DROP TRIGGER IF EXISTS UPDATE_ON_family;
CREATE TRIGGER UPDATE_ON_family
AFTER UPDATE ON family
FOR EACH ROW INSERT INTO
zz_family ( female_id, male_id, _zz_date, _zz_op )
VALUES ( NEW.female_id, NEW.male_id, NOW() , 'u' );
DROP TRIGGER IF EXISTS DELETE_ON_family;
CREATE TRIGGER DELETE_ON_family
AFTER DELETE ON family
FOR EACH ROW INSERT INTO
zz_family ( female_id, male_id, _zz_date, _zz_op )
VALUES ( OLD.female_id, OLD.male_id, NOW() , 'd' );
测试小提琴:
http://sqlfiddle.com/#!2/d05e6/1