database - Oracle SQL Developer - Adding Constraints on Foreign Key -
I'm new to Oracle SQL and I'm having a hard time adding an obstacle. I am trying to add obstacles in my table to implement specific business rules which only allow students to take up to 4 courses and a maximum of 25 students per class.
Please tell me what additional information I need to help me answer this question. I am in a loss ...
Make Table Grade (STU_ID int not gross enabled, CRSE_ID CHAR (9) No, enable enabled, STU_CRSE_GRADE VARCHAR2 (20) Check (STU_CRSE_GRADE = 'A' Or STU_CRSE_GRADE = 'B' or STU_CRSE_GRADE = 'C' or STU_CRSE_GRADE = 'D' or STU_CRSE_GRADE = 'F'), CONSTRAINT GRADES_PK Primary key (STU_ID, CRSE_ID), Obligation fk_Grades Foreign Key (Stu_ID) reference student, obligation fk_Grades_Crse_ID foreign key Crse_ID Reference Reference Course); No problem! See tables below:
Make table students (Stu_ID int constraint pk_Stu_ID Primary key, stu_name VARCHAR (255) No, No. Stu_Add varchar (255), Stumaj CHAR (6)); Create a Table Instructor (Instr_ID char (3) Barrier pk_Instr_ID Primary key, Instr_Name VARCHAR (255) faucet, Insta_office formatting (8)); Create table course (CRSEID four (9) barrier pk_Crse_ID primary key, CRCITETRRARAr (255) No, student name: Lai Xia Instra_ID CRAR (3) No faucet, hindered fk_Courses_Instr_ID foreign key (Instr_ID reference instructor);
foreign key alone only one-many relationships If you want to limit the "many" parts to a specific number, then you will either need it: - Do this in the application code or trigger.
- Or again redefine FK in "Help" to get the remaining target database. It's easy to implement (1), but it's easy to do the wrong: You have to be careful about the Concurrent environment to avoid race conditions. Arrange locking for:
- Let's assume that two concurrent transactions are trying to add the same student to a different course.
- The first transaction currently counters the courses associated with the student and finds that 3 of them are all good and good.
- The second transaction is the same and only sees 3 courses (because the first transaction is not committed yet).
- So both transactions think that they are more than the 'allowed number' and proceed happily, while adding both to their student-curriculum connection.
- Last result: Student is affiliated with 5 courses, violates the rule that it can be added only for 4.
To avoid this, you will need to serialize these actions, possibly updating and locating the student via SELECT ... .
By changing the key design (2) can be implemented, and then one key can limit the values. For example, to apply it, a student can take up to 4 courses, can be done this way:
Make Table Student (STUDENT_ID INT Primary Key); Create Table Course (Corio_ID INT Primary Key); Make Table STUDENT_COURSE (STUDENT_ID INT Reference Student, Course IID INT Reference Course, Course INO INT No Faucet Check (Corosive_NA in (1, 2, 3, 4)), Primary Key (STUDENT_ID, COURSE_ID), Unique (STUDENT_ID, COURSE_NO) ; A combination of checks and unique obstacles means that DBMS itself will refuse to connect the student to more than 4 courses.
This will be successful by:
STUDENT_COURSE value in INSERT (11, 111, 1); INSERT (11, 222, 2) in STUDENT_COURSE VALUES; INSERT (11, 333, 3) in STUDENT_COURSE values; INSERT (11, 444, 4) in STUDENT_COURSE values; But this will not be obvious (violation of check barrier):
STUDENT_COURSE VALUES INSERT (11, 555, 5); BTW, when the student is already connected to some courses and you want to find the remaining free "slot", you can do it like this: Select from NEW_NO SELECT COURSE_NO + 1 NEW_NO, LEAD (COURSE_NO) (by course B_NO) STUDENT_COURSE NEXT_NO WHERE STUDENT_ID = 11) Where NEW_NO & lt; & Gt; NEXT_NO or NEXT_NO is Null;
Comments
Post a Comment