Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> DB2視圖、序列、約束、外鍵練習測試

DB2視圖、序列、約束、外鍵練習測試

編輯:關於android開發

DB2視圖、序列、約束、外鍵練習測試


簡單視圖:

db2 => create view myview1 as select * from t1 where empno<80
DB20000I SQL 命令成功完成。
db2 => select * from myview1


EMPNO NAME SEQNO
----------- ---------- -----------
10 wan qi 1
30 xu xin 3
50 bbbb 6


3 條記錄已選擇。


簡單序列


db2 => create sequence myseq start with 1 increment by 1 cache 5
DB20000I SQL 命令成功完成。


db2 => insert into tank1 values(nextval for myseq,'asdf')
DB20000I SQL 命令成功完成。
db2 => select * from tank1


ID NAME
----------- ----------
2 asdf
3 asdf
4 asdf
5 asdf
6 asdf
7 asdf
8 asdf



簡單約束實驗

create table employee
(
ID integer not null constraint ID_pk primary key,
NAME varchar(9),
DEPT smallint constraint dept_ck1
check (dept between 10 and 100),
JOB char(5) constraint dept_ck2
check (JOB IN('Sales','Mgr','Clerk')),
HIREDATE date,
SALARY decimal(7,2),
constraint yearsal_ck
check (year(HIREDATE) > 1986 OR SALARY > 40000 )


)


[db2inst2@localhost ~]$ db2 "insert into employee values(1,'asdf',11, 'Sales','1788-02-10',40100)"
DB20000I SQL 命令成功完成。
[db2inst2@localhost ~]$ db2 "insert into employee values(1,'asdf',13, 'Sales','1788-02-10',40100)"
DB21034E 該命令被當作 SQL
語句來處理,因為它是無效的“命令行處理器”命令。在 SQL
處理期間,它返回:
SQL0803N INSERT 語句、UPDATE 語句或由 DELETE
語句導致的外鍵更新中的一個或多個值無效,因為由 "1"
標識的主鍵、唯一約束或者唯一索引將表 "DB2INST2.EMPLOYEE"
的索引鍵限制為不能具有重復值。 SQLSTATE=23505
[db2inst2@localhost ~]$ db2 "insert into employee values(2,'asdf',101, 'Sales','1788-02-10',40100)"
DB21034E 該命令被當作 SQL
語句來處理,因為它是無效的“命令行處理器”命令。在 SQL
處理期間,它返回:
SQL0545N 因為行不滿足檢查約束
"DB2INST2.EMPLOYEE.DEPT_CK1",所以不允許所請求的操作。
SQLSTATE=23513
[db2inst2@localhost ~]$ db2 "insert into employee values(2,'asdf',99, 'Sales','1788-02-10',40100)"
DB20000I SQL 命令成功完成。
[db2inst2@localhost ~]$ db2 "insert into employee values(3,'asdf',99, 'Kales','1788-02-10',40100)"
DB21034E 該命令被當作 SQL
語句來處理,因為它是無效的“命令行處理器”命令。在 SQL
處理期間,它返回:
SQL0545N 因為行不滿足檢查約束
"DB2INST2.EMPLOYEE.DEPT_CK2",所以不允許所請求的操作。
SQLSTATE=23513
[db2inst2@localhost ~]$ db2 "insert into employee values(3,'asdf',99, 'Sales','1788-02-10',40100)"
DB20000I SQL 命令成功完成。
[db2inst2@localhost ~]$ db2 "insert into employee values(4,'asdf',98, 'Mgr','1788-02-10',40100)"
DB20000I SQL 命令成功完成。
[db2inst2@localhost ~]$ db2 "insert into employee values(5,'asdf',98, 'Mgr','1987-02-10',40)"
DB20000I SQL 命令成功完成。
[db2inst2@localhost ~]$ db2 "insert into employee values(6,'asdf',98, 'Mgr','1984-02-10',40)"
DB21034E 該命令被當作 SQL
語句來處理,因為它是無效的“命令行處理器”命令。在 SQL
處理期間,它返回:
SQL0545N 因為行不滿足檢查約束
"DB2INST2.EMPLOYEE.YEARSAL_CK",所以不允許所請求的操作。
SQLSTATE=23513
[db2inst2@localhost ~]$ db2 "insert into employee values(6,'asdf',98, 'Mgr','1981-02-10',41000)"
DB20000I SQL 命令成功完成。
[db2inst2@localhost ~]$ db2 "insert into employee values(6,'asdf',98, 'Mgr','1981-02-10',41000)"



簡單主外鍵測試

1、查看約束:

SELECT * FROM SYSCAT.TABCONST

2、新建父表:

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


3、新建子表:

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 //限制刪除
);

4、父表插入數據,,

[db2inst2@localhost ~]$ more inste2.sql
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)
;

5、子表插入數據

[db2inst2@localhost ~]$ more inste3.sql
insert into employee.salesboys values(100,'raju',98998976),
(101,'kiran',98911176),
(102,'radha',943245176),
(103,'wali',89857330),
(104,'rayan',89851130);

6、測試外建約束:


[db2inst2@localhost ~]$ db2 "insert into employee.salesboys values(103,'rayan',89851130)"
DB20000I SQL 命令成功完成。
[db2inst2@localhost ~]$ db2 "insert into employee.salesboys values(109,'rayan',89851130)"
DB21034E 該命令被當作 SQL
語句來處理,因為它是無效的“命令行處理器”命令。在 SQL
處理期間,它返回:
SQL0530N FOREIGN KEY "EMPLOYEE.SALESBOYS.FK_BOY_ID"
的插入或更新值不等於父表的任何父鍵值。 SQLSTATE=23503
[db2inst2@localhost ~]$


7、刪除外建測試

[db2inst2@localhost ~]$ db2 "ALTER TABLE EMPLOYEE.SALESBOYS DROP FOREIGN KEY FK_BOY_ID"
DB20000I SQL 命令成功完成。
[db2inst2@localhost ~]$ db2 "insert into employee.salesboys values(109,'rayan',89851130)"
DB20000I SQL 命令成功完成。
[db2inst2@localhost ~]$

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved