我明白该怎么做:
create table album2 (id int, title varchar(20), key (id));
create table track2 (
track_id int primary key,
album_id int not null,
title varchar(100),
foreign key (album_id) references album2 (id)
);
mysql> show create table album2;
+--------+-----------------------------
| Table | Create Table
+--------+-----------------------------
| album2 | CREATE TABLE `album2` (
`id` int(11) DEFAULT NULL,
`title` varchar(20) DEFAULT NULL,
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+-----------------------------
mysql> show create table track2;
+--------+-----------------------------------------------------------------------
| Table | Create Table
+--------+-----------------------------------------------------------------------
| track2 | CREATE TABLE `track2` (
`track_id` int(11) NOT NULL,
`album_id` int(11) NOT NULL,
`title` varchar(100) DEFAULT NULL,
PRIMARY KEY (`track_id`),
KEY `album_id` (`album_id`),
CONSTRAINT `track2_ibfk_1` FOREIGN KEY (`album_id`) REFERENCES `album2` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+-----------------------------------------------------------------------
我只需要在专辑 2 中创建非唯一键。