Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 部分列选择之间的外键或链接_Sql Server 2008_Tsql_Indexing_Foreign Key Relationship_Create Table - Fatal编程技术网

Sql server 2008 部分列选择之间的外键或链接

Sql server 2008 部分列选择之间的外键或链接,sql-server-2008,tsql,indexing,foreign-key-relationship,create-table,Sql Server 2008,Tsql,Indexing,Foreign Key Relationship,Create Table,在以下设置中,我有3个表 CREATE TABLE [dbo].[codevariable] ( [id] [int] NULL, [code] [nchar](10) NULL, [variable] [int] NULL ) ON [PRIMARY] CREATE TABLE [dbo].[proxy] ( [id] [int] NULL, [description] [nvarchar](50) NULL, [status] [bit] N

在以下设置中,我有3个表

CREATE TABLE [dbo].[codevariable] (
    [id] [int] NULL,
    [code] [nchar](10) NULL,
    [variable] [int] NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[proxy] (
    [id] [int] NULL,
    [description] [nvarchar](50) NULL,
    [status] [bit] NULL,
    [added] [datetime] NULL
) ON [PRIMARY]


CREATE TABLE [dbo].[wall] (
    [id] [int] NULL,
    [description] [nvarchar](50) NULL
) ON [PRIMARY]
表中的下列值 桌墙

1   This is a basic wall
2   This is a medium wall
3   This is an advanced wall
表代理

1   Small Proxy True    2013-05-08 00:00:00.000
2   Medium Proxy    False   2013-05-08 00:00:00.000
表代码变量

1   Proxy       1         
2   Proxy       2         
3   Wall        1         
4   Wall        2         
5   Wall        3      
Owke现在我面临的问题是,如果我想在proxy中插入一行新代码。然后它将有Id 3,现在我需要确保Id 3也存在于代码代理下的CodeVariable中

如果没有外键,则无法检查代码变量中是否存在代码

我试过用外键,但没有成功。如何在code和variable列上的CodeVariable表与表代理和表墙之间创建链接

我还可以在代码和变量上创建唯一的索引。但是你不能将外键链接到它

我正在使用SQL 2008


感谢一种更改表定义的方法,因此可以强制执行
外键
约束

对表
codevaluate
(重命名为
code
)使用复合主键
(codeid,codetype)
,其中
codetype
只能取两个可能的值,
'p'
'W'

(您使用的
code
可以代替
codetype
,但我更喜欢窄一点的列,因为索引中使用键(主键和外键)。
代码
已转换为计算列):

在另外两个表中,还添加了
codetype
,因此
(codeid,codetype)
可以定义为
主键和
外键:

CREATE TABLE [dbo].[proxy] (
    [proxyid] [int] NOT NULL,
    [codetype] [char](1) NOT NULL DEFAULT 'P',
    [description] [nvarchar](50) NULL,
    [status] [bit] NULL,
    [added] [datetime] NULL,
    CONSTRAINT proxy_PK
        PRIMARY KEY (proxyid, codetype),
    CONSTRAINT code_proxy_FK
        FOREIGN KEY (proxyid, codetype)
        REFERENCES code (codeid, codetype),
    CONSTRAINT codetype_proxy_CK
        CHECK (codetype = 'P')
) ;


CREATE TABLE [dbo].[wall] (
    [wallid] [int] NOT NULL,
    [codetype] [char](1) NOT NULL DEFAULT 'W',
    [description] [nvarchar](50) NULL,
    CONSTRAINT wall_PK
        PRIMARY KEY (wallid, codetype),
    CONSTRAINT code_wall_FK
        FOREIGN KEY (wallid, codetype)
        REFERENCES code (codeid, codetype),
    CONSTRAINT codetype_wall_CK
        CHECK (codetype = 'W')
) ;

是的,您可以将FK链接到复合唯一。但是你必须链接到整个密钥。而且类型必须相同。为什么Wall甚至在问题中?这是否意味着我必须在代理和Wall表中添加额外的列?这意味着howl列将具有相同的值。谢谢,这做得很好:)
CREATE TABLE [dbo].[proxy] (
    [proxyid] [int] NOT NULL,
    [codetype] [char](1) NOT NULL DEFAULT 'P',
    [description] [nvarchar](50) NULL,
    [status] [bit] NULL,
    [added] [datetime] NULL,
    CONSTRAINT proxy_PK
        PRIMARY KEY (proxyid, codetype),
    CONSTRAINT code_proxy_FK
        FOREIGN KEY (proxyid, codetype)
        REFERENCES code (codeid, codetype),
    CONSTRAINT codetype_proxy_CK
        CHECK (codetype = 'P')
) ;


CREATE TABLE [dbo].[wall] (
    [wallid] [int] NOT NULL,
    [codetype] [char](1) NOT NULL DEFAULT 'W',
    [description] [nvarchar](50) NULL,
    CONSTRAINT wall_PK
        PRIMARY KEY (wallid, codetype),
    CONSTRAINT code_wall_FK
        FOREIGN KEY (wallid, codetype)
        REFERENCES code (codeid, codetype),
    CONSTRAINT codetype_wall_CK
        CHECK (codetype = 'W')
) ;