1

私は2つのテーブルを持っています:

create table [Customer]
(
    [Id] int primary key identity not null,
    [Type] int not null check([Type >= 0 and [Type] <= 2)
    -- other columns
)
create table [Partial Record]
(
    [Id] int primary key identity not null,
    [Student Id] int references [Customer]([Id])
)

[Partial Record][Student Id] を呼び出したのは、Customer テーブルに継承があるためです。問題は次のとおりです。関連が学生のみに属していることを確認するためのチェックを"[Type] = 1"追加[Partial Record]したいと考えています。

出来ますか?

4

3 に答える 3

1

これを行うには、テーブルにスーパー キーをCustomer追加し、強制的な外部キーを追加します。

create table [Customer]
(
    [Id] int primary key identity not null,
    [Type] int not null check([Type] >= 0 and [Type] <= 2)
    ,constraint UQ_Customer_TypeCheck UNIQUE (ID,Type)
)
create table [Partial Record]
(
    [Id] int primary key identity not null,
    [Student Id] int references [Customer]([Id]),
    Type as 1 persisted,
    constraint FK_Partial_TypeCheck FOREIGN KEY ([Student Id],Type) references Customer (ID,Type)
)

(各生徒が1行しか持たない場合は、おそらく削除Idします-主キーを作成するだけです)[Partial Record][Student Id]

于 2012-10-09T06:38:15.670 に答える
0

SQL Server: halt an INSERT in a trigger のようなものを使用して、トリガーでそれを行うことしか考えられません。おそらく非常に効率的またはパフォーマンスが高いとは言えませんが、必要なことは行う必要があります。

于 2012-10-05T21:40:50.243 に答える