久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

強制執行依賴于父列值的復合唯一約束

Enforce composite unique constraint that depends on parent column value(強制執行依賴于父列值的復合唯一約束)
本文介紹了強制執行依賴于父列值的復合唯一約束的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

使用提供的架構,我想以某種方式強制每個顯示都有唯一的 reserved_seat:seat_id.換句話說,如果該放映中已經預訂了特定座位,則您無法預訂該座位.

With provided schema i want to somehow enforce that there is unique reserved_seat:seat_id per showing. In other words you can't reserve specific seat if it is already reserved in that showing.

一種選擇是同時將showing_id添加到reservation_seat(這是多余的),然后對(showing_id,seat_id)進行唯一約束.

One option is to also add showing_id to reservation_seat (which is redundant) and then make unique constraint on (showing_id, seat_id).

這可以在 sql 中完成還是落在應用程序代碼中?

Can this be done in sql or it falls to application code?

DDL:

CREATE TABLE showing
(
    id              INT  NOT NULL  AUTO_INCREMENT,
    name            VARCHAR(45) NOT NULL,
    PRIMARY KEY (id)
)

CREATE TABLE reservation
(
    id              INT  NOT NULL  AUTO_INCREMENT,
    showing_id      INT  NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (showing_id) REFERENCES showing(id)
)

CREATE TABLE reservation_seat
(
    id              INT  NOT NULL  AUTO_INCREMENT,
    reservation_id  INT  NOT NULL,
    seat_id         INT  NOT NULL,
    confirmed       TINYINT,
    PRIMARY KEY (id),
    FOREIGN KEY (reservation_id) REFERENCES reservation(id),
    FOREIGN KEY (seat_id) REFERENCES seat(id)
)

CREATE TABLE seat
(
    id              INT  NOT NULL  AUTO_INCREMENT,
    row             VARCHAR(45) NOT NULL,
    column          VARCHAR(45) NOT NULL,
    PRIMARY KEY (id)
)

推薦答案

我相信這是使用代理鍵(auto_increment id's)而不是自然鍵導致您誤入歧途的罕見情況之一.考慮一下如果您使用自然鍵,您的表定義會是什么樣子:

I believe that this is one of those rare cases where the use of surrogate keys (auto_increment id's) instead of natural keys has led you astray. Consider how your table definitions would look if you used natural keys instead:

CREATE TABLE showing
(
    name            VARCHAR(45) NOT NULL,   -- globally unique
    PRIMARY KEY (name)
)

CREATE TABLE reservation
(
    showing_name    VARCHAR(45) NOT NULL,
    name            VARCHAR(45) NOT NULL,   -- only unique within showing_name
    PRIMARY KEY (name, showing_name),
    FOREIGN KEY (showing_name) REFERENCES showing(name)
)

CREATE TABLE reservation_seat
(
    showing_name    VARCHAR(45) NOT NULL,
    reservation_name VARCHAR(45) NOT NULL,
    seat_row        VARCHAR(45) NOT NULL,
    seat_column     VARCHAR(45) NOT NULL,
    confirmed       TINYINT,
    PRIMARY KEY (showing_name, reservation_name, seat_row, seat_column),
    FOREIGN KEY (showing_name, reservation_name) REFERENCES reservation(showing_name, name),
    FOREIGN KEY (seat_row, seat_column) REFERENCES seat(row, column)
)

現在,您可以將每個顯示約束的預留座位添加為reservation_seat 上的備用鍵:

Now you can add your reserved seat per showing constraint as an Alternate Key on reservation_seat:

CREATE TABLE reservation_seat
(
    showing_name    VARCHAR(45) NOT NULL,
    reservation_name VARCHAR(45) NOT NULL,
    seat_row        VARCHAR(45) NOT NULL,
    seat_column     VARCHAR(45) NOT NULL,
    confirmed       TINYINT,
    PRIMARY KEY (showing_name, reservation_name, seat_row, seat_column),
    FOREIGN KEY (showing_name, reservation_name) REFERENCES reservation(showing_name, name),
    FOREIGN KEY (seat_row, seat_column) REFERENCES seat(row, column),
    CONSTRAINT UC_seat_showing_reserved UNIQUE(showing_name, seat_row, seat_column)
)

然而,這清楚地表明主鍵是多余的,因為它只是我們添加的約束的較弱版本,所以我們應該用我們的新約束替換它.

However, this makes it clear that the primary key is superfluous because it's just a weaker version of the constraint that we have added, so we should replace it with our new constraint.

CREATE TABLE reservation_seat
(
    showing_name    VARCHAR(45) NOT NULL,
    reservation_name VARCHAR(45) NOT NULL,
    seat_row        VARCHAR(45) NOT NULL,
    seat_column     VARCHAR(45) NOT NULL,
    confirmed       TINYINT,
    PRIMARY KEY (showing_name, seat_row, seat_column),
    FOREIGN KEY (showing_name, reservation_name) REFERENCES reservation(showing_name, name),
    FOREIGN KEY (seat_row, seat_column) REFERENCES seat(row, column)
)

我們現在可能會擔心,我們的reservation_seat 可能會引用與reservation_seat 本身不同的showing_id 的預訂,但這對于自然鍵來說不是問題,因為第一個外鍵引用阻止了這種情況.

We may worry now that our reservation_seat could be referencing a reservation with a different showing_id than the reservation_seat itself, but that's not a problem for natural keys because the first foreign key reference prevents that.

現在我們需要做的就是將其轉換回代理鍵:

Now all we need to do is to translate this back into surrogate keys:

CREATE TABLE reservation_seat
(
    id              INT  NOT NULL  AUTO_INCREMENT,
    showing_id      INT  NOT NULL,
    reservation_id  INT  NOT NULL,
    seat_id         INT  NOT NULL,
    confirmed       TINYINT,
    PRIMARY KEY (id),
    FOREIGN KEY (showing_id, reservation_id) REFERENCES reservation(showing_id, id),
    FOREIGN KEY (seat_id) REFERENCES seat(id),
    CONSTRAINT UC_seat_showing_reserved UNIQUE(showing_id, seat_id)
)

因為我們將reservation_seat(id) 作為主鍵,我們必須將命名的PK 定義改回唯一約束.與您的原始預訂座位定義相比,我們最終添加了 Showing_id,但通過修改后的更強的第一個外鍵定義,我們現在確保預訂座位在放映中是唯一的,并且預訂座位不能具有與其父預訂不同的顯示 ID.

Because we're making the reservation_seat(id) the primary key, we have to change the named PK definition back into a unique constraint. Compared to your original reservation_seat definition, we end up with showing_id added, but with the modified stronger first foreign key definition we now insure both that reservation_seat are unique within a showing and that reservation_seat cannot have a showing_id different from its parent reservation.

(注意:您可能需要在上面的 SQL 代碼中引用行"和列"列名稱)

(Note: you will probably have to quote the 'row' and 'column' column names in the SQL code above)

附加說明: DBMS 對此有所不同(在這種情況下我不確定 MySql),但許多會要求外鍵關系在目標上具有相應的主鍵或唯一約束(參考)表.這意味著您必須使用新的約束來更改 reservation 表,例如:

Additional Note: DBMS's vary on this (and I am not sure about MySql in this case), but many will require that a Foreign Key relation have a corresponding Primary Key or Unique Constraint on the target (referenced) table. This would mean that you would have to alter the reservation table with a new constraint like:

CONSTRAINT UC_showing_reserved UNIQUE(showing_id, id)

匹配我上面建議的 reservation_seat 上的新 FK 定義:

to match the new FK definition on reservation_seat that I suggested above:

FOREIGN KEY (showing_id, reservation_id) REFERENCES reservation(showing_id, id),

從技術上講,這將是一個冗余約束,因為它是保留表上主鍵的較弱版本,但在這種情況下,SQL 可能仍需要它來實現 FK.

Technically, this would be a redundant constraint since it is a weaker version of the primary key on the reservation table, but in this case SQL would probably still require it to implement the FK.

這篇關于強制執行依賴于父列值的復合唯一約束的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數據)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 午夜91| 欧洲视频一区二区 | 超碰在线人 | 色在线免费视频 | 欧美一区二区三区四区五区无卡码 | 九九久久精品视频 | 欧美视频在线播放 | 国产精品久久久久久久久久久久久 | 色偷偷噜噜噜亚洲男人 | 高清免费av | 在线国产精品一区 | 99精品在线观看 | 在线不卡av | 秋霞在线一区 | 五月婷婷在线播放 | 国产一级黄色网 | 久久精品网 | 人人九九精 | 亚洲精品久久国产高清情趣图文 | 精品视频在线一区 | 一区二区三区在线观看免费视频 | 欧美精品在线免费 | 亚洲欧洲一区 | 欧美中文字幕一区二区 | 男女激情网| 91超碰在线| 天天躁日日躁狠狠躁2018小说 | 欧美高清视频 | 亚洲精品视频免费 | 欧美在线视频网 | 亚洲 一区 | 日韩视频在线观看中文字幕 | 日韩三级| 日韩免费av网站 | 91在线精品秘密一区二区 | 黄色成人在线网站 | 精品国产不卡一区二区三区 | 欧美成人a∨高清免费观看 欧美日韩中 | 日韩成人在线播放 | 色综合av | 国产精品免费在线 |