一个数据库设计的问题,一对多关系,如何设计表?

时间:2022-12-11 18:40:29
在企业中有一级部门和子部门

比如有企划中心 和研发中心,都属于一级部门
企划中心包括设计部,企划部和编辑部,这三个属于二级部
研发中心包括delphi研发部和java研发部,这两个属于二级部门

在设计部门信息表的时候,怎么处理呢?
我目前是这样设计的
一共两个表
一个表为一级部门信息表
department1:
  一级部门编号
  一级部门名

department2:
  一级部门编号
  二级部门编号
  二级部门名

因为有的部门没有子二级部门.

各位觉得我上面的设计是不是有问题?
一个一级部门可以下辖多个二级部门,也可以没有二级部门
一个二级部门只对应一个一级部门

在企业实际情况中,应该不会出现在不同的一级部门下有两个相同的二级部门名吧?

怎么优化下我上面的数据库设计?或者更好的建议???
谢谢..

11 个解决方案

#1


一个表应该就可以了。

#2


举例如下:
create table dept (
code                 int                  not null,
dep_code             int                  null,
name                 char(50)             not null,
constraint PK_DEPT primary key  (code)
)
go

alter table dept
   add constraint FK_DEPT_RELATIONS_DEPT foreign key (dep_code)
      references dept (code)
go


#3


這種問題用父子表關係來完成

一個是部門碼表

一個是職位碼表,這個表要包含有這個職位所在的部門

#4


一個是部門碼表(dept_no,dept_name),主鍵為dept_no

一個是職位碼表(position_no,position_name,dept_no),主鍵為position_no

這兩個表的結構對整個公司的構架來說非常重要

#5


同意一樓的

#6


在企业实际情况中,应该不会出现在不同的一级部门下有两个相同的二级部门名吧?

會出現兩個相同的二級部門的,但是只是名稱相同而已,編號是不一樣的!

#7


create table #部门
(
dep_no numeric primary key,
dep_name varchar(20),
father_dep_no numeric references #部门(dep_no)
)

insert into #部门 values(1,'企划中心',null)
insert into #部门 values(2,'研发中心',null)
insert into #部门 values(3,'设计部',1)
insert into #部门 values(4,'企划部',1)
insert into #部门 values(5,'编辑部',1)
insert into #部门 values(6,'delphi研发部',2)
insert into #部门 values(7,'java研发部',2)

#8


不是很明白 sdhylj(青锋-SS)的意思。。

用关系模式写出来看看好吗??

#9


因为我这里没有涉及到员工职位。。

只涉及到部门

#10


create table dept (
code int not null,
dep_code int null,
name char(50) not null,
constraint PK_DEPT primary key (code)
)
go

alter table dept
add constraint FK_DEPT_RELATIONS_DEPT foreign key (dep_code)
references dept (code)
go
------------------
code是部门表的主键,dept_code是个特殊的外键,是对自身主键(code)的引用,也就是说dept和它自身有一对多的关系.

#11


一个表就可以(上级部门编号,本部门编号,名称)

#1


一个表应该就可以了。

#2


举例如下:
create table dept (
code                 int                  not null,
dep_code             int                  null,
name                 char(50)             not null,
constraint PK_DEPT primary key  (code)
)
go

alter table dept
   add constraint FK_DEPT_RELATIONS_DEPT foreign key (dep_code)
      references dept (code)
go


#3


這種問題用父子表關係來完成

一個是部門碼表

一個是職位碼表,這個表要包含有這個職位所在的部門

#4


一個是部門碼表(dept_no,dept_name),主鍵為dept_no

一個是職位碼表(position_no,position_name,dept_no),主鍵為position_no

這兩個表的結構對整個公司的構架來說非常重要

#5


同意一樓的

#6


在企业实际情况中,应该不会出现在不同的一级部门下有两个相同的二级部门名吧?

會出現兩個相同的二級部門的,但是只是名稱相同而已,編號是不一樣的!

#7


create table #部门
(
dep_no numeric primary key,
dep_name varchar(20),
father_dep_no numeric references #部门(dep_no)
)

insert into #部门 values(1,'企划中心',null)
insert into #部门 values(2,'研发中心',null)
insert into #部门 values(3,'设计部',1)
insert into #部门 values(4,'企划部',1)
insert into #部门 values(5,'编辑部',1)
insert into #部门 values(6,'delphi研发部',2)
insert into #部门 values(7,'java研发部',2)

#8


不是很明白 sdhylj(青锋-SS)的意思。。

用关系模式写出来看看好吗??

#9


因为我这里没有涉及到员工职位。。

只涉及到部门

#10


create table dept (
code int not null,
dep_code int null,
name char(50) not null,
constraint PK_DEPT primary key (code)
)
go

alter table dept
add constraint FK_DEPT_RELATIONS_DEPT foreign key (dep_code)
references dept (code)
go
------------------
code是部门表的主键,dept_code是个特殊的外键,是对自身主键(code)的引用,也就是说dept和它自身有一对多的关系.

#11


一个表就可以(上级部门编号,本部门编号,名称)