MySQL Phone 表:如何为每个 ID 指定唯一的主要联系人号码?

发布于 2024-09-14 23:08:43 字数 326 浏览 7 评论 0原文

我的表格如下:

CREATE TABLE IF NOT EXISTS PHONES (
    number VARCHAR(10),
    id INT,
    type VARCHAR(10),
    PRIMARY KEY (number),
    FOREIGN KEY (id)
        REFERENCES TECHNICIANS(id)
        ON DELETE CASCADE
) ENGINE = INNODB;

我想为每个 id 指定一个主要联系号码。我正在考虑添加一个布尔列,但我不知道如何让它只允许一个“真”值。 ID。

My table is as follows:

CREATE TABLE IF NOT EXISTS PHONES (
    number VARCHAR(10),
    id INT,
    type VARCHAR(10),
    PRIMARY KEY (number),
    FOREIGN KEY (id)
        REFERENCES TECHNICIANS(id)
        ON DELETE CASCADE
) ENGINE = INNODB;

I would like to specify for each id one primary contact number. I was thinking of adding a boolean column, but I can't figure out how to get it to only allow one "true" value per. id.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有心的人 2024-09-21 23:08:43

我会将技术人员的外键添加回电话:

ALTER TABLE TECHNICIANS
  ADD COLUMN primary_number VARCHAR(10),
  ADD CONSTRAINT FOREIGN KEY (primary_number) REFERENCES PHONES (number)
    ON UPDATE CASCADE
    ON DELETE SET NULL;

这将创建一个循环引用:技术人员引用电话,电话引用技术人员。这没问题,但是当您执行删除表、恢复备份等操作时,需要特殊处理。

I'd add a foreign key from TECHNICIANS back to PHONES:

ALTER TABLE TECHNICIANS
  ADD COLUMN primary_number VARCHAR(10),
  ADD CONSTRAINT FOREIGN KEY (primary_number) REFERENCES PHONES (number)
    ON UPDATE CASCADE
    ON DELETE SET NULL;

This creates a cyclical reference: technicians references phones, and phones references technicians. This is okay, but it requires special handling when you do things like dropping tables, restoring backups, etc.

梦初启 2024-09-21 23:08:43

利用MySQL中的“漏洞”。 MySQL文档

UNIQUE 索引创建约束
这样索引中的所有值都必须
与众不同。如果您执行以下操作,则会发生错误
尝试添加带有键值的新行
与现有行匹配。这
约束不适用于 NULL
除 BDB 存储之外的值
引擎。对于其他发动机,独特的
索引允许多个 NULL 值
可以包含 NULL 的列。

这意味着您可以创建一个具有两个值的布尔列:true(或 1)和 NULL。
在该列+您的键上创建一个唯一索引。这允许您仅将一条记录设置为 true,但任意数量的记录都可以为 NULL。

Use a "loophole" in MySQL. The MySQL documentation says:

A UNIQUE index creates a constraint
such that all values in the index must
be distinct. An error occurs if you
try to add a new row with a key value
that matches an existing row. This
constraint does not apply to NULL
values except for the BDB storage
engine. For other engines, a UNIQUE
index permits multiple NULL values for
columns that can contain NULL.

This means that you can create a boolean column that has two values: true (or 1) and NULL.
Create a UNIQUE index over that column + your key. That allows you to only set one record to true, but any number of them can have NULL.

窗影残 2024-09-21 23:08:43

你基本上有3个选项......

  1. 有一个布尔列,但由你的应用程序来维护它
  2. 有一个整数,所以你再次存储优先级(0=prime,1=secondary,2=tertiary,...)必须维护它
  3. 具有父子关系,因此父(技术人员?)记录具有多个子(电话号码)记录。然后,父记录还将包含主要子记录的 ID。唯一的缺点是添加记录要么需要多个步骤(添加技术人员、添加电话号码、为技术人员设置主要电话号码),要么您需要一个智能 DAL 来为您完成此操作:)

顺便说一句,我假设您实际上是指每个 TechnicianId 而不是每个 PhoneId 一个主要号码

You've basically got 3 options...

  1. have a boolean column but it's up to your application to maintain it
  2. have an integer so you store priority (0=prime, 1=secondary, 2=tertiary,...) again you'll have to maintain it
  3. Have a parent-child relationship so a parent (technician?) record has multiple child (phone number) records. The parent record would then also contain the Id of the primary child record. The only down-side is that adding records either becomes multi-step (add technician, add phone numbers, set primary phone number for technician) or you'll need a smart DAL which does it for you :)

Incidentally, I'm assuming you actually mean one primary number per TechnicianId not per PhoneId

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文