是否可以在 oracle table 中使多个列组合起来唯一?
Is it possible to make more than one columns combinedly unique in a oracle table?
问题可能不是那么好理解
我创建了两个 tables - 电影 (FILMAS) 和电影院 (ZALES)。他们之间的关系是 M:M,因此 table 会话(SEANSI)是为 link 他们创建的。
是否可以保证同一日期同一时间同一影厅内不能有两场不同的场次?怎么样?
Oracle 18c XE 数据库
CREATE SEQUENCE zlid_seq;
CREATE TABLE ZALES(
Z_ID INTEGER DEFAULT zlid_seq.NEXTVAL CONSTRAINT ZL_PK PRIMARY KEY,
ZNOSAUKUMS VARCHAR2(10) NOT NULL UNIQUE,
IETILPIBA INTEGER NOT NULL,
ATVERTS BOOLEAN DEFAULT(YES),
ID_K INTEGER,
CONSTRAINT SAITE_KT FOREIGN KEY (ID_K) REFERENCES KINOTEATRI(K_ID)
);
CREATE TABLE FILMAS(
F_ID INTEGER CONSTRAINT FM_PK PRIMARY KEY,
FNOSAUKUMS VARCHAR2(20) NOT NULL UNIQUE,
PR_DATUMS DATE NOT NULL,
ZANRS VARCHAR(20) NOT NULL,
CONSTRAINT FM_ZN CHECK (ZANRS IN ('ANIMACIJAS','KOMEDIJA','PIEDZIVOJUMU','ŠAUSMU','VESTURISKAIS','DRAMA'))
);
CREATE TABLE SEANSI(
SDATUMS DATE NOT NULL,
LAIKS TIME NOT NULL,
ID_F INTEGER,
ID_Z INTEGER,
VALODA VARCHAR(2) DEFAULT 'LATVIESU' NOT NULL,
CONSTRAINT FM_VAL CHECK (VALODA IN ('LATVIESU', 'KRIEVU', 'ANGLU')),
CONSTRAINT SAITE_S_FM FOREIGN KEY (ID_F) REFERENCES FILMAS(F_ID),
CONSTRAINT SAITE_S_ZL FOREIGN KEY (ID_Z) REFERENCES ZALES(Z_ID)
);
是对(日期,时间,ID_z)的唯一约束
CREATE TABLE SEANSI(
SDATUMS DATE NOT NULL,
LAIKS TIME NOT NULL,
ID_F INTEGER,
ID_Z INTEGER,
VALODA VARCHAR(2) DEFAULT 'LATVIESU' NOT NULL,
CONSTRAINT FM_VAL CHECK (VALODA IN ('LATVIESU', 'KRIEVU', 'ANGLU')),
CONSTRAINT SAITE_S_FM FOREIGN KEY (ID_F) REFERENCES FILMAS(F_ID),
CONSTRAINT SAITE_S_ZL FOREIGN KEY (ID_Z) REFERENCES ZALES(Z_ID),
CONSTRAINT UQ_SEANSI UNIQUE (SDATUMS ,LAIKS, ID_z) -- here
);
Is it possible to ensure that at the same date and time there can not be two different sessions in one cinema hall? How?
您需要一个唯一约束(或索引)。您可以使用 table:
声明
CREATE TABLE SEANSI(
SDATUMS DATE NOT NULL,
LAIKS TIME NOT NULL,
ID_F INTEGER,
ID_Z INTEGER,
VALODA VARCHAR(2) DEFAULT 'LATVIESU' NOT NULL,
CONSTRAINT FM_VAL CHECK (VALODA IN ('LATVIESU', 'KRIEVU', 'ANGLU')),
CONSTRAINT SAITE_S_FM FOREIGN KEY (ID_F) REFERENCES FILMAS(F_ID),
CONSTRAINT SAITE_S_ZL FOREIGN KEY (ID_Z) REFERENCES ZALES(Z_ID),
CONSTRAINT UNQ_SEANSI_SDATUMS_LAIK_IDZ UNIQUE (SDATUMS, LAIK, ID_Z)
);
约束保证每个 date/time 和 zales 一行——所以,一部电影。
您可以在这四列上创建唯一约束。此约束将强制组合为您想要的唯一。
ALTER TABLE SEANSI
add CONSTRAINT SEANSI_unique UNIQUE (SDATUMS , LAIKS , ID_F ,ID_Z );
或者,如果您想在创建 table 时创建约束,则:
CREATE TABLE SEANSI(
SDATUMS DATE NOT NULL,
LAIKS TIME NOT NULL,
ID_F INTEGER,
ID_Z INTEGER,
VALODA VARCHAR(2) DEFAULT 'LATVIESU' NOT NULL,
CONSTRAINT FM_VAL CHECK (VALODA IN ('LATVIESU', 'KRIEVU', 'ANGLU')),
CONSTRAINT SAITE_S_FM FOREIGN KEY (ID_F) REFERENCES FILMAS(F_ID),
CONSTRAINT SAITE_S_ZL FOREIGN KEY (ID_Z) REFERENCES ZALES(Z_ID),
CONSTRAINT SEANSI_unique UNIQUE (SDATUMS , LAIKS , ID_F ,ID_Z )
);
问题可能不是那么好理解
我创建了两个 tables - 电影 (FILMAS) 和电影院 (ZALES)。他们之间的关系是 M:M,因此 table 会话(SEANSI)是为 link 他们创建的。
是否可以保证同一日期同一时间同一影厅内不能有两场不同的场次?怎么样?
Oracle 18c XE 数据库
CREATE SEQUENCE zlid_seq;
CREATE TABLE ZALES(
Z_ID INTEGER DEFAULT zlid_seq.NEXTVAL CONSTRAINT ZL_PK PRIMARY KEY,
ZNOSAUKUMS VARCHAR2(10) NOT NULL UNIQUE,
IETILPIBA INTEGER NOT NULL,
ATVERTS BOOLEAN DEFAULT(YES),
ID_K INTEGER,
CONSTRAINT SAITE_KT FOREIGN KEY (ID_K) REFERENCES KINOTEATRI(K_ID)
);
CREATE TABLE FILMAS(
F_ID INTEGER CONSTRAINT FM_PK PRIMARY KEY,
FNOSAUKUMS VARCHAR2(20) NOT NULL UNIQUE,
PR_DATUMS DATE NOT NULL,
ZANRS VARCHAR(20) NOT NULL,
CONSTRAINT FM_ZN CHECK (ZANRS IN ('ANIMACIJAS','KOMEDIJA','PIEDZIVOJUMU','ŠAUSMU','VESTURISKAIS','DRAMA'))
);
CREATE TABLE SEANSI(
SDATUMS DATE NOT NULL,
LAIKS TIME NOT NULL,
ID_F INTEGER,
ID_Z INTEGER,
VALODA VARCHAR(2) DEFAULT 'LATVIESU' NOT NULL,
CONSTRAINT FM_VAL CHECK (VALODA IN ('LATVIESU', 'KRIEVU', 'ANGLU')),
CONSTRAINT SAITE_S_FM FOREIGN KEY (ID_F) REFERENCES FILMAS(F_ID),
CONSTRAINT SAITE_S_ZL FOREIGN KEY (ID_Z) REFERENCES ZALES(Z_ID)
);
是对(日期,时间,ID_z)的唯一约束
CREATE TABLE SEANSI(
SDATUMS DATE NOT NULL,
LAIKS TIME NOT NULL,
ID_F INTEGER,
ID_Z INTEGER,
VALODA VARCHAR(2) DEFAULT 'LATVIESU' NOT NULL,
CONSTRAINT FM_VAL CHECK (VALODA IN ('LATVIESU', 'KRIEVU', 'ANGLU')),
CONSTRAINT SAITE_S_FM FOREIGN KEY (ID_F) REFERENCES FILMAS(F_ID),
CONSTRAINT SAITE_S_ZL FOREIGN KEY (ID_Z) REFERENCES ZALES(Z_ID),
CONSTRAINT UQ_SEANSI UNIQUE (SDATUMS ,LAIKS, ID_z) -- here
);
Is it possible to ensure that at the same date and time there can not be two different sessions in one cinema hall? How?
您需要一个唯一约束(或索引)。您可以使用 table:
声明CREATE TABLE SEANSI(
SDATUMS DATE NOT NULL,
LAIKS TIME NOT NULL,
ID_F INTEGER,
ID_Z INTEGER,
VALODA VARCHAR(2) DEFAULT 'LATVIESU' NOT NULL,
CONSTRAINT FM_VAL CHECK (VALODA IN ('LATVIESU', 'KRIEVU', 'ANGLU')),
CONSTRAINT SAITE_S_FM FOREIGN KEY (ID_F) REFERENCES FILMAS(F_ID),
CONSTRAINT SAITE_S_ZL FOREIGN KEY (ID_Z) REFERENCES ZALES(Z_ID),
CONSTRAINT UNQ_SEANSI_SDATUMS_LAIK_IDZ UNIQUE (SDATUMS, LAIK, ID_Z)
);
约束保证每个 date/time 和 zales 一行——所以,一部电影。
您可以在这四列上创建唯一约束。此约束将强制组合为您想要的唯一。
ALTER TABLE SEANSI
add CONSTRAINT SEANSI_unique UNIQUE (SDATUMS , LAIKS , ID_F ,ID_Z );
或者,如果您想在创建 table 时创建约束,则:
CREATE TABLE SEANSI(
SDATUMS DATE NOT NULL,
LAIKS TIME NOT NULL,
ID_F INTEGER,
ID_Z INTEGER,
VALODA VARCHAR(2) DEFAULT 'LATVIESU' NOT NULL,
CONSTRAINT FM_VAL CHECK (VALODA IN ('LATVIESU', 'KRIEVU', 'ANGLU')),
CONSTRAINT SAITE_S_FM FOREIGN KEY (ID_F) REFERENCES FILMAS(F_ID),
CONSTRAINT SAITE_S_ZL FOREIGN KEY (ID_Z) REFERENCES ZALES(Z_ID),
CONSTRAINT SEANSI_unique UNIQUE (SDATUMS , LAIKS , ID_F ,ID_Z )
);