DB2约束

若要强制数据库的完整性,一组规则的定义,也就是所谓约束。约束即允许或禁止在所述列中的值。

在实时数据库活动,该数据应该有一定的限制被添加。例如,在一个销售数据库,销售-ID或事务id应该是唯一的。约束类型是:

  • NOT NULL
  • 唯一
  • 主键
  • 外键
  • 检查
  • 信息化

约束只能与表关联。它们被施加到唯一特定的表。它们被定义和应用于表在创建表时。

每个约束的说明:

NOT NULL

它是一个规则来从表内的一个或多个列禁止空值。

语法:

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

示例:[在此加入“not null”约束所有列,以避免形成表中的任何空单元格创建销售表,四列(id, itemname, qty, price)]

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  

主键

类似唯一约束,可以使用“主键”和“外键”约束声明多个表之间的关系。

语法:

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

示例:[创建“salesboys”表将“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> 
联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2022

备案号:京ICP备15003423号-3