我正在尝试创建具有特定限制的表,并且我不允许使用触发器。
我正在使用Oracle SQL。
限制是:
Bolded 2和3是我遇到问题的地方。
CREATE TABLE STUDENT(
STUDENT_ID NUMERIC(9),
STUDENT_NAME VARCHAR(32) NOT NULL,
STUDENT_ADDRESS VARCHAR(50),
MAJOR VARCHAR(4),
CONSTRAINT student_id_pk PRIMARY KEY(STUDENT_ID)
);
CREATE TABLE COURSE(
COURSE_ID VARCHAR(6),
COURSE_TITLE VARCHAR(32) NOT NULL,
INSTRUCTOR_ID NUMERIC(3) UNIQUE,
CONSTRAINT course_id_pk PRIMARY KEY(COURSE_ID),
CONSTRAINT instructor_id_fk FOREIGN KEY (INSTRUCTOR_ID) REFERENCES INSTRUCTOR(INSTRUCTOR_ID)
);
CREATE TABLE INSTRUCTOR(
INSTRUCTOR_ID NUMERIC(3),
INSTRUCTOR_NAME VARCHAR(50) NOT NULL,
INSTRUCTOR_OFFICE VARCHAR(4),
CONSTRAINT instructor_id_pk PRIMARY KEY(INSTRUCTOR_ID)
);
CREATE TABLE STU_CRSE(
STUDENT_ID NUMERIC (9),
COURSE_ID VARCHAR(6),
GRADE CHAR(1),
CONSTRAINT grade_range CHECK(GRADE IN ('A','B','C','D','F')),
CONSTRAINT stu_crs_pk PRIMARY KEY (STUDENT_ID,COURSE_ID),
CONSTRAINT crs_fk FOREIGN KEY (COURSE_ID) REFERENCES COURSE(COURSE_ID),
CONSTRAINT str_fk FOREIGN KEY (STUDENT_ID) REFERENCES STUDENT(STUDENT_ID),
--
--Below are the constraints I'm having trouble with.
--
CONSTRAINT stu_limit CHECK (COUNT(STU_CRSE.STUDENT_ID)<5),
CONSTRAINT stu_crse_limit CHECK (count(STU_CRSE.STUDENT_ID,STU_CRSE.COURSE_ID)<26)
);
答案 0 :(得分:3)
您可以尝试:
CREATE MATERIALIZED VIEW LOG ON STU_CRSE
WITH SEQUENCE, ROWID(course_id, student_id)
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW STU_CRSE__NUM_STUDENTS__MV
BUILD IMMEDIATE
REFRESH FAST ON COMMIT
AS SELECT COURSE_ID,
COUNT( STUDENT_ID ) AS num_students
FROM STU_CRSE
GROUP BY COURSE_ID;
ALTER TABLE STU_CRSE__NUM_STUDENTS__MV
ADD CONSTRAINT STU_CRSE__NUM_STU__MV__COUNT CHECK ( num_students <= 25 );
CREATE MATERIALIZED VIEW STU_CRSE__NUM_COURSES__MV
BUILD IMMEDIATE
REFRESH FAST ON COMMIT
AS SELECT STUDENT_ID,
COUNT( COURSE_ID ) AS num_courses
FROM STU_CRSE
GROUP BY STUDENT_ID;
ALTER TABLE STU_CRSE__NUM_COURSES__MV
ADD CONSTRAINT STU_CRSE__NUM_CRS__MV__COUNT CHECK ( num_courses <= 4 );
<强>测试强>:
INSERT INTO STU_CRSE
SELECT 1, LEVEL, 'A' FROM DUAL CONNECT BY LEVEL <= 4;
4 rows inserted.
COMMIT;
Commit complete.
INSERT INTO STU_CRSE VALUES (1, 5, 'A' );
1 row inserted.
COMMIT;
Error report -
SQL Error: ORA-12048: error encountered while refreshing materialized view "TEST"."STU_CRSE__NUM_COURSES__MV"
ORA-02290: check constraint (TEST.STU_CRSE__NUM_CRS__MV__COUNT) violated
答案 1 :(得分:0)
制定约束的简单解决方案是:
将整数STUDENT_LFDNR和COURSE_LFDNR添加到STU_CRSE
在(STUDENT_ID,STUDENT_LFDNR),(COURSE_ID,COURSE_LFDNR)上添加唯一键
在STUDENT_LFDNR上添加constarint(&gt; 0&amp;&lt; = 4)
在COURSE_LFDNR上添加约束(&gt; 0&amp;&lt; = 25)
但与此合作会很痛苦......