SQL Server - 公司信息

时间:2010-12-26 09:39:08

标签: sql sql-server

我正在创建公司目录,需要一些SQL Server数据库的帮助。

我所拥有的表格为EmployeesDepartmentsEquipmentPhonenumber

EmployeesDepartmentEquipment可以有电话号码(许多电话号码)

就像这样,Employees可以有电话录音和设备

Departments可以拥有多个电话号码和许多设备

Equipment可以有多个电话号码

希望得到一些帮助

1 个答案:

答案 0 :(得分:1)

我认为您没有指出实际想到的所需相关性。我并不质疑它背后的意义。但是为了强调我从你的解释中得到的结论,我给出了一个简短的总结:

  • 员工可能有一个电话号码和一个设备。
  • 部门可能有很多电话号码和许多设备。
  • 设备可能有很多电话号码。

在这里,您将使用相应的表定义(摘录):

CREATE TABLE Employee (
  id INT PRIMARY KEY,
  equipment_id INT NOT NULL,
  phone_number_id INT NOT NULL,
  ...
  FOREIGN KEY (employee_equipment_id) REFERENCES Equipment (id)
  FOREIGN KEY (employee_phone_number_id) REFERENCES PhoneNumber (id)
)

CREATE TABLE Department (
  id INT PRIMARY KEY,
  ...
)

CREATE TABLE Equipment (
  id INT PRIMARY KEY,
  ...
)

CREATE TABLE PhoneNumber (
  id INT PRIMARY KEY,
  ...
)

CREATE TABLE DepartmentEquipment (
  department_id INT NOT NULL,
  equipment_id INT NOT NULL,
  FOREIGN KEY (department_equipment_department_id) REFERENCES Department (id)
  FOREIGN KEY (department_equipment_equipment_id) REFERENCES Equipment (id)
)

CREATE TABLE DepartmentPhoneNumber (
  department_id INT NOT NULL,
  phone_number_id INT NOT NULL,
  FOREIGN KEY (department_phone_number_department_id) REFERENCES Department (id)
  FOREIGN KEY (department_phone_number_phone_number_id) REFERENCES PhoneNumber (id)
)

CREATE TABLE EquipmentPhoneNumber (
  equipment_id INT NOT NULL,
  phone_number_id INT NOT NULL,
  FOREIGN KEY (equipment_phone_number_equipment_id) REFERENCES Equipment (id)
  FOREIGN KEY (equipment_phone_number_phone_number_id) REFERENCES PhoneNumber (id)
)