美文网首页
mysql操作

mysql操作

作者: 孙子衡 | 来源:发表于2018-09-07 20:35 被阅读0次

    命令行练习

    sunziheng@sunziheng-virtual-machine:~$ mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 17
    Server version: 5.7.23-0ubuntu0.16.04.1 (Ubuntu)
    
    Copyright (c) 2000, 2018, 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 user,host,password from mysql.user;
    ERROR 1054 (42S22): Unknown column 'password' in 'field list'
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mydb               |
    | mysql              |
    | performance_schema |
    | py01               |
    | sys                |
    +--------------------+
    6 rows in set (0.00 sec)
    
    mysql> select user,host,password from mysql.user;
    ERROR 1054 (42S22): Unknown column 'password' in 'field list'
    mysql> use mysql;
    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 user,host from mysql.user;
    +------------------+-----------+
    | user             | host      |
    +------------------+-----------+
    | debian-sys-maint | localhost |
    | mysql.session    | localhost |
    | mysql.sys        | localhost |
    | root             | localhost |
    +------------------+-----------+
    4 rows in set (0.00 sec)
    
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mydb               |
    | mysql              |
    | performance_schema |
    | py01               |
    | sys                |
    +--------------------+
    6 rows in set (0.00 sec)
    
    mysql> use mydb;
    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> show tables;
    +----------------+
    | Tables_in_mydb |
    +----------------+
    | classed        |
    | student        |
    | tb01           |
    | tb_date        |
    | tb_num         |
    | tb_str         |
    +----------------+
    6 rows in set (0.00 sec)
    
    mysql> desc tb01;
    +-------+------------+------+-----+---------+-------+
    | Field | Type       | Null | Key | Default | Extra |
    +-------+------------+------+-----+---------+-------+
    | id    | int(11)    | NO   | PRI | NULL    |       |
    | name  | varchar(6) | YES  |     | NULL    |       |
    | age   | int(11)    | YES  |     | NULL    |       |
    +-------+------------+------+-----+---------+-------+
    3 rows in set (0.00 sec)
    
    mysql> insert into tb01(name,age) values('szh',18);
    ERROR 1364 (HY000): Field 'id' doesn't have a default value
    mysql> show create table tb01;
    +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table                                                                                                                                                             |
    +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | tb01  | CREATE TABLE `tb01` (
      `id` int(11) NOT NULL,
      `name` varchar(6) DEFAULT NULL,
      `age` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
    +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> select *from tb01;
    +----+-------+------+
    | id | name  | age  |
    +----+-------+------+
    |  9 | szh06 |   22 |
    +----+-------+------+
    1 row in set (0.00 sec)
    
    mysql> delete from tb01 where age=22;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into tb01 values(9,'szh',12);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> \q
    Bye
    sunziheng@sunziheng-virtual-machine:~$ mysql -u root -p
    Enter password: 
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
    sunziheng@sunziheng-virtual-machine:~$ mysql -u root -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 22
    Server version: 5.7.23-0ubuntu0.16.04.1 (Ubuntu)
    
    Copyright (c) 2000, 2018, 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> 
    mysql> 
    mysql> 
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mydb               |
    | mysql              |
    | performance_schema |
    | py01               |
    | sys                |
    +--------------------+
    6 rows in set (0.00 sec)
    
    mysql> use mydb;
    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> show tables;
    +----------------+
    | Tables_in_mydb |
    +----------------+
    | classed        |
    | student        |
    | tb01           |
    | tb_date        |
    | tb_num         |
    | tb_str         |
    +----------------+
    6 rows in set (0.00 sec)
    
    mysql> drop table tb01;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> show tables;
    +----------------+
    | Tables_in_mydb |
    +----------------+
    | classed        |
    | student        |
    | tb_date        |
    | tb_num         |
    | tb_str         |
    +----------------+
    5 rows in set (0.00 sec)
    
    mysql> show tables;
    +----------------+
    | Tables_in_mydb |
    +----------------+
    | classed        |
    | student        |
    | tb01           |
    | tb_date        |
    | tb_num         |
    | tb_str         |
    +----------------+
    6 rows in set (0.00 sec)
    
    mysql> select *from tb01;
    +----+------+------+
    | id | name | age  |
    +----+------+------+
    |  9 | szh  |   12 |
    +----+------+------+
    1 row in set (0.00 sec)
    
    mysql> select database();
    +------------+
    | database() |
    +------------+
    | mydb       |
    +------------+
    1 row in set (0.00 sec)
    
    mysql> show tables;
    +----------------+
    | Tables_in_mydb |
    +----------------+
    | classed        |
    | student        |
    | tb01           |
    | tb_date        |
    | tb_num         |
    | tb_str         |
    +----------------+
    6 rows in set (0.00 sec)
    
    mysql> show create table student;
    +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table   | Create Table                                                                                                                                                                                                                                                                                                                      |
    +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | student | CREATE TABLE `student` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(16) NOT NULL,
      `age` tinyint(3) unsigned DEFAULT NULL,
      `sex` enum('w','m') NOT NULL DEFAULT 'w',
      `classid` int(10) unsigned DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `name` (`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
    +---------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> show create table student\G;
    *************************** 1. row ***************************
           Table: student
    Create Table: CREATE TABLE `student` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(16) NOT NULL,
      `age` tinyint(3) unsigned DEFAULT NULL,
      `sex` enum('w','m') NOT NULL DEFAULT 'w',
      `classid` int(10) unsigned DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `name` (`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    1 row in set (0.00 sec)
    
    ERROR: 
    No query specified
    
    mysql> insert into student values(null,'szh01',18,'m',1),
        -> (null,'daojian',34,'m',1),
        -> (null,'lainxisheng',22,'m',1),
        -> (null,'luoshen',33,'w',2),
        -> (null,'sunziheng02',16,'m',2),
        -> (null,'yangmi',34,'w',3),
        -> (null,'nazha',28,'w',1);
    Query OK, 7 rows affected (0.01 sec)
    Records: 7  Duplicates: 0  Warnings: 0
    
    mysql> select *from student;
    +----+-------------+------+-----+---------+
    | id | name        | age  | sex | classid |
    +----+-------------+------+-----+---------+
    |  1 | szh01       |   18 | m   |       1 |
    |  2 | daojian     |   34 | m   |       1 |
    |  3 | lainxisheng |   22 | m   |       1 |
    |  4 | luoshen     |   33 | w   |       2 |
    |  5 | sunziheng02 |   16 | m   |       2 |
    |  6 | yangmi      |   34 | w   |       3 |
    |  7 | nazha       |   28 | w   |       1 |
    +----+-------------+------+-----+---------+
    7 rows in set (0.00 sec)
    
    mysql> insert into student values(null,'szh03',25,'m',4),
        -> (null,'hongchenxue',38,'w',4),
        -> (null,'tianji',66,'m',4),
        -> (null,'dimin',44,'m',4),
        -> (null,'junfengtian',88,'m',4),
        -> (null,'feichangjun',55,'m',5),
        -> (null,'zhongshanjun',46,'m',5);
    Query OK, 7 rows affected (0.00 sec)
    Records: 7  Duplicates: 0  Warnings: 0
    
    mysql> seleect *from student;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'seleect *from student' at line 1
    mysql> select *from student;
    +----+--------------+------+-----+---------+
    | id | name         | age  | sex | classid |
    +----+--------------+------+-----+---------+
    |  1 | szh01        |   18 | m   |       1 |
    |  2 | daojian      |   34 | m   |       1 |
    |  3 | lainxisheng  |   22 | m   |       1 |
    |  4 | luoshen      |   33 | w   |       2 |
    |  5 | sunziheng02  |   16 | m   |       2 |
    |  6 | yangmi       |   34 | w   |       3 |
    |  7 | nazha        |   28 | w   |       1 |
    |  8 | szh03        |   25 | m   |       4 |
    |  9 | hongchenxue  |   38 | w   |       4 |
    | 10 | tianji       |   66 | m   |       4 |
    | 11 | dimin        |   44 | m   |       4 |
    | 12 | junfengtian  |   88 | m   |       4 |
    | 13 | feichangjun  |   55 | m   |       5 |
    | 14 | zhongshanjun |   46 | m   |       5 |
    +----+--------------+------+-----+---------+
    14 rows in set (0.00 sec)
    
    mysql> select name,age from where age >30;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where age >30' at line 1
    mysql> select name,age from student where age >30;
    +--------------+------+
    | name         | age  |
    +--------------+------+
    | daojian      |   34 |
    | luoshen      |   33 |
    | yangmi       |   34 |
    | hongchenxue  |   38 |
    | tianji       |   66 |
    | dimin        |   44 |
    | junfengtian  |   88 |
    | feichangjun  |   55 |
    | zhongshanjun |   46 |
    +--------------+------+
    9 rows in set (0.00 sec)
    
    mysql> select * from student where in(1,3,5,6,7);
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'in(1,3,5,6,7)' at line 1
    mysql> select * from student where id in(1,3,5,6,7);
    +----+-------------+------+-----+---------+
    | id | name        | age  | sex | classid |
    +----+-------------+------+-----+---------+
    |  1 | szh01       |   18 | m   |       1 |
    |  3 | lainxisheng |   22 | m   |       1 |
    |  5 | sunziheng02 |   16 | m   |       2 |
    |  6 | yangmi      |   34 | w   |       3 |
    |  7 | nazha       |   28 | w   |       1 |
    +----+-------------+------+-----+---------+
    5 rows in set (0.00 sec)
    
    mysql> select *from student where between 16 and 50;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'between 16 and 50' at line 1
    mysql> select *from student where age  between 16 and 50;
    +----+--------------+------+-----+---------+
    | id | name         | age  | sex | classid |
    +----+--------------+------+-----+---------+
    |  1 | szh01        |   18 | m   |       1 |
    |  2 | daojian      |   34 | m   |       1 |
    |  3 | lainxisheng  |   22 | m   |       1 |
    |  4 | luoshen      |   33 | w   |       2 |
    |  5 | sunziheng02  |   16 | m   |       2 |
    |  6 | yangmi       |   34 | w   |       3 |
    |  7 | nazha        |   28 | w   |       1 |
    |  8 | szh03        |   25 | m   |       4 |
    |  9 | hongchenxue  |   38 | w   |       4 |
    | 11 | dimin        |   44 | m   |       4 |
    | 14 | zhongshanjun |   46 | m   |       5 |
    +----+--------------+------+-----+---------+
    11 rows in set (0.00 sec)
    
    mysql> select * from student where like name sz%;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like name sz%' at line 1
    mysql> select * from student like name sz%;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like name sz%' at line 1
    mysql> select * from student like name 'sz%';
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like name 'sz%'' at line 1
    mysql> select * from student where like name 'sz%';
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like name 'sz%'' at line 1
    mysql> select * from student where like name '%sz';
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'like name '%sz'' at line 1
    mysql> select * from student where name like '%sz';
    Empty set (0.00 sec)
    
    mysql> select * from student where name like 'sz%';
    +----+-------+------+-----+---------+
    | id | name  | age  | sex | classid |
    +----+-------+------+-----+---------+
    |  1 | szh01 |   18 | m   |       1 |
    |  8 | szh03 |   25 | m   |       4 |
    +----+-------+------+-----+---------+
    2 rows in set (0.01 sec)
    
    mysql> select * from student where name like '%n';
    +----+--------------+------+-----+---------+
    | id | name         | age  | sex | classid |
    +----+--------------+------+-----+---------+
    |  2 | daojian      |   34 | m   |       1 |
    |  4 | luoshen      |   33 | w   |       2 |
    | 11 | dimin        |   44 | m   |       4 |
    | 12 | junfengtian  |   88 | m   |       4 |
    | 13 | feichangjun  |   55 | m   |       5 |
    | 14 | zhongshanjun |   46 | m   |       5 |
    +----+--------------+------+-----+---------+
    6 rows in set (0.00 sec)
    
    mysql> select * from student where name like '%n%';
    +----+--------------+------+-----+---------+
    | id | name         | age  | sex | classid |
    +----+--------------+------+-----+---------+
    |  2 | daojian      |   34 | m   |       1 |
    |  3 | lainxisheng  |   22 | m   |       1 |
    |  4 | luoshen      |   33 | w   |       2 |
    |  5 | sunziheng02  |   16 | m   |       2 |
    |  6 | yangmi       |   34 | w   |       3 |
    |  7 | nazha        |   28 | w   |       1 |
    |  9 | hongchenxue  |   38 | w   |       4 |
    | 10 | tianji       |   66 | m   |       4 |
    | 11 | dimin        |   44 | m   |       4 |
    | 12 | junfengtian  |   88 | m   |       4 |
    | 13 | feichangjun  |   55 | m   |       5 |
    | 14 | zhongshanjun |   46 | m   |       5 |
    +----+--------------+------+-----+---------+
    12 rows in set (0.00 sec)
    
    mysql> select * from student where name like '----';
    Empty set (0.01 sec)
    
    mysql> select * from student where name like '_____';
    +----+-------+------+-----+---------+
    | id | name  | age  | sex | classid |
    +----+-------+------+-----+---------+
    |  1 | szh01 |   18 | m   |       1 |
    |  7 | nazha |   28 | w   |       1 |
    |  8 | szh03 |   25 | m   |       4 |
    | 11 | dimin |   44 | m   |       4 |
    +----+-------+------+-----+---------+
    4 rows in set (0.00 sec)
    
    mysql> select *from student where name regexp '^[a-z0-9]{5}';
    +----+--------------+------+-----+---------+
    | id | name         | age  | sex | classid |
    +----+--------------+------+-----+---------+
    |  1 | szh01        |   18 | m   |       1 |
    |  2 | daojian      |   34 | m   |       1 |
    |  3 | lainxisheng  |   22 | m   |       1 |
    |  4 | luoshen      |   33 | w   |       2 |
    |  5 | sunziheng02  |   16 | m   |       2 |
    |  6 | yangmi       |   34 | w   |       3 |
    |  7 | nazha        |   28 | w   |       1 |
    |  8 | szh03        |   25 | m   |       4 |
    |  9 | hongchenxue  |   38 | w   |       4 |
    | 10 | tianji       |   66 | m   |       4 |
    | 11 | dimin        |   44 | m   |       4 |
    | 12 | junfengtian  |   88 | m   |       4 |
    | 13 | feichangjun  |   55 | m   |       5 |
    | 14 | zhongshanjun |   46 | m   |       5 |
    +----+--------------+------+-----+---------+
    14 rows in set (0.00 sec)
    
    mysql> select *from student where name regexp '^[a-z0-9]{5}$';
    +----+-------+------+-----+---------+
    | id | name  | age  | sex | classid |
    +----+-------+------+-----+---------+
    |  1 | szh01 |   18 | m   |       1 |
    |  7 | nazha |   28 | w   |       1 |
    |  8 | szh03 |   25 | m   |       4 |
    | 11 | dimin |   44 | m   |       4 |
    +----+-------+------+-----+---------+
    4 rows in set (0.00 sec)
    
    mysql> select max(age) from student;
    +----------+
    | max(age) |
    +----------+
    |       88 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> select min(age) from student;
    +----------+
    | min(age) |
    +----------+
    |       16 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> select max(age),min(age),avg(age) from student;
    +----------+----------+----------+
    | max(age) | min(age) | avg(age) |
    +----------+----------+----------+
    |       88 |       16 |  39.0714 |
    +----------+----------+----------+
    1 row in set (0.00 sec)
    
    mysql> select count(*) from student;
    +----------+
    | count(*) |
    +----------+
    |       14 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> delete from student where id = 7;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select count(*) from student;
    +----------+
    | count(*) |
    +----------+
    |       13 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> select age, count(*) from student;
    ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'mydb.student.age'; this is incompatible with sql_mode=only_full_group_by
    mysql> select  count(*) from student where age;
    +----------+
    | count(*) |
    +----------+
    |       13 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> select count(*) from student where sex='m';
    +----------+
    | count(*) |
    +----------+
    |       10 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> select *from student;
    +----+--------------+------+-----+---------+
    | id | name         | age  | sex | classid |
    +----+--------------+------+-----+---------+
    |  1 | szh01        |   18 | m   |       1 |
    |  2 | daojian      |   34 | m   |       1 |
    |  3 | lainxisheng  |   22 | m   |       1 |
    |  4 | luoshen      |   33 | w   |       2 |
    |  5 | sunziheng02  |   16 | m   |       2 |
    |  6 | yangmi       |   34 | w   |       3 |
    |  8 | szh03        |   25 | m   |       4 |
    |  9 | hongchenxue  |   38 | w   |       4 |
    | 10 | tianji       |   66 | m   |       4 |
    | 11 | dimin        |   44 | m   |       4 |
    | 12 | junfengtian  |   88 | m   |       4 |
    | 13 | feichangjun  |   55 | m   |       5 |
    | 14 | zhongshanjun |   46 | m   |       5 |
    +----+--------------+------+-----+---------+
    13 rows in set (0.00 sec)
    
    mysql> select count(*) from student where classid;
    +----------+
    | count(*) |
    +----------+
    |       13 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> select count(*) from student where classid=1;
    +----------+
    | count(*) |
    +----------+
    |        3 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> select count(*) from student where classid=2;
    +----------+
    | count(*) |
    +----------+
    |        2 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> select count(*) from student where classid=5;
    +----------+
    | count(*) |
    +----------+
    |        2 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> select sex from student group by sex;
    +-----+
    | sex |
    +-----+
    | w   |
    | m   |
    +-----+
    2 rows in set (0.00 sec)
    
    mysql> select classid from student group by classid;
    +---------+
    | classid |
    +---------+
    |       1 |
    |       2 |
    |       3 |
    |       4 |
    |       5 |
    +---------+
    5 rows in set (0.00 sec)
    
    mysql> select sex,count(*) from student group by sex;
    +-----+----------+
    | sex | count(*) |
    +-----+----------+
    | w   |        3 |
    | m   |       10 |
    +-----+----------+
    2 rows in set (0.00 sec)
    
    mysql> select count() from student;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') from student' at line 1
    mysql> select classid from student order by classid;
    +---------+
    | classid |
    +---------+
    |       1 |
    |       1 |
    |       1 |
    |       2 |
    |       2 |
    |       3 |
    |       4 |
    |       4 |
    |       4 |
    |       4 |
    |       4 |
    |       5 |
    |       5 |
    +---------+
    13 rows in set (0.00 sec)
    
    mysql> select classid,sex,count(*) from group by classid,sex;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group by classid,sex' at line 1
    mysql> select classid,sex,count(*) from student group by classid,sex;
    +---------+-----+----------+
    | classid | sex | count(*) |
    +---------+-----+----------+
    |       1 | m   |        3 |
    |       2 | w   |        1 |
    |       2 | m   |        1 |
    |       3 | w   |        1 |
    |       4 | w   |        1 |
    |       4 | m   |        4 |
    |       5 | m   |        2 |
    +---------+-----+----------+
    7 rows in set (0.00 sec)
    
    mysql> select *from student order by classid asc,age desc;
    +----+--------------+------+-----+---------+
    | id | name         | age  | sex | classid |
    +----+--------------+------+-----+---------+
    |  2 | daojian      |   34 | m   |       1 |
    |  3 | lainxisheng  |   22 | m   |       1 |
    |  1 | szh01        |   18 | m   |       1 |
    |  4 | luoshen      |   33 | w   |       2 |
    |  5 | sunziheng02  |   16 | m   |       2 |
    |  6 | yangmi       |   34 | w   |       3 |
    | 12 | junfengtian  |   88 | m   |       4 |
    | 10 | tianji       |   66 | m   |       4 |
    | 11 | dimin        |   44 | m   |       4 |
    |  9 | hongchenxue  |   38 | w   |       4 |
    |  8 | szh03        |   25 | m   |       4 |
    | 13 | feichangjun  |   55 | m   |       5 |
    | 14 | zhongshanjun |   46 | m   |       5 |
    +----+--------------+------+-----+---------+
    13 rows in set (0.00 sec)
    
    sunziheng@sunziheng-virtual-machine:~/mysqlFiles$ ls
    sunziheng@sunziheng-virtual-machine:~/mysqlFiles$ mysqldump -u root -p mydb >mydb.sql
    Enter password: 
    mysqldump: Got error: 1045: Access denied for user 'root'@'localhost' (using password: YES) when trying to connect
    sunziheng@sunziheng-virtual-machine:~/mysqlFiles$ mysqldump -u root -p mydb >mydb.sql
    Enter password: 
    sunziheng@sunziheng-virtual-machine:~/mysqlFiles$ ls
    mydb.sql
    sunziheng@sunziheng-virtual-machine:~/mysqlFiles$ vim mydb.sql 
    程序 'vim' 已包含在下列软件包中:
     * vim
     * vim-gnome
     * vim-tiny
     * vim-athena
     * vim-athena-py2
     * vim-gnome-py2
     * vim-gtk
     * vim-gtk-py2
     * vim-gtk3
     * vim-gtk3-py2
     * vim-nox
     * vim-nox-py2
    请尝试:sudo apt install <选定的软件包>
    sunziheng@sunziheng-virtual-machine:~/mysqlFiles$ vi mydb.sql 
    sunziheng@sunziheng-virtual-machine:~/mysqlFiles$ mysqldump -u root -p mydb tb01 >tb01.sql
    Enter password: 
    sunziheng@sunziheng-virtual-machine:~/mysqlFiles$ ls
    mydb.sql  tb01.sql
    sunziheng@sunziheng-virtual-machine:~/mysqlFiles$ vi tb01.sql 
    sunziheng@sunziheng-virtual-machine:~/mysqlFiles$ mysql -u root -p mydb <tb01.sq
    
    mysql> 
    mysql> 
    mysql> use mydb;
    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> show tables;
    +----------------+
    | Tables_in_mydb |
    +----------------+
    | classed        |
    | student        |
    | tb01           |
    | tb_date        |
    | tb_num         |
    | tb_str         |
    +----------------+
    6 rows in set (0.00 sec)
    
    mysql> 
    mysql> 
    mysql> \s
    --------------
    mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper
    
    Connection id:      25
    Current database:   mydb
    Current user:       root@localhost
    SSL:            Not in use
    Current pager:      stdout
    Using outfile:      ''
    Using delimiter:    ;
    Server version:     5.7.23-0ubuntu0.16.04.1 (Ubuntu)
    Protocol version:   10
    Connection:     Localhost via UNIX socket
    Server characterset:    latin1
    Db     characterset:    utf8
    Client characterset:    utf8
    Conn.  characterset:    utf8
    UNIX socket:        /var/run/mysqld/mysqld.sock
    Uptime:         2 days 23 hours 31 min 10 sec
    
    Threads: 1  Questions: 439  Slow queries: 0  Opens: 197  Flush tables: 1  Open tables: 85  Queries per second avg: 0.001
    --------------
    
    mysql> create database test02;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use test02;
    Database changed
    mysql> \s
    --------------
    mysql  Ver 14.14 Distrib 5.7.23, for Linux (x86_64) using  EditLine wrapper
    
    Connection id:      25
    Current database:   test02
    Current user:       root@localhost
    SSL:            Not in use
    Current pager:      stdout
    Using outfile:      ''
    Using delimiter:    ;
    Server version:     5.7.23-0ubuntu0.16.04.1 (Ubuntu)
    Protocol version:   10
    Connection:     Localhost via UNIX socket
    Server characterset:    latin1
    Db     characterset:    latin1
    Client characterset:    utf8
    Conn.  characterset:    utf8
    UNIX socket:        /var/run/mysqld/mysqld.sock
    Uptime:         2 days 23 hours 32 min 34 sec
    
    Threads: 1  Questions: 447  Slow queries: 0  Opens: 197  Flush tables: 1  Open tables: 85  Queries per second avg: 0.001
    

    相关文章

      网友评论

          本文标题:mysql操作

          本文链接:https://www.haomeiwen.com/subject/tichgftx.html