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.