DB2 - ข้อ จำกัด
บทนี้จะอธิบายถึงข้อ จำกัด ต่างๆในฐานข้อมูล
บทนำ
ในการบังคับใช้ความสมบูรณ์ของฐานข้อมูลจะมีการกำหนดชุดของกฎที่เรียกว่าข้อ จำกัด ข้อ จำกัด อนุญาตหรือห้ามไม่ให้มีค่าในคอลัมน์
ในกิจกรรมฐานข้อมูลแบบเรียลไทม์ควรเพิ่มข้อมูลด้วยข้อ จำกัด บางประการ ตัวอย่างเช่นในฐานข้อมูลการขายรหัสการขายหรือรหัสธุรกรรมควรไม่ซ้ำกัน ประเภทข้อ จำกัด คือ:
- ไม่เป็นโมฆะ
- Unique
- คีย์หลัก
- คีย์ต่างประเทศ
- Check
- Informational
ข้อ จำกัด เกี่ยวข้องกับตารางเท่านั้น ใช้กับตารางเฉพาะเท่านั้น มีการกำหนดและนำไปใช้กับตารางในขณะสร้างตาราง
คำอธิบายของแต่ละข้อ จำกัด :
ไม่เป็นโมฆะ
เป็นกฎที่ห้ามไม่ให้มีค่า null จากคอลัมน์อย่างน้อยหนึ่งคอลัมน์ภายในตาราง
Syntax:
db2 create table <table_name>(col_name col_type not null,..)
Example: [ในการสร้างตารางการขายโดยมีสี่คอลัมน์ (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 ลงในตาราง
คุณสามารถแทรกค่าในตารางดังที่แสดงด้านล่าง:
Example: [ข้อผิดพลาดในการค้นหา]
db2 insert into shopper.sales(id,itemname,qty)
values(1,'raagi',12)
Output: [คำค้นหาที่ถูกต้อง]
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
Example: [คำค้นหาที่ถูกต้อง]
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)
Output:
DB20000I The SQL command completed successfully.
ข้อ จำกัด เฉพาะ
เมื่อใช้ข้อ จำกัด เหล่านี้คุณสามารถกำหนดค่าของคอลัมน์ได้โดยไม่ซ้ำกัน ด้วยเหตุนี้ข้อ จำกัด ที่ไม่ซ้ำกันจะถูกประกาศโดยมีข้อ จำกัด "ไม่ใช่ null" ในขณะสร้างตาราง
Syntax:
db2 create table <tab_name>(<col> <col_type> not null unique, ...)
Example:
db2 create table shopper.sales1(id bigint not null unique,
itemname varchar(40) not null, qty int not null,price
double not null)
การแทรกค่าลงในตาราง
Example: ในการแทรกสี่แถวที่แตกต่างกันโดยมีรหัสเฉพาะเป็น 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)
Example: ในการแทรกแถวใหม่ด้วยค่า“ id” 3
db2 insert into shopper.sales1(id, itemname, qty, price)
values(3, 'cheese', 60, 80)
Output: เมื่อคุณพยายามแทรกแถวใหม่ด้วยค่า 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
คีย์หลัก
เช่นเดียวกับข้อ จำกัด เฉพาะคุณสามารถใช้ข้อ จำกัด "คีย์หลัก" และ "คีย์ต่างประเทศ" เพื่อประกาศความสัมพันธ์ระหว่างตารางต่างๆ
Syntax:
db2 create table <tab_name>(
,.., primary key ())
Example: สร้างตาราง 'พนักงานขาย' โดยมี "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" ตอนนี้คุณกำลังสร้างตารางใหม่ที่มีรายละเอียดส่วนตัวของพนักงานขายโดยมีสคีมาที่แตกต่างกันชื่อ "พนักงาน" และตารางชื่อ "พนักงานขาย" ในกรณีนี้ "sid" คือคีย์ต่างประเทศ
Syntax:
db2 create table <tab_name>(<col> <col_type>,constraint
<const_name> foreign key (<col_name>)
reference <ref_table> (<ref_col>)
Example: [ในการสร้างตารางชื่อ 'salesboys' ด้วยคอลัมน์ Foreign Key '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
)
Example: [การแทรกค่าลงในตารางคีย์หลัก“ 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)
Example: [การแทรกค่าลงในตารางคีย์ต่างประเทศ“ staff.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
Example: [การดำเนินการผิดพลาด]
db2 insert into employee.salesboys values(105,'rayan',89851130)
Output:
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
กำลังตรวจสอบข้อ จำกัด
คุณต้องใช้ข้อ จำกัด นี้เพื่อเพิ่มข้อ จำกัด ตามเงื่อนไขสำหรับคอลัมน์เฉพาะในตาราง
Syntax:
db2 create table
(
primary key (
), constraint
check (condition or condition) )
Example: [To create emp1 table with constraints values]
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)
)
Inserting values
You can insert values into a table as shown below:
db2 insert into empl values (1,'lee', 15, 'mgr', '1985-01-01' ,
40000.00, 1000.00)
Dropping the constraint
Let us see the syntaxes for dropping various constraints.
Dropping UNIQUE constraint
Syntax:
db2 alter table <tab_name> drop unique <const_name>
Dropping primary key
Syntax:
db2 alter table <tab_name> drop primary key
Dropping check constraint
Syntax:
db2 alter table <tab_name> drop check <check_const_name>
Dropping foreign key
Syntax:
db2 alter table <tab_name> drop foreigh key <foreign_key_name>