69pao国产精品视频-久久精品一区二区二三区-精品国产精品亚洲一本大道-99国产综合一区久久

DB2約束

DB2約束

本章介紹DB2數(shù)據(jù)庫(kù)中的各種制約。

若要強(qiáng)制數(shù)據(jù)庫(kù)的完整性,一組規(guī)則的定義,也就是所謂約束。約束即允許或禁止在所述列中的值。

在實(shí)時(shí)數(shù)據(jù)庫(kù)活動(dòng),該數(shù)據(jù)應(yīng)該有一定的限制被添加。例如,在一個(gè)銷(xiāo)售數(shù)據(jù)庫(kù),銷(xiāo)售-ID或事務(wù)id應(yīng)該是唯一的。約束類(lèi)型是:

  • NOT NULL
  • 唯一
  • 主鍵
  • 外鍵
  • 檢查
  • 信息化
  • 約束只能與表關(guān)聯(lián)。它們被施加到唯一特定的表。它們被定義和應(yīng)用于表在創(chuàng)建表時(shí)。

     

    每個(gè)約束的說(shuō)明:

     

    NOT NULL

    它是一個(gè)規(guī)則來(lái)從表內(nèi)的一個(gè)或多個(gè)列禁止空值。

    語(yǔ)法:

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

    示例:[在此加入“not null”約束所有列,以避免形成表中的任何空單元格創(chuàng)建銷(xiāo)售表,四列(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值插入表

    如下圖所示,可以插入表中的值:

    例如:[錯(cuò)誤查詢(xún)]

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

    輸出:[正確的查詢(xún)]

    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
    

    示例:[正確的查詢(xún)]

    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.
    

     

    唯一約束

    使用這些限制,可以設(shè)置唯一的列值。對(duì)于這一點(diǎn),唯一約束使用“not null”約束在創(chuàng)建表時(shí)聲明。

    語(yǔ)法:

    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)
    

     

    插入值到表

    示例:要插入四個(gè)不同的行的唯一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)
    

    輸出:當(dāng)嘗試插入與現(xiàn)有id值相同的一個(gè)新的行會(huì)顯示這樣的結(jié)果:

    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
    

     

    主鍵

    類(lèi)似唯一約束,可以使用“主鍵”和“外鍵”約束聲明多個(gè)表之間的關(guān)系。

    語(yǔ)法:

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

    示例:[創(chuàng)建“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))
    

     

    外鍵

    外鍵是在需要匹配另一個(gè)表中至少有一個(gè)主一排鍵的表一組列。它是參照約束或參照完整性約束。它是一個(gè)關(guān)于在一個(gè)或多個(gè)表中的多個(gè)列中的值的邏輯規(guī)則。它使得表之間有所需的關(guān)系。

    此前,創(chuàng)建了一個(gè)名為“shopper.salesboys”表。對(duì)于表中,主鍵是“SID”?,F(xiàn)在,要?jiǎng)?chuàng)建一個(gè)新表,已銷(xiāo)售男孩的個(gè)人信息使用名為“employee”和表“salesboys”在不同的模式。在這種情況下,“sid”是外鍵。

    語(yǔ)法:

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

    示例:[創(chuàng)建一個(gè)表名為“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”[無(wú)誤]

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

    如果輸入一個(gè)未知的數(shù)字,這不存儲(chǔ)在“shopper.salesboys”表,它會(huì)顯示SQL錯(cuò)誤。

    例如:[錯(cuò)誤執(zhí)行]

    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
    

     

    檢查約束

    需要使用此約束來(lái)添加條件限制,在一個(gè)表中的特定列。

    語(yǔ)法

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

    示例:[創(chuàng)建表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)
    

     

    刪除約束

    讓我們看看各種刪除約束的語(yǔ)法。

     

    刪除UNIQUE約束

    語(yǔ)法:

    db2 alter table <tab_name> drop unique <const_name>
    

     

    刪除主鍵

    語(yǔ)法:

    db2 alter table <tab_name> drop primary key
    

     

    刪除檢查約束

    語(yǔ)法:

    db2 alter table <tab_name> drop check <check_const_name>
    

     

    刪除外鍵

    語(yǔ)法:

    db2 alter table <tab_name> drop foreigh key <foreign_key_name>
    

    下一節(jié):DB2 索引

    DB2 教程

    相關(guān)文章