mysql客户端工具

一、登录数据库

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

二、输入查询

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

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

  mysql> select version(),current_date;
+------------+--------------+
| version()  | current_date |
+------------+--------------+
| 5.7.17-log | 2017-06-07   |
+------------+--------------+
1 row in set (0.00 sec)

不必全在一个行内给出一个命令,较长命令可以输入到多个行中。mysql通过寻找终止分号而不是输入行的结束来决定语句在哪儿结束。(换句话说,mysql接受自由格式的输入:它收集输入行但直到看见分号才执行。)
这里是一个简单的多行语句的例子:mysql连接方式

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

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

* mysql> SELECT
    -> USER()
    -> /c

下表显示出可以看见的各个提示符并简述它们所表示的mysql的状态:
提示符 含义
mysql> 准备好接受新命令。
-> 等待多行命令的下一行。
'> 等待下一行,等待以单引号(“'”)开始的字符串的结束。
"> 等待下一行,等待以双引号(“"”)开始的字符串的结束。
> 等待下一行,等待以反斜点(‘’)开始的识别符的结束。
/> 等待下一行,等待以/开始的注释的结束。


查询用户 select user();

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

三、创建并使用数据库

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

显示数据库 show databases;

* mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| test               |
| world              |
+--------------------+
7 rows in set (0.00 sec)

如果test数据库存在,尝试访问它:

使用数据库 use test

* mysql> use test
Database changed

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

* mysql> create database student;
Query OK, 1 row affected (0.01 sec)

创建数据库并不表示选定并使用它,你必须明确地操作。为了使student成为当前的数据库,使用这个命令:

* mysql> use student
Database changed

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

* shell> mysql -h host -u user -p menagerie
Enter password: ********

四、创建表

创建数据库是很容易的部分,但是在这时它是空的,正如SHOW TABLES将告诉你的:

显示表 show tables;

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

创建表

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

* mysql> create table student(
   -> name varchar(20),
   -> age int,
   -> sex char(1),
   -> birth date,
   -> death date);
Query OK, 0 rows affected (0.07 sec)

显示表结构 describe student;

为了验证你的表是按你期望的方式创建,使用一个DESCRIBE语句:

* 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> insert into student
    -> values('hsy',18,'f','1999-09-09',null);
Query OK, 1 row affected (0.01 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 name='hsy';
+------+------+------+------------+-------+
| 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 name from student;
+------+
| name |
+------+
| hsy  |
| hsy  |
| hsy  |
+------+
3 rows in set (0.00 sec)

请注意该查询只是简单地检索每个记录的owner列,并且他们中的一些出现多次。为了使输出减到最少,增加关键字DISTINCT检索出每个唯一的输出记录

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

分类行

mysql> select * from student order by birth;
+------+------+------+------------+------------+
| name | age  | sex  | birth      | death      |
+------+------+------+------------+------------+
| 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 |
| rrrr |   29 | f    | 1997-03-04 | 1992-02-01 |
| rrrr |   29 | 男   | 1997-03-04 | 1992-02-01 |
| rrrr |   29 | 女   | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hsy  |   18 | f    | 1999-09-09 | NULL       |
| hsy  |   18 | m    | 1999-09-09 | NULL       |
+------+------+------+------------+------------+
15 rows in set (0.00 sec)

mysql> select * from student order by birth desc;
+------+------+------+------------+------------+
| name | age  | sex  | birth      | death      |
+------+------+------+------------+------------+
| hsy  |   18 | f    | 1999-09-09 | NULL       |
| hsy  |   18 | m    | 1999-09-09 | NULL       |
| 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 |
| rrrr |   29 | f    | 1997-03-04 | 1992-02-01 |
| rrrr |   29 | 男   | 1997-03-04 | 1992-02-01 |
| rrrr |   29 | 女   | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| hh   |   29 | f    | 1997-03-04 | 1992-02-01 |
| 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 |
| hsy  |   18 | m    | 1989-09-09 | NULL       |
+------+------+------+------------+------------+
15 rows in set (0.00 sec)

日期计算

curdate()当前系统日期

要想确定每个student有多大,可以计算当前日期的年和出生日期之间的差。如果当前日期的日历年比出生日期早,则减去一年。以下查询显示了每个student的出生日期、当前日期和年龄数值的年数字。

mysql> select name ,birth, curdate(),
    -> (year(curdate())-year(birth))-(right(curdate(),5)<right(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)=9;
+------+------------+
| name | birth      |
+------+------------+
| hsy  | 1999-09-09 |
| hsy  | 1999-09-09 |
| hsy  | 1989-09-09 |
+------+------------+
3 rows in set (0.00 sec)

如果当前月份是12月,就有点复杂了。你不能只把1加到月份数(12)上并寻找在13月出生的,因为没有这样的月份。相反,你应寻找在1月出生的(1月) 。

你甚至可以编写查询,不管当前月份是什么它都能工作。采用这种方法不必在查询中使用一个特定的月份,DATE_ADD( )允许在一个给定的日期上加上时间间隔。如果在NOW( )值上加上一个月,然后用MONTH()提取月份,结果产生生日所在月份:

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 ,birth
    -> from student
    -> where month(birth)=mod(month(curdate()),12)+1;
+----------+------------+
| name     | birth      |
+----------+------------+
| xiaomimg | 1997-07-04 |
| hong     | 1997-07-08 |
+----------+------------+
2 rows in set (0.00 sec)

注意,MONTH返回在1和12之间的一个数字,且MOD(something,12)返回在0和11之间的一个数字,因此必须在MOD( )以后加1,否则我们将从11月( 11 )跳到1月(1)。

NULL值操作

相反使用IS NULL和IS NOT NULL操作符:

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 name , death is null,death is not null from student;
+----------+---------------+-------------------+
| name     | death is null | death is not null |
+----------+---------------+-------------------+
| hsy      |             1 |                 0 |
| hsy      |             1 |                 0 |
| hsy      |             1 |                 0 |
| hh       |             0 |                 1 |
| hh       |             0 |                 1 |
| hh       |             0 |                 1 |
| hh       |             0 |                 1 |
| hh       |             0 |                 1 |
| hh       |             0 |                 1 |
| hrr      |             0 |                 1 |
| rrrr     |             0 |                 1 |
| rrrr     |             0 |                 1 |
| rrrr     |             0 |                 1 |
| hh       |             0 |                 1 |
| hh       |             0 |                 1 |
| xiaomimg |             0 |                 1 |
| hong     |             0 |                 1 |
+----------+---------------+-------------------+
17 rows in set (0.00 sec)

模式匹配

SQL模式匹配允许你使用“_”匹配任何单个字符,而“%”匹配任意数目字符(包括零字符)。在MySQL中,SQL的模式默认是忽略大小写的。下面给出一些例子。注意使用SQL模式时,不能使用=或!=;而应使用LIKE或NOT LIKE比较操作符,mysql like

找出含h的


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 like '_ _ _';
+------+------+------+------------+------------+
| 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       |
| hrr  |   29 | f    | 1997-03-04 | 1992-02-01 |
+------+------+------+------------+------------+
4 rows in set (0.00 sec)

由MySQL提供的模式匹配的其它类型是使用扩展正则表达式。当你对这类模式进行匹配测试时,使用REGEXP和NOT REGEXP操作符(或RLIKE和NOT RLIKE,它们是同义词)。

以h开头(like 'h%')

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 'g$';
+----------+------+------+------------+------------+
| name     | age  | sex  | birth      | death      |
+----------+------+------+------------+------------+
| xiaomimg |   29 | 女   | 1997-07-04 | 1992-02-01 |
| hong     |   29 | 女   | 1997-07-08 | 1992-02-01 |
+----------+------+------+------------+------------+
2 rows in set (0.00 sec)

含h的(like '%h%')


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 * from student where name regexp '^...$';
+------+------+------+------------+------------+
| 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       |
| hrr  |   29 | f    | 1997-03-04 | 1992-02-01 |
+------+------+------+------------+------------+
4 rows in set (0.00 sec)

计数行

COUNT(*)函数计算行数,所以计算数目的查询应为:

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

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

mysql> select name, count(*) from student group by name;
+----------+----------+
| name     | count(*) |
+----------+----------+
| hh       |        8 |
| hong     |        1 |
| hrr      |        1 |
| hsy      |        3 |
| rrrr     |        3 |
| xiaomimg |        1 |
+----------+----------+
6 rows in set (0.00 sec)

使用1个以上的表

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> select database();
+------------+
| database() |
+------------+
| student    |
+------------+

1 row in set (0.00 sec)

显示表

为了找出当前的数据库包含什么表(例如,当你不能确定一个表的名字),使用这个命令:

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

显示表结构

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


mysql> describe event;
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| name   | varchar(20)  | YES  |     | NULL    |       |
| date   | date         | YES  |     | NULL    |       |
| type   | varchar(15)  | YES  |     | NULL    |       |
| remark | varchar(255) | YES  |     | NULL    |       |
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

七、 常用查询的例子

下面是一些学习如何用MySQL解决一些常见问题的例子。

在一些例子中,使用数据库表“shop”来储存某个商人(经销商)的每件物品(物品号)的价格。假定每个商人对每项物品有一个固定价格,那么(物品,商人)即为该记录的主关键字。

创建表

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)

插入数据

 insert into shop values(1,'A',3.45),(1,'B',3.99),(2,'A',5.22),(2,'B',10.33),(3,'C',1.56),(3,'D',1.23);
Query OK, 6 rows affected (0.01 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 max(article) as article from shop ;
+---------+
| article |
+---------+
|       3 |
+---------+
1 row 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 ,dealer,price
    -> from shop
    -> order by price desc
    -> limit 1;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|    0002 | B      | 10.33 |
+---------+--------+-------+
1 row in set (0.00 sec)

列的最大值:按组

任务:每项物品的的最高价格是多少?

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 article,dealer,price
    -> from shop s1
    -> where price=(select max(s2.price)
    ->              from shop s2
    ->              where s1.article=s2.article);
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|    0001 | B      |  3.99 |
|    0002 | B      | 10.33 |
|    0003 | C      |  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)
mysql> select * from shop where price= @min_price or price= @max_price;;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|    0002 | B      | 10.33 |
|    0003 | D      |  1.23 |
+---------+--------+-------+
2 rows in set (0.00 sec)

使用外键

只是联接两个表时,不需要外部关键字。对于除InnoDB类型的表,当使用REFERENCES tbl_name(col_name)子句定义列时可以使用外部关键字,该子句没有实际的效果,只作为备忘录或注释来提醒,你目前正定义的列指向另一个表中的一个列。执行该语句时,实现下面很重要:

· MySQL不执行表tbl_name 中的动作,例如作为你正定义的表中的行的动作的响应而删除行;换句话说,该句法不会致使ON DELETE或ON UPDATE行为(如果你在REFERENCES子句中写入ON DELETE或ON UPDATE子句,将被忽略)。

创建表

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);
 

mysql> select * from person;
+----+---------------------+
| id | name                |
+----+---------------------+
|  1 | Antonio Paz         |
|  2 | Lilliana Angelovska |
+----+---------------------+
2 rows in set (0.00 sec)

mysql> select * from shirt;
+----+---------+--------+-------+
| id | style   | color  | owner |
+----+---------+--------+-------+
|  1 | polo    | blue   |     1 |
|  2 | dress   | white  |     1 |
|  3 | t-shirt | blue   |     1 |
|  4 | dress   | orange |     2 |
|  5 | polo    | red    |     2 |
|  6 | dress   | blue   |     2 |
|  7 | t-shirt | white  |     2 |
+----+---------+--------+-------+
7 rows in set (0.00 sec)
 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只搜索一个关键字,可以进行优化:



SELECT field1_index, field2_index
    FROM test_table WHERE field1_index = '1'
UNION
SELECT field1_index, field2_index
    FROM test_table WHERE field2_index = '1';

根据天计算访问量

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 |
+------+-------+------+

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

SELECT year,month,BIT_COUNT(BIT_OR(1<<day)) AS days FROM t1
       GROUP BY year,month;

将返回:

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

使用AUTO_INCREMENT

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

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
 );
 
INSERT INTO animals (name) VALUES 
    ('dog'),('cat'),('penguin'),
    ('lax'),('whale'),('ostrich');
 
SELECT * FROM animals;

将返回:

+----+---------+
| id | name    |
+----+---------+
|  1 | dog     |
|  2 | cat     |
|  3 | penguin |
|  4 | lax     |
|  5 | whale   |
|  6 | ostrich |
+----+---------+
CREATE TABLE`animals` (
  `grp` ENUM('fish', 'mammal', 'bird') NOT NULL,
  `id` MEDIUMINT NOT NULL AUTO_INCREMENT,
  `name` CHAR(20) NOT NULL,
  PRIMARY KEY (`id`, `grp`));


 
INSERT INTO animals (grp,name) VALUES 
    ('mammal','dog'),('mammal','cat'),
    ('bird','penguin'),('fish','lax'),('mammal','whale'),
    ('bird','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
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2021-11-16 09:12  DB哥  阅读(58)  评论(0编辑  收藏  举报