SQLベース01

4023 ワード

SQLとは
     SQL(structured query language):       
    SQL                        
    SQL    ,    ,    
            ,    (    ,CRUD)       ,    SQL  
    Create , Retrive, Update, Delete
SQLでよく使われるキーワード
    select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index  
SQLの文の種類
          (DDL:Data Definition Language)
          (DML:Data Manipulation Language)
          (DQL:Data Query Language)
      DQL       where,order by,group by having
SQLでの制約
簡単な制約:
     ①      not  null
     ②       unique
     ③     default
例:
create table t_dog (id integer, name test not null unique,age integer not null default 60);
メインキー:
Primary Key,  PK             ,            
  t_dog       id    ,        
メインキー制約の理由を追加:
              ,       
キーの設計原則:
        ①       
        ②      
        ③           
        ④          
キーの宣言:
           Primary Key       
      :create table t_dog (id integer primary key,name text, age integer);integer   id  t_dog    
              (   integer  ),    autoincrement
      :create table t_dog(id integer primary key autoincrement,name text,age integer);
DDL文
時計を作る
  :create table    (   1     1,    2     2, …) ;
  :create table t_dog (id integer, name text, age inetger, score real) ;
      :
     ,                ,                
create table if not exists    (   1     1,    2     2, …) ;
DML文
挿入データ(insert)
    :insert into    (  1,   2, …) values (  1  ,   2  , …) ;
    :insert into t_student (name, age) values (‘zs’, 10) ;
    :                 ’   
データの更新(udate)
      :update    set   1 =   1  ,   2 =   2  , … ;
      :update t_student set name = ‘zs’, age = 20 ;
      :       t_student       name   zs,age   20
データを削除(delete)
      :delete from    ;
      :delete from t_dog ;
      :       t_student         
DQL文
書式:
    select   1,   2, … from    ;
    select * from   ;   //         
例:
    select name, age from t_dog ;
    select * from t_dog ;
    select * from t_dog where age > 10 ;  //      
条件文
      :                 ,     DML         
条件文の一般的な書式
    where    =     ;   //       =
    where    is     ;   // is     =
    where    !=     ;
    where    is not     ;   // is not     !=
    where    >     ;
    where   1 =     and   2 >     ;  // and   C     &&
    where   1 =     or   2 =     ;  //  or    C     ||
条件文の練習
  :
 ① t_dog      10         zs   ,      5
  update t_dog set age = 5 where age > 10 and name != ‘zs’ ;
  ② t_dog      li   ,score         age    
  update t_dog set score = age where name = ‘li’ ;
関連語句の照会
統計
  count(X)
  select count(*) from t_student
          

  select count(age) from t_student
    age       (Null     )

  avg(X):          
  sum(X):         
  max(X):          
  min(X):          
並べ替え:
              order by    
    select   1,   2 from    order by    ;
    select * from t_student order by age ;

             (    ),       (    )
    select * from t_student order by age desc ;  //  
    select * from t_student order by age asc ;   //   (  )

                
    select * from t_student order by age asc, height desc ;
           (  ),           (  )
limit改ページ
    limit              ,       10   
  select * from    limit   1,   2 ;
    :select * from t_student limit 4, 8 ;
       :     4   ,   8   
改ページ
    limit        ,        5   ,         
     1 :limit 0, 5
     2 :limit 5, 5
     3 :limit 10, 5
     n :limit 5*(n-1), 5

        :select * from t_student limit 7 ;
       select * from t_student limit 0, 7 ;
           7