如何在插入过程中插入引用与另一个表的关系的外键。
我正在尝试在乐队表中插入带有现有乐队的 CD 'duck'。
INSERT INTO CD (TITLE, YEAR) VALUES ('Ducks', 2013);
我的应用程序是 Java 并且有 2 个类:
Band class:
String name
int year
ArrayList<String> members
CD class:
String title
int year
SQL 表
CREATE TABLE BAND
(
BAND_ID INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(50),
YEAR INT,
PRIMARY KEY(BAND_ID)
);
CREATE TABLE CD
(
CD_ID INT NOT NULL AUTO_INCREMENT,
TITLE VARCHAR(50),
YEAR INT,
B_ID INT,
PRIMARY KEY(CD_ID),
FOREIGN KEY(B_ID) REFERENCES BAND(BAND_ID)
);
CREATE TABLE MEMBER
(
MEMBER_ID INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(50),
B_ID INT,
PRIMARY KEY(MEMBER_ID),
FOREIGN KEY(B_ID) REFERENCES BAND(BAND_ID)
);