如何使用引用在mysql中编写级联查询

时间:2014-06-10 09:21:38

标签: mysql sql

这是我的查询。但它没有用。 它显示了这个错误:

  

#1064 - 您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在附近使用')'在第23行

查询:

CREATE TABLE tbl_student 
  ( 
     studentrollno     INT(11), 
     studentname       VARCHAR(255), 
     studentpassword   VARCHAR(255), 
     studentslug       VARCHAR(300), 
     studentstatus     VARCHAR(255), 
     degreetitle       VARCHAR(100), 
     batchtitle        VARCHAR(100), 
     batchautomationno VARCHAR(100), 
     PRIMARY KEY (studentrollno), 
     FOREIGN KEY (degreetitle) REFERENCES tbl_batch(degreetitle) 
        ON DELETE CASCADE ON UPDATE CASCADE, 
     FOREIGN KEY (batchtitle) REFERENCES tbl_batch(batchtitle) 
        ON DELETE CASCADE ON UPDATE CASCADE, 
     FOREIGN KEY (batchautomationno) REFERENCES tbl_batch(batchautomationno)
        ON DELETE CASCADE ON UPDATE CASCADE,
  )

这是我的批处理表

CREATE TABLE tbl_batch (
  degreeTitle varchar(100),
  batchTitle varchar(100),
  batchAutomationNo varchar(100),
  batchSlug varchar(255),
  batchStatus int(1),
  PRIMARY KEY (degreeTitle, batchTitle, batchAutomationNo),
  FOREIGN KEY (degreeTitle)
  REFERENCES tbl_degree(degreeTitle)
  ON DELETE CASCADE
  ON UPDATE CASCADE

) ENGINE=InnoDB;

1 个答案:

答案 0 :(得分:0)

  

在批处理表中.. batchTitle,degreeTitle,batchAutomationNo是主键。

您可以将复合外键映射到父组合主键。

CREATE TABLE tbl_student 
( 
    studentrollno     INT(11), 
    studentname       VARCHAR(255), 
    studentpassword   VARCHAR(255), 
    studentslug       VARCHAR(300), 
    studentstatus     VARCHAR(255), 
    degreetitle       VARCHAR(100), 
    batchtitle        VARCHAR(100), 
    batchautomationno VARCHAR(100), 
    PRIMARY KEY (studentrollno), 
    FOREIGN KEY (degreeTitle, batchTitle, batchAutomationNo) 
      REFERENCES tbl_batch(degreeTitle, batchTitle, batchAutomationNo) 
        ON DELETE CASCADE ON UPDATE CASCADE
);