´ÙÀ½ ÀÌÀü Â÷·Ê

1. INDEX, SEQUENCE, FUNCTION(1)

1.1 Create Index

INDEX ´Â µ¥ÀÌŸº£À̽º³»ÀÇ relation(Å×À̺í)¿¡ ´ëÇÑ °Ë»öÀÇ ¼º´ÉÀ» ³ô¿©ÁØ´Ù.


  CREATE  [UNIQUE]  INDEX  index_name 
   ON  table_name  (name_of_attribute);


  CREATE  [UNIQUE]  INDEX  index_name  ON  table_name
   [USING  acc_name] (column [ops_name] [,...]);


  CREATE  [UNIQUE]  INDEX  index_name  ON  table_name
   [USING  acc_name] (func_name() ops_name );
 
 
   acc_name   :  ACCESS METHOD . µðÆúÆ®´Â BTREE ÀÌ´Ù.(BTREE,RTREE,HASH)
   func_name  :  »ç¿ëÀÚ Á¤ÀÇ ÇÔ¼ö.
   ops_name   :  operator class (int4_ops,int24_ops,int42_ops)

   btree(sid int4_ops) ´Â btree ¸¦ ÀÌ¿ëÇÑ INDEX ÀÚ·áÇüÀÌ 4-BYTE Á¤¼öÀÓ.
   µðÆúÆ® operator class ´Â Field Type ÀÌ´Ù.
   ÇöÀç btree ´Â 7°³±îÁöÀÇ Multi-Column INDEX¸¦ Áö¿øÇÑ´Ù. 

example

   example 1) INDEX »ý¼º 1

    CREATE  INDEX  indx1
     ON  supplier(sid);

    supplier  relation(Å×À̺í) ÀÇ sname attribute(column) À»  INDEX ·Î ÁöÁ¤ 


   example 2) INDEX »ý¼º 2

    CREATE INDEX indx2
     ON supplier USING btree(sid int4_pos);


   example 3) INDEX »ý¼º 3

    CREATE INDEX indx3
     ON supplier USING btree(sid int8_ops);


   example 4) INDEX »ý¼º 4
  
    CREATE INDEX indx4
     ON supplier USING btree(sid, tid);


   example 5)  INDEX  »èÁ¦ 
  
    DROP INDEX indx1;
    DROP INDEX indx2;
    DROP INDEX indx3;
    DROP INDEX indx4;

1.2 Create SEQUENCE

SEQUENCE ´Â ¼øÂ÷ÀûÀÎ ¼ýÀÚ ¹ß»ý±âÀÌ´Ù.

 
  CREATE  SEQUENCE  seq_name [INCREMENT increment]
    [MINVALUE  minvalue]  [MANVALUE  maxvalue]
    [START  start]  [CACHE  cache]  [CYCLE]
  

  INCREMENT : ÀÌ°ªÀÌ -1 À̸é -1 ¸¸Å­ °¨¼Ò , 3 À̸é 3¾¿ Áõ°¡, µðÆúÆ®´Â 1 ÀÌ´Ù. 
  MAXVALUE  : optional clause , Áõ°¡ÇÒ¼ö ÀÖ´Â ÃÖ°í°ªÀ» ¸í½ÃÀûÀ¸·Î ÁöÁ¤
  START     : ½ÃÀÛ°ª
  CACHE     : sequence °ªÀ» ¸ÕÀú ¸Þ¸ð¸®¿¡ ÇÒ´çÇÏ¿© ºü¸¥ ACCESS ¸¦ °¡´ÉÄÉ ÇÑ´Ù.
  CYCLE     : ÃÖ°í°ªÀ¸·Î Áõ°¡µÇ¸é ´Ù½Ã ÃÖ¼Ò°ªÀ¸·Î ¼øȯÇÏ°Ô ÇÑ´Ù. 

example

    
    CREATE  SEQUENCE  seq_name1 START 101;
    SELECT  NEXTVAL('seq_name1);


    °á°ú 
    
    nextval
    -------
        114

1.3 Create FUNCTION

FUNCTION Àº »õ·Î¿î ÇÔ¼ö¸¦ Á¤ÀÇÇÑ´Ù.


 CREATE  FUNCTION  func_name([type[,...]])
    RETURNS  return_type [with (attribute [,...])]
    AS ' definition '
    LANGUAGE 'language_name';


 LANGUAGE : sql, pgsql, c µîÀÌ ÀÖ´Ù.
 

example

   CREATE  FUNCTION  test()  RETURNS  int4
   AS ' SELECT  1 '
   LANGUAGE 'sql';


   ½ÇÇà
   SELECT  test() AS  answer;


   °á°ú
     answer
     ------
          1
         

  AS ' ¿Í ' »çÀÌ¿¡ ÇÔ¼öÀÇ º»¹®À» ±âÀÔÇÏ¸é µÈ´Ù. Âü°í·Î ¹®ÀÚ¿­ÀÏ °æ¿ì,
  'seq_test1' ¿Í °°Àº °æ¿ì ´ÙÀ½Ã³·³ ÇÑ´Ù. 


  CREATE  FUNCTION  test()  RETURNS  int4
  AS  ' SELECT  NEXTVAL(''seq_test1'') '
  LANGUAGE  'sql';


  ¿©±â¼­ NEXTVAL Àº SEQUENCE °ü·Ã ³»ÀåÇÔ¼öÀÌ´Ù. 

1.4 ¿¹Á¦

´ÙÀ½ ¿¹Á¦ÀÇ À̸§Àº test.sql ÀÔ´Ï´Ù. ´ÙÀ½ ¿¹Á¦¸¦ È­ÀÏ·Î ¸¸µé¾î ´ÙÀ½Ã³·³ ½ÇÇàÇÏ½Ã¸é µË´Ï´Ù.

nogadax=> \i /usr/local/src/test.sql

   -------------------------------------------------------cut here!!
   --code  By   nogadax@chollian.net  /2000/02/18
   --drop  all  object  for  safe_test
   DROP  SEQUENCE  seq_test1;
   DROP  SEQUENCE  seq_test2;
   DROP  SEQUENCE  seq_test3;
   DROP  INDEX     ind_test1;
   DROP  INDEX     ind_test2;
   DROP  INDEX     ind_test3;
   DROP  TABLE     tab_test1;
   DROP  TABLE     tab_test2;
   DROP  TABLE     tab_test3;
   DROP  FUNCTION  func_test();
  
   --create  sequence  seq_test1,seq_test2,seq_test3
   CREATE  SEQUENCE  seq_test1  START  101;
   CREATE  SEQUENCE  seq_test2  START    1;
   CREATE  SEQUENCE  seq_test3  START    1;
  
   --create table tab_test1,tab_test2,tab_test3 
   CREATE  TABLE  tab_test1(
       tab1_id    int4  NOT NULL,
       tab1_name  text,
       tab1_tel   text,
       teb1_memo  text
    );
    
   CREATE  TABLE  tab_test2(
       tab2_id    int4  NOT NULL,
       tab2_name  text,
       tab2_tel   text,
       teb2_memo  text
    );
   
   CREATE  TABLE  tab_test3(
       tab3_id    int4 DEFAULT  nextval('seq_test3') NOT NULL,
       tab3_name  text,
       tab3_tel   text,
       tab3_memo  text
    );
  
   --craete  index
   CREATE  UNIQUE  INDEX   ind_test1  ON  tab_test1(tab1_id);
   CREATE  UNIQUE  INDEX   ind_test2  ON  tab_test2(tab2_id);
   CREATE  UNIQUE  INDEX   ind_test3  ON  tab_test3  USING  btree(tab3_id  int4_ops);
   
   --FUNCTION  func_test()
   CREATE  FUNCTION  func_test()  RETURNS  INT4
      AS  ' SELECT NEXTVAL(''seq_test1'') '
      LANGUAGE 'sql';

   --transaction 1 
   BEGIN;
      INSERT  INTO  tab_test1  VALUES (func_test(),'jini1','000-0000','No_Memo1');
      INSERT  INTO  tab_test2  VALUES (nextval('seq_test2'),'winob1','000-0001','No_Memo1');
      INSERT  INTO  tab_test3  (tab3_name,tab3_tel,tab3_memo) 
                               VALUES ('nogadax1','000-0003','No_Memo1'); 
      
      INSERT  INTO  tab_test1  VALUES (func_test(),'jini2','100-0000','No_Memo2');
      INSERT  INTO  tab_test2  VALUES (nextval('seq_test2'),'winob2','100-0001','No_Memo2');
      INSERT  INTO  tab_test3  (tab3_name,tab3_tel,tab3_memo) 
                               VALUES ('nogadax2','100-0003','No_Memo2'); 
      
      INSERT  INTO  tab_test1  VALUES (func_test(),'jini3','200-0000','No_Memo3');
      INSERT  INTO  tab_test2  VALUES (nextval('seq_test2'),'winob3','200-0001','No_Memo3');
      INSERT  INTO  tab_test3  (tab3_name,tab3_tel,tab3_memo) 
                               VALUES ('nogadax3','200-0003','No_Memo3'); 
      
      INSERT  INTO  tab_test1  VALUES (func_test(),'jini4','300-0000','No_Memo4');
      INSERT  INTO  tab_test2  VALUES (nextval('seq_test2'),'winob4','300-0001','No_Memo4');
      INSERT  INTO  tab_test3  (tab3_name,tab3_tel,tab3_memo) 
                               VALUES ('nogadax4','300-0003','No_Memo4'); 
      
      INSERT  INTO  tab_test1  VALUES (func_test(),'jini5','400-0000','No_Memo5');
      INSERT  INTO  tab_test2  VALUES (nextval('seq_test2'),'winob5','400-0001','No_Memo5');
      INSERT  INTO  tab_test3  (tab3_name,tab3_tel,tab3_memo) 
                               VALUES ('nogadax5','400-0003','No_Memo5'); 
     
   END;
    
   --transaction 2 
   BEGIN;
      SELECT * FROM tab_test1;
      SELECT * FROM tab_test2;
      SELECT * FROM tab_test3;
      VACUUM VERBOSE ANALYZE tab_test1;
      VACUUM VERBOSE ANALYZE tab_test2;
      VACUUM VERBOSE ANALYZE tab_test3;
   END;
   
   -------------------------------------------------------------------End !!
   

´ÙÀ½ ÀÌÀü Â÷·Ê