SQL表 - 这种设计是否可接受/良好实践?

时间:2013-07-12 14:06:46

标签: sql join relational-database

我正在开发/设计一个小型系统,以跟踪服务器,在这些服务器上运行的应用程序,以及每个应用程序服务器上存在的环境。我有一个关于良好设计模式/实践的快速问题。

我有3个表,每个实体一个。它们(缩写)如下:

Server
Id : int

Application
Id : int

Environment
Id : int

现在,我的问题是:

创建连接表是否可以接受:

AppServerEnvironments
ServerId : int
ApplicationId : int
EnvironmentId : int

或者,更好的做法是创建两个连接表:

ApplicationServer
ServerId : int
ApplicationId : int

ApplicationEnvironment
ApplicationId : int
EnvironmentId : int

我已尝试使用第二个选项,但似乎无法找到正确连接所有3个表的方法。

非常感谢任何输入/指导。

谢谢!

2 个答案:

答案 0 :(得分:1)

所以你会有这样的事情:

application
-----------
application_id

server
-----------
server_id

application_instance
---------------------
application_id
instance_id
name <- name this one DEV, or PROD etc.
description <-  here describe the use of the DEV system

现在说哪个人居住在哪里......

server_instance
---------------
server_id
instance_id

答案 1 :(得分:0)

这取决于。如果您需要在联接中存在所有三个键,那么您应该使用AppServerEnvironments解决方案。如果Server/Application对不需要EnviromentApplication/Environment不需要Server,那么您最好使用ApplicationServer/ApplicationEnvironment解决方案。这样,您可以在表上定义复合主键,而不必具有NULL值。