データベースでよく使用されるsql文と操作

21687 ワード

データベース操作
ラベル(スペース区切り):mysql
  • アクセス:mysql-h hostname-u user_name-p password-h hostnameデフォルトローカル(localhost)
  • はすべてのデータベースを表示します:show databases;
  • 操作するデータベースを選択:use database_name;
  • 指定データベース内のすべてのデータテーブル:show tables;
  • データベースを終了:q
  • 現在の文の入力を終了します:c
  • ライブラリの操作:
  • 新規データベース:create dtatbase database_name;
  • データベースの削除:drop database database_name;(一度に1つしか削除できない)
  • テーブルを操作する:
  • 新規データテーブル:create tableテーブル名(列名1列タイプ[列の整合性制約],列名2列タイプ[列の整合性制約],......列名n列タイプ[列の整合性制約])default charset=utf 9;(漢字表示、デフォルト符号化)
  • 表示テーブルの作成構造:desc table_name;
  • 削除テーブル:drop table table_name;
  • 他のデータベースのテーブル:drop tableデータベース名を削除する.テーブル名;
  • テーブルへのデータの挿入:
  • 行を一度に挿入:insert[into]テーブル名([カラム名])values(値リスト);
  • テーブル名の後ろに列(セグメント)名が書かれていない場合、デフォルトではテーブル内のすべての列に値が割り当てられます.また、文字列は「和」で囲まれています.
  • で付与データ型は、挿入する値
  • の代わりにdefaultを使用するデフォルト値を持つカラムとテーブルと一致する必要があります.
  • データの表示:select*fromテーブル名;例:select*from student;
  • 一度に複数行を挿入:
  • insert [into]   ([  ]) values(   ),(   ),......;insert into student values(3,'dd',' '),(4,'vv',' '); 
  • 挿入するデータを他のテーブルから参照:
  • insert [into]   ([  ]) select  from    [where ...];
    create table student1(id int,name varchar(20),sex varchar(10),age int)default charset=utf8;
    insert into student1 values(1,"   "," ",26),(2,"  "," ",27);
    insert into student select id,name,sex from student1 where id=2;
    select * from student;
  • データベース内のテーブルのレコードの変更:
  • uodate    set[where ];
    // where             ,        
      :
            update student1 set sex=' ';
            update student1 set sex=' ' where id=1;
            update student1 set sex=' ',age=22 where id=2;
  • データテーブルのレコードを削除する:
  • delete from   [where ];
    //  where         ,        
     :
     delete from student1 where age<26;
  • クエリー・テーブルのデータ:
  • select  from   [where ][order by]{asc  desc]
    //asc:  (  )
    //desc:  
     :
      select * from student;
      select id ,name from student;
      select * from student where id<=3 order by id;          
      select * from student  where id<=3 order by id desc;
  • テーブルの構造を変更する:
  • 表に列を追加:指定された場所がない場合、デフォルトでは列の最後に
  • を追加します.
    alter table    add        [after   ];
    alter table sutdent add money int after id;
  • 列を削除:
  • alter table    drop   ;
    alter table student drop age;
  • 指定したカラムのデフォルト値を変更するには、
  • alter table    alter    set default    ;
  • 列名とタイプの変更:
  • alter table    change        [after   ];
    //           ,          ,    ,            
  • テーブルにプライマリ・キーを設定します.つまり、あるカラムをプライマリ・キーに設定します.プライマリ・キー・カラムの各メンバーは互いに異なり、一意のフラグである行
  • が存在します.
    alter table    add primary key(  );
    //          ,       ,        not null;
  • プライマリ・キーを自己成長
  • に設定
    alter table    change            auto_increment;
    //         ,  ,     ,    ,   (0);
     :
      alter table student change num num int auto_increment;
  • 特定の位置から自己成長を設定:そうでない場合、レコードを削除してから追加すると断層
  • が表示されます.
    alter table    auto_increment=  ;
  • プライマリ・キーの削除:
  • alter table    drop primary key;
    //                
     :alter table table_name change new_type;alter table student drop primary key;
  • 表名の変更:
  • alter table table_name rename as new_table_name;alter table student rename as student2;

    例:データテーブルを作成します.
      create table student3(
        -> code int not null auto_increment,
        -> name varchar(20) not null,
        -> age int,
        -> sex varchar(10),
        -> score int,
        -> grade int,
        -> address varchar(20) default '  ',
        -> major varchar(20),
        -> primary key(code))default charset=utf8;
    
     insert into student3(name,age,sex,score,grade,address,major) 
        -> values('xxx',13,'m',78,1,'asdfgh','sss'),
        -> ('xcc',15,'w',88,2,'afdfdh','nnn'),
        -> ('cxx',9,'m',99,3,'sdsvfbf','vvv'),
        -> ('ccc',45,'m',56,1,'cdfsff','sss'),
        -> ('xbb',34,'w',66,1,'cdfgdv','sss');select * from student3;select code,name from student3;select code,age+score from student3;
    select code,age+score as xxx from student3;
    
                ,       :
    select code as '  ',name as '  ' from student3;select s.code,s.name from student3 as s;
    
           (        )
    select distinct grade from student3;
    
         20         
    select name from student3 where age<20;select * from student3 order by age desc;
    
            3        。
    select * from student3 order by age desc limit 3;
    
            4 ,5        :
    select * from student3 order by age desc limit 3,2; 
    
    concat:
    select concat(name,'is ',age,' years old') from student3 order by age desc; 
  • 合計:count(カラム名)
  • select count(*) from table_name;
  • 計算一組の平均成績:
  • select avg(socre) from where grade=1;

    専門の最高点と最低点を検索します.
    select max(score) as '   ',min(score) as '    ' from student3;
  • 検索専攻の平均成績が70以上の専攻
  • select major,avg(score) as '    ' from student3 group by major having avg(score)>70;
  • 合計を求める:
  • select sum(score) from stduent3;

    年齢[いいえ]20-50の間の学生の個人情報を調べます
    select * from student3 where age[not] between 20 and 50;
    select * from where code like 21;

    名前が「a」で始まるすべての学生情報を検索します.
    select * from student3 where name like 'a%';
    select * from steudent3 where name like '%a';
    select * from student where name like '%a%';
    show tables like '%s%';
    //    ,            
    select * from student3 order by grade,score desc;

    複数テーブルクエリー:
    select s.id,s.name,s.age,c.course,c.score from stu as s inner join cour as c on s.id=c.num;
  • 接合:
  • select concat(name,'is','age,'years old')from student3 order by age;
  • 内接続(inner join)
  • select s.id,s.name,s.age,c.course,c.score from stu as s inner join cour as c on s.id=c.num;
  • 左外接続(left join):左表を主とする(左表にあるものはすべてある)
  • select s.id,s.name,s.age,c.course,c.score form stu as s left join cour as c on s.id=c.num;
  • 右外部接続(right join):
  • select s.id,s.name,s.age,c.course,c.score form stu as s right join cour as c on s.id=c.num;

    サブクエリ:別のクエリブロックのwhere句またはhaving句にネストされたクエリをサブクエリ注意と呼びます.select-from-where文をクエリブロック例と呼びます.select name、age from stu where id in(select num from cour where course='国語');
  • レプリケーションテーブル:1.既知のデータテーブルをコピー:
  • create table     select * from    ; 

    2.テーブルをコピーするときに、テーブルの内容を制限します.
    create table    select [  ...] from    ;

    3.既存のテーブルを作成する空のテーブル
    create table     select * from     where 0=1;