mysql给表添加外键并查询

时间:2021-08-15 05:53:59
CREATE TABLE `heart` (
`heart_ID` int(255) NOT NULL AUTO_INCREMENT,
`heart_name` varchar(255) CHARACTER SET utf8 NOT NULL,
`heat_anthor` varchar(255) CHARACTER SET utf8 NOT NULL,
`heart_path` varchar(255) CHARACTER SET utf8 NOT NULL,
`heat_time` date NOT NULL,
`guanlianIndex` int(255) NOT NULL,
PRIMARY KEY (`heart_ID`),
FOREIGN KEY (guanlianIndex) REFERENCES music(music_ID)#建立外键与music表中的主键music_ID建立约束
)
// 名字,作者,风格(验证查询音乐表中的字段)
SELECT music.music_name,music.music_author,music.music_style
FROM music,heart
WHERE heart.guanlianIndex=music.music_ID;