mysql客户端工具


一、登录数据库

在此就不多叙述了,mysql安装教程

二、输入查询

这是一个简单的命令,要求服务器告诉它的版本号和当前日期。在mysql>提示输入如下命令并按回车键:

查询mysql版本号,当前日期 select version(),current_date;

* mysql> SELECT
    -> USER()
    -> ,
    -> CURRENT_DATE;
+---------------+--------------+
| USER()        | CURRENT_DATE |
+---------------+--------------+
| jon@localhost | 2005-10-11   |
+---------------+--------------+

如果你决定不想执行正在输入过程中的一个命令,输入/c取消它:

* mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

三、创建并使用数据库

使用SHOW语句找出服务器上当前存在什么数据库:

显示数据库 show databases;

* mysql> use test
Database changed

创建数据库并使用 create database student; use student

* mysql> use student
Database changed

数据库只需要创建一次,但是必须在每次启动mysql会话时在使用前先选择它。你可以根据上面的例子执行一个USE语句来实现。还可以在调用mysql时,通过命令行选择数据库,只需要在提供连接参数之后指定数据库名称。例如:

* mysql> show tables;
Empty set (0.00 sec)
***

创建表

使用一个CREATE TABLE语句指定你的数据库表的布局:

* mysql> describe student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
| sex   | char(1)     | YES  |     | NULL    |       |
| birth | date        | YES  |     | NULL    |       |
| death | date        | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

插入一条数据

 mysql> select * from student;
+------+------+------+------------+-------+
| name | age  | sex  | birth      | death |
+------+------+------+------------+-------+
| hsy  |   18 | f    | 1999-09-09 | NULL  |
+------+------+------+------------+-------+
1 row in set (0.00 sec)

选择特殊行

mysql> select * from student where birth>'1999-1-1'and sex='f';
+------+------+------+------------+-------+
| name | age  | sex  | birth      | death |
+------+------+------+------------+-------+
| hsy  |   18 | f    | 1999-09-09 | NULL  |
+------+------+------+------------+-------+
1 row in set (0.00 sec)

选择特殊列

mysql> select distinct name from student;
+------+
| name |
+------+
| hsy  |
+------+
1 row in set (0.00 sec)

分类行

mysql> select name ,birth, curdate(),
    -> (year(curdate())-year(birth))-(right(curdate(),5)(birth,5)) as age
    -> from student;
+------+------------+------------+------+
| name | birth      | curdate()  | age  |
+------+------------+------------+------+
| hsy  | 1999-09-09 | 2017-06-07 |   17 |
| hsy  | 1999-09-09 | 2017-06-07 |   17 |
| hsy  | 1989-09-09 | 2017-06-07 |   27 |
| hh   | 1994-03-04 | 2017-06-07 |   23 |
| hh   | 1994-03-04 | 2017-06-07 |   23 |
| hh   | 1994-03-04 | 2017-06-07 |   23 |
| hh   | 1994-03-04 | 2017-06-07 |   23 |
| hh   | 1997-03-04 | 2017-06-07 |   20 |
| hh   | 1997-03-04 | 2017-06-07 |   20 |
| hrr  | 1997-03-04 | 2017-06-07 |   20 |
| rrrr | 1997-03-04 | 2017-06-07 |   20 |
| rrrr | 1997-03-04 | 2017-06-07 |   20 |
| rrrr | 1997-03-04 | 2017-06-07 |   20 |
| hh   | 1997-03-04 | 2017-06-07 |   20 |
| hh   | 1997-03-04 | 2017-06-07 |   20 |
+------+------------+------------+------+
15 rows in set (0.00 sec)

此处,YEAR()提取日期的年部分,RIGHT()提取日期的MM-DD (日历年)部分的最右面5个字符。比较MM-DD值的表达式部分的值一般为1或0,如果CURDATE()的年比birth的年早,则年份应减去1。整个表达式有些难懂,使用alias (age)来使输出的列标记更有意义。

找出下个月生日的student也是容易的。假定当前月是8月,那么月值是8,你可以找在9月出生的动物 (9月),方法是:

mysql> select name ,birth
    -> from student
    -> where month(birth)=month(date_add(curdate(),interval 1 month));
+----------+------------+
| name     | birth      |
+----------+------------+
| xiaomimg | 1997-07-04 |
| hong     | 1997-07-08 |
+----------+------------+
2 rows in set (0.00 sec)

完成该任务的另一个方法是加1以得出当前月份的下一个月(在使用取模函数(MOD)后,如果月份当前值是12,则“回滚”到值0):mysql select

mysql> select name , death is null from student;
+----------+---------------+
| name     | death is null |
+----------+---------------+
| hsy      |             1 |
| hsy      |             1 |
| hsy      |             1 |
| hh       |             0 |
| hh       |             0 |
| hh       |             0 |
| hh       |             0 |
| hh       |             0 |
| hh       |             0 |
| hrr      |             0 |
| rrrr     |             0 |
| rrrr     |             0 |
| rrrr     |             0 |
| hh       |             0 |
| hh       |             0 |
| xiaomimg |             0 |
| hong     |             0 |
+----------+---------------+
17 rows in set (0.00 sec)

mysql> select * from student where name like '%h%';
+------+------+------+------------+------------+
| name | age  | sex  | birth      | death      |
+------+------+------+------------+------------+
| hsy  |   18 | f    | 1999-09-09 | NULL       |
| hsy  |   18 | m    | 1999-09-09 | NULL       |
| hsy  |   18 | m    | 1989-09-09 | NULL       |
| hh   |   19 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   19 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hrr  |   29 | f    | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hong |   29 | 女   | 1997-07-08 | 1992-02-01 |
+------+------+------+------------+------------+
13 rows in set (0.00 sec)

长度是三个的

mysql> select * from student where name regexp '^h';
+------+------+------+------------+------------+
| name | age  | sex  | birth      | death      |
+------+------+------+------------+------------+
| hsy  |   18 | f    | 1999-09-09 | NULL       |
| hsy  |   18 | m    | 1999-09-09 | NULL       |
| hsy  |   18 | m    | 1989-09-09 | NULL       |
| hh   |   19 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   19 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hrr  |   29 | f    | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hong |   29 | 女   | 1997-07-08 | 1992-02-01 |
+------+------+------+------------+------------+
13 rows in set (0.00 sec)

以g结尾(like ''%g')


mysql> select * from student where name regexp 'h';
+------+------+------+------------+------------+
| name | age  | sex  | birth      | death      |
+------+------+------+------------+------------+
| hsy  |   18 | f    | 1999-09-09 | NULL       |
| hsy  |   18 | m    | 1999-09-09 | NULL       |
| hsy  |   18 | m    | 1989-09-09 | NULL       |
| hh   |   19 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   19 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1994-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hrr  |   29 | f    | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hong |   29 | 女   | 1997-07-08 | 1992-02-01 |
+------+------+------+------------+------------+
13 rows in set (0.00 sec)

为了找出包含正好3个字符的名字,使用“^”和“$”匹配名字的开始和结尾,和3个“.”实例在两者之间:
(like '_ _ _')

mysql> select count(*) from student ;
+----------+
| count(*) |
+----------+
|       17 |
+----------+
1 row in set (0.00 sec)

COUNT( )和GROUP BY以各种方式分类你的数据。下列例子显示出进行动物普查操作的不同方式。

mysql> select student.name ,birth
    -> from student ,event
    -> where student.name=event.name;
+------+------------+
| name | birth      |
+------+------------+
| hsy  | 1999-09-09 |
| hsy  | 1999-09-09 |
| hsy  | 1999-09-09 |
| hsy  | 1999-09-09 |
| hsy  | 1999-09-09 |
| hsy  | 1999-09-09 |
| hsy  | 1989-09-09 |
| hsy  | 1989-09-09 |
| hsy  | 1989-09-09 |
| hong | 1997-07-08 |
| hong | 1997-07-08 |
| hong | 1997-07-08 |
+------+------------+

关于该查询要注意的几件事情:

  • FROM子句列出两个表,因为查询需要从两个表提取信息。
  • 当从多个表组合(联结)信息时,你需要指定一个表中的记录怎样能匹配其它表的记录。这很简单,因为它们都有一个name列。查询使用WHERE子句基于name值来匹配2个表中的记录。
  • 因为name列出现在两个表中,当引用列时,你一定要指定哪个表。把表名附在列名前即可以实现。

六、获得数据库和表的信息

你已经见到了SHOW DATABASES,它列出由服务器管理的数据库。为了找出当前选择了哪个数据库,使用DATABASE( )函数:

当前使用数据库

mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| event             |
| student           |
+-------------------+
2 rows in set (0.00 sec)

显示表结构

如果你想要知道一个表的结构,可以使用DESCRIBE命令;它显示表中每个列的信息:

mysql> create table shop(
    -> article int(4)unsigned zerofill default'0000'not null,
    -> dealer char(20) default''not null,
    -> price double(16,2) default '0.00' not null,
    -> primary key (article,dealer));
Query OK, 0 rows affected (0.04 sec)

插入数据

mysql> select * from shop;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|    0001 | A      |  3.45 |
|    0001 | B      |  3.99 |
|    0002 | A      |  5.22 |
|    0002 | B      | 10.33 |
|    0003 | C      |  1.56 |
|    0003 | D      |  1.23 |
+---------+--------+-------+
6 rows in set (0.00 sec)

列的最大值

任务:最大的物品号是什么?”


mysql> select article ,dealer,price
    -> from shop
    -> where price=(select max(price) from shop);
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|    0002 | B      | 10.33 |
+---------+--------+-------+
1 row in set (0.02 sec)

另一种方案:解决方案是按价格降序排序所有行并用MySQL特定LIMIT子句只得到第一行:

mysql> select article ,max(price) as price
    -> from shop
    -> group by article;
+---------+-------+
| article | price |
+---------+-------+
|    0001 |  3.99 |
|    0002 | 10.33 |
|    0003 |  1.56 |
+---------+-------+
3 rows in set (0.00 sec)

拥有某个字段的组间最大值的行

任务:对每项物品,找出最贵价格的物品的经销商。

mysql> select @min_price:=min(price),@max_price:=max(price) from shop;
+------------------------+------------------------+
| @min_price:=min(price) | @max_price:=max(price) |
+------------------------+------------------------+
|                   1.23 |                  10.33 |
+------------------------+------------------------+
1 row in set (0.00 sec)
CREATE TABLE person (
    id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    name CHAR(60) NOT NULL,
    PRIMARY KEY (id)
);
 
CREATE TABLE shirt (
    id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
    color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,
    owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),
    PRIMARY KEY (id)
);
 
INSERT INTO person VALUES (NULL, 'Antonio Paz');
 
SELECT @last := LAST_INSERT_ID();
 
INSERT INTO shirt VALUES
(NULL, 'polo', 'blue', @last),
(NULL, 'dress', 'white', @last),
(NULL, 't-shirt', 'blue', @last);
 
INSERT INTO person VALUES (NULL, 'Lilliana Angelovska');
 
SELECT @last := LAST_INSERT_ID();
 
INSERT INTO shirt VALUES
(NULL, 'dress', 'orange', @last),
(NULL, 'polo', 'red', @last),
(NULL, 'dress', 'blue', @last),
(NULL, 't-shirt', 'white', @last);
 
 select * from person p,shirt s
    -> where p.id=s.owner and p.name like 'Anton%'and s.color<>'white';
+----+-------------+----+---------+-------+-------+
| id | name        | id | style   | color | owner |
+----+-------------+----+---------+-------+-------+
|  1 | Antonio Paz |  1 | polo    | blue  |     1 |
|  1 | Antonio Paz |  3 | t-shirt | blue  |     1 |
+----+-------------+----+---------+-------+-------+
2 rows in set (0.00 sec)

根据两个键搜索

每个SELECT只搜索一个关键字,可以进行优化:

CREATE TABLE t1 (year YEAR(4), month INT(2) UNSIGNED ZEROFILL,
             day INT(2) UNSIGNED ZEROFILL);
INSERT INTO t1 VALUES(2000,1,1),(2000,1,20),(2000,1,30),(2000,2,2),
            (2000,2,23),(2000,2,23);
mysql> select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
+------+-------+------+

示例表中含有代表用户访问网页的年-月-日值。可以使用以下查询来确定每个月的访问天数:

+------+-------+------+
| year | month | days |
+------+-------+------+
| 2000 |    01 |    3 |
| 2000 |    02 |    2 |
+------+-------+------+

使用AUTO_INCREMENT

可以通过AUTO_INCREMENT属性为新的行产生唯一的标识:

+----+---------+
| id | name    |
+----+---------+
|  1 | dog     |
|  2 | cat     |
|  3 | penguin |
|  4 | lax     |
|  5 | whale   |
|  6 | ostrich |
+----+---------+
SELECT * FROM animals ORDER BY grp,id;

将返回

+--------+----+---------+
| grp    | id | name    |
+--------+----+---------+
| fish   |  1 | lax     |
| mammal |  1 | dog     |
| mammal |  2 | cat     |
| mammal |  3 | whale   |
| bird   |  1 | penguin |
| bird   |  2 | ostrich |
+--------+----+---------+

MySql可视化工具MySQL Workbench使用教程

1. MySQL Workbench

MySQL Workbench 为数据库管理员、程序开发者和系统规划师提供可视化的Sql开发、数据库建模、以及数据库管理功能。

   

2.MySQL Workbench 的下载和安装

(1)安装最新MySql时,有是否安装MySql Workbench的选项,可选择安装。
(2)可以独立安装MySql Workbench。官方下载地址:http://dev.mysql.com/downloads/workbench/ 安装很简单,基本就是一路Next。

3.MySQL Workbench的功能使用

功能界面:


   

分为三个主要功能模块:Sql Development(Sql开发 相当于Sql2000中的查询分析器), Data Modeling(数据库建模), Server Administration(服务器管理 相当于Sql2000中的企业管理器)

(1) Sql Development的使用

   

对应的操作分别是:Connection列表(供选择已经建好的数据库连接),新建一个Connection,编辑数据库表,编辑SQL脚本,Connections管理
点击New Connection 会弹出如下操作界面

   

输入服务器的名称,端口,用户名,和密码 即可。
连接后的操作界面如下:

   

具体操作SQL2005 SQL2008中的差不多,这里不再描述。

(2) Data Modeling的使用

Workbench中的数据库建模我还没有用到 这里略过 以后用到了再补充上

(3)Server Administration的使用

   

对应的功能分别是:服务器实例列表,新建一个服务实例,数据库的导入导出,安全管理,服务器列表管理
创建一个服务实例,创建的操作和Sql Development中的创建Connection一样 输入服务器的名称,端口,用户名,和密码 即可。

   

创建进入服务实例管理的功能界面如下:


   

Management中的功能主要有:

  • 查看服务器状态,包括 连接数量, CUP使用率等

  • 开启关闭服务器实例 可以开启或关闭服务器实例,查看运行日志

  • 查看服务实例日志 包括存储日志,错误日志,通知日志 等

  • Configuration 服务器配置 这里的功能我还没有研究 略过

  • Security 服务实例安全 这里设置用户权限,角色,架构 和MS SQL的安全一样

  • Data Export/Restore 数据库的导入导出和恢复功能

数据导出的操作:

      Paste_Image.png

可以选择要导出的数据库和数据表,已经导出选项。这里的导出选项有 导入到一个文件夹中每个表对应一个sql脚本文件还是所有表导入到一个sql文件中,是否丢弃存储过程,是否丢弃Event定时器,是否清空数据

数据导入操作:

       

数据导入操作只有两个选择 一是导入一个文件夹中的所有Sql脚本 还是导入一个单独的Sql脚文件 (分别对应导出的两个选项)



作者:流水潺湲
链接:https://www.jianshu.com/p/ebc027856895
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关