Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 15th, 2009, 10:46 AM
webdbguy webdbguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 webdbguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 5 sec
Reputation Power: 0
Database Design

Hi all,
I am designing a database as a practice exercise, but have come across a problem.

Here is my design:
Code:
Branch           Manager             Employee
-------         ----------           ----------
BranchID   ---- ManagerID      |---- EmpoyeeID
...        |    EmployeeID ----      ...
Manager ----                         EmployeeIDOfManager
                                     ...
                                     Branch


Note: The ... in the tables are just placements symbolising that there are multiple irrelevant (to this thread) fields.

The problem is with the field highlighted in italics whilst the primary keys are in bold. I need to somehow retrieve that ID, but I can't create a many-to-many relationship.

I have tried to think of a solution to the problem, but don't know how to solve it

Could anyone help?

Reply With Quote
  #2  
Old May 15th, 2009, 11:04 AM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Oct 2003
Posts: 1,652 MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level)MrFujin User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 14 h 19 m 51 sec
Reputation Power: 945
can you be more specific on what you want?

eventually give an example of what data you have and what the result you want.

Reply With Quote
  #3  
Old May 15th, 2009, 11:17 AM
webdbguy webdbguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 webdbguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 5 sec
Reputation Power: 0
Well, the Branch table is going to hold the different branches where the banks are located.
The Employee table holds the employees which work within those banks at the different branches.
I added in the Manager table to create 2, one-to-many relationships.
Along with these tables there will also be customer details, but that is not important for the problem at hand.

So, each row in the Employee table holds an employee, and each employee has a manager. I need to somehow get that manager's EmployeeID and detail it in the row. However, I do not know how to achieve this because the data I need is from the same table.

Some example data for each table:
Branch:
BranchID City Postcode Manager
'001' 'London' 'W1 2ED' 'Fujin'

Manager:
ManagerID EmployeeID
'A00' '00A'

Employee:
EmployeeID Name Steet Manager Branch
'00A' 'Fujin' '123 Street' 'Null' '001'
'00B' 'John' '124 Street' 'A00' '001'

Reply With Quote
  #4  
Old May 15th, 2009, 01:38 PM
MadDogBrown MadDogBrown is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Posts: 730 MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 3 h 40 m 5 sec
Reputation Power: 516
Simplify your design.

Eliminate the un-needed manager table; all manager's are already employees....

Let the ManagerID field in the employee table contain NULL for those that are managers, and an EmployeeID of the manager for those that are managed.

Then do "self-join":
Code:
select emp.name as EmployeeName,
		mgr.name as ManagerName
from emp
LEFT OUTER JOIN emp as mgr
	on emp.MgrId = mgr.id

Reply With Quote
  #5  
Old May 15th, 2009, 05:54 PM
webdbguy webdbguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 webdbguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 5 sec
Reputation Power: 0
What you have responded is exactly what I thought of doing and in fact how I started off, but would it be possible to do it any other way using only fields and tables?

Reply With Quote
  #6  
Old May 15th, 2009, 06:31 PM
MadDogBrown MadDogBrown is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Posts: 730 MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 3 h 40 m 5 sec
Reputation Power: 516
Lot's of things are possible.

But why over-complicate things?

What do you stand to gain from doing so?

By giving a manager an arbitary ID - now you can no longer reference him as an employee without one extra layer of indirection.

THere is no absolute "right" or "wrong": there are only your design goals and a best-fit implementation.

You could treat managers and employees as separate entities, and model that relationship by way of two tables: employee, manager.

But what do you gain from doing so? I now see that you likely followed this approach to it's conclusion, which is why you are mapping a manager ID to an employeeID. Consider using an attribute (field) in your employee table instead (IsManager or similar)

With a self-referential model you can also represent hierarchies that are many levels deep: the queries then become recursive.

CTE (Common table expressions) can be used to traverse these hierarchies with ease.

This self-referential model is very similar to the "composite" design pattern.

BOM's (bill of materials) are commonly represented in this fashion.

Last edited by MadDogBrown : May 15th, 2009 at 06:37 PM.

Reply With Quote
  #7  
Old May 15th, 2009, 10:51 PM
webdbguy webdbguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 webdbguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by MadDogBrown
Lot's of things are possible.

But why over-complicate things?

What do you stand to gain from doing so?

By giving a manager an arbitary ID - now you can no longer reference him as an employee without one extra layer of indirection.

THere is no absolute "right" or "wrong": there are only your design goals and a best-fit implementation.

You could treat managers and employees as separate entities, and model that relationship by way of two tables: employee, manager.

But what do you gain from doing so? I now see that you likely followed this approach to it's conclusion, which is why you are mapping a manager ID to an employeeID. Consider using an attribute (field) in your employee table instead (IsManager or similar)

With a self-referential model you can also represent hierarchies that are many levels deep: the queries then become recursive.

CTE (Common table expressions) can be used to traverse these hierarchies with ease.

This self-referential model is very similar to the "composite" design pattern.

BOM's (bill of materials) are commonly represented in this fashion.


I just wish to learn how I could achieve such a result without the use of SQL code, but I am finding it difficult to overcome this problem. I just can't escape the many-to-many relationships

I can't use boolean values since I'm using Oracle.

Reply With Quote
  #8  
Old May 15th, 2009, 11:16 PM
MadDogBrown MadDogBrown is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Mar 2009
Posts: 730 MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level)MadDogBrown User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 3 h 40 m 5 sec
Reputation Power: 516
I'm not following you, sorry.

Could you clarify, because I fail to see what the problem is.

It's not a many to many relationship at all.

1 instance of an employee can only have one manager.

1 instance of a manager can can have 1 to many employees.

It's really no different than a "parent/child" or "has-a" relationship involving two tables: this concept of a self-referential relationship is well-established in the SQL community.

Again, it's really similar to the composite design pattern, or a bill of materials.

Last edited by MadDogBrown : May 15th, 2009 at 11:19 PM.

Reply With Quote
  #9  
Old May 16th, 2009, 09:52 AM
webdbguy webdbguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 webdbguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 5 sec
Reputation Power: 0
My problem is that I need to somehow get the EmployeeID of the manager and have it stored in each row for each employee.
Take the following tables:
Branch: BranchID City
Employee: EmployeeID Name Address TelNo Manager Branch
In the Employee table I need the Manager field to store the EmployeeID of that manager, but I also need the Branch field to relate to the BranchID field in the Branch table.
This would need me to therefore acquire data from the same table, but I want it to be able to get that data without SQL code. Perhaps the introduction of new fields, or a new table? I'm just unsure how to solve the problem

I want to achieve this without SQL code so that it could be implemented in any database type, whether it be Oracle, MySQL, MSSql, Access etc.

Reply With Quote
  #10  
Old May 17th, 2009, 12:44 AM
clivew clivew is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 1,124 clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 6 Days 1 h 39 m 23 sec
Reputation Power: 272
Quote:
Originally Posted by webdbguy
I want to achieve this without SQL code so that it could be implemented in any database type, whether it be Oracle, MySQL, MSSql, Access etc.


I'm sorry; but this makes absolutely no sense.

If you don't want to use SQL don't use a SQL database for storage.

Anyway, you can get nothing from an SQL database without using SQL, be it explicit code you wrote,
or code being written behind the scenes by the tool you are using to read it.

Relational databases are founded upon mathematical principles and you should not be
ignoring those rules without good and carefully considered reasons.

In any case, all the databases you mention above can extract the data you want using SQL.

If you want to access multiple SQL databases, you should have a common interface and instantiate the appropriate class with the appropriate optimized SQL for each database inside that class.

Indeed, if you do that properly, you can store and retrieve the data any way you want. Its up to you in the class you write to implement the common interface.

Clive

Reply With Quote
  #11  
Old May 17th, 2009, 09:35 AM
webdbguy webdbguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 webdbguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 5 sec
Reputation Power: 0
So my only option to get the Manager field in the Employee table to display the EmployeeID of that manager is by using an SQL statement like:
Code:
select Employee.Branch, Employee.Manager, Branch.BranchID from Employee, Branch
where Employee.Branch = Branch.BranchID and where Employee.Manager = (select EmployeeID from Employee
where Job = 'Manager');

Such that I would introduce a Job field in the Employee table.

Reply With Quote
  #12  
Old May 17th, 2009, 01:22 PM
clivew clivew is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 1,124 clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 6 Days 1 h 39 m 23 sec
Reputation Power: 272
I think you have not picked up on the point that you do not need a separate Manager Id.

The employee table has all the info. you need if you follow that suggestion.
Code:
EmployeeID Name Steet Manager Branch
'00A' 'Fujin' '123 Street' 'Null' '001'
'00B' 'John' '124 Street' '00A' '001'


Note that in the (changed) second line the manager Id IS the employee Id of the manager.

If you want more information about the manager in the row then there are some special recursive SQL
syntax that can be used; but if you just need something simple then:

SQL Code:
Original - SQL Code
  1. SELECT E1.EmployeeID, E1.Name, E1.Steet, E1.Manager, E1.Branch,
  2. E2.Name AS MgrName
  3. FROM employee E1
  4. LEFT OUTER JOIN Employee E2
  5. ON (E1.Manager = E2.EmployeeId)


As MadDogBrown already explained more succinctly.

You can also turn the SQL into a view so that others can query it AS IF it was all in one table.

Clive

Last edited by clivew : May 17th, 2009 at 01:23 PM. Reason: Forgot left outer join

Reply With Quote
  #13  
Old May 17th, 2009, 06:18 PM
webdbguy webdbguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 webdbguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 5 sec
Reputation Power: 0
I did remove the Manager table :S

I understand what is being said, I just wondered all along if it were possible to achieve this result with the use of tables only, regardless of the fact that the specified SQL code may be better. However, from what I have gathered it would not be possible.

By the way is that LEFT OUTER JOIN code for Oracle? Isn't (+) used instead?

Reply With Quote
  #14  
Old May 17th, 2009, 10:11 PM
clivew clivew is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 1,124 clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level)clivew User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 6 Days 1 h 39 m 23 sec
Reputation Power: 272
Quote:
Originally Posted by webdbguy
By the way is that LEFT OUTER JOIN code for Oracle? Isn't (+) used instead?


Unless you are using a seriously old version of Oracle, it now supports the standard.

I don't remember off hand when the change came.

Reply With Quote
  #15  
Old May 18th, 2009, 07:50 AM
webdbguy webdbguy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 13 webdbguy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 40 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by clivew
Unless you are using a seriously old version of Oracle, it now supports the standard.

I don't remember off hand when the change came.


Ah, I just read it changed after 8i

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Database Design


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
Stay green...Green IT