1、創建數據庫
eg1. 創建不記錄日志的庫testdb,參考語句如下:
CREATE DATABASE testdb;
eg2. 創建帶緩沖式的記錄日志的數據庫testdb(SQL語句不一定在事務之中,擁有者名字不被用于對象的解析),參考語句如下:
CREATE DATABASE testdb WITH BUFFERED LOG;
eg3. 創建無緩沖式的記錄日志的數據庫testdb(SQL語句不一定在事務之中,擁有者名字不被用于對象的解析),參考語句如下:
CREATE DATABASE testdb WITH LOG;
eg4. 創建ANSI的數據庫(記錄日志時無緩沖,SQL總在事務之中,擁有者名字被用于對象的解析),參考語句如下:
CREATE DATABASE testdb WITH LOG MODE ANSI;
2、創建普通數據表
普通數據表又被稱為持久數據表,它在system catalog里注冊。一個普通數據表可對多個session和connection。創建時可以指定dbspace。
eg1、如下語句創建了一個集團信息表cti_vccinfo:
create table cti_vccinfo(
vccid CHAR(6) not null,
vccname VARCHAR(255),
effective INTEGER default 0 not null,
agentmax INTEGER default 0 not null,
ivrmax INTEGER default 0 not null,
updatekey VARCHAR(30),
primary key (vccid) constraint PK_CTI_VI
);
3、創建臨時數據表
臨時數據表不在system catalog里注冊。一個臨時數據表只對對應的某個session或connection可見,在對應的session或connection結束時被自動清除。如果dbspace存在的話,臨時數據表將建于臨時dbspace中。缺省情況下,是沒有日志的。臨時數據表支持索引。
eg1:如下創建一個customer_temp的表,語句如下:
CREATE TEMP TABLE customer_temp (
num SERIAL NOT NULL,
name CHAR(15),
create_time DATETIME YEAR TO FRACTION(3)
);
eg2:也可以將正式表中customer中的數據通過select......into temp語句將數據導入到臨時表中,如下實例創建了一個正式的表customer,并插入了三條數據,接著通過select....into temp語句將這個正式表中的數據導入到臨時表customer_temp。
首先,創建customer普通數據表,建表語句如下:
CREATE TABLE customer (
num SERIAL NOT NULL,
name CHAR(15),
create_time DATETIME YEAR TO FRACTION(3)
);
接著,在普通數據表customer中插入三條記錄,語句如下:
insert into customer (name, create_time) values('amigo', '2010-11-17 15:41:00');
insert into customer (name, create_time) values('xiexingxing', '2010-11-17 15:42:00');
insert into customer (name, create_time) values('amigoxie', '2010-11-17 15:43:00');
最后,通過select......into temp語句將普通數據表customer中的數據導入到臨時表customer_temp中(注意:需要保證customer_temp表不存在,操作成功后,customer_temp中的字段為select出的字段),參考語句如下所示:
SELECT num, name, create_time FROM customer into TEMP customer_temp;
4、創建主鍵約束
1)主鍵約束定義在一個數據列或一組數據列上;
2)主鍵的值是不允許重復的;
3)主鍵的值不允許為NULL。
在2中的實例,創建了cti_vccinfo表,并指定了vccid為其主鍵,并將其取名為PK_CTI_VI,以方便進行刪除操作。
接下來看一個使用復合主鍵的實例,如下語句創建了cti_humantaskgroup表,該表的serviceid和agentid組成聯合主鍵,首先看下該表的建表語句:
create table cti_humantaskgroup (
serviceid VARCHAR(30) not null,
agentid VARCHAR(30) not null,
priority INTEGER default 0 not null,
updatekey VARCHAR(30)
);
如下的語句為該表的serviceid和agentid創建了唯一索引:
create unique index Index_CTI_HTG on cti_humantaskgroup(
serviceid ASC,
agentid ASC
);
5、創建引用約束
1)一個數據表的主鍵可以被同一個數據表或其它數據庫表使用。主鍵被引用的數據表被稱為父表,引用了附表的主鍵的數據表被稱為子表;
2)如果在定義引用約束時使用了ON DELETE CASCADE,當把父表的數據行刪除時,子表的相關數據行也會被自動刪除。
在4中的實例中,cti_humantaskgroup表中的serviceid為cti_humantask中的主鍵,引用約束可在創建表的時候指明,也可以創建完成后通過alter語句創建,參考語句如下:
alter table cti_humantaskgroup
add constraint foreign key (serviceid)
references cti_humantask (serviceid) on delete cascade
constraint FK_CTI_HTG_HT;
讀者可以注意點,如上語句加上了on delete cascade,表示在刪除cti_humantask表時,數據庫系統會自動刪除子表cti_humantaskgroup中serviceid與之相同的數據。
6、檢查約束
定義了檢查約束后,數據庫將數據賦給一個數據列之前將根據檢查約束檢查數據是否滿足條件。
例如創建一個student表,該表有id(學號)、name(姓名)、age(年齡)和birthday(出生日期)4個字段,age必須在5到35之間,則在創建該表時需要添加檢查約束,建表語句參考如下:
create table student (
id VARCHAR(10) not null,
name VARCHAR(10) not null,
age INTEGER default 0 not null check (age between 5 and 35),
birthday VARCHAR(8)
);
若通過如下語句插入一條不滿足age的檢查約束的數據:
insert into student values('1234', 'amigo', 40, '19821121');
運行后會出現如下提示信息:
530: Check constraint (ines.c2209_13601) failed.
7、創建視圖
1)創建視圖時使用select語句;
2)視圖在system catalog里注冊;
3)視圖數據不被存儲在磁盤上;
4)對于一些數據表,可為不同的用戶建立不同的視圖;
5)可配置存取權限。
例如,創建一個student_age的視圖,查出age在20~25的學生,語句如下:
CREATE VIEW student_age
(id, name, age, birthday)
AS SELECT id, name, age, birthday FROM student WHERE age>=20 and age<=25
若要查詢視圖中的數據,例如得到student_age視圖中的數據,可通過select語句進行查詢,參考如下:
select * from student_age;
若要刪除student_age視圖,語句如下:
drop view student_age;
8、查詢語句
我們使用select語句從數據庫中查詢數據,select語句的使用語法如下所示:
SELECT 字段列表(各個字段之間用英文逗號隔開)
FROM 表列表(多個表之間用英文逗號隔開)
[WHERE 查詢條件]
[GROUP BY 字段列表]
[HAVING 條件]
[ORDER BY 字段列表]
[INTO TEMP 臨時表的名稱]
例如查詢student表中的所有數據,語句參考如下:
select * from student;
查詢student表中的記錄,語句參考如下:
select count(*) from student;
查詢student表中的name和age字段,語句參考如下:
select name, age from student;
在查詢語句中,可以使用關系運算符,可使用的關系運算符如下:
1)=
例如查詢出student表中姓名為amigo的字段,語句參考如下:
select * from student where name='amigo';
2)!=或<>
例如查詢出年齡不為23的記錄,語句參考如下:
select * from student where age!=23;
3)>
例如查詢出年齡大于23的記錄,語句參考如下:
select * from student where age>23;
4)>=
大于等于,與大于使用方法類似。
5)<
小于,與大于使用方法類似。
6)<=
小于等于,與大于使用方法類似。
在where語句中,可使用的關鍵字如下所示:
1)AND(邏輯與)
例如,當需要在student表中查出name為amigo,并且學號為1000的記錄,此時可以使用AND,參考語句如下:
select * from student where name='amigo' and id='1000';
2)OR(邏輯或)
例如,需要查詢name為amigo,或者name為xingxing的記錄,因為是或的關系,所以可以使用OR,參考語句如下:
select * from student where name='amigo' or name='xingxing';
3)[NOT] BWTWEEN([不]在......之間)
例如,查找student表中age在24和30之間的記錄的id、name和age字段,參考語句如下:
select id, name, age from student where age between 24 and 30;
4)[NOT] IN([不]在....中)
[NOT] IN后可以是一個具體的值,也可以是一個子查詢。
例如,查找student表中的name不在“amigo”和“xingxing”的記錄的id和name字段,參考語句如下:
select id, name from student where name not in ('amigo', 'xingxing');
5)IS [NOT] NULL:[不]是NULL
例如需要查詢出student表中birthday不為空的記錄,參考語句如下:
select * from student where birthday is not null;
6)[NOT] MATCHES:[不]匹配
“?”表示匹配單個字符,“*”表示0到正整數個字符。
例如,查找總共為5個字符,而且后4個字符為migo的記錄,參考語句如下:
select id, name from student where name matches '?migo';
例如,查找student表中以go結尾的任意長度的記錄,參考語句如下:
select * from student where name matches '*go';
7)[NOT] LIKE:[不]匹配
使用方法與[NOT] MATCHES類似,但是是使用“_”表示單個字符,“%”表示0到正整數個字符。
GROUP BY
我們可以使用group by對查詢結果進行分組。分組后我們可以得到各個分組的統計消息,例如平均值、總和、數據行數等。
例如,需要根據detail(詳細情況分類)分組查詢CTI_CallStat表中taskid為000001200002111864的記錄,并將每種detail的數量顯示出來,語句參考如下:
select detail, count(*) as ratio from CTI_CallStat where taskid='000001200002111864' group by detail
CASE子句
我們可以使用CASE表達式對返回值進行轉換,CASE表達式的語法如下:
CASE (expr)
WHEN expr1 THEN result1
WHEN expr2 THEN result2


ELSE result_else
END
上面的CASE表達式的意思是:
當expr為expr1時,返回result1;
當expr為expr2時,返回result2;
...
當expr為其它情況時,返回result_else.
例如查詢student表,當age為1時,顯示為too little,100時,顯示為too old,其余的情況顯示為normal,參考語句如下:
select id, name, age,
case age
when 1 then 'too little'
when 100 then 'too old'
else 'normal'
end
ageinfo
from student;
DECODE
我們可以使用DECODE函數對返回值進行轉換,DECODE函數的語法如下:
DECODE (expr,
expr1, result1,
expr2, result2,
…
result_else)
上面的DECODE函數的意思搜:
當expr為expr1時,返回result1;
當expr為expr2時,返回result2;
...
當expr為其它情況時,返回result_else。
該函數能達到CASE子句類似的功能,例如達到前面的功能,可使用如下的DECODE語句:
SELECT id, name, age,
DECODE (age,
1, 'too little',
100, 'too old',
'normal')
ageinfo
FROM student;
UNION和UNION ALL
如果兩個或多個select語句的結果相似,我們可以用“union”或“union all”把這些select語句合并起來。“union”和“union all”的區別是:“union”將去掉結果中的非第一次出現的值,而“union all”將保留結果中的非第一次出現的值。
表連接的語法
我們可以使用兩種方式進行數據表連接:
1)數據表之間使用逗號,連接條件前使用WHERE;
2)數據表之間使用JOIN,連接條件前使用ON。
第一種方式的參考實例如下:
SELECT order_num, order_time, c.customer_num
FROM customer c , orders o
WHERE c.customer_num = o.customer_num;
第二種方式的參考實例如下:
SELECT order_num, order_time, c.customer_num
FROM customer c JOIN orders o
ON c.customer_num = o.customer_num;
外連接
例如,有兩個表,員工表和項目表,員工可以負責項目,項目也可以有負責人(員工)。
若想知道:那個員工負責哪個項目,哪些員工不負責項目,可以使用左外連接,參考語句如下:
select e.employee_num, employee_name, project_num, project_name
from employee e LEFT OUTER JOIN project p ON e.employee_num=p.employee_num
若想知道:哪個員工負責哪個項目,哪些項目沒有人負責,可以使用右外連接,參考語句如下:
select e.employee_num, employee_name, project_num, project_name
from employee e RIGHT OUTER JOIN project p ON e.employee_num=p.employee_num
若想知道:哪個員工負責哪個項目,哪些員工不負責項目,哪些項目沒有人負責,可以使用全連接,參考語句如下:
select e.employee_num, employee_name, project_num, project_name
from employee e FULL OUTER JOIN project p ON e.employee_num=p.employee_num
子查詢
子查詢分兩種:相關子查詢和不相關子查詢。在相關子查詢中,子查詢中涉及到父查詢的數據列;在不相關子查詢中,子查詢中不涉及到父查詢的數據列。
相關子查詢實例:
select * from customer
where exists
(select * from vip
where vip.customer_num = customer.customer_num);
不相關子查詢實例:
select * from project
where employee_num=
(select employee_num from employee where employee_name='amigo');
在很多情況下,我們可以將相關字查詢轉換為表連接,這樣數據庫引擎有可能更方便的得到更優的查詢計劃,從而使SQL語句的執行時間更少。例如可將上面的相關子查詢實例轉換為:
select customer.* FROM customer, vip
where customer.customer_num=vip.customer_num;
9、插入語句
我們可以使用insert語句往數據表中插入數據行。
如果各值按序賦給數據表中的所有數據列,則不需要指明數據列,例如往student表中插入數據,參考語句如下:
insert into student values('1000', 'amigo', 27, '19821121');
如果是將值賦給指定數據列,則需指定數據列,例如只插入student表中的前三個字段,若使用如下的語句:
insert into student values('1005', 'amigo', 27);
此時會報錯提示值的個數不正確,錯誤提示如下所示:
236: Number of columns in INSERT does not match number of VALUES.
這時,需要指定數據列,參考語句如下:
insert into student (id, name, age) values('1005', 'amigo', 27);
可以在insert語句中嵌入select語句,從而將從一個或多個數據表查詢來的數據插入到目標數據表。
例如將student_bak表中age大于25的數據插入到student表中,參考語句如下:
insert into student
select id, name, age, birthday
from student_bak where age>25;
10、更新語句
可以使用update語句為數據表更新數據行。
例如想將student中的id為1000的記錄的name字段更新為amigo,age字段更新為28,語句參考如下:
update student set name='amigoxie', age=28 where id='1000';
使用如下的寫法也是等效的:
update student set (name, age)=('amigoxie', 28) where id='1000';
11、刪除語句
可以使用delete語句從數據表中刪除數據行。
例如,刪除student表中的所有數據,參考語句如下:
delete from student;
例如,刪除student表中id為1001的數據,參考語句如下:
delete from student where id='1001';
例如,刪除student表中以go結尾的記錄,參考語句如下:
delete from student where name matches '*go';
posted on 2010-11-05 16:41
阿蜜果 閱讀(15755)
評論(1) 編輯 收藏 所屬分類:
database