UNIQUE constraint vs checking before INSERT

后端 未结 4 497
轮回少年
轮回少年 2020-12-13 13:22

I have a SQL server table RealEstate with columns - Id, Property, Property_Value. This table has about 5-10 million rows and can increase even more in the future. I want to

4条回答
  •  暖寄归人
    2020-12-13 14:15

    The description of your problem is exactly why primary keys can be compound, e.g., they consist of multiple fields. That way, the database will handle the uniqueness for you, and you don't need to care about it.

    In your case, the table definition could be something similar to the following like:

     CREATE TABLE `real_estate` (
       `id` int(11) NOT NULL AUTO_INCREMENT,
       `property` varchar(255) DEFAULT NULL,
       `property_value` varchar(255) DEFAULT NULL,
       PRIMARY KEY (`id`),
       UNIQUE KEY `index_id_property_property_value` (`id`, `property`, `property_value`),
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
    

提交回复
热议问题