MySQLデータベースおよび基本コマンドの使用

10812 ワード

MySQLの基本コマンド
  • 使用前準備(MySQLデータベース)
  • 一、基本命令
  • 1、サービス開始
  • 2、サービス停止
  • 3、接続データ
  • 4、ログオン終了(接続解除)
  • 5、バージョン表示(接続後実行可能)
  • 6、現時刻表示(接続後実行可能)
  • 7、リモート接続
  • MySQLリモートアクセス許可リモート接続を開く
  • (1)mysqlデータベースへの登録
  • (2)リモート接続実現(授権法)
  • (3)リモート接続実現(改表法)
  • 二、データベース操作
  • 1、データベースの作成
  • 2、データベースの削除
  • 3、データベースの切り替え
  • 4、現在選択されているデータベースの表示
  • 三、表操作
  • 1、現在のデータベース内のすべてのテーブルを表示する
  • 2、表作成
  • 3、削除表
  • 4、表構造の表示
  • 5、建表文の表示
  • 6、表名の変更
  • 7、修正表
  • 四、データ操作
  • 1、増
  • 2、削除
  • 3、改
  • 4、調べ
  • 五、調べ
  • 1、基本文法
  • 2、重複除外
  • 3、条件照会
  • 4、重合
  • 5、グループ
  • 6、並べ替え
  • 7、ページング
  • 六、関連
  • 建表文:
  • いくつかのデータを挿入する:
  • 関連照会:
  • 分類:
  • 1、表A inner join表B:
  • 2、表A left join表B:
  • 3、表A right join表B:
  • 使用前準備(MySQLデータベース)
  • リンク:https://share.weiyun.com/5DEKiPHパスワード:Delong
  • 各バージョンのMySQLおよび基本的なコマンドが含まれている
  • 一、基本命令
    1、サービスを開始する
      :        cmd
      :net start     
      :net start mysql57
    

    2、サービス停止
      :        cmd
      :net stop     
      :net stop mysql57
    

    3、接続データ
      :mysql -u     -p
      :mysql -u root -p
        (      )
    

    4、ログオンを終了(接続解除)
    quit exit
    

    5、バージョンの表示(接続後に実行可能)
      :select version();
    

    6、現在時刻を表示(接続後に実行可能)
      :select now();
    

    7、リモート接続
    MySQLリモートアクセス権限を有効にしてリモート接続を許可
      :mysql -h ip   -u     -p
        mysql  
    

    (1)mysqlデータベースへの登録
    mysql -u root -p
    userテーブルの表示
    mysql> use mysql;
    Database changed
    mysql> select host,user,password from user;
    +--------------+------+-------------------------------------------+
    | host         | user | password                                  |
    +--------------+------+-------------------------------------------+
    | localhost    | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
    | 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
    +--------------+------+-------------------------------------------+
    2 rows in set (0.00 sec)
    

    userテーブルで作成されたrootユーザーが表示されます.hostフィールドはログインしたホストを表し、その値はIPでもホスト名でも使用できます.
    (1)ローカルIPでログインしたい場合は,以上のHost値を自分のIpに変更すればよい.
    (2)リモート接続の実現(授権法)
    hostフィールドの値を%に変更すると、どのクライアントマシンでもrootユーザーでmysqlサーバにログインできることを示し、開発時に%に設定することを推奨します.update user set host = ’%’ where user = ’root’;
    権限をALL PRIVILEGESに変更
    mysql> use mysql;
    Database changed
    mysql> grant all privileges  on *.* to root@'%' identified by "password";
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select host,user,password from user;
    +--------------+------+-------------------------------------------+
    | host         | user | password                                  |
    +--------------+------+-------------------------------------------+
    | localhost    | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
    | 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
    | %            | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E |
    +--------------+------+-------------------------------------------+
    3 rows in set (0.00 sec)
    

    これにより、機器はユーザ名rootパスワードrootで当該機器上のMySqlにリモートアクセスすることができる.
    (3)リモート接続の実現(改表法)
    use mysql;
    
    update user set host = '%' where user = 'root';
    

    これにより、rootユーザを介してMysqlにアクセスすることができる.
    二、データベース操作
    1、データベースの作成
          :create database      charset=utf8;
          :create database sunck charset=utf8;
    

    2、データベースの削除
          :drop database     ;
          :drop database sunck;
    

    3、データベースの切り替え
          :use     ;
          :use sunck;
    

    4、現在選択されているデータベースの表示
        select database();
    

    三、表操作
    1.現在のデータベース内のすべてのテーブルの表示
        show tables;
    

    2、表の作成
          :create table   (    );
          :
              auto_increment        
              primary key      
              not null     
          :create table student(id int auto_increment primary key, name varchar(20) not null, age int not null, gender bit default 1, address varchar(20), isDelete bit default 0);
    

    3、テーブルの削除
          :drop table   ;
          :drop table student;
    

    4、表構造の表示
          :desc   ;
          :desc student;
    

    5、建表文の表示
          :show create table   ;
          :show create table student;
    

    6、テーブル名の変更
          :rename table     to    ;
          :rename table car to newCar;
    

    7、表の修正
          :alter table    add|change|drop      ;
          :alter table newcar add isDelete bit default 0
    

    四、データ操作
    1、増加
    a、    
          :insert into    values(...);
          :        ,            ,    0,             
          :insert into student values(0,"tom",19,1,"  ",0);
    b、    
          :insert into   ( 1, 2,……) values( 1, 2,……);
          :insert into student(name,age,address) values("lilei",19,"  ");
    c、        
          :insert into    values(...),(...),……
          :insert into student values(0,"hanmeimei",18,0,"  ",0),(0,"poi",22,1,"  ",0),(0,"wenli",20,0,"   ",0);
    

    2、削除
      :delete from    where   ;  
      :delete from student where id=4;
      :         ,  
    

    3、変更
      :update    set  1= 1, 2= 2,…… where   ;
      :update student set age=16 where id=7;  
      :           ,  
    

    4、調べる
      :         
      :select * from   ;
      :select * from student;
    

    五、調べる
    1、基本文法
          :select * from   ;
          :
              a、from        ,          
              b、select        ,   *               
              c、 select       ,    as      ,           
              d、        ,        
    
          :
              select * from student;
              select name, age from student;
              select name as a, age from student;
    

    2、重複行の排除
         select       distinct        
          :
              select gender from student;
              select distinct gender from student;
    

    3、条件照会
         a、  
            select * from    where   
         b、     
                      =
                      >
                      <
                    >=
                    <=
                     != <>
              :  id   8     
              :select * from student where id>8;
         c、     
            and      
            or       
            not     
              :  id   7    
              :select * from student where id>7 and gender=0;
         d、    
            insert into student values(0,"   ",65,1,"  ",0);
            insert into student values(0,"  ",66,1,"  ",0);
    
            like
            %          
            _        
    
              :       
              :
                  select * from student where name like " %";
                  select * from student where name like " _";
         e、    
            in                            
            between...and...             
    
              :     8、10、12   
              :select * from student where id in (8,10,12);
              :     6 8   
              :select * from student where id between 6 and 8;
         f、   
            insert into student(name,age) values("   ",70);
              :null ""   
               :is null
                : is not null
    
              :         
              :select * from student where address is null;
              :        
              :select * from student where address is not null;
         g、   
               ,not      ,     
            and or    ,           or,    ()   
    

    4、重合
                  ,   5     
        a、count(*)           ,      *   
        b、max( )              
        c、min( )              
        d、sum( )            
        e、avg( )              
    
          :      
          :select count(*) from student;
          :          
          :select max(id) from student where gender=0;
          :          
          :select min(id) from student where gender=0;
          :          
          :select sum(age) from student;
          :            
          :select avg(age) from student;
    

    5、グループ化
              ,                   。
           ,           ,                  
                     ,     
    
          :select  1, 2,  …… from    group by  1, 2, 3,……;
          :       
          :
              select gender,count(*) from student group by gender;
              select name,gender,count(*) from student group by gender,age;
    
                :select  1, 2,  …… from    group by  1, 2, 3,…… having  1,……  ……;
          :select gender,count(*) from student group by gender having gender;
    
    
        where having   :
        where  from          ,          
        having  group by       
    

    6、並べ替え
          :select * from    order by  1 asc|desc, 2 asc|desc , ……;
          :
            a、      1    ,     1    ,    2    
            b、             
            c、asc  
            d、desc  
    
          :              
          :
              select * from student where isDelete=0 order by age desc;
              select * from student where isDelete=0 order by age desc, id desc;
    

    7、ページング
          :select * from    limit start,count;
          :start   0  
          :
              select * from student limit 0,3;
              select * from student limit 3,3;
              select * from student where gender=1 limit 0,3;
    

    六、関連
    テーブル文:
    1、create table class(id int auto_increment primary key, name varchar(20) not null, stuNum int not null);
    2、create table students(id int auto_increment primary key, name varchar(20) not null, gender bit default 1, classid int not null, foreign key(classid) references class(id));
    

    いくつかのデータを挿入します.
    insert into class values(0, "python01", 55),(0, "python02", 50),(0, "python03", 60),(0, "python04", 80);
    insert into students values(0, "tom", 1, 1);
    insert into students values(0, "lilei", 1, 10);
    insert into students values(0, "jack", 1, 2);
    select * from students;
    

    関連クエリー:
    select students.name,class.name from class inner join students on class.id=students.classid;
    
    select students.name,class.name from class left join students on class.id=students.classid;
    
    select students.name,class.name from class right join students on class.id=students.classid;
    

    分類:
    1、表A inner join表B:
         A  B            
    

    2、表A left join表B:
         A  B            ,   A      ,        null  
    

    3、表A right join表B:
         A  B            ,   B      ,        null