I have 3 tables in my sql. I want to have you guys feedback on how I am building my tables relationship. So I have 4 tables: application_table, teacher_table, student_table and class_table.
Here are several conditions:
A teacher can teach one or many class, but there also teacher teachers 0 class. (1 -> N, teacher -> class_table) relationship. One thing to notice is a teacher may leave the school.
A student can attend many class. One thing to notice is that a student may get expelled or graduated. (1 -> N student-> class)
An application can be used by many student, and a student can use many application. (N -> N relationship)
An application can be used by many teacher, and also a teacher can use many application. (N -> N relationship)
ps. An application may be cut or expired or not used by the school anymore.
application_table
applicationId
applicationName
expiryDate
teacher and class relationship
class_table
classId
classCode
teacherId
Student and class relationship
student_table
studentId
firstName
lastName
classId
application and teacher relationship
application_teacher
applicationId
teacherId
application and student relationship
appliaction_student
applicationId
studentId
IMHO you should first study normalization. One good source is the wikipedia. Your structures are against normalization (ie: Student - Class shouldn't have firstName, lastName ).
A 'school' database is more complex than this but your structures might work for a sandbox learning application, provided you correct the normalization problems.
Related
We have a classroom table. And each classroom has many classroom users.
A classroom_user has a kind attribute, which is either teacher or student. Each classroom_user is associated with a user_id from the Users table.
What I'm looking for: I want to find all of the classrooms for a given teacher and student.
Which SQL query could I write to satisfiy this need:
"List the names of the students who take a course from instructor named John."
Not sure that you can, from the depicted relations.
You can identify tutors by selecting on InstructorID and filtering on Instructor.FirstName.
You can join that subset onto course, via the InstructorCourses Join Table - join InstructorID to that and join the result to Courses using CourseID
In this way, Instructor.InstructorID -> (InstructorCourses.InstructorID , InstructorCourses.CourseID ) -> Courses.CourseID.
This lets you find information about the courses taught by instructors filtered on their name.
You don't present any link between students and courses in your diagram. I suspect you're missing a relation StudentCourses, which ought to be similar to InstructorCourses, but rather links students to courses. With that data in the mix, you can extend the join to match students to the courses from the relationship you already have.
Your diagram implies a relation between Student and InstructorCourses, which seems incorrect - both because there is no key to join on, and also because the logical relationship would not be correct. I think this is probably an error.
It is impossible to satisfy the SQL query you need because your conception does not allow it in that there is no relationship between the 2 tables Student and InstructorCourses.
SQL newbie here and I need help with a complex(?) view for my assignment. It's a database which allows teachers to set the grades and number of times a student was absent, for each class the teacher is responsible.
My tables are:
USERS (user_id, name, surname)
STUDENTS(student_id (fk to users.user_id), student_number number)
TEACHERS(teacher_id (fk to users.user_id), title varchar2)
CLASSES(class_id, subject, teacher_id(fk to teacher.teacher_id)
CLASS_REGISTRATION(class_id (fk to classes.class_id), student_id (fk to students.student_id), grade, absent_number)
I am trying to create two views. One where the student can see the class.subject, class.teacher, class_registration.grade, class_registration.absent_number, for every class he has registered.
The other view is for the teachers where they can view, student name, class subject, grade, absent_number, for every class the teacher is responsible.
The generalisation of users -> students, teachers is required and that's what's mostly giving me problems.
EDIT: Here is a query I wrote which seems to give the desired result for the teacher view, I am not sure it the best way though
CREATE OR REPLACE FORCE VIEW "TEACHER_VIEW" ("NAME", "SURNAME", "SUBJECT", "GRADE", "ABSENT") AS
select u.name, u.surname, c.class, r.grade, r.abstent
from users u, class_registration r, students s, class c
where u.user_id = s.student_id
and s.student_id = r.student_id
and c.class_id = r.class_id
/
/
I am not giving you the create view statement but here are some links for you which can help you to learn view and it's related concept.
first
second
And go through these links and as it's your assignment so try to do it by yourself.
And if you face any problems post here.
I'm not a DB guy so this may be a trivial question...
Suppose
1) i have a relation table (I think that's what it's called), student_class, which holds a student_id and a class_id, (representing a many-to-many between a student table and a class table)
2) i do various query that results in a student_id (perhaps among other things) and then the results are "LEFT OUTER JOIN"ed to the student_class and LOJed again to the class table to get the associated class information.
3) i do that a lot, but i don't care to find the students from a given class, or any other thing you may think is common to do in the context of students and classes.
4) i have tens of thousands of students but only about 100 classes
5a) 99% of the students are not enrolled in any class (what a great school) and the rest are enrolled in only and only 1 class
5b) alternatively to 5a, on the average, every student is enrolled in about 2 classes
So how many and which of the indices below should i create in the student_class table for this sole purpose, and is the answer different for 5a and 5b?
a. index on student_id
b. index both student_id and class_id
c. index on class_id
I would create one index for each column.
There's not an argument from your question to only add index to class_id. You select values according to student_id and to class_id, so i think it's reasonable to have them both.
Additionally, your index needs don't change if there are more students enrolled in each class.
You make use of the indexes in the same way for both cases.
And for the amount of records you have, the indexes are going to be relatively small.
I have one situation given below :
Each student can enrolled into more than one class.
Each class can accommodate more than one student.
which of the following is possible answer :
(1) 1 to N
(2) M to N to 1
(3) M to N
(4) Anything else
if answer is (4) than which other answer is possible ......
any comments appreciable,
thnks in advance, Milan Mendpara
It's M:N.
Look at the problem from both entity's perspective to find the answer.
Each class can have many student entities. So we have 1:M.
Each student can be enrolled in many class. So that's 1:M as well.
In a normalized database you'd have Class and Student defined (minimally) like this:
Class: Id, Name
Student: Id, FirstName, LastName
If you add a foreign key on Class to Student you're only allowing each Class to have a single Student. If you put the foreign key on Student to Class you're only allowing each Student to take one Class at a time. The answer is to use an association table as this is an M:N relationship:
StudentClass: StudentId, ClassId
Hope that helps.
It's number 3.
Multiple students (M) call enroll in multiple classes (N).
The answer is
3 M to N
You need to define a table with fields class_id and student_id to store the relations.
Check out this article about database normalization
http://en.wikipedia.org/wiki/Database_normalization