MySQL官方文档阅读笔记&入门基础

说明,MySQL的关键词大小写不敏感。

例如:

mysql> SELECT VERSION(), CURRENT_DATE;

mysql> select version(), current_date;

mysql> SeLeCt vErSiOn(), current_DATE; 是相同的。

 

创建和使用一个数据库

SHOW DATABASES;

SHOW 可以找到现有的数据库。

假如有一个名为test的数据库,我们使用USE来接入。

USE test(此处注意USE 是不需要使用分号的,但是使用了也不会出错。)

创建数据库

使用CREATE DATABASE ;

mysql> CREATE DATABASE menagerie;

这样就创建了一个名为menagerie的数据库。

然后我们接入数据库:

mysql> USE menagerie

接入之后我们可以查看数据库中的表:

mysql> SHOW TABLES;

Empty set (0.00 sec)

可见现在数据库是空的,所以接下来我们创建表。

创建一张表

使用CREATE TABLE创建。

    创建表之前要先思考表格需要包含哪些信息,数据的格式是什么。

例如:

mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),

-> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);

这个语句创建了一张名为pet的表格,表格中包含姓名,主人,物种,性别,出生日期,死亡日期。其中姓名,主人和物种都是可变字符型。性别是字符,出生日期和死亡日期都是日期类型。

现在我们就可以看到数据库当中存在了一张表格

mysql> SHOW TABLES;
+---------------------+
| Tables_in_menagerie |
+---------------------+
| pet                 |
+---------------------+
1 row in set (0.00 sec)

可以用DESCRIBE 来确认你所创建的表格。

mysql> DESCRIBE pet;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(20) | YES  |     | NULL    |       |
| owner   | varchar(20) | YES  |     | NULL    |       |
| species | varchar(20) | YES  |     | NULL    |       |
| sex     | char(1)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| death   | date        | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

把数据导入表

txt文件的数据导入到表格中。

mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;

注意斜杠,我在Windows下直接复制资源管理器的文件路径时是'\'。

也可以手动插入数据:

mysql> INSERT INTO pet
    -> VALUES ('Puffball','Dianee','hamster','f','1999-03-30',NULL);
Query OK, 1 row affected (0.02 sec)

用各种方式从表中获取数据

SELECT

我们先看看刚刚创建的表格。

mysql> SELECT * FROM pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    | f    | 1997-12-09 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
| Puffball | Dianee | hamster | f    | 1999-03-30 | NULL       |
+----------+--------+---------+------+------------+------------+
9 rows in set (0.00 sec)

*代表全部。

也可以用UPDATE对表格中的数据进行更新:

mysql> UPDATE pet SET birth = '1989-08-31' WHERE name = 'Bowser';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

SELECT 结合其他的关键词可以非常灵活的返回我们需要的数据:

例如:

WHERE

mysql> SELECT * FROM pet WHERE name = 'Bowser';
+--------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+

这句话的功能是,从pet表中,提取所有姓名是’Bowser’的宠物。

mysql> SELECT * FROM pet WHERE birth >= '1998-1-1';
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Chirpy   | Gwen  | bird    | f    | 1998-09-11 | NULL  |
| Puffball | Diane | hamster | f    | 1999-03-30 | NULL  |
+----------+-------+---------+------+------------+-------+

pet表格中,提取所有在1998-1-1之后出生的宠物。

AND OR

mysql> SELECT * FROM pet WHERE species = 'dog' AND sex = 'f';
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
mysql> SELECT * FROM pet WHERE species = 'snake' OR species = 'bird';
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Chirpy   | Gwen  | bird    | f    | 1998-09-11 | NULL  |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL  |
| Slim     | Benny | snake   | m    | 1996-04-29 | NULL  |
+----------+-------+---------+------+------------+-------+

对提取的数据进行排序:ORDER BY

Here are animal birthdays, sorted by date:
mysql> SELECT name, birth FROM pet ORDER BY birth;
+----------+------------+
| name     | birth      |
+----------+------------+
| Buffy    | 1989-05-13 |
| Bowser   | 1989-08-31 |
| Fang     | 1990-08-27 |
| Fluffy   | 1993-02-04 |
| Claws    | 1994-03-17 |
| Slim     | 1996-04-29 |
| Whistler | 1997-12-09 |
| Chirpy   | 1998-09-11 |
| Puffball | 1999-03-30 |
+----------+------------+

默认是升序排列,降序排列只需要在最后加上DESC即可。

mysql> SELECT name, birth FROM pet ORDER BY birth DESC;
+----------+------------+
| name     | birth      |
+----------+------------+
| Puffball | 1999-03-30 |
| Chirpy   | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim     | 1996-04-29 |
| Claws    | 1994-03-17 |
| Fluffy   | 1993-02-04 |
| Fang     | 1990-08-27 |
| Bowser   | 1989-08-31 |
| Buffy    | 1989-05-13 |
+----------+------------+

日期的计算:TIMESTAMPDIFF()

mysql> SELECT name, birth, CURDATE(),
-> TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age
-> FROM pet;
+----------+------------+------------+------+
| name     | birth      | CURDATE()  | age  |
+----------+------------+------------+------+
| Fluffy   | 1993-02-04 | 2003-08-19 | 10   |
| Claws    | 1994-03-17 | 2003-08-19 | 9    |
| Buffy    | 1989-05-13 | 2003-08-19 | 14   |
| Fang     | 1990-08-27 | 2003-08-19 | 12   |
| Bowser   | 1989-08-31 | 2003-08-19 | 13   |
| Chirpy   | 1998-09-11 | 2003-08-19 | 4    |
| Whistler | 1997-12-09 | 2003-08-19 | 5    |
| Slim     | 1996-04-29 | 2003-08-19 | 7    |
| Puffball | 1999-03-30 | 2003-08-19 | 4    |
+----------+------------+------------+------+

CURDATE()为当前的时间

TIMESTMPDIFFYEAR, birth, CURDATE()

计算的是从出生日期到现在的年数。

 

提取日期的月份:MONTH(birth)

mysql> SELECT name, birth, MONTH(birth) FROM pet;
+----------+------------+--------------+
| name     | birth      | MONTH(birth) |
+----------+------------+--------------+
| Fluffy   | 1993-02-04 | 2            |
| Claws    | 1994-03-17 | 3            |
| Buffy    | 1989-05-13 | 5            |
| Fang     | 1990-08-27 | 8            |
| Bowser   | 1989-08-31 | 8            |
| Chirpy   | 1998-09-11 | 9            |
| Whistler | 1997-12-09 | 12           |
| Slim     | 1996-04-29 | 4            |
| Puffball | 1999-03-30 | 3            |
+----------+------------+--------------+

DATE_ADD()可以计算以个时间间隔后的时间。

 

mysql> SELECT name, birth FROM pet 
WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));
+--------+------------+
| name   | birth      |
+--------+------------+
| Fang   | 1990-08-27 |
| Bowser | 1989-08-31 |
+--------+------------+
2 rows in set (0.00 sec)

可以找出下个月过生日的宠物

模式匹配

说明:在MySQL中,模式匹配是默认大小写不敏感的。

mysql> SELECT * FROM pet WHERE name LIKE 'b%';
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+

提取名字以b开头。

mysql> SELECT * FROM pet WHERE name LIKE '%fy';
+--------+--------+---------+------+------------+-------+
| name   | owner  | species | sex  | birth      | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
+--------+--------+---------+------+------------+-------+

提取名字以fy结尾的。

mysql> SELECT * FROM pet WHERE name LIKE '_____';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

名字有5个字符的

正则表达式

正则表达式用^开头,$结尾 语句前要加一个 REGEXP

. 匹配单个字符

[...] 匹配括号中的任何字符。例如:[abc]匹配字幕a,b,c [a-z]匹配所有字母。[0-9]匹配所有数字。

* 匹配它前面字符0次或多次。例如: [0-9]*可以匹配任何数字  .*可以匹配任何数量的任何字符

{n}代表重复次数

mysql> SELECT * FROM pet WHERE name REGEXP '^b';
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+

名字以B开头的。

mysql> SELECT * FROM pet WHERE name REGEXP '^.{5}$';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  | 
+-------+--------+---------+------+------------+-------+

名字有5个字符

计算表的行数

mysql> SELECT COUNT(*) FROM pet;
+----------+
| COUNT(*) |
+----------+
| 9        |
+----------+
mysql> SELECT owner, COUNT(*) FROM pet GROUP BY owner;
+--------+----------+
| owner  | COUNT(*) |
+--------+----------+
| Benny  | 2        |
| Diane  | 2        |
| Gwen   | 3        |
| Harold | 2        |
+--------+----------+

同时使用多于一张表

之前的表格中已经记录了每个宠物的信息,现在我们想建立一个表格用于记录发生的各项事务。那么当然,这个表格应该包括宠物的名字,事件的时间,事件的类型,以及具体描述。我们把下面的信息存入表中。

MySQL官方文档阅读笔记&入门基础_第1张图片

同样,我们可以用txt文件导入,把数据复制到txt文件当中,注意用tab键作为分隔符。

MySQL官方文档阅读笔记&入门基础_第2张图片

就会变成这个样子,但是要注意,最后用于描述那一列数据之后也要用tab键 否则导入之后格式会有问题,像这样:

mysql> select * from event;
+-----------+------------+-----------+------------------------------+
| name      | date       | type      | remark                       |
+-----------+------------+-----------+------------------------------+
 |Fluffy    | 1995-05-15 | litter    | 4 kittens, 3 female, 1 male
 |Buffy     | 1993-06-23 | litter    | 5 puppies, 2 female, 3 male
         |  | 1994-06-19 | litter    | 3 puppies, 3 female
    |rpy    | 1999-03-21 | vet       | needed beak straightened
                  |08-03 | vet       | broken rib
                           |ennel    |
                           |ennel    |
     |      | 1998-08-28 | birthday  | Gave him a new chew toy
  |laws     | 1998-03-17 | birthday  | Gave him a new flea collar
| Whistler  | 1998-12-09 | birthday  | First birthday               |
+-----------+------------+-----------+------------------------------+
10 rows in set (0.00 sec)

注:

  • 如果在过程中发现warning,但是又看不到warning是什么的话,可以使用SHOW warnings
Query OK, 10 rows affected, 7 warnings (0.00 sec)
Records: 10  Deleted: 0  Skipped: 0  Warnings: 7

mysql> SHOW WARNINGS;
+---------+------+---------------------------------------------------------------------------+
| Level   | Code | Message                                                                   |
+---------+------+---------------------------------------------------------------------------+
| Warning | 1262 | Row 1 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 2 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 3 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 4 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 5 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 8 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 9 was truncated; it contained more data than there were input columns |
+---------+------+---------------------------------------------------------------------------+
  • 如果导入失败,可以用delete from 表格名 删除表格的内容,重新修改后导入。

 

下面通过INNER JOIN 语句,同时使用两张表

mysql> SELECT pet.name,
-> TIMESTAMPDIFF(YEAR,birth,date) AS age, remark
-> FROM pet INNER JOIN event
-> ON pet.name = event.name
-> WHERE event.type = 'litter';
+--------+------+-----------------------------+
| name   | age  | remark                      |
+--------+------+-----------------------------+
| Fluffy | 2    | 4 kittens, 3 female, 1 male |
| Buffy  | 4    | 5 puppies, 2 female, 3 male |
| Buffy  | 5    | 3 puppies, 3 female         |
+--------+------+-----------------------------+

因为我们需要的信息需要从两张表中分别提取出来,所以我们要这样做。把多张表组合在一起的时候,要注意我们需要明确如何让一张表中的数据与其他表中的数据匹配起来,比如这个宠物的事件和它的年龄,我们可以通过姓名这一信息做到。在这个例子中,我们用 ON 语句 可以以name的值来将两张表匹配。INNER JOIN 可以把两张表合并,只要满足ON语句给出的条件,它允许把任一表格的行显示在结果中。因为两张表都有name,所以要在name前面加上表格的名字。

下面另一个例子:

mysql> SELECT p1.name, p1.sex, p2.name, p2.sex, p1.species
-> FROM pet AS p1 INNER JOIN pet AS p2
-> ON p1.species = p2.species AND p1.sex = 'f' AND p2.sex = 'm';
+--------+------+--------+------+---------+
| name   | sex  | name   | sex  | species |
+--------+------+--------+------+---------+
| Fluffy | f    | Claws  | m    | cat     |
| Buffy  | f    | Fang   | m    | dog     |
| Buffy  | f    | Bowser | m    | dog     |
+--------+------+--------+------+---------+

注:AS是别名关键字,就是取一个别名。

这个例子实现了,把宠物中同一物种的雌雄配对。

 

到这里,最基础的知识基本就结束了。

你可能感兴趣的:(Python知识)