MySQL命令行测试基础SQL

说明

用命令行能增强初学者编写SQL的准确度和熟练度,一旦发现输错了,确保命令是错的,输入英文分号,直接会命令报错从而重新输入。

登录

输入用户名和密码即可登录(免密除外)

mysql -u (your username such as 'root') -p
Enter password: ***************(your password)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jindu              |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
6 rows in set (0.75 sec)

删库

由于我们需要重新使用test这个数据库,而里面确实乏善可陈,所以删了重建。

mysql> drop database test;
Query OK, 0 rows affected (0.21 sec)

接下来重新查库,发现少了test库。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jindu              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

建库

删库是为了重建,那就建一下:

mysql> create database test;
Query OK, 1 row affected (0.17 sec)

再查库,发现test库出现了。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jindu              |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
6 rows in set (0.00 sec)

进入test数据库

mysql使用use命令可以进入指定数据库。

mysql> use test;
Database changed

接下来的操作就在test库中展开啦!

查库中所有的表

新库显然无表,查到空集。

mysql> show tables;
Empty set (0.00 sec)

库中建表

没有表,就建立一个表呗,使用create可以建表,名为stu_info,因为接下来的数据是学生信息。

mysql> create table stu_info
    -> (id int primary key not null,
    -> name varchar(10) not null,
    -> age int not null,
    -> email varchar(20),
    -> country varchar(10),
    -> gpa float(4) not null);
Query OK, 0 rows affected (0.93 sec)

再查表,发现有了stu_info这张表。

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| stu_info       |
+----------------+
1 row in set (0.41 sec)

全表查询

select * 虽说在高级应用中是低效的,但对于初学者也是必须掌握的,可查全表信息并返回。
由于我们刚建的表是空表,所以返回空集。

mysql> select * from stu_info;
Empty set (0.36 sec)

表中插入一行

在表中插入一行信息(一个元组)

mysql> insert into stu_info
    -> values('1', 'XiaoMing', '19', '[email protected]', 'China', '3.5945');
Query OK, 1 row affected (0.41 sec)

我们查一下全表:

mysql> select * from stu_info;
+----+----------+-----+------------------+---------+--------+
| id | name     | age | email            | country | gpa    |
+----+----------+-----+------------------+---------+--------+
|  1 | XiaoMing |  19 | 123456789@qq.com | China   | 3.5945 |
+----+----------+-----+------------------+---------+--------+
1 row in set (0.00 sec)

确实有一行数据了。

接下来再插入四行:

mysql> insert into stu_info
    -> values('2', 'Bob', '20', '[email protected]', 'USA', '3.9047');
Query OK, 1 row affected (0.41 sec)

mysql> insert into stu_info
    -> values('3', 'Steven', '21', '[email protected]', 'UK', '2.5633');
Query OK, 1 row affected (0.41 sec)

mysql> insert into stu_info
    -> values('4', 'LiHua', '20', '[email protected]', 'China', '4.9999');
Query OK, 1 row affected (0.44 sec)

mysql> insert into stu_info
    -> values('5', 'Amy', '19', '[email protected]', 'China', '4.1000');
Query OK, 1 row affected (0.40 sec)

我们查一下全表:

mysql> select * from stu_info;
+----+----------+-----+---------------------+---------+--------+
| id | name     | age | email               | country | gpa    |
+----+----------+-----+---------------------+---------+--------+
|  1 | XiaoMing |  19 | 123456789@qq.com    | China   | 3.5945 |
|  2 | Bob      |  20 | bobbob@gmail.com    | USA     | 3.9047 |
|  3 | Steven   |  21 | steven521@gmail.com | UK      | 2.5633 |
|  4 | LiHua    |  20 | lihua@thu.stu.com   | China   | 4.9999 |
|  5 | Amy      |  19 | 20205205@163.com    | China   |    4.1 |
+----+----------+-----+---------------------+---------+--------+
5 rows in set (0.00 sec)

OK,五行信息插入完了。

更改表中一条记录的信息

使用update可以改表中的记录:

mysql> update stu_info
    -> set gpa='1.8888'
    -> where name='Steven';
Query OK, 1 row affected (0.44 sec)
Rows matched: 1  Changed: 1  Warnings: 0

我们查一下全表:

mysql> select * from stu_info;
+----+----------+-----+---------------------+---------+--------+
| id | name     | age | email               | country | gpa    |
+----+----------+-----+---------------------+---------+--------+
|  1 | XiaoMing |  19 | 123456789@qq.com    | China   | 3.5945 |
|  2 | Bob      |  20 | bobbob@gmail.com    | USA     | 3.9047 |
|  3 | Steven   |  21 | steven521@gmail.com | UK      | 1.8888 |
|  4 | LiHua    |  20 | lihua@thu.stu.com   | China   | 4.9999 |
|  5 | Amy      |  19 | 20205205@163.com    | China   |    4.1 |
+----+----------+-----+---------------------+---------+--------+
5 rows in set (0.00 sec)

可怜的Steven啊,他的GPA降到1.8888了,太惨了orz

查询结果排序

使用 order by,查询结果排序可以是升序或者降序,默认是升序(升序是指从上到下依次增大,降序反是):

mysql> select * from stu_info
    -> order by gpa;
+----+----------+-----+---------------------+---------+--------+
| id | name     | age | email               | country | gpa    |
+----+----------+-----+---------------------+---------+--------+
|  3 | Steven   |  21 | steven521@gmail.com | UK      | 1.8888 |
|  1 | XiaoMing |  19 | 123456789@qq.com    | China   | 3.5945 |
|  2 | Bob      |  20 | bobbob@gmail.com    | USA     | 3.9047 |
|  5 | Amy      |  19 | 20205205@163.com    | China   |    4.1 |
|  4 | LiHua    |  20 | lihua@thu.stu.com   | China   | 4.9999 |
+----+----------+-----+---------------------+---------+--------+
5 rows in set (0.00 sec)

加上desc,结果呈现降序:

mysql> select * from stu_info
    -> order by gpa desc;
+----+----------+-----+---------------------+---------+--------+
| id | name     | age | email               | country | gpa    |
+----+----------+-----+---------------------+---------+--------+
|  4 | LiHua    |  20 | lihua@thu.stu.com   | China   | 4.9999 |
|  5 | Amy      |  19 | 20205205@163.com    | China   |    4.1 |
|  2 | Bob      |  20 | bobbob@gmail.com    | USA     | 3.9047 |
|  1 | XiaoMing |  19 | 123456789@qq.com    | China   | 3.5945 |
|  3 | Steven   |  21 | steven521@gmail.com | UK      | 1.8888 |
+----+----------+-----+---------------------+---------+--------+
5 rows in set (0.00 sec)

查询结果只返回部分属性

把 select * 的 “*”换成指定的属性,可以有很多并用逗号分隔,这里还加了排序:

mysql> select id, name, gpa from stu_info
    -> order by gpa desc;
+----+----------+--------+
| id | name     | gpa    |
+----+----------+--------+
|  4 | LiHua    | 4.9999 |
|  5 | Amy      |    4.1 |
|  2 | Bob      | 3.9047 |
|  1 | XiaoMing | 3.5945 |
|  3 | Steven   | 1.8888 |
+----+----------+--------+
5 rows in set (0.00 sec)

使用where查询符合指定条件的数据

mysql> select id, name, gpa from stu_info
    -> where gpa>3.5
    -> order by gpa desc;
+----+----------+--------+
| id | name     | gpa    |
+----+----------+--------+
|  4 | LiHua    | 4.9999 |
|  5 | Amy      |    4.1 |
|  2 | Bob      | 3.9047 |
|  1 | XiaoMing | 3.5945 |
+----+----------+--------+
4 rows in set (0.00 sec)

where条件相当于编程语言中的boolean值,可使用and表示且、or表示或。

下面的条件是且,查询结果会少一些:

mysql> select id, name, gpa from stu_info
    -> where gpa>3.5 and id>2
    -> order by gpa desc;
+----+-------+--------+
| id | name  | gpa    |
+----+-------+--------+
|  4 | LiHua | 4.9999 |
|  5 | Amy   |    4.1 |
+----+-------+--------+
2 rows in set (0.00 sec)

下面是或,查询结果会多一些:

mysql> select id, name, gpa from stu_info
    -> where gpa>3.5 or id>2
    -> order by gpa desc;
+----+----------+--------+
| id | name     | gpa    |
+----+----------+--------+
|  4 | LiHua    | 4.9999 |
|  5 | Amy      |    4.1 |
|  2 | Bob      | 3.9047 |
|  1 | XiaoMing | 3.5945 |
|  3 | Steven   | 1.8888 |
+----+----------+--------+
5 rows in set (0.00 sec)

查询结果去重

select后加上distinct,可以实现查询去重。

这里简单的选择了country一个属性,数据库所有的country属性只有China、USA、UK三种,China是有重复的,使用distinct可以做到查询结果去重:

mysql> select distinct country from stu_info;
+---------+
| country |
+---------+
| China   |
| USA     |
| UK      |
+---------+
3 rows in set (0.00 sec)

从表中删除一行

delete可以删一行数据,这里指定的where条件是id=3(别忘了使用引号),可删除id=3的一行数据:

mysql> delete from stu_info
    -> where id='3';
Query OK, 1 row affected (0.41 sec)
mysql> select * from stu_info;
+----+----------+-----+-------------------+---------+--------+
| id | name     | age | email             | country | gpa    |
+----+----------+-----+-------------------+---------+--------+
|  1 | XiaoMing |  19 | 123456789@qq.com  | China   | 3.5945 |
|  2 | Bob      |  20 | bobbob@gmail.com  | USA     | 3.9047 |
|  4 | LiHua    |  20 | lihua@thu.stu.com | China   | 4.9999 |
|  5 | Amy      |  19 | 20205205@163.com  | China   |    4.1 |
+----+----------+-----+-------------------+---------+--------+
4 rows in set (0.00 sec)

清空数据表

如果不用where做限定,就会把整个表的数据全删了,但表的结构还保留。

因为表在建立之初就定好了结构,所以表还在,哪怕是空的,也还保留了结构。

delete * 是不合语法规范的,至少不合mysql的规范,既不能清表也不能删表:

mysql> delete * from stu_info;
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 stu_info' at line 1

真想删就别加*:

mysql> delete from stu_info;
Query OK, 4 rows affected (0.43 sec)

删完了可以查全表,真的空了:

mysql> select * from stu_info;
Empty set (0.00 sec)

删表

看一眼库中所有的表,果然这个表只是空了,表本身还在:

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| stu_info       |
+----------------+
1 row in set (0.00 sec)

接下来这个表没啥用了,删掉吧,反正只是一个实验罢了,drop才是正解:

mysql> drop table stu_info;
Query OK, 0 rows affected (0.53 sec)

重新查库中全表:

mysql> show tables;
Empty set (0.00 sec)

确实空了,表删没了。

再删库

查一下,六个库:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jindu              |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
6 rows in set (0.00 sec)

使用drop把test库删掉:

mysql> drop database test;
Query OK, 0 rows affected (0.41 sec)

再查查所有的库:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| jindu              |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

OK删掉了。

退出

退出有多种方式,反正\q是可以退出的啦:

mysql> \q
Bye

总结

本文带领读者使用命令行运行MySQL,执行了基础的SQL增删改查语句,还完成了登录退出、建库删库、建表删表的操作,还望能带领初学者快速入门SQL。

发布了645 篇原创文章 · 获赞 1334 · 访问量 57万+

猜你喜欢

转载自blog.csdn.net/weixin_43896318/article/details/104615308