sql >> Database teknologi >  >> RDS >> Mysql

MySQL udenlandsk nøgle PÅ SLET CASCADE på tværs af 3 borde

Det er nok at have ON SLET CASCADE mulighed. Tag et kig på dette eksempel:

Opret og udfyld tabeller:

CREATE TABLE molecule (
  id INT(11) NOT NULL,
  PRIMARY KEY (id)
)
ENGINE = INNODB;

CREATE TABLE descriptor (
  id INT(11) NOT NULL,
  molecule_id INT(11) DEFAULT NULL,
  PRIMARY KEY (id),
  CONSTRAINT FK_descriptor_molecule_id FOREIGN KEY (molecule_id)
    REFERENCES molecule(id) ON DELETE CASCADE ON UPDATE RESTRICT
)
ENGINE = INNODB;

CREATE TABLE tdepdescriptor (
  id INT(11) NOT NULL,
  descriptor_id INT(11) DEFAULT NULL,
  PRIMARY KEY (id),
  CONSTRAINT FK_tdepdescriptor_descriptor_id FOREIGN KEY (descriptor_id)
    REFERENCES descriptor(id) ON DELETE CASCADE ON UPDATE RESTRICT
)
ENGINE = INNODB;

INSERT INTO molecule VALUES 
  (1),
  (2),
  (3);

INSERT INTO descriptor VALUES 
  (1, 1),
  (2, 1),
  (3, 2);

INSERT INTO tdepdescriptor VALUES 
  (1, 1),
  (2, 2),
  (3, 3);

Slet ét molekyle og hele dets deskriptor og hele dets tdepdescriptor:

DELETE FROM molecule WHERE id = 1;

SELECT * FROM molecule;
+----+
| id |
+----+
|  2 |
|  3 |
+----+

SELECT * FROM descriptor;
+----+-------------+
| id | molecule_id |
+----+-------------+
|  3 |           2 |
+----+-------------+

SELECT * FROM tdepdescriptor;
+----+---------------+
| id | descriptor_id |
+----+---------------+
|  3 |             3 |
+----+---------------+


  1. zend ramme sammenføj 3 borde

  2. Hvordan forbindes i java som SYS til Oracle?

  3. Hurtigt at finde forskellige værdier

  4. ClassNotFoundException:com.mysql.jdbc.GoogleDriver