I have a requirement like below:
Department -->> ( has many) employees
Employee -->> (can belong to only one department) Department, no employee can exist without any department.
For setting this up, I have setup the model like
Department
relationships
name: employees
destination: Employee
optional:yes
to-many:yes
delete-rule:cascade
inverse-relationship: none
Employee
relationships
name: department
destination: Department
optional:NO
to-many:NO
delete-rule: No action
Should I create a inverse relationship anywhere in this model?
Have I setup delete-rule for department relationship in Employee right?
Is there anything else I should to get this model right?
Thanks
Yes, you should almost always specify the inverse relationship according to apple's documentation. You've got the option of not doing so, but it should rarely be used.
I think there may be situations where your database can become corrupted if you don't create an inverse relationship, but I'm no expert.
Those two relationships should be inverse relationships of each other.
I'm not sure about the delete rule. Sorry.
EDIT: #jrturton's comment suggests your delete rule is fine.
Related
Employee_Department_ER
In this example, entity Department is managed by many employees and each Department must participate in such relationship (double lines indicate total participation). Entity Employee can manage one Department and each Employee may or may not participate in such relationship.
I'm wondering what is the correct way to map this into a relational schema in a database. I've done some search on Google and other posts but their answers still confuse me. One approach is to use
Employee(<attrs_for_employee>, department_id) // department_id is a foreign key that refers to Department's primary key
Department(<attrs_for_department>)
This approach clearly shows the 1:N relationship, but I don't see any total participation in the Department (Department might or might not be managed by an Employee).
If we extends Department instead,
Department(<attrs_for_department>, employee_id)
Employee(<attrs_for_employee>
This approach shows the total participation in the Department but loses the 1:N relationship.
Is there a correct way to show both the 1:N and total participation in relational schema? Is it necessary to create a new schema, Employee_Department that merges both of the primary keys like in a M:N relationship?
Use a database trigger. The trick here is that "a department must be managed by one or more employees" can't be directly represented in the model itself. It has to be in logic. The best logic is in a trigger. The exact code for your vendor product will vary, but the logic is this:
Put a BEFORE INSERT trigger on DEPARTMENT.
In that trigger, get the EMPLOYEE_ID of the incoming INSERT.
Go find that in the EMPLOYEE table.
If it is there, INSERT.
If it is not, throw a message saying that one EMPLOYEE must be added first, and that such an employee must be used in the record to be inserted in DEPARTMENT.
Put a BEFORE DELETE trigger on EMPLOYEE. This is to ensure you don't later orphan the DEPARTMENT.
In that trigger, ensure that DEPARTEMENT has at least two EMPLOYEEs, so that the one you're about to delete is not the last one.
If two or more, let the DELETE pass.
If just one, throw an error saying you can't DELETE the last EMPLOYEE.
Categorically, there are many rules that must be enforced in code that cannot be enforced in the model. Triggers are the way to go.
If you're on a platform that doesn't do triggers well or at all, you have to write this either in stored procedures run on a schedule, or in stand-alone code (e.g. Python, Java, etc.) and run on a schedule. Triggers are magical for data rules!
A company database needs to store information about employees
(identified by ssn, with salary and phone as attributes),
departments (identified by dno, with dname and budget as attributes),
and children of employees (with name and age as attributes).
Employees work in departments; each department is managed by an
employee;
a child must be identified uniquely by name when the parent (who is an
employee; assume that only one parent works for the company) is known.
We are not interested in information about a child once the parent
leaves the company.
Draw an ER diagram that captures this information.
I am using "Look Across" Min-Max notation here.
Are the cardinalities between Employee and Department correct?
For instance, is it possible that a Department has zero Employees? Or, is this a relevant information?
Your answer is correct and matches the instructor key for this common example in two different texts that I own.
Other thoughts
Unfortunately most textbooks are poor reflections of reality because they trivialize things, but this is considered necessary when introducing students to complex topics such as modeling reality in a database.
This illustrates problem with creating ERD from English-language narrative which can be ambiguous and open to interpretation, however. Might score some extra points with your instructor if you point that out. In reality, it is not uncommon for employees to work for multiple departments (I do), and for managers to manage multiple departments (mine does). When working with clients it is a good idea to also express the more ambiguous designs with a physical data model (tables and rows) or an actual user interface (form for data entry) so that they are certain of what they are getting.
If a department MUST have a manager and that manager is by relationship working for that department then a department must have minimum 1 employee.
An Employee can (I'm assuming) manage more than 1 department theoretically and a department can also (assumption) be managed by more than 1 Employee.
So I see it like this
Employee *-* Department (many to many relationship)
or in your terms
Employee 1-N Works In 1-N Department
Employee 1-N Managed By 1-N Department
You cardinality is incorrect for the case employee works in department. Since an employee can work in 0 or many departments. Here, the cardinality should be as follows:
employee (0,n) - works in (0,n) departments.
While in the other case department has to have an employee associated with it.
The cardinality in the case department managed by employee is correct in your case.
What is the best practise if I have "is a" and "has a" relationships both in the same time as in figure below
Any help will be appreciated.
These kind of relationships where is-a and has-a both exist together are mostly self-referential relationships.
Classic example of such a relationship is employee to manager. Manager is an employee and Manager has many employees reporting to him.
So, the best practice is to make a foreign-key point to the same table. To elaborate - if we take the same example as employee-manager as I told above, then employee table will have a column 'manager' which is nothing but a foreign key to employee table itself.
I'm doing a project for my Database exam, and I'm stuck at a tricky point. This is an excerpt of my ER diagram:
As you can see, the entity Employee is generalization of Waiter and Cook. Head waiter and Chief cook are specializations of respectively Waiter and Cook. Only chiefs can fullfill an Order (non included in this excerpt) to the supplier. Obiously only one Chief at a time can Fulfil an order (I've made a single Fulfilment relationship to make the diagram less messy).
After remodeling the ER diagram (sorry if I'm not using the correct words, I'm translating in English), this is the result:
So an Employee must have a Role (Waiter, Cook or whatever else), and for each role there could (obviously) be only one Chief. I really don't know if this is correct, the double generalization confuses me a lot.
Next step is the logical diagram (tables), and this is a total mess!
This is a possible solution:
This is a possible translation to the relational model (with an Unique Not Null constraint on FK2 in tblChief) , but it has a weakness: if you change Employees role in tblEmployee, tblChief won't see this error and integrity will be broken. Could adding a FK between tblEmployee(Role) and tblChief(Role) be a good solution? Or would it be ugly/messy?
I've thought of a second possible solution. I prefer it, but still I don't know if it's OK:
Here I've merged the entity Role with the entity Chief in a single table. The field Chief references IDEmployee, again it must be Unique and Not Null.
Are these solutions correct? I'm sure that this will be a big mess when inserting and updating Employees and Roles (AFAIK it can't be done without temporarily removing the integrity checks). Could be this done in a different way?
Sorry for the long post (and for the image links, but I can't post them directly) and many thanks if you'll answer me :)
The last diagram does not make sense.
Each employee has 1 role (this is a FK relationship)
Each role can have one superior role. This is a nullable FK relationship back to the role table. (Itself). Not to employee as you have it.
To find all employees that are managers you would have a select statement like this:
SELECT *
FROM employees
WHERE roleID in (SELECT roleID
FROM role
WHERE superiorID IS NULL)
or
SELECT *
FROM employees E
LEFT JOIN role R ON E.roleID = R.roleID
WHERE R.superiorID IS NULL
I am mapping an idea for a relationship using Core Data.
I have an Employer entity who has a many-to-many relationship with Employees. Basically, an employee can work for multiple employers, and an employer can have multiple employees.
The problem I'm facing is, I'm not sure how to manage the contracts between employees and employers.
As an employee can work for either 1 or many employers, they would naturally have a contract for each employer they work for (complete with salary, duration) and a list of dates when they are working for a specific employer.
My question is - how to manage the relationships between Contracts, ContractDates with Employers and Employees?
Thank you.
Image follows.
in the real world you have a contract between an employer and an employee. So why not do the same in core data?
Use a junction table.
That is, the contracts table refers both to Employers and Employees, linking them together. It also contains information on contracts. This way, the contract applies to the relation between employer and employee, which is exactly what you want.