DB2 - 約束



本章描述資料庫中的各種約束。

簡介

為了強制執行資料庫完整性,定義了一套規則,稱為約束。約束允許或禁止列中的值。

在即時資料庫活動中,資料應在某些限制下新增。例如,在銷售資料庫中,銷售 ID 或交易 ID 應唯一。約束型別有:

  • NOT NULL (非空)
  • UNIQUE (唯一)
  • PRIMARY KEY (主鍵)
  • FOREIGN KEY (外部索引鍵)
  • CHECK (檢查)
  • Informational (資訊性)

約束僅與表關聯。它們僅應用於特定表。它們在建立表時定義並應用於表。

每個約束的解釋

NOT NULL (非空)

這是一條規則,用於禁止表中的一列或多列出現空值。

語法

db2 create table <table_name>(col_name col_type not null,..)  

示例:[建立一個包含四個列(id、itemname、qty、price)的銷售表,並向所有列新增“not null”約束,以避免在表中形成任何空單元格。]

db2 create table shopper.sales(id bigint not null, itemname 
varchar(40) not null, qty int not null,price double not null)   

將 NOT NULL 值插入表中

您可以按如下所示向表中插入值

示例:[錯誤查詢]

db2 insert into shopper.sales(id,itemname,qty) 
values(1,'raagi',12) 

輸出:[正確的查詢]

DB21034E  The command was processed as an SQL statement because 
it was not a 

valid Command Line Processor command.  During SQL processing 
it returned: 

SQL0407N  Assignment of a NULL value to a NOT NULL column 
"TBSPACEID=5, 

TABLEID=4, COLNO=3" is not allowed.  SQLSTATE=23502 
 

示例:[正確的查詢]

db2 insert into shopper.sales(id,itemname,qty,price) 
values(1,'raagi',12, 120.00)  

db2 insert into shopper.sales(id,itemname,qty,price) 
values(1,'raagi',12, 120.00) 

輸出

DB20000I The SQL command completed successfully.

唯一約束

使用這些約束,您可以唯一地設定列的值。為此,在建立表時,唯一約束與“not null”約束一起宣告。

語法

db2 create table <tab_name>(<col> <col_type> not null unique, ...) 

示例

db2 create table shopper.sales1(id bigint not null unique, 
itemname varchar(40) not null, qty int not null,price 
double not null)  

將值插入表中

示例:插入四行不同的行,其唯一 ID 為 1、2、3 和 4。

db2 insert into shopper.sales1(id, itemname, qty, price) 
values(1, 'sweet', 100, 89)  

db2 insert into shopper.sales1(id, itemname, qty, price) 
values(2, 'choco', 50, 60)  

db2 insert into shopper.sales1(id, itemname, qty, price) 
values(3, 'butter', 30, 40)  

db2 insert into shopper.sales1(id, itemname, qty, price) 
values(4, 'milk', 1000, 12)  

示例:插入一個新的行,其“id”值為 3

db2 insert into shopper.sales1(id, itemname, qty, price) 
values(3, 'cheese', 60, 80)   

輸出:當您嘗試插入一個具有現有 id 值的新行時,將顯示此結果

DB21034E  The command was processed as an SQL statement 
because it was not a 

valid Command Line Processor command.  During 
SQL processing it returned: 

SQL0803N  One or more values in the INSERT statement, 
UPDATE statement, or foreign key update caused by a
DELETE statement are not valid because the primary key, 
unique constraint or unique index identified by "1" constrains 
table "SHOPPER.SALES1" from having duplicate values for the 
index key. SQLSTATE=23505    

PRIMARY KEY (主鍵)

類似於唯一約束,您可以使用“主鍵”和“外部索引鍵”約束來宣告多個表之間的關係。

語法

db2 create table <tab_name>( ,.., primary
key ()) 

示例:建立一個名為“salesboys”的表,其中“sid”作為主鍵

db2 create table shopper.salesboys(sid int not null, name 
varchar(40) not null, salary double not null, constraint 
pk_boy_id primary key (sid))

外部索引鍵

外部索引鍵是表中的一組列,這些列需要與另一個表中行的至少一個主鍵匹配。它是一個參照約束或參照完整性約束。它是一個關於一個或多個表中多列值的邏輯規則。它能夠在表之間建立所需的關係。

前面,您建立了一個名為“shopper.salesboys”的表。對於此表,“sid”是主鍵。現在,您正在建立一個新的表,該表包含具有不同模式(名為“employee”)和表(名為“salesboys”)的銷售人員的個人詳細資訊。在這種情況下,“sid”是外部索引鍵。

語法

db2 create table <tab_name>(<col> <col_type>,constraint 
<const_name> foreign key (<col_name>)  
                  reference <ref_table> (<ref_col>)  

示例:[建立一個名為“salesboys”的表,其中包含外部索引鍵列“sid”]

db2 create table employee.salesboys( 
            sid int,  
            name varchar(30) not null,  
            phone int not null,  
            constraint fk_boy_id  
            foreign key (sid)  
            references shopper.salesboys (sid) 
			 on delete restrict 
                       ) 

示例:[將值插入主鍵表“shopper.salesboys”]

db2 insert into shopper.salesboys values(100,'raju',20000.00), 
(101,'kiran',15000.00), 
(102,'radha',10000.00), 
(103,'wali',20000.00), 
(104,'rayan',15000.00)

示例:[將值插入外部索引鍵表“employee.salesboys”(無錯誤)]

db2 insert into employee.salesboys values(100,'raju',98998976), 
(101,'kiran',98911176), 
(102,'radha',943245176), 
(103,'wali',89857330),  
(104,'rayan',89851130) 

如果您輸入了一個未知的數字,該數字未儲存在“shopper.salesboys”表中,它將顯示 SQL 錯誤。

示例:[錯誤執行]

db2 insert into employee.salesboys values(105,'rayan',89851130) 

輸出

DB21034E  The command was processed as an SQL statement because it 
was not a valid Command Line Processor command.  During SQL 
processing it returned: SQL0530N  The insert or update value of 
the FOREIGN KEY "EMPLOYEE.SALESBOYS.FK_BOY_ID" is not equal to any 
value of the parent key of the parent table.  SQLSTATE=23503  

檢查約束

您需要使用此約束來為表中的特定列新增條件限制。

語法

db2 create table                                                      
 (  
  primary key (),                                                       
  constraint  check (condition or condition)  
 )
 

示例:[建立一個帶有約束值的 emp1 表]

db2 create table empl                                                     
 (id           smallint not null,                                         
  name         varchar(9),                                                
  dept         smallint check (dept between 10 and 100), 
  job          char(5)  check (job in ('sales', 'mgr', 'clerk')), 
  hiredate     date,                                                      
  salary       decimal(7,2),                                              
  comm         decimal(7,2),                                              
  primary key (id),                                                       
  constraint yearsal check (year(hiredate) > 1986 or salary > 40500)  
 )
 

插入值

您可以按如下所示向表中插入值

db2 insert into empl values (1,'lee', 15, 'mgr', '1985-01-01' , 
40000.00, 1000.00) 

刪除約束

讓我們看看刪除各種約束的語法。

刪除 UNIQUE 約束

語法

db2 alter table <tab_name> drop unique <const_name>

刪除主鍵

語法

db2 alter table <tab_name> drop primary key 

刪除檢查約束

語法

db2 alter table <tab_name> drop check <check_const_name>  

刪除外部索引鍵

語法

db2 alter table <tab_name> drop foreigh key <foreign_key_name>  
廣告
© . All rights reserved.