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?
Related
There are three tables linked to each other as in the diagram.
This Youtuber is using IsDeleted=0 or IsDeleted=1 column to indicate that the data in the table is deleted or not.
When one SQL table is linked to another table, is it impossible to delete the data from the database? If it is impossible, then I think its really bad. Isn't there any workarounds for completely deleting the data?
Also, maybe Facebook also does something like IsDeleted=1 and they don't let us see us our own data when we 'delete' it while they can still access our past interests. What do you think?
No, data can always be deleted from a database. There may be constraints what has to happen before the data gets deleted (permissions, foreign-key constraints, etc), but it eventually can be deleted.
A column such as that is frequently used for maintaining data history in the application. The "links" you refer to are called Foreign Key relationships and they are used to maintain integrity within relational data. The IsDeleted column in the resultset doesn't have anything to do with the FK.
Also, that column would not indicate that the table is deleted, but rather the row of data the values of the column belong to.
Well defined and static and structured arrangement of data is one of the most important things in relational databases. In the tables you show, Employee belongs to a department. Now if you delete the department, you have a broken record in the Employee table. This is why, SQL will not allow you to delete the department.
To solve this, you will need to either stop the deletion of department with message like "There are users in this department. Cannot be deleted." or remove the department id (set it to null) from the employee records which belong to this department.
There are situations, business needs where the data is to be retained for various reasons. In those cases, IsActive like flags come to rescue. The records can be filtered using them and thus are hidden on the screen. This is generally referred to as soft delete.
Actually, i think it is very good practice to do Soft Delete rather than hard delete because you can safely recover the data later. To your question you can only delete parent table data if there are no more child table data. Else you get this errror:
the delete statement conflicted with the reference constraint ...
for example in your database say you have data of employee and departments. e.g this is your department table:
ID DepartmentName
1 HR
2 marketing
and this is your employee table:
ID DepartmentId Name Address
1 1 Name1 address1
2 1 Name2 address2
3 1 Name3 address3
Now if you want to delete marketing department from Department table , you can delete it easily and there is no error. But now if you try to delete HR department you will get error as mentioned above because the HR department Id(i.e. 1) is already linked with another table called Employee table. So if you want to delete HR department anyhow, you must first delete all the record from employee table with DepartmentId=1 . otherwise you can use the approach as the youtuber you mentioned.
Hope it helps !
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.
I am wondering if it is okay to have master and detail table for employees?
As per requirments, data can filtered by department by country and by employee code on report level.
If employee's department or country code is changed then the changes will go in detail table and old record will be set to IS_ACTIVE = 'T'.
---------------------Master Table--------------------------------------
**EMPLOYEE_CODE** VARCHAR2(20 BYTE) NOT NULL,
EMAIL VARCHAR2(100 BYTE)
FIRST_NAME VARCHAR2(50 BYTE)
LAST_NAME VARCHAR2(50 BYTE)
WORKING_HOURS NUMBER
---------------------Detail Table--------------------------------------
**PK_USER_DETAIL_ID** NUMBER,
FK_EMPLOYEE_CODE VARCHAR2(20 BYTE),
FK_GROUP NUMBER,
FK_DEPARTMENT_CODE NUMBER,
FK_EMPLOYER_COUNTRY_CODE VARCHAR2(5 BYTE),
FK_MANAGER_ID VARCHAR2(20 BYTE),
FK_ROLE_CODE VARCHAR2(6 BYTE),
START_DATE DATE,
END_DATE DATE,
IS_ACTIVE VARCHAR2(1 BYTE),
INACTIVE_DATE DATE
Employee table will be linked with Timesheet table and for timesheet reports data can be filtered by department, country and by employee code.
OPTION : I
Have one employee table with one Primary Key and create a new entry whenever department or role is updated for an employee.
Add country and department code in the timesheet table.
--> This way i don't need to search employee table.
OPTION : II
Have master and detail table.
Add country and department code in timesheet table.
--> This way i don't need to search employee table plus i will have master detail table
OPTION : NEW
Have master and detail table.
Timesheet table will have EmpCode.
If user move to new location or change department then Insert a new row in the detail table with the new dept Code and same Emp No.
Update an old row and set the End Date field so if he changes his location or department then the End Date field needs to be updated.
Which one is a best option and is there any other better option available?
This is one way of implementing this requirement, and it's an approach many people take. However, it has on emajor drawback: every time you query the current employee status you need to filter the details on start and end date. This may seem like a trivial thing, but you wouldn't believe how much confusion it can cause, and it has performance implications too.
These things matter, because most of the time you will want only the current details, with queries on history being a relatively rare occurence. Consequently you are hampering the implementation of your most common use case to make it easier to implement a less-used one. (Of course I am making assumptions about your business requirements, and perhaps yours is not a run-of-the-mill employee application...)
The better solution would be to have two tables, an EMPLOYEES table with all the detail columns too and an EMPLOYEES_HISTORY table with the same columns plus the start and end date. When you change an employee's record insert a copy of the old record in the History table, probably by a trigger. Your standard processes have just the one table to query, and your history needs are met fully.
By the way, your proposed data model is wrong. Working_hours, email_address and last_name are definitely things which can change and perhaps even first name (e.g. through changes in personal circumstances such as getting married). So all those columns should be held in your details name
Option 3 - Please note that this option is useful only for the reports Point of View.
Whenever you insert the data, create a De-Normalized entry in a new table.
Whenever an entry will be updated, the De-Normalized entry will be updated in the new table.
The New Table will have all De-Normalized columns of Employee.
So while Performing the search, this will benefit you as the results will be calculated without using Joins. Thus, the access time will be reduced.
Records in the new table will be Created/Updated in The Insert/Update Trigger.
Improvements in Option - 2 and Option 1
Don't create redundancy by adding duplicate columns.
I have a question about entity creations that is specific to a student information system that i am building. i have created a Person table (id..) and i am trying to find out how i can handle my student, parent references. is it a good idea to create two separate tables (Student, Parent) that reference the Person table by FK relationship? All of the details about a Person (firstname, last name, SSN ...) have been set in the Person table but there are differences between a parent and student, how do you handle this in a database?
Since there are fundamental differences between parents and students, two tables would be the preferred solution. This way you can easily create a relation connecting the students and parents.
The other option is to use null values in the columns that do not apply for a given record. However, it will be more difficult to ensure that the relation always connects a student and parent.
I agree with Casey Robinson in that its a clean solution.
But if you already have a populated Person table which is being used by other code... in short you can't change the Person table then here is what I would suggest:
Create a table (studentParent) which will have two columns (student_id and parent_id) both foreign keys. The studentParent.student_id = Person.id of the student and studentParent.parent_id = Person.id of the parent.
This way you won't have to change the Person table. And will be able to create the parent, student relationship.
Without knowing more details it seems that two tables Person and Student should be sufficient. Have two columns in Student table like Student_id and Parent_id each of which is a FK to person_id in Person table. This is assuming you will need to know for only student which is a parent and not for every person. Also assuming that both student and parent are person.
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.