我有三张 table 。
Person (id, FirstName, LastName, BirthDate)
Contact (id, contact, type)
PersonContact( Person_id, Contact_id )
如您所见,Person_id 来自 PersonContact 表中的 Person 表 Contact_id 来自 PersonContact 表中的 Contact 表
我想编写一个查询,将 PersonContact 表的 Person_id 列与 Person 表的 id 列作为外键关联起来。
PersonContact 的 Contact_id 列以 Contact 表的 id 列作为外键
最佳答案
这应该可以解决问题
CREATE TABLE IF NOT EXISTS `person_contact` (
`contact_id` int(11) NOT NULL,
`person_id` int(11) NOT NULL,
PRIMARY KEY (`contact_id`,`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `person_contact`
ADD CONSTRAINT `person_contact_ibfk_2` FOREIGN KEY (`person_id`) REFERENCES `person` (`id`),
ADD CONSTRAINT `person_contact_ibfk_1` FOREIGN KEY (`contact_id`) REFERENCES `contact` (`id`);
关于mysql - 使用外键更改表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5705197/