Database Table Referencing Several Records - database

I'm looking for some guidance as to how to set up a database I am creating. I think the best way to ask the question is to describe the specific scenario:
I have a database that will hold information about student-employees at a school.
I have one table called "Semesters" that contains information about a specific semester (e.g. "Fall/Spring/Summer", year, etc.).
I have another table called "Employees" that contains information about a specific student-employee (e.g. name, jobTitle, startDate, endDate). I would also like to have a reference to the "Semesters" table indicating which semester(s) the employee was hired. So, for example, one column in the "Employees" table would tell me that this specific employee was working during the "Fall2010, Spring2011, and Fall2011" semesters.
The problem is that I don't know how to reference several records in one field.
I am sure that this is a fairly common scenario, I'm just not sure how to go about it.
Answers that reference MS Access or SqlServer would be excellent.
Thanks.

This sounds like a many to many relationship. In such a case you would likely utilize a junction table.
Employees (empID, name)
1 bob
2 moe
3 george
Semesters (semID, name)
1 fall 2010
2 spring 2010
EmploymentHistory (empID & semID)
1 1 (bob was employed fall 2010)
1 2 (bob was employed spring 2010)
2 2 (moe was employed fall 2010)

You can accomplish this with several foreign keys to the Semester table (not Semesters) from the Employee table (not Employees).
If you want to keep track of the semesters that an Employee has worked, I'd normalize that into a WorkHistory table that's one-to-many with Employee and many-to-many with Semester.

Related

Not allowed to create or change a record in MS Access

I have read numerous thread on the above question, but none of the answers satisfy the problem. My problem is two tables that are linked to each other (in a one to one relationship) using an "ID" field which is also the primary key. When I try to enter a record in the the main table (Don't know if Access knows this is the main table) the I get the following error:
"You cannot add or change a record because a related record is required in table"
Could anyone please help me with this?
Thanks
A "One-To-One" relationship is really a One-To-Zero_or_One relationship, but the table diagrams on the Relationships page in Access do not make it clear which table is the "parent":
However, if you right-click on the line joining the tables and choose "Edit Relationship..." you will see
In this case [Table1] is the "parent" table and [Table2] is the "child" ("Related") table, so we must insert a row into [Table1] before trying to insert the corresponding row into [Table2].
The issue seems to be occuring because you are trying to set the primary key of your table as a foreign table.
What you need to do is create a new column in the 2nd table and link it to the id of the first table, therefore creating a relationship.
Let's take the following simple example since you haven't mentioned the names given to your tables.
In a university environment, you would have the table LECTURER which is linked to the table SUBJECT, so that a lecturer can teach a subject. The relationship between the 2 tables would be so:
LECTURER TABLE
LECTURER_ID | LECTURER_NAME | LECTURER_ADDRESS
SUBJECT TABLE
SUBJECT_ID | SUBJECT_NAME | SUBJECT_DESCRIPTION | SUBJECT_LECTURER_ID
The link between these two tables would be between the SUBJECT_LECTURER_ID from the SUBJECT table and the LECTURER_ID from the LECTURER table. This is a one to one relationship since we are assuming that each subject in this particular university is only taught by one lecturer, and a lecturer can only teach one single subject.
Hope this helps :)

SQL hierarchyid on employee table with inactive records (multiple root nodes)

For those using a SQL hierarchyid for an employee table, how do you accomodate inactive (retired, terminated) employee records contained on the same table? In the case of a new CEO (root) this leads to several rows that have a NULL parentID. Is it common practice to migrate those records to a separate table?
We have an employee table that has both active and inactive records. This is useful for many of the downstream systems that use employee data but are slow to update their own related data after an employee leaves. For example, a project system that uses the employee table to track the project manager. If those systems were to refer to a table that only had active employees they would not find matching rows when pulling employee data.
Clarification on question
To clarify the question, I have read articles on multiple roots and I realize that is possible. My question is more about how this is dealt with in the field. As stated, one option is to have inactive records could be moved to a separate table so the root node is always a single CEO. Another option might be a dummy root for "org" followed by all the CEO records (past and present). Or you can skip the "/" root altogether and just start with all the CEO records. I assume this scenario (employee table with active/inactive CEO type records) is common so I wanted to hear what solutions are being used.
Update on source data
FYI - The source employee table is upstream from my system and we get updates from an Active Directory feed. I cannot switch the source to eliminate the child_id / parent_id columns. I can however update our copy to include hierarchyid for the apps downstream from us. I would keep the child_id / parent_id in our data so I could use the feed to update our copy.
One way you can do this and maintain the historical hierarchy is to include a Start & End Date for each relationship. So there are 5 VPs reporting to CEO John, and John is leaving next month. When new CEO Dave come in, these 5 VPs will report to Dave instead. You don't need to change the whole hierarchy.
Another approach is a few fixed title at the top, and simply update the CEO's name from John to Dave (I don't recommend this though but I have seen it being used).

Foreign Key hold values of Primay Key

I am trying to design a Database for a Patient record.
I am stuck at one point when dealing with Medicine List.
"Table_Patient_Record" - is having a field - "Medicine_Ids" ...
and "Table_Medicine_Record" - having unique - "Id" & "Medicine_Name" ...
Now, my problem is, How to hold multiple(variable) "Id" of "Table_Medicine_Record" into "Medicine_Ids" of "Table_Patient_Record"?
Since, the number of medicines belong to a patient varies from other patients.
Then, how to design database so that from a UI we can Insert, Update and Select.
Thanks, I hope my question is to clear to understand.
If I'm correct, you have a database 'Patients' and 'Medicines'.
The easiest solution is to create a table 'PatientMedicines' that holds a userid and a medicineid.
This way you can connect multiple patients to multiple medicines and the other way around.
And still leaves the freedom that a patient does not need to have any medicine.
Hope this helps.
This seems like a many to many relationship to me. One patient can use multiple medicine, and a medicine can be used by multiple patients. Use a junction table that holds the patient id and the medicine id. Like this:
PatientId | MedicineId
1 1
1 2
2 1
2 3

access: how to add a workorder to a customer

I have a noob question but he, i'm learning :-)
I'm making a form with the following tables 1 tblCustomers and 1 tblWorkorders.
My question is:
When I add a customer to a new record, this person is stored in the table: tblCustomers this is going fine.
The problem is that I also have a table: tblWorkorders, in this table I store all the technical information, sollutions and the customers belongings. (adapter, notebook bag etc etc)
My problem excists when for example a customer named John Doe comes back with another problem 2 weeks later. In the table tblWorkorders should be 2 records with the problems of John Doe I think it has something to do with relationships between the tables, can someone tell me where to find a good example or when it's a short story, how to do this?
Very difficult to explain this concept and start you off from scratch. Be prepared for further research on different item. Here is a place to start: http://office.microsoft.com/en-us/access-help/guide-to-table-relationships-HA010120534.aspx
The following is how you would use your tables:
You need to have a common field in both tables (it can be more than one field, but let's keep it simple). The easy way is to have a CustomerID field that is a Data Type field set to: AutoNumber (It does just what it says.).
tblWorkOrders will have the same field (doesn't have to be the same name, but let's keep it simple) BUT, the Data Type is: Number Field Size: Long Interger.
If you're able to use: Database Tools | Relationships, and join the two tables by this field, developing forms and reports is a lot easier.
Your form will be based on the tblCustomers table (I know, let's keep it simple.) and a Sub Form will use the tblWorkorders table and the 'Link Master Fields' and the 'Link Child Fields' will use the CustomerID from each table.

merging two duplicate contacts/ColdFusion

Having to do with data integrity - I maintain a coldfusion database at a small shop that keeps addresses of different contacts. These contacts sometimes contain notes in them.
When you are merging two duplicate contacts, one may be created in 2002 and one in 2008. If the contact in 2002 has notes prior to 2008, my question would be does it matter if you merge these contacts and keep the 2008 contact's ID number? Would that affect the data integrity or create any sort of issues with the notes earlier than 2008?
I hope I've accurately described my scenario, as I am not familiar with the proper technical terms.
I really appreciate the help sir!
I will say that the fact that the app is ColdFusion is pretty well irrelevant to your problem.
It seems like some of what you're asking depends on your business requirements. Do you need to retain older notes?
As other folks are saying, it depends in large part on your table structure. If, as I suspect, you've got just one table that has a NOTES column in it, you'll need to figure out how to concatenate the values in multiple rows that all refer to the same person.
It sounds like you have two tables - contacts and notes. The notes table has a foreign key back to the contacts table to record which contact a note belongs to.
So, imagine two Contacts - Bill (primary key 1, created in 2002) and William (primary key 2, created in 2008).
Imagine one Note with a foreign key 1 (ie this note belongs to Bill).
If you merge Bill and William, and only keep the William record, then you would need to update the foreign key from 1 (Bill - deleted) to 2 (William) on the note or it will not display on William's record.
(If you add further details about your table structure we can probably help more.)

Resources