How to Create subjects table that includes multiple teachers and multiple classes - sql

My condition is like this:
There's a teachers table:Id, name and classes table:Id, name.
I want to create a third table which is subjects table.
I want to figure out columns for the subjects table, where
1)one teacher can teach multiple subjects.
2)and one subject is taught to a multiple classes.

For that you need multiple tables, it has to have multiple to multiple relations like this. You can even have multiple teachers in the same course or subject.

Related

Selecting those Affected by a 1:1 relationship

My teacher asked us to select from a 1:1 table called Employee; the supervisor and their role, and each employee they supervise with their role (employee is the primary key and those are the only 3 values in the table).
A 1:1 relationship are two tables with only one possible matching id.
It assumes that each supervisors only has one employee. so your query would look something like this.
SELECT Employee.Name, Employee.Role, supervise.Name, supervise.Role
FROM Employee
INNER JOIN supervise
ON Employee.EmployeeId = Supervise.EmployeeId
However, do note that in a real world context. This should have been a 1:N (One-To-Many) as each supervisors can have many employees.
Usually a 1:1 relationships are only used when you want to extended a table that you have no access or cannot modify. Otherwise, you would just add more columns to that original table. (or if you are working with a very old database system and you reach the max number of columns)

Many to many relation table name for tables with similar name

I have two tables named business and business_category that have many-to-many relation. What name should I use for a third table that bind them together? I don't like business_business_category name at all.
You could name it - BusinessCategoryLink or BusinessesPerCategory
I would like to suggest you. Can make three table. Business, categories, businesses_x_categories. All business are will be in business table , all categories will be categories table and both table link will be business_x_categories.
Multible. Business have same categories. So we can link with same category and different business.

Doctors, Patients and Contact information for both

I have two tables DOCTORS and PATIENTS. I want both the doctors and the patients to have contact information (such as telephone numbers, addresses and so on) that aren't fixed. For example we can add multiple phones to either a doctor or a patient.
I thought about creating a separate table e.g. PHONES with fields phoneID, the phone number and a foreign key that points to a contact, such as below:
PHONES
phoneID [pk]
number
contactID [fk]
DOCTORS
docID [pk]
fname
sname
specialization
.
.
.
PATIENTS
patID [pk]
fname
sname
.
.
.
The first problem comes from the fact that the patID and the docID might (and will eventually) have the same value. So relating a phone to one and only one person becomes more difficult.
So far I've thought three possible solutions:
Have custom format primary keys for the doctors and patients. For example doctors could have ids in the form of "d00001", "d00002" and so on, and patients ids like "p00001", "p00002". My concern is this could complicate things unnecessarily.
Another solution would be to keep both doctors and patients in one table, and define if they are a doctor or a patient by using another field.
Create separate PHONE tables for doctors and patients, but that's even more clumsy.
Somehow I think both approaches are not the best. Any advice?
You could introduce a PERSON-table. This is 1:1 related to your doctors and patients (and later maybe to employees, suppliers, institutions, whatever). Let the contacts be related to this person-table.
Application code can model this with inheritance quite easily...
Within your person-table you keep some general information like DisplayName and PersonType (reference to a person-type-table with entries like Doctor, Patient and ...).
Keep this table slim...
If you have to choose out of your own ideas, I'd prefer the second. Keep them in one table and mark them with a type column. Avoid speaking keys...
You are thinking about the relationships wrong.
There is a many to many relationship between person and phone number.
A single person can have many different phone numbers (home, work, mobile, etc.)
A single phone number can be associated with many people (husband, wife, children, co-worker, ...)
I don't see why you need a primary entity with phone number as a primary or alternate key.
I would use 5 tables:
Doctor PK=DoctorID
Patient PK=PatientID
PhoneType PK=PhoneTypeCode
DoctorPhone PK=DoctorID,PhoneTypeCode,PhoneNum
FK1=Doctor.DoctorID
FK2=PhoneType.PhoneTypeCode
PatientPhone PK=PatientID,PhoneTypeCode,PhoneNum
FK1=Patient.PatientID
FK2=PhoneType.PhoneTypeCode
You might consider having distinct DoctorPhoneType and PatientPhoneType tables, given that the phone roles are likely to be different between the two classes of people.

Query linking many-to-many relationship over several tables

I am making a quiz-program. In a quiz I have many participants, but on the other hand participants can enter many quizzes (over time). So I have overcome this many-to-many relationship with a linking table. So far I can understand.. now for the difficulty for me: a participant can either be a group, or a single player.
So a quiz has a participant (with a linking table) and this participant is either a group which has several persons, or this participant is a player and only one single person.
table Quiz : PrimaryKey = quiz_id, (name, date,... )
table QuizParticipant : PrimaryKey = quiz_participant_id, quiz_id
table ParticipantGroup : PrimaryKey = quiz_participant_id, group_id
table participantPlayer : PrimaryKey = quiz_participant_id, person_id
The problem for me is: how do I query all participants of a quiz by quiz_id, and preferably sort them by type (group or player)?
Tips on how to google this stuff are usefull as well :)
It's an interesting design decision. The way you proceed can shape your entire application.
My advice is to record the participant type within each occurance of participation. This would be either "GROUP" or "PLAYER" and would define whether the ParticipantID referred to a GroupID or a PlayerID. This will result in the cleanest database design and application logic in the end.
So your tables look like:
Quiz (QuizID,QuizName)
QuizParticipation (ParticipationID, ParticipantType, ParticipantID)
Group (GroupID,GroupName)
GroupMembers(GroupID,PlayerID)
Player(PlayerID,Name)
You might need to make a custom constraint to ensure that duplicates cannot be created within the ParticipantID and ParticipationType combined fields of the QuizParticipation table, or manage the generation of primary keys carefully.
My suggestion is to always link your quizzes to groups (many to many) and treat single players as a group of 1. So your groups are linked to players (also many to many)
If a person can belong to many groups and a group can have many people, that's another many to many relationship. Since you know how to create these linking tables, create one for this relationship.

Mysql how to avoid repeating myself

I have a table students with the following fields.
Id,FirstName,SecondName,Photo,Student_ID
I also have a table called class_of_2011 with the fields
Id,FirstName,SecondName,Photo,Student_ID,Subjects
I want to select some specific students from table students and have them in table class_of_2011,but I already have the names in table students.I am thinking the only way to do this is to copy the names i want to the table class_of_2011,but since there will be a class of 2012 and beyond,I feel like I will be simply copying data from one table to the other.
Is repeating myself inevitable in my case?
It looks like this could be normalized easily. Why not have your class_of_ tables simply have a foreign key to the student table's id column?
StudentId,Subjects
In this way, one student record could be associated with several classes, in case someone is on the 5-year plan.
I'm assuming that the Student_ID field in the Students table is their id number or something, and not the primary key of that table.
Students Table
Id,FirstName,SecondName,Photo,Student_ID
Subjects Table
Id,Subject
Student_Subjects Table
Id,Student_Id,Subject_Id,Year
You may then assign a student multiple subjects, for multiple years.
The class_of_2011 table should contain the primary key of the students table and none of the other "repeated" data. All of the other columns you're interested in can then be obtained by joining the two columns together in a query.
I would restructure the data if possible... Something like....
Student Table
ID, Name, Address, other common specific to student
GraduatingClass Table
YearGraduate, StudentID
Enrollment Table
StudentID, ClassID, SemesterID