如何创建复合键以及如何在其他表中将其作为外键引用

发布于 2024-08-13 08:08:45 字数 505 浏览 6 评论 0原文

我有一个表,

create table tblcountry (
  unqid uniqueidentifier 
  name varchar(100)
  isremoved bit
)

我想在 unqid + isremoved 的基础上创建主键,其中 isremoved 必须为 true

我有另一个表:

create table tblstate (
  unqid uniqueidentifier,
  name varchar(100)
  f_tblcountry uniqueidentifier,
  isremoved bit
)

主要是我希望当我尝试将 isremoved 字段设为 true 或 1 时如果我在引用的表 tabstate 中使用了它,并且它使用其 isremoved 字段的记录不正确,那么它应该给我错误。如果该主键的 tabstate isremoved 为 true,那么它不应该给出任何错误。

I have a table following table

create table tblcountry (
  unqid uniqueidentifier 
  name varchar(100)
  isremoved bit
)

I want to create primary key on basis of unqid + isremoved and in which isremoved must be true

i have got another table:

create table tblstate (
  unqid uniqueidentifier,
  name varchar(100)
  f_tblcountry uniqueidentifier,
  isremoved bit
)

the main thing is that i want that when i try to make isremoved field as true or 1 of tabcountry than it should give me error if i have useid it in referenced table tabstate and the record to which it used its isremoved field is not true. and if the tabstate the isremoved is true for that primary key than its should not give any error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在巴黎塔顶看东京樱花 2024-08-20 08:08:45

我执行了你的解决方案,它运行良好。当我在 tabcountry 中插入数据时,它让我插入,但是当我尝试插入到 tabstate 中时,

select * from tblcountry 
5CF96D52-994B-45E3-9CF9-1BC948280E57    india   0
AC2AB153-7FBA-48BC-911B-74A178C74FB5    pak 0
763D2186-68BF-4334-AAA1-CCE16E14E6B1    us  0

当我按照下面的查询插入到 tabstate 中时,

insert INto tblstate values (NEWID(),'raj','5CF96D52-994B-45E3-9CF9-1BC948280E57',0)

它会给我错误,我收到以下错误:

消息 547,级别 16,状态 0,第 1 行
INSERT 语句与 FOREIGN KEY 约束“fk”冲突。冲突发生在数据库“usecomp”、表“dbo.tblcountry”中。
该声明已终止。

i executed your solution its running fine. when i insert data in tabcountry its let me insert but when i try to insert into tabstate its giveing me error

select * from tblcountry 
5CF96D52-994B-45E3-9CF9-1BC948280E57    india   0
AC2AB153-7FBA-48BC-911B-74A178C74FB5    pak 0
763D2186-68BF-4334-AAA1-CCE16E14E6B1    us  0

when i insert into tabstate as below query

insert INto tblstate values (NEWID(),'raj','5CF96D52-994B-45E3-9CF9-1BC948280E57',0)

i am getting following error:

Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "fk". The conflict occurred in database "usecomp", table "dbo.tblcountry".
The statement has been terminated.

╰◇生如夏花灿烂 2024-08-20 08:08:45

我想基于unqid + isremoved创建主键,其中isremoved必须为true

对于值,主键仅确保它们是唯一的。当主键是复合键(由多个列组成)时,这意味着该键是所涉及的所有列的任意唯一组合。这意味着,如果主键同时是 unqidisremoved - 以下内容将是表中的有效行:

UNQID                                  |   ISREMOVED
---------------------------------------------------------------
6F9619FF-8B86-D011-B42D-00C04FC964FF   |   1
6F9619FF-8B86-D011-B42D-00C04FC964FF   |   0

您的主键不应包含isremoved 列。

...当我尝试将 tblcountry 表中的 isremoved 字段值设置为 true/1 时,如果我在引用表 tblstate 中使用了它,如果其' < code>isremoved 字段不正确。如果该主键的 tabstate isremoved 为 true,那么它不应该给出任何错误。

为此,您需要一个外键引用,将 tblcountry 中的 unqidisremoved 列关联到 unqid tblstate 表中的 isremoved 列。

ALTER TABLE tblcountry 
  ADD CONSTRAINT tstate_fk FOREIGN KEY (unqid, isremoved) references tblstate (unqid, isremoved)

约书亚的想法是正确的,但方向是错误的。
但这意味着,为了将记录插入 tblcountry,您必须tblstate 中已有 unqid 值。具有外键约束的列可以为空 - 值为空不会触发外键约束 - 但您不能指定外键的一部分。要么满足外键,要么不满足。

我不清楚你希望建模什么 - 如果我有更好的想法,我会提供替代方案。

I want to create primary key on basis of unqid + isremoved and in which isremoved must be true

With regards to values, a primary key only ensures that they are unique. When a primary key is a composite - comprised of multiple columns - it means that the key is any unique combination of all the columns involved. That means if the primary key is both unqid and isremoved - the following would be valid rows in your table:

UNQID                                  |   ISREMOVED
---------------------------------------------------------------
6F9619FF-8B86-D011-B42D-00C04FC964FF   |   1
6F9619FF-8B86-D011-B42D-00C04FC964FF   |   0

Your primary key should not include the isremoved column.

...when I try to make the isremoved field value set to true/1 in the tblcountry table, I should get an error if I have used it in referenced table tblstate if its' isremoved field is not true. and if the tabstate the isremoved is true for that primary key than its should not give any error.

To do this, you need a foreign key reference associating the unqid and isremoved columns in the tblcountry to the unqid and isremoved columns in the tblstate table.

ALTER TABLE tblcountry 
  ADD CONSTRAINT tstate_fk FOREIGN KEY (unqid, isremoved) references tblstate (unqid, isremoved)

Joshua had the right idea, the wrong direction.
But this means that in order to insert a record into tblcountry, you must have the unqid value already in tblstate. A column with a foreign key constraint can be nullable - the value being null won't trip the foreign key constraint - but you can not specify a portion of the foreign key. Either you satisfy the foreign key, or you don't.

I'm not clear on what you were hoping to model - if I had a better idea, I would provide alternatives.

锦上情书 2024-08-20 08:08:45
create table tblcountry (
  unqid uniqueidentifier,
  name varchar(100),
  isremoved bit,
  PRImARY KEY (unqid, isremoved)
)


create table tblstate (
    unqid uniqueidentifier,
    name varchar(100),
    f_tblcountry uniqueidentifier,
    isremoved bit,
  )

  CREATE INDEX tblstateref ON tblstate (unqid, isremoved) -- always use index w/ foreign keys

  ALTER TABLE tblstate ADD CONSTRAINT fk FOREIGN KEY (unqid, isremoved) references tblcountry (unqid, isremoved)
create table tblcountry (
  unqid uniqueidentifier,
  name varchar(100),
  isremoved bit,
  PRImARY KEY (unqid, isremoved)
)


create table tblstate (
    unqid uniqueidentifier,
    name varchar(100),
    f_tblcountry uniqueidentifier,
    isremoved bit,
  )

  CREATE INDEX tblstateref ON tblstate (unqid, isremoved) -- always use index w/ foreign keys

  ALTER TABLE tblstate ADD CONSTRAINT fk FOREIGN KEY (unqid, isremoved) references tblcountry (unqid, isremoved)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文