I have the following tables: Department, Section, Employee,Manager, and Position. The relations between the tables are as follows:
Each Department contains many sections
Each Department contains many positions and each position might be shared with different departments
Each department section has a manager
Each manager might has different positions, for instance, Urology Department Chairman is a position as manager, and as an employee he is a Urology Consultant. As an employee, he has two positions.
As you can see from the image above
DepartmentSections is a Join table between Departments and Sections
DepartmentPositions is a Join table between Departments and Positions
My problem now, How I should configure Managers? I am thinking of making two relations, first between the Manager and DepartmentSections and between the Manager and DepartmentPositions, the problem here is that I will get two DepartmentId fields in the table Manager.
Is there any mistake in my analysis? how to build this model in order to avoid 2 DepartmentId in the table Managers?
Some thoughts on your post. It is not an answer (too long to comment) but might help a bit.
Each Department contains many sections
From this I guess that you don't need the lookup table DepartmentSections since this sentence describes one-to-many and not many-to-many relationship. You could add the DepartmentId in the Sections table.
Each department section has a manager
So you could add the ManagerId to Sections table.
Now for positions and managers, If I understood correctly, I would add the ManagerId to DepartmentPositions.
Related
I have an ERD.
The Stationlog table logs which employee worked at the station and at what time the employee logged in and out. The station produces orders.
As of now there can only be 1 employee logged in at a station but I am trying to figure out how I can have multiple employees in a stationlog. I want one Employee to be the manager within that Stationlog and give him the ability to add an infinite amount of employees to that stationlog.
I have tried adding fields such as employee1, employee2, employee3 in the Stationlog (not shown in this ERD) but that means I cannot add an infinite amount of employees to each stationlog.
How would I be able for one manager (who is an employee) to be able to add x amount of employees to 1 Stationlog?
Your current design is that one specific Stationlog row refers to exactly one Station and one Employee. Another Employee for the same Station would imply that there's a different Stationlog row for that other employee. (hopefully not overlapping with the first). Nothing prevents 10 employees working at the same time on the same station, each with a different log entry.
If this is not sufficient, and you want one employee define the "session" with login and logout time but allow several employees on that log, you'd need to make the relation Stationlog >o----|< Employee many-to-many.
Of couse many-to-many is easily drawn on the diagram, but would require an association table for its implementation to hold the valid pairs.
Stationlog >o------ Logteam ----|< Employee
------------
StationlogID
Employee
(OrderOfArrival etc...)
In Logteam you'd put everything which is specific go a given session and a given employee, such as arrival/departure, if this could be different from login/logout. If managers are predefined, you'd put a IsManager flag in Employee table. But if it's dynamic, e.g. the first who come (or has the key for the station), then you'd put this flag in this association table as well
In AdventureWorks2008R2 the Sales.SalesPerson table contains a TerritoryID which creates an easy reference to the current Territory assigned to a SalesPerson. The Sales.SalesTerritoryHistory table is also available to analyze past assignments.
I noticed the HumanResources.EmployeeDepartmentHistory follows a similar pattern; however, the HumanResources.Employee table does not have a direct reference to the current Department. In other words, there is no DepartmentID on the HumanResources.Employee table.
Is there a good reason why they wouldn't follow the same pattern?
It's most likely the employee can work in multiple departments at the same time and that's why it is set up that way
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.
I have a core data project which has a fairly simple data model but there is one feature that I am having trouble implementing. It is a really basic feature so I know it must be able to be implemented but I am, unfortunately, stumped.
Rather than burden you with the project details I will instead use an employee scenario to explain.
Here is the data model
Entity = Employee
Attributes:
firstName
lastName
Relationships:
grade
department
Entity = Grade
Attributes:
grade
Relationships:
employeeGrade
Entity = Department
Attributes:
department
Relationships:
employeeDepartment
employeeGrade is a many-to-one relationship with grade from the Employee Entity
employeeDepartment is a many-to-one relationship with department from the Employee Entity
All attributes are strings.
I have a table set to add grades to the grade entity.
I have another table beneath the grade table to display the list of employees who have the grade that is selected in the grade table.
I want to display the first name, last name and department for each employee in this table. By using array controllers and bindings in IB, displaying the first and last names is no problem but getting the department to display is what is proving problematic for me. It is as you can see a many to one relationship and I cannot figure out how to set this up with bindings so that the department for the employee is shown in the table.
I can easily populate a popupmenu cell with the all departments but I simply want to display only the appropriate department in a text cell
I know the answer must be simple but after days of searching the net and experimenting I am lost.
I am learning how to create view using SQL server. I am trying to add a view called Managers in the Northwind database that shows only employees that supervise other employees. This is what I have so far.
Create View Manager_vw
As
Select LastName,
FirstName,
EmployeeID
From Employees
Where
What I am stuck on is how and I going to put in supervise other employees. I am not to sure how to do this. If someone can help me understand how to do this.
In northwind.dbo.employees you would find employees who supervise other employees by looking the reportsto column. Basically you want to return employees whose id is in the reportsto column in another row. That can be done like this:
SELECT LastName,
FirstName,
EmployeeID
FROM employees E
WHERE EXISTS(SELECT * FROM Employees WHERE reportsTo = E.EmployeeID)
The EXISTS is like a JOIN but is usually implemented as a "semi-join" which will stop processing after it finds a singe match (rather than finding all the subordinate employees which would take extra work) Because it doesn't return any additional records, you also save the cost of the additional step to eliminate duplicates (a JOIN would do more work to process the join, and even more work to undo the work that wasn't necessary by doing a DISTINCT.)
You'll notice that I reference E.EmployeeID in the subquery, which relates the subquery to the outer query, this is called a Correlated Subquery.
A word of caution: Views have their place in a DB but can easily be misused. When an engineer comes to the database from an OO background, views seem like a convenient way to promote inheritance and reusability of code. Often people eventually find themselves in a position where they have nested views joined to nested views of nested views. SQL processes nested views by essentially taking the definition of each individual view and expanding that into a beast of a query that will make your DBA cry.
Also, you followed excellent practice in your example and I encourage you to continue this. You specified all your columns individually, never ever use SELECT * to specify the results of your view. It will, eventually, ruin your day. You'll see I do have a SELECT * in my EXISTS clause but EXISTS does not return a resultset and the optimizer will ignore that in that specific case.
Here's another option:
SELECT DISTINCT manager_tbl.*
FROM Employees AS staff_tbl
JOIN Employees AS manager_tbl
ON staff_tbl.ReportsTo = manager_tbl.EmployeeID
Adapted from this site. There are a number of example queries there that you might find interesting and useful.
Notes:
Using the DISTINCT keyword because a single manager could have more than one direct report. DISTINCT will omit the repetition caused by such a one-to-many relationship.
The Employees table in the Northwind database is an example of a hierarchical relationship modeled in a single table.
All together:
CREATE VIEW Manager_vw
AS
SELECT DISTINCT manager_tbl.*
FROM Employees AS staff_tbl
JOIN Employees AS manager_tbl
ON staff_tbl.ReportsTo = manager_tbl.EmployeeID