7

ACIPin の主キー「pkey」である、CDRLive に新しい列「IPkey」を追加したいと考えています。pkey は null ではなく bigint です。

ALTER TABLE CDRLive ADD IPkey bigint NOT NULL
go
ALTER TABLE CDRLive
ADD CONSTRAINT CDRLive_IPkey FOREIGN KEY(IPkey) REFERENCES ACIPin(pkey) 

失敗した。エラー:

Msg 4901, Level 16, State 1, Line 2
ALTER TABLE only allows columns to be added that can contain nulls, or have a DEFAULT     definition specified, or the column being added is an identity or timestamp column, or alternatively if none of the previous conditions are satisfied the table must be empty to allow addition of this column. Column 'IPkey' cannot be added to non-empty table 'CDRLive' because it does not satisfy these conditions.
 Msg 1769, Level 16, State 1, Line 1

 Foreign key 'CDRLive_IPkey' references invalid column 'IPkey' in referencing table 'CDRLive'.
 Msg 1750, Level 16, State 0, Line 1
 Could not create constraint. See previous errors.
4

1 に答える 1

18

列にNOT NULL制約を持たせたい場合は、次の順序で行う必要があります。

  1. ALTER TABLE CDRLive ADD IPkey bigint NULL go
  2. 必要なデータを列に入力します。
  3. ALTER TABLE CDRLive ADD IPkey bigint NOT NULL go
  4. ALTER TABLE CDRLive ADD CONSTRAINT CDRLive_IPkey FOREIGN KEY(IPkey) REFERENCES ACIPin(pkey)
于 2013-03-08T16:25:16.320 に答える