SQL relationship between student, lessons and attendance - sql

For a school project I'm trying to store students, their attendance and which online lessons they are allowed to view.
For example a yellow belt student is allowed to watch white and yellow belt lessons. The belt system is replaced by a kyu, which can also have a dan.
So far I've created the following:
Student: studentID (PK), firstname, lastname, age, kyu, dan
Attendance: attendanceID (PK), studentID (FK), date
Lesson: lessonID(PK), name, kyu, dan
I'm stuck at finding a possible relationship between lesson and student, since it feels wrong to just put a FK to lesson in student.
As far as I'm aware, it's just one big class, so I can't form a relationship between student-class and class-lesson.

I understand that you have a N-M relationship between lessons and students, where each student can enroll in several lessons, and each lesson may be (and hopefully is) taken by many students.
The typical way to represent such relationship is to create a bridge table, that stores which student participates which lesson. Say table student_lesson :
id : primary key
lessonID : foreign key towards table lesson
studentID : foreign key towards table student
You would need to create a unique constraint on columns (lessonID, studentID) - or drop skip id, and make (lessonID, studentID) the primary key of the bridge table.

As far as I understood, it should be many to many relationship, as many students can go to the same lesson, and student can have many leasons, so another table holding student and leason id will solve the problem, I think

Related

Record in one table unique to a record in another table

Let me try to make it simple with an example.
I am creating a database with 2 tables, School and Students
School table with 2 columns SchoolID(PK) and Location
Student table with 3 columns StudentID(PK), SchoolID(FK) and Grade
The Student table will have students from all the schools which can be identified by the foreign key SchoolID. The StudentID column will be unique to each student across all schools. Well and good so far.
I need another column in Student table which will be unique only with respect to the foreign key value. Let's call this ID. I need this column to be unique only to each SchoolID. So, If I filter out the students belonging to a particular SchoolID, I shouldn't get 2 students with same ID.
I'm not sure if that's a valid scenario.
Edit:
This is good
This is not
I think there is something wrong with the datamodel. Since you have StudentID as primary key in Student-table, this column will always be unique in this table. But it seems like you are trying to create a Student-School table where you can have the same student connected to multiple schools (but no the same school multiple times for one single student). I think you need at least 3 tables:
Student (Pk StudentID)
School (Pk SchoolId)
StudentSchool
The StudentSchool table will have two FK-columns: StudentID and SchoolID. To protect from the same student beeing mapped to the same school multiple times you could either have the PK include StudentId and SchoolId or create a Unique constraint.

How to implement ER Relationships :One to one, One To Many, Many to Many in Oracle?

Perhaps, this could seem like the basics of database design.But,certainly i find these concepts a lil tricky in the sense that how they relate to each other.
Theory as per my understanding.
One to One :
When each particular entity has one record.
One to Many :
When each particular entity has many record
Many to Many :
Multiple entities have multiple records.
As per the above if i could relate an example as
ONE TO ONE
Each employee having a unique passport number.
Table Employee
Empid(pk)
empname
passpordid(fk to passport)
Table passport
passportid(pk)
passportno
ONE TO MANY
An organisation having multiple employees
TABLE ORGANISATION
ORGID (PK)
ORGNAME
EMPID (FK TO EMPLOYEE)
TABLE EMPLOYEE
EMPID (PK)
EMPNAME
SALARY
This is the part that i want to know more that is many to many. I mean if we see one to many here. As a whole it could be said as many to many as many organisations having many employees but does that mean the whole relationship is many to many not one to many or one to many is a subset of many to many in the above example.
I wanna know the difference mainly between one to many and many to many both theoritically and by implementation.
An example of a many-to-many relationship would be EMPLOYEES and SKILLS, where SKILLS are things like "SQL", "Javascript", "Management" etc. An employee may have many skills (e.g. may know SQL and Javascript), and a particular skill by be possessed by many employees (e.g. Jack and Jill both know SQL).
In a database like Oracle, you need a third table to express the many-to-many relationship between EMPLOYEES and SKILLS:
create table EMPLOYEE_SKILLS
( empid references EMPLOYEES
, skillid references SKILLS
, constraint EMPLOYEE_SKILLS_PK primary key (empid, skillid)
);
Note that this third table has a foreign key to both of the other tables.
The table can also hold further information about the relationship - for example:
create table EMPLOYEE_SKILLS
( empid references EMPLOYEES
, skillid references SKILLS
, rating number
, date_certified date
, constraint EMPLOYEE_SKILLS_PK primary key (empid, skillid)
);
This is more a theory question that a programming one, but why not.
You could imagine a "many to many" as 1 table being a list of employees, and another table being a list of products sold.
Each employee is likely to "handle" more than 1 product, and each product could very well be handled by multiple employees, and it's quite possible that products A and B can be handled by, at the same time, employees C and D.
You seem to have the basic idea of one-to-one and one-to many down, in theory.
One-to-one: each item A has a separate and unique item B
In a database, you could include the new data in the same table. You could put it in a separate table, and enforce the one-to-one constraint by making the id into the remote table "unique" (indexing constraint), forcing the id for item B to not be repeated in the table. In your example, this would be a constraint added to the passportid.
One to many: each item A has some number of item Bs
In a database, you put the key in the table on the "many" side. So many Bs would have the same foreign key into table A. In your example, put an orgid as a foireign key into the employee table. Many employees can then point to a single organization, and an employee can belong to one organization.
Many to many: some number of As can be linked to any number of Bs and vice-versa
In a database, this is usually implemented as a link table, having just the IDs from both A and B. Following you example, consider that an employee can be part of multiple groups (or projects), multitasking across those groups. You could have a group table:
GROUPID (PK)
GROUPNAME
And the link table would then look like
LINKID (PK)
EMPID (FK TO EMPLOYEE)
GROUPID (FK TO GROUP)
This lets any employee be part of any number of groups, and groups to have any number of employees, AKA many-to-many.
A side comment:
Technically, you don't HAVE to have a LINKID in the link table. But it makes it easier to manage the data in that table, and is generally considered "best practice".

what is the correct design for ER mapping for this situation?

I need to model this problem :
One faculty may supervise several students or not. One student has at
least one, at most two supervisors.
I'm looking for actual Relational table design on how to do this.
The easiest (somewhat bold) way to model this is
a STUDENT table
a FACULTY table
Now give STUDENT columns FACULTY1 and FACULTY2. Both are foreign keys to FACULTY. Constrain FACULTY1 with NOT_NULL. This enforces that a student has at least one FACULTY. FACULTY2 can be null, but it still a foreign key.
FACULTY somehow does not know anything about STUDENT. When no STUDENT references a FACULTY it has no students and any number of Students can reference faculty.
This model has a number of drawbacks (I said it is bold):
A STUDENT being supervised by only one FACULTY must have this set in FACULTY1 (not FACULTY2). Likewise when a student who was supervised by 2 FACULTIES deletes one of them, you can only delete FACULTY2 or you must first swap the fields. You can circumvent this by a more clever constraint (FACULTY1 is not null or FFACULTY2 is not null)
If you ever want to change the design, so a STUDENT can have 3 FACULTIES you need to add columns to STUDENT. This however, is not as bad as it may sound.
On the pro side, there is nothing but referential integrity and NOT NULL involved in this design.

A table with all attributes making the primary key

I have two tables: Student and Shop and I would like to record information about which student visited which shop and if the number of visits is above n, they should received a discount:
This is how I did it:
All of attributes (studentID, shopID, time, date) in table StudentShop makes the primary key for this table. I just wanted to know if this design is good?
As relation between entieties Student - Shop is many to many relationship,
it is always implemented using associative table (consists of primary keys from both relations), so StudentShop table - is good choice for implementation of such relation.

A One To Many relationship model where the data on the right side can be quite variable?

Here is the problem I'm trying to solve:
Entity A : a_id
Entity B : b_id
One A can use Many B's. However, not all Bs are used by all As.
Here is the best example I can think of :
One teacher has many students.
Some students are taught by more than one teacher.
What is a relationship so I can add/remove students being taught by one teacher, but not affect the teachers already teaching said students?
you need a third table known as mapping between this two tables.create this table so:
Table Student_Teacher_Mapping
Id (Int)
TeacherId(Int) // foreign key for teacher table
StudentId(Int) // foreign key for student table
i think this is what you want.
You need a StudentTeacher entity that will relate the two together. It would have an a_id column and a b_id column.