views:

49

answers:

2

good day,

database programming and logical problem.

for instance:

i got 3 tables. table 'Companies', table 'Branches' and table 'departments'

from companies table (Primary), linked by company code to enter the branch table, the branch has many department linked by branch code to department table.

the problem comes when 1 branch in a company, (apart from it's departments)is also a company on it's own, with smaller branches,

meaning i have to copy the branch dets also to company dets in another row of data not relating to the company it is in.

how do i copy the branch name, number,... to the company table.

by theory it works, put it up for testing, i got nowhere,

i need that programming minds, thanks

+2  A: 

it looks like you are in need of a hierarchical structure ParentCompany->ChildCompany in a single table. so something like:

CREATE TABLE Company
(
    Id inr primary key not null,
    ParentID int,
    Name varchar(100),
    Other Needed Columns ...
)
Mladen Prajdic
+1  A: 

I think your question is really about how to implement recursive tree structures using SQL. There are quite a lot of SO questions on this topic, for example Database Structure for Tree Data Structure, and to see more you can use a Google query of the form:

site:stackoverflow.com sql tree structure
anon