mysql有很多的可视化管理工具,比如“mysql-workbench”和“sequel-pro-”。 现在我写mysql的终端命令操作的文章,是想强化一下自己对于mysql的理解,总会比使用图形化的理解透彻,因为我本来就比较喜欢写代码。同时写出来这些文章,是想要给大家当个参考,希望也能对大家有所帮助,有所提升,这就是我为什么要写终端操作mysql的文章了。
注意:mysql数据库命令不区分大小写。但在mac的终端,如果你想使用tab自动补全命令,那么你就必须使用大写,这样mac的终端才会帮你补全命令,否则你按n遍tab都不会有响应。
1、数据库(database)管理1.1 create 创建数据库
create database firstdb;
1.2 show 查看所有数据库
mysql> show databases;
+--------------------+
| database |
+--------------------+
| information_schema |
| firstdb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
1.3 alter 修改数据库
alter 命令修改数据库编码:
默认创建的数据库默认不支持中文字符,如果我们需要它支持中文字符,则将它的编码设置为utf8格式:
mysql> alter database testdb character set utf8;
query ok, 1 row affected (0.00 sec)
1.4 use 使用数据库
mysql> use firstdb;
database changed
1.5 查看当前使用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| firstdb |
+------------+
1 row in set (0.00 sec)
1.6 drop 删除数据库
mysql> drop database firstdb;
query ok, 0 rows affected (0.00 sec)
2、数据表(table)管理我们首先创建一个数据库,提供我们往后的使用:
mysql> create database testdb;
query ok, 1 row affected (0.00 sec)
创建后记得用use命令进入(使用)数据库,不然后面的操作都会不成功的。
2.1 create 创建表
mysql> create table people (
-> id int auto_increment primary key,
-> name varchar(20) not null,
-> age int not null,
-> birthday datetime);
query ok, 0 rows affected (0.01 sec)
2.2 show 显示表
显示当前数据库所有的数据表
mysql> show tables;
+------------------+
| tables_in_testdb |
+------------------+
| people |
+------------------+
1 row in set (0.00 sec)
2.3 desc 查看表结构
mysql> desc people
-> ;
+----------+-------------+------+-----+---------+----------------+
| field | type | null | key | default | extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | no | pri | null | auto_increment |
| name | varchar(20) | no | | null | |
| age | int(11) | no | | null | |
| birthday | datetime | yes | | null | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
2.4 alter 修改表结构(增、删、改)
默认创建的表不支持中文字符,所以需将表编码设置为utf8:
mysql> alter table keychain convert to character set utf8;
query ok, 1 row affected (0.02 sec)
records: 1 duplicates: 0 warnings: 0
2.4.1 insert 在表中添加列(字段)
mysql> alter table people add star bool;
query ok, 0 rows affected (0.02 sec)
records: 0 duplicates: 0 warnings: 0
提示:在mysql里,布尔类型会自动转换为tinyint(1)类型。
我们不妨使用desc去查看一下people表结构:
mysql> desc people;
+----------+-------------+------+-----+---------+----------------+
| field | type | null | key | default | extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | no | pri | null | auto_increment |
| name | varchar(20) | no | | null | |
| age | int(11) | no | | null | |
| birthday | datetime | yes | | null | |
| star | tinyint(1) | yes | | null | |
+----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
现在,你该相信我了吧?
2.4.2 alter 修改表(列)字段
mysql> alter table people modify star int;
query ok, 0 rows affected (0.01 sec)
records: 0 duplicates: 0 warnings: 0
我们再次使用desc查看people表结构:
mysql> desc people;
+----------+-------------+------+-----+---------+----------------+
| field | type | null | key | default | extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | no | pri | null | auto_increment |
| name | varchar(20) | no | | null | |
| age | int(11) | no | | null | |
| birthday | datetime | yes | | null | |
| star | int(11) | yes | | null | |
+----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
2.4.3 delete 删除表(列)字段
mysql> alter table people drop column star;
query ok, 0 rows affected (0.02 sec)
records: 0 duplicates: 0 warnings: 0
删除后,再次查看people表结构:
mysql> desc people;
+----------+-------------+------+-----+---------+----------------+
| field | type | null | key | default | extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | no | pri | null | auto_increment |
| name | varchar(20) | no | | null | |
| age | int(11) | no | | null | |
| birthday | datetime | yes | | null | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
删除字段成功,现在我们已经不能看到star的字段了。
2.4.4 rename 重命名表名
mysql> rename table people to new_people;
query ok, 0 rows affected (0.00 sec)
2.5 create 利用已有数据创建新表
mysql> create table newtable select * from people;
query ok, 0 rows affected (0.01 sec)
records: 0 duplicates: 0 warnings: 0
我们查看一下目前数据库存在的表:
mysql> show tables;
+------------------+
| tables_in_testdb |
+------------------+
| people |
| newtable |
+------------------+
2 rows in set (0.00 sec)
3、数据的操作及管理数据表的基本操作,包含增、删、改、查数据。
以下命令均在people表上操作。
3.1 增加数据(增)
people表目前是没有数据的,它是空的数据表,我们现在先添加一些数据。
insert into 命令添加数据:
mysql> insert into people values (null, 'anny', 22, '1992-05-22');
query ok, 1 row affected (0.00 sec)
使用select命令查看表(会在后面介绍),现在我们查看people数据表的数据:
mysql> select * from people;
+----+------+-----+---------------------+
| id | name | age | birthday |
+----+------+-----+---------------------+
| 1 | anny | 22 | 1992-05-22 00:00:00 |
+----+------+-----+---------------------+
1 row in set (0.00 sec)
数据表现在有一条数据。
我们多添加几条数据,如:
mysql> select * from people;
+----+--------+-----+---------------------+
| id | name | age | birthday |
+----+--------+-----+---------------------+
| 1 | anny | 22 | 1992-05-22 00:00:00 |
| 2 | garvey | 23 | 1991-05-22 00:00:00 |
| 3 | lisa | 25 | 1989-05-22 00:00:00 |
| 4 | nick | 24 | 1990-05-22 00:00:00 |
| 5 | rick | 24 | 1991-05-22 00:00:00 |
+----+--------+-----+---------------------+
5 rows in set (0.00 sec)
3.2 删除数据(删)
delete 命令删除数据:
mysql> delete from people where name = 'lisa';
query ok, 1 row affected (0.01 sec)
再次查询people表:
mysql> select * from people;
+----+--------+-----+---------------------+
| id | name | age | birthday |
+----+--------+-----+---------------------+
| 1 | anny | 22 | 1992-05-22 00:00:00 |
| 2 | garvey | 23 | 1991-05-22 00:00:00 |
| 4 | nick | 24 | 1990-05-22 00:00:00 |
| 5 | rick | 24 | 1991-05-22 00:00:00 |
+----+--------+-----+---------------------+
4 rows in set (0.00 sec)
已经看不到名为“lisa”的数据了。
3.3 修改数据(改)
update 命令修改数据:
mysql> update people set name='calvin' where name = 'garvey';
query ok, 1 row affected (0.00 sec)
rows matched: 1 changed: 1 warnings: 0
查询people表内容:
mysql> select * from people;
+----+--------+-----+---------------------+
| id | name | age | birthday |
+----+--------+-----+---------------------+
| 1 | anny | 22 | 1992-05-22 00:00:00 |
| 2 | calvin | 23 | 1991-05-22 00:00:00 |
| 4 | nick | 24 | 1990-05-22 00:00:00 |
| 5 | rick | 24 | 1991-05-22 00:00:00 |
+----+--------+-----+---------------------+
4 rows in set (0.00 sec)
名为“garvey”的记录已经修改为“calvin”。
3.4 查询数据(查)
select 命令查询数据,最简单的就是查询表的所有数据,也就是我们最初使用到的那条命令:
mysql> select * from people;
+----+--------+-----+---------------------+
| id | name | age | birthday |
+----+--------+-----+---------------------+
| 1 | anny | 22 | 1992-05-22 00:00:00 |
| 2 | calvin | 23 | 1991-05-22 00:00:00 |
| 4 | nick | 24 | 1990-05-22 00:00:00 |
| 5 | rick | 24 | 1991-05-22 00:00:00 |
+----+--------+-----+---------------------+
4 rows in set (0.00 sec)
格式:select * from <表名>,*代表所有字段。
查询数据时也可指定显示的(列)字段:
mysql> select name, age, birthday from people;
+--------+-----+---------------------+
| name | age | birthday |
+--------+-----+---------------------+
| anny | 22 | 1992-05-22 00:00:00 |
| calvin | 23 | 1991-05-22 00:00:00 |
| nick | 24 | 1990-05-22 00:00:00 |
| rick | 24 | 1991-05-22 00:00:00 |
+--------+-----+---------------------+
4 rows in set (0.00 sec)
格式:select b1fbf6fe5b2012d82009bbcec12b9c40 from a26d98d33123a70024fa8ba5642906c6。
select查询命令还有很多的高级用法,比如用来查找不重复(distinct)的数据,使数据按条件排序(order by),按查询条件显示数据(where)等等。这些都会在下一篇文章作重点介绍,请大家继续留意我的博客,谢谢。
以上就是mysql终端管理数据库操作指南的内容。