码迷,mamicode.com
首页 > 数据库 > 详细

老男孩mysql练习题自我学习

时间:2015-09-21 00:12:19      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:

  1. 登陆数据库
    root@bj-idc-testdb-001 ~]#mysql
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.5.38 MySQL Community Server (GPL) by Remi
    
    Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    [root@bj-idc-testdb-001 ~]#mysql -u root -p123456
    
    
    Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.


  2. 查看数据库版本及当前登陆用户是什么
    mysql> select version();
    +-----------+
    | version() |
    +-----------+
    | 5.5.38    |
    +-----------+
    1 row in set (0.00 sec)
    
    mysql> select user();
    +----------------+
    | user()         |
    +----------------+
    | root@localhost |
    +----------------+
    1 row in set (0.00 sec)
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | activiti_test      |
    | monitor            |
    | mysql              |
    | osa                |
    | osa_guard          |
    | sms                |
    | test               |
    | yandi_monitor      |
    +--------------------+
  3. 创建GBK字符集的数据库inline,并查看已建库的完整语句。
    mysql> create database inline default charset gbk;
    Query OK, 1 row affected (0.01 sec)
    
    mysql> show create database inline;
    +----------+----------------------------------------------------------------+
    | Database | Create Database                                                |
    +----------+----------------------------------------------------------------+
    | inline   | CREATE DATABASE `inline` /*!40100 DEFAULT CHARACTER SET gbk */ |
    +----------+----------------------------------------------------------------+
    1 row in set (0.00 sec)
  4. 创建用户inline,使之可以管理数据库inline;
    mysql> grant all on inline.* to inline@localhost identified by ‘inline‘;
    Query OK, 0 rows affected (0.00 sec)
  5. 查看创建用户inline拥有哪些权限
    mysql> show grants for inline@localhost;
    +---------------------------------------------------------------------------------------------------------------+
    | Grants for inline@localhost                                                                                   |
    +---------------------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO ‘inline‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*49129FD3218473FFCCE66D799E25FB6AFD6E0A73‘ |
    | GRANT ALL PRIVILEGES ON `inline`.* TO ‘inline‘@‘localhost‘                                                    |
    +---------------------------------------------------------------------------------------------------------------+
    2 rows in set (0.00 sec)
  6. 进入inline数据库:
    mysql> use inline
    Database changed
  7. 创建一innodb引擎字符集为GBK表test,字段id和name varchar(16),查看建表结构及SQL语句
    mysql> create table test(
        -> id int,
        -> name varchar(16))
        -> engine innodb default charset gbk;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> show create table test;
    +-------+--------------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table                                                                                                             |
    +-------+--------------------------------------------------------------------------------------------------------------------------+
    | test  | CREATE TABLE `test` (
      `id` int(11) DEFAULT NULL,
      `name` varchar(16) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=gbk |
    +-------+--------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> show variables like ‘char%‘;
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | utf8                       |
    | character_set_connection | utf8                       |
    | character_set_database   | gbk                        |
    | character_set_filesystem | binary                     |
    | character_set_results    | utf8                       |
    | character_set_server     | latin1                     |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+
    8 rows in set (0.00 sec)
    
    mysql> desc test
        -> ;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(11)     | YES  |     | NULL    |       |
    | name  | varchar(16) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    2 rows in set (0.01 sec)
  8. 插入一条数据:
    mysql> insert into test value(0,‘inlineuser‘);
    Query OK, 1 row affected (0.00 sec)
  9. 批量插入数据,要求中文不能为乱码
    mysql> insert into test value(1,‘inlineuser‘),(2,‘中文‘);
    Query OK, 2 rows affected (0.02 sec)
    Records: 2  Duplicates: 0  Warnings: 0
  10. 查询插入的所有记录
    mysql> ?select * from test;
    +------+------------+
    | id   | name       |
    +------+------------+
    |    0 | inlineuser |
    |    1 | inlineuser |
    |    2 | 中文       |
    +------+------------+
    3 rows in set (0.00 sec)
  11. 查询id大于1的记录
    mysql> select * from test where id>1;
    +------+--------+
    | id   | name   |
    +------+--------+
    |    2 | 中文   |
    +------+--------+
    1 row in set (0.00 sec)
  12. 更新id=1的记录
    ysql> update test set name=‘inuser‘ where id=1;
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> select * from test;
    +------+--------------+
    | id   | name         |
    +------+--------------+
    |    0 | inlineuser   |
    |    1 | inuser |
    |    2 | 中文         |
    +------+--------------+
    3 rows in set (0.00 sec)
  13. 在字段name前插入age字段,类型int(4)
    mysql> alter table test add age int(4) after id;
    Query OK, 3 rows affected (0.01 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> desc test;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(11)     | YES  |     | NULL    |       |
    | age   | int(4)      | YES  |     | NULL    |       |
    | name  | varchar(16) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
    
    mysql>
  14. 备份inline数据库及mysql数据库
    [root@bj-idc-testdb-001 ~]#mysqldump -B inline,mysql >/tmp/backup.sqlmysqldump: Got error: 1049: Unknown database ‘inline,mysql‘ when selecting the database
    [root@bj-idc-testdb-001 ~]#mysqldump -B inline mysql >/tmp/backup.sql 
    -- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
    [root@bj-idc-testdb-001 ~]#mysqldump --events -B inline mysql >/tmp/backup.sql
  15. 删除表中的所有数据。
    mysql> delete from test;
    Query OK, 3 rows affected (0.02 sec)
  16. 恢复数据
    [root@bj-idc-testdb-001 ~]#vim /tmp/backup.sql 
    [root@bj-idc-testdb-001 ~]#mysql </tmp/backup.sql
    [root@bj-idc-testdb-001 ~]#mysql
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 10
    Server version: 5.5.38 MySQL Community Server (GPL) by Remi
    
    Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
    
    
    mysql> use inline
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> select * from test;
    +------+------+--------------+
    | id   | age  | name         |
    +------+------+--------------+
    |    0 | NULL | inlineuser   |
    |    1 | NULL | inlineunuser |
    |    2 | NULL | 中文         |
    +------+------+--------------+
    3 rows in set (0.00 sec)
    
    mysql> desc test
        -> ;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(11)     | YES  |     | NULL    |       |
    | age   | int(4)      | YES  |     | NULL    |       |
    | name  | varchar(16) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
  17. 将inline数据库改为utf8编码
    [root@bj-idc-testdb-001 ~]#sed -i "s/gbk/utf8/g" /tmp/backup.sql
    [root@bj-idc-testdb-001 ~]#vim /tmp/backup.sql
    
    [root@bj-idc-testdb-001 ~]#mysql < /tmp/backup.sql
    [root@bj-idc-testdb-001 ~]#mysql inline
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 12
    Server version: 5.5.38 MySQL Community Server (GPL) by Remi
    
    Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
    
    mysql> select * from test;
    +------+------+--------------+
    | id   | age  | name         |
    +------+------+--------------+
    |    0 | NULL | inlineuser   |
    |    1 | NULL | inlineunuser |
    |    2 | NULL | 中文         |
    +------+------+--------------+
    3 rows in set (0.00 sec)
    
    mysql>
  18. 忘记mysql root用户密码
    [root@bj-idc-testdb-001 ~]#/usr/bin/mysqld_safe --skip-grant-tables &
    mysql> select host,user,password from mysql.user where user=‘root‘;
    +-----------------------+------+-------------------------------------------+
    | host                  | user | password                                  |
    +-----------------------+------+-------------------------------------------+
    | localhost             | root | *509910ED20429303306B7D5FA9215C7EC9E54B68 |
    | localhost.localdomain | root |                                           |
    | 127.0.0.1             | root |                                           |
    +-----------------------+------+-------------------------------------------+
    3 rows in set (0.00 sec)
    
    mysql> update mysql.user set password=password(123456) where user=‘root‘ and host=‘localhost‘;
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> exit
    Bye
    [root@bj-idc-testdb-001 ~]#/etc/init.d/mysqld stop
    150920 09:37:22 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
    Stopping mysqld:                                           [  OK  ]
    [1]+  Done                    /usr/bin/mysqld_safe --skip-grant-tables
    [root@bj-idc-testdb-001 ~]#/etc/init.d/mysqld start
    Starting mysqld:                                           [  OK  ]
    [root@bj-idc-testdb-001 ~]#mysql -uroot -p123456
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.5.38 MySQL Community Server (GPL) by Remi
    
    Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
    
    mysql>


  19. 在把id列设置为主键,在Name字段上创建普通索引
    mysql> alter table test add primary key idx_p(id);
    Query OK, 3 rows affected (0.02 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> desc test
        -> ;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(11)     | NO   | PRI | 0       |       |
    | age   | int(4)      | YES  |     | NULL    |       |
    | name  | varchar(16) | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
    
    
    mysql> alter table test add index idx_name(name);
    Query OK, 0 rows affected (0.02 sec)
    Records: 0  Duplicates: 0  Warnings: 0
  20. 在字段name后插入手机号码字段(moble),类型char(11)
    mysql> alter table test add mobile char(11);
    Query OK, 3 rows affected (0.02 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> desc test
        -> ;
    +--------+-------------+------+-----+---------+-------+
    | Field  | Type        | Null | Key | Default | Extra |
    +--------+-------------+------+-----+---------+-------+
    | id     | int(11)     | NO   | PRI | 0       |       |
    | age    | int(4)      | YES  |     | NULL    |       |
    | name   | varchar(16) | YES  | MUL | NULL    |       |
    | mobile | char(11)    | YES  |     | NULL    |       |
    +--------+-------------+------+-----+---------+-------+
    4 rows in set (0.00 sec)
    
    mysql> select * from test;
    +----+------+--------------+--------+
    | id | age  | name         | mobile |
    +----+------+--------------+--------+
    |  0 | NULL | inlineuser   | NULL   |
    |  1 | NULL | inlineunuser | NULL   |
    |  2 | NULL | 中文         | NULL   |
    +----+------+--------------+--------+
    3 rows in set (0.00 sec)
    
    mysql> insert into test values(3,5,‘foson‘,‘13455556666‘),(4,5,‘foson‘,‘13577889966‘);
    Query OK, 2 rows affected (0.01 sec)
    Records: 2  Duplicates: 0  Warnings: 0
    
    mysql> select * from test;
    +----+------+--------------+-------------+
    | id | age  | name         | mobile      |
    +----+------+--------------+-------------+
    |  0 | NULL | inlineuser   | NULL        |
    |  1 | NULL | inlineuser | NULL        |
    |  2 | NULL | 中文         | NULL        |
    |  3 |    5 | foson     | 13455556666 |
    |  4 |    5 | foson      | 13577889966 |
    +----+------+--------------+-------------+
    5 rows in set (0.00 sec)
  21. 在手机字段上对前8个字符创建普通索引
    mysql> alter table test add index idx_m (mobile(8));
    Query OK, 0 rows affected (0.02 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    mysql> show index from test;
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | test  |          0 | PRIMARY  |            1 | id          | A         |           5 |     NULL | NULL   |      | BTREE      |         |               |
    | test  |          1 | idx_name |            1 | name        | A         |           5 |     NULL | NULL   | YES  | BTREE      |         |               |
    | test  |          1 | idx_m    |            1 | mobile      | A         |           5 |        8 | NULL   | YES  | BTREE      |         |               |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    3 rows in set (0.00 sec)
    
    mysql> show index from test\G;
    *************************** 1. row ***************************
            Table: test
       Non_unique: 0
         Key_name: PRIMARY
     Seq_in_index: 1
      Column_name: id
        Collation: A
      Cardinality: 5
         Sub_part: NULL
           Packed: NULL
             Null: 
       Index_type: BTREE
          Comment: 
    Index_comment: 
    *************************** 2. row ***************************
            Table: test
       Non_unique: 1
         Key_name: idx_name
     Seq_in_index: 1
      Column_name: name
        Collation: A
      Cardinality: 5
         Sub_part: NULL
           Packed: NULL
             Null: YES
       Index_type: BTREE
          Comment: 
    Index_comment: 
    *************************** 3. row ***************************
            Table: test
       Non_unique: 1
         Key_name: idx_m
     Seq_in_index: 1
      Column_name: mobile
        Collation: A
      Cardinality: 5
         Sub_part: 8
           Packed: NULL
             Null: YES
       Index_type: BTREE
          Comment: 
    Index_comment: 
    3 rows in set (0.00 sec)
    
    ERROR: 
    No query specified
  22. 删除name mobile列的索引
    mysql> alter table test drop index idx_name;
    Query OK, 0 rows affected (0.00 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    
    mysql> alter table test drop index idx_m;
    Query OK, 0 rows affected (0.00 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> show index from test\G;
    *************************** 1. row ***************************
            Table: test
       Non_unique: 0
         Key_name: PRIMARY
     Seq_in_index: 1
      Column_name: id
        Collation: A
      Cardinality: 5
         Sub_part: NULL
           Packed: NULL
             Null: 
       Index_type: BTREE
          Comment: 
    Index_comment: 
    1 row in set (0.00 sec)
    
    ERROR: 
    No query specified
  23. 对name列的钱6个字符以及手机列的钱8个字符组建联合索引
    mysql> alter table test add index idx_n_m (name(6),mobile(8));
    Query OK, 0 rows affected (0.01 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> show index from test\G;
    *************************** 1. row ***************************
            Table: test
       Non_unique: 0
         Key_name: PRIMARY
     Seq_in_index: 1
      Column_name: id
        Collation: A
      Cardinality: 5
         Sub_part: NULL
           Packed: NULL
             Null: 
       Index_type: BTREE
          Comment: 
    Index_comment: 
    *************************** 2. row ***************************
            Table: test
       Non_unique: 1
         Key_name: idx_n_m
     Seq_in_index: 1
      Column_name: name
        Collation: A
      Cardinality: 5
         Sub_part: 6
           Packed: NULL
             Null: YES
       Index_type: BTREE
          Comment: 
    Index_comment: 
    *************************** 3. row ***************************
            Table: test
       Non_unique: 1
         Key_name: idx_n_m
     Seq_in_index: 2
      Column_name: mobile
        Collation: A
      Cardinality: 5
         Sub_part: 8
           Packed: NULL
             Null: YES
       Index_type: BTREE
          Comment: 
    Index_comment: 
    3 rows in set (0.00 sec)
    
    ERROR: 
    No query specified
  24. 有索引及没有索引执行计划的不同
    mysql> select * from test where name=‘foson‘ and mobile like ‘135%‘;
    +----+------+---------+-------------+
    | id | age  | name    | mobile      |
    +----+------+---------+-------------+
    |  4 |    5 | foson | 13577889966 |
    +----+------+---------+-------------+
    1 row in set (0.00 sec)
    
    mysql> explain select * from test where name=‘foson‘ and mobile like ‘135%‘;
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    |  1 | SIMPLE      | test  | range | idx_n_m       | idx_n_m | 46      | NULL |    1 | Using where |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    1 row in set (0.01 sec)
    
    
    mysql> alter table test drop index idx_n_m;
    Query OK, 0 rows affected (0.01 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> explain select * from test where name=‘foson‘ and mobile like ‘135%‘;
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
    |  1 | SIMPLE      | test  | ALL  | NULL          | NULL | NULL    | NULL |    5 | Using where |
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    
    mysql> 
    
    mysql> alter table test add index idx_n_m (name(6),mobile(8));
    Query OK, 0 rows affected (0.01 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> explain select * from test where  mobile like ‘135%‘;
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
    |  1 | SIMPLE      | test  | ALL  | NULL          | NULL | NULL    | NULL |    5 | Using where |
    +----+-------------+-------+------+---------------+------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    
    mysql> explain select * from test where name=‘foson‘ and mobile like ‘135%‘;
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    | id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    |  1 | SIMPLE      | test  | range | idx_n_m       | idx_n_m | 46      | NULL |    1 | Using where |
    +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
    1 row in set (0.00 sec)
    
    mysql> explain select * from test where name=‘foson‘;
    +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
    | id | select_type | table | type | possible_keys | key     | key_len | ref   | rows | Extra       |
    +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
    |  1 | SIMPLE      | test  | ref  | idx_n_m       | idx_n_m | 21      | const |    2 | Using where |
    +----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
    1 row in set (0.00 sec)
    
    mysql>


老男孩mysql练习题自我学习

标签:

原文地址:http://my.oschina.net/zhailibao2010/blog/508934

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!