如何在插入過程中插入引用與其他表的關係的外鍵。MYSQL插入外鍵
我想插入與現有樂隊在樂隊表中的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)
);
你能澄清你想做什麼嗎?插入一張不存在的樂隊的CD? – Nick
插入現有樂隊的CD,抱歉 – user1352609