在下表中,我希望 property_id 是唯一的,这样它就没有重复值。这是什么语法?
CREATE TABLE IF NOT EXISTS `propFeatures` (
`id` bigint(20) NOT NULL auto_increment,
`bedroom` int(10) NOT NULL,
`bathroom` int(10) NOT NULL,
`balcony` int(10) NOT NULL,
`furnished` tinyint(1) NOT NULL,
`property_id` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
最佳答案
您可以将 UNIQUE
约束添加到您的 property_id
列,以便在您需要时不允许重复。
要检查重复项,您可以运行此查询:
select property_id, count(property_id) as total from propFeatures
group by property_id having count(property_id) > 1
如果您想删除重复项,请查看此帖子以获取更多信息:
关于mysql - 我如何确保表实体中的值在 mysql 中不重复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4573521/