简体   繁体   中英

Question about SQL Server database design

I have a database table named Student that stores student's information, however when a student graduates, their info is no longer stored in Student table anymore, but I still want to stored that info for further use. So my best method is push that info into another table named GraduatedStudent (Student table is not allowed to stored graduated student info)

I also have a table named StudentsPayment that uses a foreign key (Student ID) of student table. When I push Graduated Student away from student table, the StudentsPayment get an error that it can't find that student ID in the Student table anymore. I can't figure out anyway in design to solve this problem so please help.

You, essentially, have 2 options

  1. Mirror all of the tables foreign keyed to Student for the GraduatedStudent table. So you end up with a mirror of StudentPayment as GraduatedStudentPayment and when you move a student you must also move all of their payments (and any other relationships)

  2. Add a flag to Student table to say IsGraduated (or, even better the opposite IsActive ) and you're done. You can always use views to replicate the functionality of CurrentStudent and GraduatedStudent if thats what you want to do.

I know which option I would choose. No points for guessing which!


Edited to add some points about a comment:

in the student table there is a foreign key call IsInRoom stored room ID of Room table, when i tick that is graduate flag, the info of that room still there (that graduate student not suppose to in that room because.. he graduated) but some how i still want to know what room he used

This is easily solveable. You introduce a RoomHistory table and make the IsInRoom nullable. When a student graduates you null their current IsInRoom and add a record to the RoomHistory table. This might be useful also to srtore a complete history when/if a student moves rooms. You can add some extra columns to store when they started and stopped using a particular room.

You could have a table, lets call it StudentInformations, that store student infos wether they have graduated or not. Then you could access it with the Student, GraduatedStudent and StudentsPayment. 在此处输入图像描述

Unfortunately you have stated two mutually exclusive requirements (1) Student table is not allowed to stored graduated student info, and (2) the student id in the student payments table must exist in the students table. You can relax the first requirement and then, as the previous commentator suggested, add a "Graduated" flag to the student table. Alternatively, you could create a new table, maybe call it Person, then primary key of which (person id) then becomes the foreign key in both the student and student loan tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM