Create a SQL table with a varying number of columns - sql-server

I have two tables, say teacher and student. I want to build a third table called class.
The class table will have one column for the teacher of the class, but I want to see if their is an elegant way to represent the students. My first thought is to have say 30 columns.. student1, student2 and have quids for each of these that tie back to a row in the student table.
But I am asking to see if there is a more preferred solution. The above solution seems clunky.

If it was me I would have a 4th table called attendees or similar which links the students to the class as it's a many to many relationship. Which would contain the Class ID and the Student ID as a minimum..

The class "table" is not a table but a result of running a stored procedure with pivoting of data.
And data structure is as follows:
Student: Id, ...
Teacher: Id, ...
StudentClass: StudentId, ClassId, ...
Class: Id, TeacherId, ...

SQL Server has this thing called sparse columns However I would advice against it. Normalize your data and then PIVOT/crosstab the results when you want to display it side by side

It's very clunky.
Have three tables:
Teacher
Student
TeacherStudent, which is a many-many table to join the two.
Do the pivot method expressed in the other answer. It makes more sense.

Related

SQL-SERVER- get way relationship between tables

i have a big DB, and i wants to create query that i give her two name of tables and she give me the relationships between her.
for example:
in my DB i have three table:Person,SupportActitvity and ProcessesSupportActivity.
Person related to SupportActitvity , and SupportActitvity related to ProcessesSupportActivity
if i want to get the ProcessesSupportActivity for Person i write:
select *
from person
join SupportActivity on Person.PersonId=SupportActivity.PersonId
join ProcessSupportActivity on ProcessSupportActivity.SupportActivityId=SupportActivity.SupportActivityId
I want that when i write that for Person and ProcessSupportActivity he give me the relationships.
he return:
Person PersonId SupportActivity PersonId
SupportActivity SupportActivityId ProcessSupportActivity SupportActivityId
Thank you very much!!!!
to look for the shorter way, I call to dijkstra algorithm.
The graph is constructed from all tables in DB, Each table it's was a node in the graph.
and the path it's was all of the relation of table.
obviously that the graph build in signaltone אם economize runtime.
And when you want to discover a connection between two tables in the database you send the source table and destination table and get the path....
It works amazing

How do I create multiple table relationships on Access 2016?

I have a table that shows each student's details with their module choices for semester 1 under each student 1.
I want to add the semester 2 module choices as well so they can be seen on the student detail table. Is there a way to do this? When I try to set up the relationships, the student details are shown in the semester 2 options table 2, which is not what I want. I haven't done that much on access so apologies if this is a simple question or if it isn't possible...
You already created relationships. As I understand, you want to see more than one "child" table in table designer. If so, it's not possible.
If you want to select options for different semesters, create a form based on Students table and two subforms for Semester1 and 2.
Also I would recommend to change database structure. You don't need two tables for semester modules. Create just one table with additional field like SemesterNumber and place subforms, based on this table with different criterias for SemesterNumber. And add own primary key for SemesterOptions, do not use StudentID as PK, actually you have one-to-one relationship, I believe you want select more than one module for each student.
In my opinion, you should have more tables, which in end will make it much easier to query data and get reports out of it, as well it will make it easier to create forms for each table to record data.
Here is simple table structure I would do for student/class registration.
You can also link tblSemester table to tblClass, if you like to make a list of classes offered each semester and make it bit more complicated, but with this organization you will be able to get list of all classes for student, or all student for particular class or semester, and with ease add more data later.

Best Practices for Table Structure

Consider a table Employee, employee table stores various employee details.
One of such detail is Type of the Employee
The type of employee would be
Permenant
Contract
Freelance
There could be many employee types
So I name the column in Employee table as 'EmployeeType'
My Application has a table called ApplicationSettings(Id, Code, Description), I prefer to put the Employee Type into this table, So the value of Code field will be stored in Employee Table for Employee Type.
What i want is an advice from you what will be a better way to organise this structure.
The above method, or create a new Table called EmployeeType ?
Thanks,
You can use what you have proposed if you add a CodeType field to your code table. That way when you just want your employee types you can query it like:
SELECT Id, Code, Description FROM Codes WHERE CodeType='EmployeeType'
Its also perfectly acceptable (and maybe a litter clearer for future people looking at your database) to have a separate employeetype table.

Designing a good database

I have the following tree:
courses, that have many groups, each having multiple students.
So the drilled-down tree looks like this: courses -> groups -> students.
I think there are two ways to represent this:
1) students table that has group_id FK to groups; groups table that has course_id FK or:
2) first option plus students table having both group_id and course_id FKs so that I can have more freedom to fetch data without having to JOIN the "parent" table everytime.
One good example is to get all students that are part of a course (whatever the group). In this case, going with only the first option forces me to JOIN the groups table, which is not needed in that scope. So i tend to always choose the second option, even if the "main" table gets a few more columns of FKs.
How do you approach this?
The example gets more complicated if you add a couple more tables at the top of the courses table, like teachers (that teach courses) and schools (that has teachers). If you need to see all the students in a school, you need to join the groups, courses, teachers and schools.
Thank you!
LE: I am excluding many to many relationships from this example, those are treated differently.
LLE: And yes, if it sounds like convenience (aka performance)... it might be true :)
Personally, I try to keep a database as normalized as possible for clarity in the data model. So I would say if every student is going to be linked to the course through a group (no possibility of a student not in a group), than there should not be a relationship directly between students and courses. Don't sacrifice the clarity of your data model for having to write less SQL.
Also, you probably realize this, but you'll need linking tables for any many-to-many relationships. I'm not sure what kind of groups you're talking about, but if they can exist over multiple courses you'll need a Course --> CourseGroup (FK CourseID, FK GroupID) --> Group structure, and if students can belong to multiple groups you'll need Group --> GroupMembership (FK GroupID, FK StudentID)--> Student.
The addition of the course_id into the students table would be considered "denormalization" and is perfectly acceptable for exactly the reason you are trying to solve : "performance".
Denormalization Wiki
In computing, denormalization is the process of attempting to optimize the read performance of a database by adding redundant data or by grouping data
So yeah, your second option is doing just this ... attempting to improve performance by adding redundant data.

Help with many-to-many relation

I have a problem with a many-to-many relation in my tables, which is between an employee and instructor who work in a training centre. I cannot find the link between them, and I don't know how to get it. The employee fields are:
employee no.
employee name
company name
department job title
business area
mobile number
ext
ranking
The Instructors fields are
instructor name
institute
mobile number
email address
fees
in a many-to-many relationship the relationships will be in a 3rd table, something like
table EmployeeInstructor
EmployeeID
InstructorID
to find all the employees for a specific instructor, you'd use a join against all three tables.
Or more likely there will be classes involved --
Employee takes Class
Instructor teaches Class
so you'll have and EmployeeClass table,
an InstructorClass table,
and join through them. And Class needs to be unique, or else you'll need
Class is taught in Quarter on ClassSchedule
and end up joining EmplyeeClassSchedule to InstructorClassSchedule.
This ends up being one of your more interesting relational designs pretty quickly. If you google for "Terry Halpin" and "Object Role Modeling", this is used as an illustrative situation in the tutorial.
First of all, you will need a unique key in both tables. The employee number may work for the employee table, but you will need another for the instructor table. Personally, I tend to use auto incrementing identity fields called ID in my tables. This is the primary key.
Second, create a new table, InstructorEmployee. This table has two columns, InstructorID and EmployeeID. Both fields should be indexed. Now you can create an association between any Employee and any Instructor by creating a record which contains the two IDs.

Resources