小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

informix的常用SQL語(yǔ)句

 趨明 2012-03-15
 
1、創(chuàng)建數(shù)據(jù)庫(kù)
      eg1. 創(chuàng)建不記錄日志的庫(kù)testdb,參考語(yǔ)句如下:
CREATE DATABASE testdb;
      eg2. 創(chuàng)建帶緩沖式的記錄日志的數(shù)據(jù)庫(kù)testdb(SQL語(yǔ)句不一定在事務(wù)之中,擁有者名字不被用于對(duì)象的解析),參考語(yǔ)句如下:
CREATE DATABASE testdb WITH BUFFERED LOG;
      eg3. 創(chuàng)建無(wú)緩沖式的記錄日志的數(shù)據(jù)庫(kù)testdb(SQL語(yǔ)句不一定在事務(wù)之中,擁有者名字不被用于對(duì)象的解析),參考語(yǔ)句如下:
CREATE DATABASE testdb WITH LOG;
      eg4. 創(chuàng)建ANSI的數(shù)據(jù)庫(kù)(記錄日志時(shí)無(wú)緩沖,SQL總在事務(wù)之中,擁有者名字被用于對(duì)象的解析),參考語(yǔ)句如下:
CREATE DATABASE testdb WITH LOG MODE ANSI;
2、創(chuàng)建普通數(shù)據(jù)表
       普通數(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
);
3、創(chuàng)建臨時(shí)數(shù)據(jù)表
      臨時(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)
);
     eg2:也可以將正式表中customer中的數(shù)據(jù)通過(guò)select......into temp語(yǔ)句將數(shù)據(jù)導(dǎo)入到臨時(shí)表中,如下實(shí)例創(chuàng)建了一個(gè)正式的表customer,并插入了三條數(shù)據(jù),接著通過(guò)select....into temp語(yǔ)句將這個(gè)正式表中的數(shù)據(jù)導(dǎo)入到臨時(shí)表customer_temp。
      首先,創(chuàng)建customer普通數(shù)據(jù)表,建表語(yǔ)句如下:
CREATE TABLE customer (
    num SERIAL 
NOT NULL,
    name 
CHAR(15),
    create_time 
DATETIME YEAR TO FRACTION(3)
);
    接著,在普通數(shù)據(jù)表customer中插入三條記錄,語(yǔ)句如下:
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');
     最后,通過(guò)select......into temp語(yǔ)句將普通數(shù)據(jù)表customer中的數(shù)據(jù)導(dǎo)入到臨時(shí)表customer_temp中(注意:需要保證customer_temp表不存在,操作成功后,customer_temp中的字段為select出的字段),參考語(yǔ)句如下所示:
SELECT num, name, create_time FROM customer into TEMP customer_temp;
4、創(chuàng)建主鍵約束
     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)
);
     如下的語(yǔ)句為該表的serviceid和agentid創(chuàng)建了唯一索引:
create unique index Index_CTI_HTG on cti_humantaskgroup(
    serviceid  
ASC,
    agentid    
ASC
);
5、創(chuàng)建引用約束
    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;
      讀者可以注意點(diǎn),如上語(yǔ)句加上了on delete cascade,表示在刪除cti_humantask表時(shí),數(shù)據(jù)庫(kù)系統(tǒng)會(huì)自動(dòng)刪除子表cti_humantaskgroup中serviceid與之相同的數(shù)據(jù)。
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)
);
     若通過(guò)如下語(yǔ)句插入一條不滿足age的檢查約束的數(shù)據(jù):
insert into student values('1234''amigo'40'19821121');
     運(yùn)行后會(huì)出現(xiàn)如下提示信息:
530Check constraint (ines.c2209_13601) failed.  
7、創(chuàng)建視圖
     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
     若要查詢視圖中的數(shù)據(jù),例如得到student_age視圖中的數(shù)據(jù),可通過(guò)select語(yǔ)句進(jìn)行查詢,參考如下:
select * from student_age;   
     若要?jiǎng)h除student_age視圖,語(yǔ)句如下:
drop view student_age;
8、查詢語(yǔ)句
     我們使用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)算符如下:
     1)=
     例如查詢出student表中姓名為amigo的字段,語(yǔ)句參考如下:

select * from student where name='amigo';

     2)!=或<>
     例如查詢出年齡不為23的記錄,語(yǔ)句參考如下:

select * from student where age!=23;

     3)> 
     例如查詢出年齡大于23的記錄,語(yǔ)句參考如下:

select * from student where age>23;

     4)>=
     大于等于,與大于使用方法類似。
     5)<
     小于,與大于使用方法類似。
     6)<=
     小于等于,與大于使用方法類似。
     在where語(yǔ)句中,可使用的關(guān)鍵字如下所示:
     1)AND(邏輯與)
     例如,當(dāng)需要在student表中查出name為amigo,并且學(xué)號(hào)為1000的記錄,此時(shí)可以使用AND,參考語(yǔ)句如下:

select * from student where name='amigo' and id='1000';

      2)OR(邏輯或)
      例如,需要查詢name為amigo,或者name為xingxing的記錄,因?yàn)槭腔虻年P(guān)系,所以可以使用OR,參考語(yǔ)句如下:

select * from student where name='amigo' or name='xingxing';

      3)[NOT] BWTWEEN([不]在......之間)
      例如,查找student表中age在24和30之間的記錄的id、name和age字段,參考語(yǔ)句如下:

select id, name, age from student where age between 24 and 30;

      4)[NOT] IN([不]在....中)
      [NOT] IN后可以是一個(gè)具體的值,也可以是一個(gè)子查詢。
      例如,查找student表中的name不在“amigo”和“xingxing”的記錄的id和name字段,參考語(yǔ)句如下:

select id, name from student where name not in ('amigo''xingxing');

      5)IS [NOT] NULL:[不]是NULL
      例如需要查詢出student表中birthday不為空的記錄,參考語(yǔ)句如下:

select * from student where birthday is not null;

      6)[NOT] MATCHES:[不]匹配
      “?”表示匹配單個(gè)字符,“*”表示0到正整數(shù)個(gè)字符。
      例如,查找總共為5個(gè)字符,而且后4個(gè)字符為migo的記錄,參考語(yǔ)句如下:

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:[不]匹配
       使用方法與[NOT] MATCHES類似,但是是使用“_”表示單個(gè)字符,“%”表示0到正整數(shù)個(gè)字符。
       GROUP BY
       我們可以使用group by對(duì)查詢結(jié)果進(jìn)行分組。分組后我們可以得到各個(gè)分組的統(tǒng)計(jì)消息,例如平均值、總和、數(shù)據(jù)行數(shù)等。
       例如,需要根據(jù)detail(詳細(xì)情況分類)分組查詢CTI_CallStat表中taskid為000001200002111864的記錄,并將每種detail的數(shù)量顯示出來(lái),語(yǔ)句參考如下:

select detail, count(*as ratio from CTI_CallStat where taskid='000001200002111864' group by detail

       CASE子句
       我們可以使用CASE表達(dá)式對(duì)返回值進(jìn)行轉(zhuǎn)換,CASE表達(dá)式的語(yǔ)法如下:

CASE (expr)
WHEN expr1 THEN result1
WHEN expr2 THEN result2

ELSE result_else
END

       上面的CASE表達(dá)式的意思是:
       當(dāng)expr為expr1時(shí),返回result1;
       當(dāng)expr為expr2時(shí),返回result2;
       ...
       當(dāng)expr為其它情況時(shí),返回result_else.
       例如查詢student表,當(dāng)age為1時(shí),顯示為too little,100時(shí),顯示為too old,其余的情況顯示為normal,參考語(yǔ)句如下:

select id, name, age, 
        
case age
    
when 1 then 'too little'
    
when 100 then 'too old'
    
else 'normal'
        
end
        ageinfo
from student;

      DECODE
      我們可以使用DECODE函數(shù)對(duì)返回值進(jìn)行轉(zhuǎn)換,DECODE函數(shù)的語(yǔ)法如下:

DECODE (expr,
expr1, result1,
expr2, result2,

result_else)

      上面的DECODE函數(shù)的意思搜:
      當(dāng)expr為expr1時(shí),返回result1;
      當(dāng)expr為expr2時(shí),返回result2;
      ...
      當(dāng)expr為其它情況時(shí),返回result_else。
      該函數(shù)能達(dá)到CASE子句類似的功能,例如達(dá)到前面的功能,可使用如下的DECODE語(yǔ)句:

SELECT id, name, age, 
       DECODE (age,
          
1'too little',
          
100'too old',
           
'normal')
     ageinfo
    
FROM student;

      UNION和UNION ALL
      如果兩個(gè)或多個(gè)select語(yǔ)句的結(jié)果相似,我們可以用“union”或“union all”把這些select語(yǔ)句合并起來(lái)?!皍nion”和“union all”的區(qū)別是:“union”將去掉結(jié)果中的非第一次出現(xiàn)的值,而“union all”將保留結(jié)果中的非第一次出現(xiàn)的值。
      表連接的語(yǔ)法
      我們可以使用兩種方式進(jìn)行數(shù)據(jù)表連接:
      1)數(shù)據(jù)表之間使用逗號(hào),連接條件前使用WHERE;
      2)數(shù)據(jù)表之間使用JOIN,連接條件前使用ON。
      第一種方式的參考實(shí)例如下:

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;

       外連接
       例如,有兩個(gè)表,員工表和項(xiàng)目表,員工可以負(fù)責(zé)項(xiàng)目,項(xiàng)目也可以有負(fù)責(zé)人(員工)。
       若想知道:那個(gè)員工負(fù)責(zé)哪個(gè)項(xiàng)目,哪些員工不負(fù)責(zé)項(xiàng)目,可以使用左外連接,參考語(yǔ)句如下:

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

      子查詢
      子查詢分兩種:相關(guān)子查詢和不相關(guān)子查詢。在相關(guān)子查詢中,子查詢中涉及到父查詢的數(shù)據(jù)列;在不相關(guān)子查詢中,子查詢中不涉及到父查詢的數(shù)據(jù)列。
      相關(guān)子查詢實(shí)例:

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;

9、插入語(yǔ)句
      我們可以使用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ò)誤提示如下所示:

236Number 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ù)表。
     例如將student_bak表中age大于25的數(shù)據(jù)插入到student表中,參考語(yǔ)句如下:

insert into student
     
select id, name, age, birthday
         
from student_bak where age>25;

10、更新語(yǔ)句
      可以使用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'28where id='1000';

11、刪除語(yǔ)句
     可以使用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';

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多