Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplified/shorthand SQL Data Definition Languages?

Some people like describing their database structure in a simple textual way instead of using CREATE TABLE statements. A few example:

  • Foreign key as Primary key
  • Multiple Primary Keys as single foreign key
  • How can I set up a database schema where there are two concurrent many-many relationships?
  • primary key and foreign key
  • How should I set up database tables for this order situation

Do you know about any software which converts this kind of shorthand notation to actual SQL statements?

like image 665
biziclop Avatar asked Mar 09 '12 20:03

biziclop


1 Answers

Actually, I just finished creating a php script, which does exactly this, but I hope there is something more professional out there...

Demo of my converter:

http://simpleddl.orgfree.com

Example input:

= ID id P AI

person
  ID
  mother_id -> person
  father_id -> person
  !FK mother_id, father_id -> family U:C, D:C

family
  P female_id -> person
  P male_id   -> person

Output:

CREATE TABLE IF NOT EXISTS person (
   id         INT NOT NULL AUTO_INCREMENT,
   mother_id  INT NOT NULL,
   father_id  INT NOT NULL,
   PRIMARY KEY ( id )
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS family (
   female_id  INT NOT NULL,
   male_id    INT NOT NULL,
   PRIMARY KEY ( female_id, male_id )
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE person ADD FOREIGN KEY ( mother_id ) REFERENCES person( id );
ALTER TABLE person ADD FOREIGN KEY ( father_id ) REFERENCES person( id );
ALTER TABLE person ADD FOREIGN KEY ( mother_id, father_id ) REFERENCES family( female_id, male_id ) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE family ADD FOREIGN KEY ( female_id ) REFERENCES person( id );
ALTER TABLE family ADD FOREIGN KEY ( male_id ) REFERENCES person( id );
like image 91
biziclop Avatar answered Oct 05 '22 23:10

biziclop