40 likes | 215 Views
Referential Integrity MySQL. CREATE TABLE exmp1( id INT NOT NULL, val TEXT, UNIQUE (id) ) TYPE=InnoDB; CREATE TABLE exmp2( id INT, blah TEXT, INDEX(id), CONSTRAINT id_fkey FOREIGN KEY (id) REFERENCES exmp1(id) ON UPDATE CASCADE ) TYPE=InnoDB;. Examples:Zoo animals.
E N D
Referential Integrity MySQL • CREATE TABLE exmp1( id INT NOT NULL, val TEXT, UNIQUE (id) ) TYPE=InnoDB; • CREATE TABLE exmp2( id INT, blah TEXT, INDEX(id), CONSTRAINT id_fkey FOREIGN KEY (id) REFERENCES exmp1(id) ON UPDATE CASCADE ) TYPE=InnoDB;
Examples:Zoo animals • CREATE TABLE species (id TINYINT NOT NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL, PRIMARY KEY(id)) TYPE=INNODB; • INSERT INTO species VALUES (1, 'orangutan'), (2, 'elephant'), (3, 'hippopotamus'), (4, 'yak'); • CREATE TABLE zoo (id INT(4) NOT NULL, name VARCHAR(50) NOT NULL, FK_species TINYINT(4) NOT NULL, INDEX (FK_species), constraint FOREIGN KEY (FK_species) REFERENCES species (id) on update cascade, PRIMARY KEY(id)) TYPE=INNODB; • INSERT INTO zoo VALUES (1, 'Harry', 5);//error:violate R.I. • INSERT INTO zoo VALUES (1, 'Harry', 2);//OK • Update species set id=5 where name=‘elephant’;//change zoo too