欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySQL设置外键

程序员文章站 2022-05-31 23:44:01
...
CREATE TABLE SPJ(
  SNO CHAR(2) NOT NULL ,
  PNO CHAR(2) NOT NULL ,
  JNO CHAR(4) NOT NULL ,
  QTY INT NOT NULL ,
  PRIMARY KEY (SNO,PNO,JNO,QTY)
);

# 设置外键
ALTER TABLE SPJ ADD CONSTRAINT fk_SPJ_SS
FOREIGN KEY (SNO) REFERENCES SS (SNO);

ALTER TABLE SPJ ADD CONSTRAINT fk_SPJ_P
FOREIGN KEY (PNO) REFERENCES P (PNO);

ALTER TABLE SPJ ADD CONSTRAINT fk_SPJ_J
FOREIGN KEY (JNO) REFERENCES J (JNO);