制約の確認をサンプルのテーブルを作成して行う
以下のような関係および、整合性を保つための制約を考える。
上記の制約を 以下のように SQL ファイルにまとめた
-- ************************************ -- 得意先マスタ -- ------------------------------------ -- 制約 各項目の not null -- 得意先コード 数字 2桁(0埋め) -- ************************************ create table customer_master ( customer_code char(2) not null, customer_name varchar(30) not null, constraint pk_customer_master primary key(customer_code), check (customer_code like '[0-9][0-9]') ) -- ************************************ -- 商品マスタ -- ------------------------------------ -- 制約 各項目の not null -- 商品コード 数字 2桁(0埋め) -- ************************************ create table article_master ( article_code char(2) not null, article_name varchar(30) not null, price smallmoney not null, cost smallmoney not null, constraint pk_article_master primary key(article_code), check (article_code like '[0-9][0-9]'), check (price >= cost) ) -- ************************************ -- 受注 -- ------------------------------------ -- 制約 各項目の not null -- 得意先コードの参照制約、 -- および 外部キー制約 -- ************************************ create table receive_order ( order_no int not null, customer_code char(2) not null, order_date char(10) not null, constraint pk_receive_order primary key(order_no), foreign key (customer_code) references customer_master(customer_code) ) -- ************************************ -- 受注明細 -- ------------------------------------ -- 制約 各項目の not null -- 商品コードの参照制約 -- および 外部キー制約 -- ************************************ create table order_item ( order_no int not null, seqno int not null, article_code char(2) not null, qty int not null, price smallmoney not null, constraint pk_order_item primary key(order_no,seqno), foreign key (order_no) references receive_order(order_no), foreign key (article_code) references article_master(article_code) ) テーブルの削除
作成した SQL ファイルを SQL Server から実行 c:\temp フォルダに置くものとする C:\MSSQL7\Binn>isql -S cafeaulait -U sa -P -d lesson -i c:\temp\create_table.sql 1> 2> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12> 13> 14> 15> 16> 17> 18> 19> 20> 21> 22> 2 3> 24> 25> 26> 27> 28> 29> 30> 31> 32> 33> 34> 35> 36> 37> 38> 39> 40> 41> 42> 4 3> 44> 45> 46> 47> 48> 49> 50> 51> 52> 53> 54> 55> 56> 57> 58> 59> 60> 61> 62> 6 3> 64> 65> 66> 67> 68> 69> 70> 71> 72> 73> 74> 75> 76> C:\MSSQL7\Binn>isql -S cafeaulait -U sa -P -d lesson -i c:\temp\insert_data.sql 1> 2> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12> 13> 14> 15> 16> 17> 18> 19> 20> 21> 22> 2 3> 24> 25> 26> 27> 28> 29> 30> 31> 32> 33> 34> 35> 36> 37> 38> 39> 40> 41> 42> 4 3> 44> 45> 46> 47> 48> 49> 50> 51> 52> 53> 54> 55> 56> 57> 58> 59> 60> 61> 62> 6 3> 64> 65> 66> 67> 68> 69> 70> 71> 72> 73> 74> 75> 76> 77> 78> 79> 80> 81> 82> 8 3> 84> 85> 86> (1 row affected) (1 row affected) (1 row affected) (1 row affected) (1 row affected) (1 row affected) (1 row affected) insert_data.sql サンプルデータを作成する
受注伝票
C:\MSSQL7\Binn>isql -S cafeaulait -U sa -P -d lesson 1> delete from article_master where article_code = '01' 2> go Msg 547, Level 16, State 1, Server CAFEAULAIT, Line 1 DELETE ステートメントは COLUMN REFERENCE で、制約 'FK__order_ite__artic__1BC821D D' と矛盾しています。矛盾が発生したのは、データベース 'lesson'、テーブル 'order_ite m', column 'article_code' です。 ステートメントは終了されました。 0 rows affected) 1>
制約が作成してあるため、参照されているマスタデータは削除できない。
1> delete from article_master where article_code = '03' 2> go (1 row affected) 参照されていないレコードは問題なく削除できる
1> delete from receive_order where order_no = 1 2> go Msg 547, Level 16, State 1, Server CAFEAULAIT, Line 1 DELETE ステートメントは COLUMN REFERENCE で、制約 'FK__order_ite__order__1AD3FDA 4' と矛盾しています。矛盾が発生したのは、データベース 'lesson'、テーブル 'order_ite m', column 'order_no' です。 ステートメントは終了されました。 (0 rows affected) 商品マスタと同じく、受注明細から参照されているので削除することはできない
1> insert into article_master( 2> article_code, article_name, price, cost 3> )values( 4> 'AA', 'ポストイット', 50, 30 5>) 6> go Msg 547, Level 16, State 0, Server CAFEAULAIT, Line 1 INSERT ステートメントは COLUMN CHECK で、制約 'CK__article_m__artic__14270015' と矛盾しています。矛盾が発生したのは、データベース 'lesson'、テーブル 'article_m aster', column 'article_code' です。 ステートメントは終了されました。 (0 rows affected) |