Best Practices for Table Structure - database

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.

Related

What is the best way to maintain shared fields between tables in database?

I want to create a database for school management system. The database has two tables contain shared fields like Students and Teachers.
For example, Student table has fields(id, name, phone, class), and Teacher table has fields(id, name, phone, department).
Is it better to make: a table called Person which has fields(id, name, phone), Student table has fields(id, person_id, class), and Teacher table has fields(id, person_id, department).
Which of the two ways is better?
Giving a direct answer to this question might be opinion based. There is no generally best strategy to design database.
Theoretical example: if there is a large amount of data you might want to think about performance: what and how you search and joins.
If you search mostly Students and Teachers you might not create Person table and you could search them easily. Then if you would like to search all Persons from db you would need to make two queries and a UNION between those and with fields that are common to both types of Person.
If you search also Persons more frequently then you might create Person table and implement Teacher and Student to have foreign key to Person. Then when searching two last mentioned you would need to make JOIN to Person.
In real life I do not know if in this use case there is really a big difference. More important is that you select some strategy and follow it in your future decisions in order to keep the desing clear.
However there might come situation where it is need to change the selected strategy still. So theoretically.
Related question here

Selecting value either from dependent or independent table?

I have three tables An Employee Table, a employeeDependents Table, and a Medicare table. The procedure I want to make is either employee or its dependent is provided with medical care. Now I want to put the primary key of either employee or dependent in the table of Medicare.
My question is how can I get one value from both tables? There is a one to many relationship from employee to employee dependent.
Hope you understand.
Following is the link for a few photos
https://www.facebook.com/media/set/?set=a.3811558138512.2159864.1564273389&type=1&l=a2c0c07671&subject=DataBase Question at Stack OverFlow
I'm guessing:
1) Employees have an ID (e.g. "employee#"),
2) Employee dependents also have a (unique) ID, as well as a foreign key for the employee#
3) "Medicare" has an ID, and possibly a column indicating whether the patient is an employee or an employee's dependent
Q: Given an employee's medical record from the "medicare" table, can you successfully link back to the employee table?
Q: How exactly are employee dependents "known" in the "medicare" table?
Q: Is this a homework assignment?
looks like you want to connect the Emp table and EmpDep table with a single table Medicare..
are you assigning any discrete id to the EmpDependents?
if so, then on wat criterion?

relational database model

Lets say we have three tables: Cars, People, Companies.
We want the owner of the car to be a people or a company. The structure of the Cars table is: Car_id, Car_model, Owner_id
What is the best way to do that?
If you can't change the current structure of the cars table, then you could add new table called owners with the following columns:
number id -- unique key
number owner_id -- this is the actual owner id
char owner_type -- this is a value indicating whether the owner is a person or a company
You will then need to cross reference cars with owners and look at the value of owner_type to determine which table to get your owner data from.
EDIT
Forgot to mention (rather important):
In the cars table, populate the owner_id with the owners.id column.
What about having another field in Cars table as "Owner_type_id" which will determine if owner is a person or a company or any other thing. Of course, you should keep another table "Owner_Types" for this scenario to work.
As Igby mentioned, your existing structure is far from ideal, but if you're stuck with it, one option is to have a new Owner table that has the owner_id from the Car table as well as a person_id and a company_id, only one of which would need to be populated.

Create a SQL table with a varying number of columns

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.

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