|
eg1. 創(chuàng)建不記錄日志的庫(kù)testdb,參考語(yǔ)句如下: CREATE DATABASE testdb; CREATE DATABASE testdb WITH BUFFERED LOG; CREATE DATABASE testdb WITH LOG; CREATE DATABASE testdb WITH LOG MODE ANSI;普通數(shù)據(jù)表又被稱為持久數(shù)據(jù)表,它在system catalog里注冊(cè)。一個(gè)普通數(shù)據(jù)表可對(duì)多個(gè)session和connection。創(chuàng)建時(shí)可以指定dbspace。 eg1、如下語(yǔ)句創(chuàng)建了一個(gè)集團(tuán)信息表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 );臨時(shí)數(shù)據(jù)表不在system catalog里注冊(cè)。一個(gè)臨時(shí)數(shù)據(jù)表只對(duì)對(duì)應(yīng)的某個(gè)session或connection可見,在對(duì)應(yīng)的session或connection結(jié)束時(shí)被自動(dòng)清除。如果dbspace存在的話,臨時(shí)數(shù)據(jù)表將建于臨時(shí)dbspace中。缺省情況下,是沒有日志的。臨時(shí)數(shù)據(jù)表支持索引。 eg1:如下創(chuàng)建一個(gè)customer_temp的表,語(yǔ)句如下: CREATE TEMP TABLE customer_temp ( num SERIAL NOT NULL, name CHAR(15), create_time DATETIME YEAR TO FRACTION(3) );首先,創(chuàng)建customer普通數(shù)據(jù)表,建表語(yǔ)句如下: CREATE TABLE customer ( num SERIAL NOT NULL, name CHAR(15), create_time DATETIME YEAR TO FRACTION(3) ); 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 num, name, create_time FROM customer into TEMP customer_temp;1)主鍵約束定義在一個(gè)數(shù)據(jù)列或一組數(shù)據(jù)列上; 2)主鍵的值是不允許重復(fù)的; 3)主鍵的值不允許為NULL。 在2中的實(shí)例,創(chuàng)建了cti_vccinfo表,并指定了vccid為其主鍵,并將其取名為PK_CTI_VI,以方便進(jìn)行刪除操作。 接下來(lái)看一個(gè)使用復(fù)合主鍵的實(shí)例,如下語(yǔ)句創(chuàng)建了cti_humantaskgroup表,該表的serviceid和agentid組成聯(lián)合主鍵,首先看下該表的建表語(yǔ)句: create table cti_humantaskgroup ( serviceid VARCHAR(30) not null, agentid VARCHAR(30) not null, priority INTEGER default 0 not null, updatekey VARCHAR(30) ); create unique index Index_CTI_HTG on cti_humantaskgroup( serviceid ASC, agentid ASC );1)一個(gè)數(shù)據(jù)表的主鍵可以被同一個(gè)數(shù)據(jù)表或其它數(shù)據(jù)庫(kù)表使用。主鍵被引用的數(shù)據(jù)表被稱為父表,引用了附表的主鍵的數(shù)據(jù)表被稱為子表; 2)如果在定義引用約束時(shí)使用了ON DELETE CASCADE,當(dāng)把父表的數(shù)據(jù)行刪除時(shí),子表的相關(guān)數(shù)據(jù)行也會(huì)被自動(dòng)刪除。 在4中的實(shí)例中,cti_humantaskgroup表中的serviceid為cti_humantask中的主鍵,引用約束可在創(chuàng)建表的時(shí)候指明,也可以創(chuàng)建完成后通過(guò)alter語(yǔ)句創(chuàng)建,參考語(yǔ)句如下: alter table cti_humantaskgroup add constraint foreign key (serviceid) references cti_humantask (serviceid) on delete cascade constraint FK_CTI_HTG_HT;6、檢查約束 定義了檢查約束后,數(shù)據(jù)庫(kù)將數(shù)據(jù)賦給一個(gè)數(shù)據(jù)列之前將根據(jù)檢查約束檢查數(shù)據(jù)是否滿足條件。 例如創(chuàng)建一個(gè)student表,該表有id(學(xué)號(hào))、name(姓名)、age(年齡)和birthday(出生日期)4個(gè)字段,age必須在5到35之間,則在創(chuàng)建該表時(shí)需要添加檢查約束,建表語(yǔ)句參考如下: 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) ); insert into student values('1234', 'amigo', 40, '19821121'); 530: Check constraint (ines.c2209_13601) failed. 1)創(chuàng)建視圖時(shí)使用select語(yǔ)句; 2)視圖在system catalog里注冊(cè); 3)視圖數(shù)據(jù)不被存儲(chǔ)在磁盤上; 4)對(duì)于一些數(shù)據(jù)表,可為不同的用戶建立不同的視圖; 5)可配置存取權(quán)限。 例如,創(chuàng)建一個(gè)student_age的視圖,查出age在20~25的學(xué)生,語(yǔ)句如下: CREATE VIEW student_age (id, name, age, birthday) AS SELECT id, name, age, birthday FROM student WHERE age>=20 and age<=25 select * from student_age; drop view student_age;我們使用select語(yǔ)句從數(shù)據(jù)庫(kù)中查詢數(shù)據(jù),select語(yǔ)句的使用語(yǔ)法如下所示: SELECT 字段列表(各個(gè)字段之間用英文逗號(hào)隔開) FROM 表列表(多個(gè)表之間用英文逗號(hào)隔開) [WHERE 查詢條件] [GROUP BY 字段列表] [HAVING 條件] [ORDER BY 字段列表] [INTO TEMP 臨時(shí)表的名稱]例如查詢student表中的所有數(shù)據(jù),語(yǔ)句參考如下: select * from student;查詢student表中的記錄,語(yǔ)句參考如下: select count(*) from student;查詢student表中的name和age字段,語(yǔ)句參考如下: select name, age from student; 在查詢語(yǔ)句中,可以使用關(guān)系運(yùn)算符,可使用的關(guān)系運(yùn)算符如下: select * from student where name='amigo'; 2)!=或<> select * from student where age!=23; 3)> select * from student where age>23; 4)>= select * from student where name='amigo' and id='1000'; 2)OR(邏輯或) select * from student where name='amigo' or name='xingxing'; 3)[NOT] BWTWEEN([不]在......之間) select id, name, age from student where age between 24 and 30; 4)[NOT] IN([不]在....中) select id, name from student where name not in ('amigo', 'xingxing'); 5)IS [NOT] NULL:[不]是NULL select * from student where birthday is not null; 6)[NOT] MATCHES:[不]匹配 select id, name from student where name matches '?migo';例如,查找student表中以go結(jié)尾的任意長(zhǎng)度的記錄,參考語(yǔ)句如下: select * from student where name matches '*go'; 7)[NOT] LIKE:[不]匹配 select detail, count(*) as ratio from CTI_CallStat where taskid='000001200002111864' group by detail CASE子句 CASE (expr) WHEN expr1 THEN result1 WHEN expr2 THEN result2![]() ![]() ELSE result_else END 上面的CASE表達(dá)式的意思是: select id, name, age, case age when 1 then 'too little' when 100 then 'too old' else 'normal' end ageinfo from student; DECODE DECODE (expr, expr1, result1, expr2, result2, … result_else) 上面的DECODE函數(shù)的意思搜: SELECT id, name, age, DECODE (age, 1, 'too little', 100, 'too old', 'normal') ageinfo FROM student; UNION和UNION ALL SELECT order_num, order_time, c.customer_num FROM customer c , orders o WHERE c.customer_num = o.customer_num;第二種方式的參考實(shí)例如下: 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若想知道:哪個(gè)員工負(fù)責(zé)哪個(gè)項(xiàng)目,哪些項(xiàng)目沒有人負(fù)責(zé),可以使用右外連接,參考語(yǔ)句如下: 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若想知道:哪個(gè)員工負(fù)責(zé)哪個(gè)項(xiàng)目,哪些員工不負(fù)責(zé)項(xiàng)目,哪些項(xiàng)目沒有人負(fù)責(zé),可以使用全連接,參考語(yǔ)句如下: 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);不相關(guān)子查詢實(shí)例: select * from project where employee_num= (select employee_num from employee where employee_name='amigo');在很多情況下,我們可以將相關(guān)字查詢轉(zhuǎn)換為表連接,這樣數(shù)據(jù)庫(kù)引擎有可能更方便的得到更優(yōu)的查詢計(jì)劃,從而使SQL語(yǔ)句的執(zhí)行時(shí)間更少。例如可將上面的相關(guān)子查詢實(shí)例轉(zhuǎn)換為: select customer.* FROM customer, vip where customer.customer_num=vip.customer_num;我們可以使用insert語(yǔ)句往數(shù)據(jù)表中插入數(shù)據(jù)行。 如果各值按序賦給數(shù)據(jù)表中的所有數(shù)據(jù)列,則不需要指明數(shù)據(jù)列,例如往student表中插入數(shù)據(jù),參考語(yǔ)句如下: insert into student values('1000', 'amigo', 27, '19821121');如果是將值賦給指定數(shù)據(jù)列,則需指定數(shù)據(jù)列,例如只插入student表中的前三個(gè)字段,若使用如下的語(yǔ)句: insert into student values('1005', 'amigo', 27);此時(shí)會(huì)報(bào)錯(cuò)提示值的個(gè)數(shù)不正確,錯(cuò)誤提示如下所示: 236: Number of columns in INSERT does not match number of VALUES. 這時(shí),需要指定數(shù)據(jù)列,參考語(yǔ)句如下: insert into student (id, name, age) values('1005', 'amigo', 27); 可以在insert語(yǔ)句中嵌入select語(yǔ)句,從而將從一個(gè)或多個(gè)數(shù)據(jù)表查詢來(lái)的數(shù)據(jù)插入到目標(biāo)數(shù)據(jù)表。 insert into student select id, name, age, birthday from student_bak where age>25;可以使用update語(yǔ)句為數(shù)據(jù)表更新數(shù)據(jù)行。 例如想將student中的id為1000的記錄的name字段更新為amigo,age字段更新為28,語(yǔ)句參考如下: update student set name='amigoxie', age=28 where id='1000';使用如下的寫法也是等效的: update student set (name, age)=('amigoxie', 28) where id='1000';可以使用delete語(yǔ)句從數(shù)據(jù)表中刪除數(shù)據(jù)行。 例如,刪除student表中的所有數(shù)據(jù),參考語(yǔ)句如下: delete from student;例如,刪除student表中id為1001的數(shù)據(jù),參考語(yǔ)句如下: delete from student where id='1001';例如,刪除student表中以go結(jié)尾的記錄,參考語(yǔ)句如下: delete from student where name matches '*go'; |
|
|
來(lái)自: 趨明 > 《編程開發(fā)》