Mysql 由多个列和空值唯一

Mysql 由多个列和空值唯一,mysql,symfony-1.4,Mysql,Symfony 1.4,我正在使用symfony 1.4(和Doctrine)进行开发,并且有一个MySQL数据库表,在多个列上有一个唯一的索引。首先,到目前为止表的YAML定义: Campaign: actAs: Sluggable: fields: [name] canUpdate: true uniqueBy: [merchant_id, deleted_at] Timestampable: ~ SoftDelete: ~ columns:

我正在使用symfony 1.4(和Doctrine)进行开发,并且有一个MySQL数据库表,在多个列上有一个唯一的索引。首先,到目前为止表的YAML定义:

Campaign:
  actAs:
    Sluggable:
      fields: [name]
      canUpdate: true
      uniqueBy: [merchant_id, deleted_at]
    Timestampable: ~
    SoftDelete: ~
  columns:
    merchant_id:      { type: integer, notnull: true }
    name:             { type: string(255), notnull: true, notblank: true }
    start_date:       { type: date, notnull: true, notblank: true }
    end_date:         { type: date, notnull: true, notblank: true }
  indexes:
    unique_name:  { fields: [name, merchant_id, deleted_at], type: unique }
  relations:
    Merchant: { local: merchant_id, foreign: id }
如你所见,我必须处理属于商人的活动。活动了解其商户并有名称(以及开始日期和结束日期)。活动的名称应该是唯一的——不是全球性的,而是针对特定的商户。到目前为止,我需要一个关于活动名称和相应商户的唯一索引。但是,由于表“充当软删除”,并且用户应该能够使用“软删除”活动的名称创建新活动,因此处的
deleted\u列也必须是唯一索引的一部分。您知道,活动名称的唯一性只与相应商户的已删除活动无关

现在来看实际问题:由于所有未删除的活动的
删除列都为空,并且唯一索引中的空值始终被视为唯一,因此所有活动都允许具有非唯一名称——真正意义上的非唯一名称。我知道,这适用于MyISAM和InnoDB表,但不适用于BDB表。然而,如果你知道我的意思,那么切换到BDB并不是我最喜欢的选择

现在进入实际问题:除了将MySQL引擎更改为BDB之外,还有哪些其他可能的选项?解决方法可以是重命名软删除的活动,例如,
name='deleted AT'+deleted_AT+':'+name
。一方面,这样做的好处是,即使在恢复软删除活动的情况下(将
处的
已删除活动重置为NULL),所有软删除活动都应有唯一的名称。在列中的
deleted_将不再是唯一索引的一部分,因此,所有活动(未删除、软删除以及一次恢复)都将有一个唯一的名称——关于各自的商户。但是,另一方面,我不认为这是最优雅的解决方案。你对此有什么看法和专业知识

我非常感谢您,并对您的贡献感到高兴。

Flinsch.

我想你可以保留你的基本结构,你只需要一种方法使deleted_不为NULL。这意味着您需要给它一个默认值。一个好的默认值是0,或0000-00-00:00:00

我的建议是添加一个新列来标记行是否被逻辑删除。你可以称之为“已删除”。然后添加默认的deleted_at并使其非null,并且include is_deleted在您的唯一索引中

下面是一个非常简单的示例,说明了这种方法的实际应用:

mysql> create table merchant(
    -> id int unsigned not null auto_increment,
    -> name varchar(50) not null, 
    -> is_deleted tinyint not null default 0, 
    -> deleted_at datetime not null default 0,
    -> primary key (id),
    -> unique key name_and_deleted_at (name,is_deleted,deleted_at)
    -> ) ENGINE = InnoDB;
Query OK, 0 rows affected (0.08 sec)

mysql> 
mysql> -- successful inserts
mysql> insert into merchant (name,is_deleted) values ('foo',0);
Query OK, 1 row affected (0.00 sec)

mysql> insert into merchant (name,is_deleted) values ('bar',0);
Query OK, 1 row affected (0.00 sec)

mysql> 
mysql> -- insert failure due to duplicate name
mysql> insert into merchant (name,is_deleted) values ('foo',0);
ERROR 1062 (23000): Duplicate entry 'foo-0-0000-00-00 00:00:00' for key 'name_and_deleted_at'
mysql> -- logical delete
mysql> update merchant set is_deleted = true, deleted_at = now() where name = 'foo';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> -- now the insert succeeds
mysql> insert into merchant (name,is_deleted) values ('foo',0);
Query OK, 1 row affected (0.01 sec)

mysql> 
mysql> -- show data
mysql> select id,name,is_deleted,deleted_at 
    -> from merchant
    -> order by id;
+----+------+------------+---------------------+
| id | name | is_deleted | deleted_at          |
+----+------+------------+---------------------+
|  1 | foo  |          1 | 2010-11-05 13:54:17 |
|  2 | bar  |          0 | 0000-00-00 00:00:00 |
|  4 | foo  |          0 | 0000-00-00 00:00:00 |
+----+------+------------+---------------------+
3 rows in set (0.00 sec)

我认为你可以保持你的基本结构,你只需要一种方法,使删除的_不为空。这意味着您需要给它一个默认值。一个好的默认值是0,或0000-00-00:00:00

我的建议是添加一个新列来标记行是否被逻辑删除。你可以称之为“已删除”。然后添加默认的deleted_at并使其非null,并且include is_deleted在您的唯一索引中

下面是一个非常简单的示例,说明了这种方法的实际应用:

mysql> create table merchant(
    -> id int unsigned not null auto_increment,
    -> name varchar(50) not null, 
    -> is_deleted tinyint not null default 0, 
    -> deleted_at datetime not null default 0,
    -> primary key (id),
    -> unique key name_and_deleted_at (name,is_deleted,deleted_at)
    -> ) ENGINE = InnoDB;
Query OK, 0 rows affected (0.08 sec)

mysql> 
mysql> -- successful inserts
mysql> insert into merchant (name,is_deleted) values ('foo',0);
Query OK, 1 row affected (0.00 sec)

mysql> insert into merchant (name,is_deleted) values ('bar',0);
Query OK, 1 row affected (0.00 sec)

mysql> 
mysql> -- insert failure due to duplicate name
mysql> insert into merchant (name,is_deleted) values ('foo',0);
ERROR 1062 (23000): Duplicate entry 'foo-0-0000-00-00 00:00:00' for key 'name_and_deleted_at'
mysql> -- logical delete
mysql> update merchant set is_deleted = true, deleted_at = now() where name = 'foo';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> -- now the insert succeeds
mysql> insert into merchant (name,is_deleted) values ('foo',0);
Query OK, 1 row affected (0.01 sec)

mysql> 
mysql> -- show data
mysql> select id,name,is_deleted,deleted_at 
    -> from merchant
    -> order by id;
+----+------+------------+---------------------+
| id | name | is_deleted | deleted_at          |
+----+------+------------+---------------------+
|  1 | foo  |          1 | 2010-11-05 13:54:17 |
|  2 | bar  |          0 | 0000-00-00 00:00:00 |
|  4 | foo  |          0 | 0000-00-00 00:00:00 |
+----+------+------------+---------------------+
3 rows in set (0.00 sec)

艾克,谢谢你的回复。我也有一个非常相似的想法。我认为不需要额外的标志
is_deleted
,因为我可以检查
deleted_at
是否为0。然而,我之前一直回避它,因为我不知道手动将
deleted_at
设置为非空值是否会导致对象列表生成等方面的不可预见的后果——因为
deleted_at
类似于symfony/doctor的“魔术栏”。但我想我会试试……:)MySQL 5.7可以在虚拟列方面提供帮助!:艾克,谢谢你的回复。我也有一个非常相似的想法。我认为不需要额外的标志
is_deleted
,因为我可以检查
deleted_at
是否为0。然而,我之前一直回避它,因为我不知道手动将
deleted_at
设置为非空值是否会导致对象列表生成等方面的不可预见的后果——因为
deleted_at
类似于symfony/doctor的“魔术栏”。但我想我会试试……:)MySQL 5.7可以在虚拟列方面提供帮助!: