I have the following tables: school, teacher, school_teacher and student.
school_teacher is a many-to-many junction table using an autogenerated PK and the FKs of school and teacher.
A student belongs to a teacher of a particular school and a teacher of a school has many students.
My question is whether I should create the foreign key in the student table to point to school_teacher table PK or to the two tables PKs (school, teacher) individually?
I believe it would be best for referential integrity to reference school_teacher to ensure that transaction records are only inserted if there is a relationship between the school and the teacher. In terms of best practices, what is the best option or the best way to address this problem? Is there any other possibility better than the ones I mention?
In general, is there a problem in which the primary key of an association table is a foreign key in another table?
This is simply an example. In a real case a student could have more than one teacher. It is to simplify the example.
Student * ------ 1 School *------*Teacher
Will you check for student belongs to a school or many schools?
Related
I have a database with three tables: Employees, courses and instructors.
I am having toubles making the relationships because some of the employees can be instructors. So how should I link my tables so that I can add some of the employees to be instructors. Also what would be the the primary and foreign keys that I should use?
Thanks
If I understand the problem right, some but not all of your instructors are also employees. There are two ways to go about it:
No data duplication: No Instructors or Employees tables, just a Person table with Yes/No fields for IsInstructor and IsEmployee.
Data duplication (if current scheme is fixed or the remaining info for instructors and employees is very different): Add an EmployeeID field to the Instructors table, leaving it Null if the instructor is not also an employee.
In all cases the primary key is an auto-increment number for each table (PersonID, EmployeeID, InstructorID, CourseID) and that is the only field used in the various relationships.
I want to make an ER diagram involving a 1 to many relationship. Should I add the attribute of the relationship on the N side too? And if possible write the correct ER diagram.
Take a simple example of many people working in one department with a constraint that a single person can work only in one department. That makes department connected to people in a one-to-many fashion.
The simplest way to represent this is to add the primary key of you department table as a foreign key in your people table.
The schema will look something like this
DEPARTMENT (dept_no, dept_name, dept_location,....)
PEOPLE (p_no, dept_works, p_name, p_dob,....)
Where, dept_works in PEOPLE table is the foreign key that references the dept_no Primary key in the DEPARTMENTS table. This way you can store the ID of the department for each person in which he/she works.
Say if a real life operation like loaning a library book is to be performed on two entities Teacher and Student. And the operation details are present in a table like Loans or Transaction. How do I define a foreign key in the Loans table for both Teacher and Student which have different schemes for primary keys? (student has 6digit key but teacher has 4 digit)
A similar question arises when a hospital database system Appointment table attempts to link to both an Employee table and EmployeeRelatives table using a foreign key, where both Employees of the hospital and their relatives are to be given free treatment. How does one specify format of foreign key in Appointments table?
A foreign key can reference only one table.
My solution in these cases is to introduce an additional table (People or whatever name you prefer) which contains all the IDs of Teacher and Student. Teacher and Student can also have the People table key as foreign key.
You can find other possible solutions here:
Foreign Key to multiple tables
I need to create a database for a fictional bookstore. 2 of the attributes of the book entity are the author's first name and the author's last name. However, some books may have more than 1 author.
(The primary key for a book is the ISBN number)
My stupid question is this: Should I create an AUTHORS table that has a foreign key of the ISBN number or is there a different way I should be going about this?
Thanks for your time
You Should create an AUTHORS table that has a foreign key of the ISBN number
Because the relationship between BOOK and AUTHOR is one to many.
so you should create AUTHORS Table to maintain the good structure of your data base.
This matter confuses me,
I have a College Information system the junction table between students table and subjects(curriculum) table, the primary key is composite key (StudentID, SubjectID) and both of them are Foreign keys but the student may be fail in exam and repeat the subject so we will have duplicate PK and we need to record all data. I have two ways to solve this matter but i don't know the best way?
Add new column as primary Key instead of composite key.
Join to the composite key Season Column and year column and the composite key will be(StudentID, SubjectID, Season, Year). I have to mention that i don't need this composite key as foreign key.
Which way is better for performance and DB integrity?
Subject and exam are separate (if related) concepts, so you should not try to represent them within the same table. Also, the fact that an exam has been held for the given subject is separate from the fact that any particular student took that exam. Split all these concepts into their own tables, and the model becomes more natural, for example:
Representing a student that took the same exam several times is just a matter of adding multiple rows to the STUDENT_EXAM table.
NOTE: STUDENT_SUBJECT just records the fact that the student has enrolled in the subject, but not when (which year/semester). Keeping semester-specific information may require additional tables and more complicated relationships within the model.
NOTE: There is a diamond-shaped dependency in this model. Since SUBJECT_ID was passed from the "top" (SUBJECT), down both "sides" (STUDENT_SUBJECT, EXAM) and then merged at the "bottom" (STUDENT_EXAM) of the diamond, a student cannot take an exam on a subject (s)he has not enrolled in.