MySQL学习记录(11.9更新)

第一章 - 了解SQL

  • 数据库:数据库是一些关联表的集合。可以类比是一座图书馆
  • 表:表是数据的矩阵。在一个数据库中的表看起来像一个简单的电子表格。可以类比是图书馆里的书
  • 列:一列(数据元素) 包含了相同类型的数据, 例如邮政编码的数据。
  • 行:一行(=元组,或记录)是一组相关的数据,例如一条用户订阅的数据。
  • 主键:主键是唯一的。一个数据表中只能包含一个主键。你可以使用主键来查询数据。
    avatar

第二章 - MySQL简介

第三章 - 使用MySQL

输入mysql -u root -p之后输入密码即可连接数据库

G:\mysql-8.0.21\bin>mysql -u root -p
Enter password: ***
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 8.0.21 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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即可显示数据库列表

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| crashcourse        |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

输入use crashcourse即可选择’crashcourse’这个数据库

mysql> use crashcourse
Database changed

输入show tables;即可查看数据库内表的列表

mysql> show tables;
+-----------------------+
| Tables_in_crashcourse |
+-----------------------+
| customers             |
| orderitems            |
| orders                |
| productnotes          |
| products              |
| vendors               |
+-----------------------+

第四章 - 检索数据

检索单个列

输入:  SELECT prod_name 
        FROM products;
+----------------+
| prod_name      |
+----------------+
| .5 ton anvil   |
| 1 ton anvil    |
| 2 ton anvil    |
| Detonator      |
| Bird seed      |
| Carrots        |
| Fuses          |
| JetPack 1000   |
| JetPack 2000   |
| Oil can        |
| Safe           |
| Sling          |
| TNT (1 stick)  |
| TNT (5 sticks) |
+----------------+

检索多个列

输入:  SELECT prod_id, prod_name, prod_price
        FROM products;
+---------+----------------+------------+
| prod_id | prod_name      | prod_price |
+---------+----------------+------------+
| ANV01   | .5 ton anvil   |       5.99 |
| ANV02   | 1 ton anvil    |       9.99 |
| ANV03   | 2 ton anvil    |      14.99 |
| DTNTR   | Detonator      |      13.00 |
| FB      | Bird seed      |      10.00 |
| FC      | Carrots        |       2.50 |
| FU1     | Fuses          |       3.42 |
| JP1000  | JetPack 1000   |      35.00 |
| JP2000  | JetPack 2000   |      55.00 |
| OL1     | Oil can        |       8.99 |
| SAFE    | Safe           |      50.00 |
| SLING   | Sling          |       4.49 |
| TNT1    | TNT (1 stick)  |       2.50 |
| TNT2    | TNT (5 sticks) |      10.00 |
+---------+----------------+------------+

检索所有列

输入:  SELECT *
        FROM products;
+---------+---------+----------------+------------+----------------------------------------------------------------+
| prod_id | vend_id | prod_name      | prod_price | prod_desc                                                      |
+---------+---------+----------------+------------+----------------------------------------------------------------+
| ANV01   |    1001 | .5 ton anvil   |       5.99 | .5 ton anvil, black, complete with handy hook                  |
| ANV02   |    1001 | 1 ton anvil    |       9.99 | 1 ton anvil, black, complete with handy hook and carrying case |
| ANV03   |    1001 | 2 ton anvil    |      14.99 | 2 ton anvil, black, complete with handy hook and carrying case |
| DTNTR   |    1003 | Detonator      |      13.00 | Detonator (plunger powered), fuses not included                |
| FB      |    1003 | Bird seed      |      10.00 | Large bag (suitable for road runners)                          |
| FC      |    1003 | Carrots        |       2.50 | Carrots (rabbit hunting season only)                           |
| FU1     |    1002 | Fuses          |       3.42 | 1 dozen, extra long                                            |
| JP1000  |    1005 | JetPack 1000   |      35.00 | JetPack 1000, intended for single use                          |
| JP2000  |    1005 | JetPack 2000   |      55.00 | JetPack 2000, multi-use                                        |
| OL1     |    1002 | Oil can        |       8.99 | Oil can, red                                                   |
| SAFE    |    1003 | Safe           |      50.00 | Safe with combination lock                                     |
| SLING   |    1003 | Sling          |       4.49 | Sling, one size fits all                                       |
| TNT1    |    1003 | TNT (1 stick)  |       2.50 | TNT, red, single stick                                         |
| TNT2    |    1003 | TNT (5 sticks) |      10.00 | TNT, red, pack of 10 sticks                                    |
+---------+---------+----------------+------------+----------------------------------------------------------------+

检索不同的行

输入:  SELECT vend_id
        FROM products;
+---------+
| vend_id |
+---------+
|    1001 |
|    1001 |
|    1001 |
|    1002 |
|    1002 |
|    1003 |
|    1003 |
|    1003 |
|    1003 |
|    1003 |
|    1003 |
|    1003 |
|    1005 |
|    1005 |
+---------+

如何检索出不同值的列表呢?在前面加上DISTINCT关键字

输入:  SELECT  DISTINCT vend_id
        FROM products;
+---------+
| vend_id |
+---------+
|    1001 |
|    1002 |
|    1003 |
|    1005 |
+---------+

限制结果

输入:  SELECT  prod_name
        FROM products
        LIMIT 5;
+--------------+
| prod_name    |
+--------------+
| .5 ton anvil |
| 1 ton anvil  |
| 2 ton anvil  |
| Detonator    |
| Bird seed    |
+--------------+

如何从指定的行数返回限制的行数呢?在这里主义第一行的序号是0。

输入:  SELECT  prod_name
        FROM products
        LIMIT 10,5;
+----------------+
| prod_name      |
+----------------+
| Safe           |
| Sling          |
| TNT (1 stick)  |
| TNT (5 sticks) |
+----------------+
4 rows in set (0.00 sec)

第五章 - 排序检索结果

排序数据

输入:  SELECT  prod_name
        FROM products
        ORDER BY prod_name;
+----------------+
| prod_name      |
+----------------+
| .5 ton anvil   |
| 1 ton anvil    |
| 2 ton anvil    |
| Bird seed      |
| Carrots        |
| Detonator      |
| Fuses          |
| JetPack 1000   |
| JetPack 2000   |
| Oil can        |
| Safe           |
| Sling          |
| TNT (1 stick)  |
| TNT (5 sticks) |
+----------------+

按多个列排序

输入:  SELECT  prod_id, prod_name, prod_price
        FROM products
        ORDER BY prod_price, prod_name;
+---------+----------------+------------+
| prod_id | prod_name      | prod_price |
+---------+----------------+------------+
| FC      | Carrots        |       2.50 |
| TNT1    | TNT (1 stick)  |       2.50 |
| FU1     | Fuses          |       3.42 |
| SLING   | Sling          |       4.49 |
| ANV01   | .5 ton anvil   |       5.99 |
| OL1     | Oil can        |       8.99 |
| ANV02   | 1 ton anvil    |       9.99 |
| FB      | Bird seed      |      10.00 |
| TNT2    | TNT (5 sticks) |      10.00 |
| DTNTR   | Detonator      |      13.00 |
| ANV03   | 2 ton anvil    |      14.99 |
| JP1000  | JetPack 1000   |      35.00 |
| SAFE    | Safe           |      50.00 |
| JP2000  | JetPack 2000   |      55.00 |
+---------+----------------+------------+

在这里只有prod_price相等时才再按照prod_name排序。

指定排序方向

输入:  SELECT  prod_id, prod_name, prod_price
        FROM products
        ORDER BY prod_price DESC, prod_name;
+---------+----------------+------------+
| prod_id | prod_name      | prod_price |
+---------+----------------+------------+
| JP2000  | JetPack 2000   |      55.00 |
| SAFE    | Safe           |      50.00 |
| JP1000  | JetPack 1000   |      35.00 |
| ANV03   | 2 ton anvil    |      14.99 |
| DTNTR   | Detonator      |      13.00 |
| FB      | Bird seed      |      10.00 |
| TNT2    | TNT (5 sticks) |      10.00 |
| ANV02   | 1 ton anvil    |       9.99 |
| OL1     | Oil can        |       8.99 |
| ANV01   | .5 ton anvil   |       5.99 |
| SLING   | Sling          |       4.49 |
| FU1     | Fuses          |       3.42 |
| FC      | Carrots        |       2.50 |
| TNT1    | TNT (1 stick)  |       2.50 |
+---------+----------------+------------+

第六章 - 过滤数据

WHERE字句

输入:  SELECT  prod_name, prod_price
        FROM products
        WHERE prod_price = 2.50;
+---------------+------------+
| prod_name     | prod_price |
+---------------+------------+
| Carrots       |       2.50 |
| TNT (1 stick) |       2.50 |
+---------------+------------+

WHERE子句操作符

操作符说明
=等于
<>不等于
!=不等于
<小于
>大于
<=小于等于
>=大于等于
BETWEEN介于两者之间

范围值检查

输入:  SELECT  prod_name, prod_price
       FROM products
       WHERE prod_price BETWEEN 5 AND 10;
+----------------+------------+
| prod_name      | prod_price |
+----------------+------------+
| .5 ton anvil   |       5.99 |
| 1 ton anvil    |       9.99 |
| Bird seed      |      10.00 |
| Oil can        |       8.99 |
| TNT (5 sticks) |      10.00 |
+----------------+------------+

空值检查

输入:  SELECT  cust_id
       FROM customers
       WHERE cust_email IS NULL;
+---------+
| cust_id |
+---------+
|   10002 |
|   10005 |
+---------+

第七章 - 数据过滤

AND操作符

输入:SELECT prod_id, prod_name, prod_price
      FROM products
      WHERE vend_id = 1003 AND prod_price <= 10;
+---------+----------------+------------+
| prod_id | prod_name      | prod_price |
+---------+----------------+------------+
| FB      | Bird seed      |      10.00 |
| FC      | Carrots        |       2.50 |
| SLING   | Sling          |       4.49 |
| TNT1    | TNT (1 stick)  |       2.50 |
| TNT2    | TNT (5 sticks) |      10.00 |
+---------+----------------+------------+

OR操作符

输入:SELECT prod_name,prod_price
      FROM products
      WHERE vend_id = 1002 OR vend_id = 1003;
+----------------+------------+
| prod_name      | prod_price |
+----------------+------------+
| Fuses          |       3.42 |
| Oil can        |       8.99 |
| Detonator      |      13.00 |
| Bird seed      |      10.00 |
| Carrots        |       2.50 |
| Safe           |      50.00 |
| Sling          |       4.49 |
| TNT (1 stick)  |       2.50 |
| TNT (5 sticks) |      10.00 |
+----------------+------------+

计算次序

AND在计算次序中优先级更高,混合使用记得加括号。

IN操作符

输入:SELECT prod_name, prod_price
      FROM products
      WHERE vend_id IN (1002,1003)
      ORDER BY prod_name;
+----------------+------------+
| prod_name      | prod_price |
+----------------+------------+
| Bird seed      |      10.00 |
| Carrots        |       2.50 |
| Detonator      |      13.00 |
| Fuses          |       3.42 |
| Oil can        |       8.99 |
| Safe           |      50.00 |
| Sling          |       4.49 |
| TNT (1 stick)  |       2.50 |
| TNT (5 sticks) |      10.00 |
+----------------+------------+

NOT操作符

输入:SELECT prod_name, prod_price
      FROM products
      WHERE vend_id NOT IN (1002,1003)
      ORDER BY prod_name;
+--------------+------------+
| prod_name    | prod_price |
+--------------+------------+
| .5 ton anvil |       5.99 |
| 1 ton anvil  |       9.99 |
| 2 ton anvil  |      14.99 |
| JetPack 1000 |      35.00 |
| JetPack 2000 |      55.00 |
+--------------+------------+

第八章 - 用通配符进行过滤

LIKE 操作符

输入:SELECT prod_id, prod_name
      FROM products
      WHERE prod_name LIKE 'jet%';
+---------+--------------+
| prod_id | prod_name    |
+---------+--------------+
| JP1000  | JetPack 1000 |
| JP2000  | JetPack 2000 |
+---------+--------------+

%通配符

%表示任何字符出现任意次数

_通配符

_只能匹配一个字符

第九章 - 用正则表达式进行搜索

基本用法

WHERE XXX REGEXP 'XXX'

匹配多个实例

元字符说明
*0个或多个匹配
+1个或多个匹配, 等于{1,}
?0个或一个匹配{0,1}
{n}指定书目的匹配
{n,}不少于指定的匹配
{n,m}指定范围的匹配(m < 255)

定位符

元字符说明
^文本的开始
$文本的结尾
[[:<:]]词的开始
[[:>:]]词的结尾

第十章 - 创建计算字段

拼接字段且使用别名

输入:SELECT Concat(Rtrim(vend_name),'(' , Rtrim(vend_country), ')') AS vend_title
      FROM vendors
      ORDER BY vend_name;
+------------------------+
| vend_title             |
+------------------------+
| ACME(USA)              |
| Anvils R Us(USA)       |
| Furball Inc.(USA)      |
| Jet Set(England)       |
| Jouets Et Ours(France) |
| LT Supplies(USA)       |
+------------------------+

执行算数计算

输入:SELECT prod_id,
             quantity,
             item_price,
             quantity*item_price AS expanded_price
      FROM orderitems
      WHERE order_num = 20005;
+---------+----------+------------+----------------+
| prod_id | quantity | item_price | expanded_price |
+---------+----------+------------+----------------+
| ANV01   |       10 |       5.99 |          59.90 |
| ANV02   |        3 |       9.99 |          29.97 |
| TNT2    |        5 |      10.00 |          50.00 |
| FB      |        1 |      10.00 |          10.00 |
+---------+----------+------------+----------------+

第十一章 - 使用数据处理函数

常用的文本处理函数

函数说明
left()返回串左边的字符
Right()返回串右边的字符
Length()返回串的长度
Lower()将串转化为小写
Upper()将串转化为大写
ltrim()去掉串左边的空格
Rtrim()取点串右边的空格
Soundex()返回串的Soundex值
SubString()返回子串的字符

常用日期和时间处理函数

第十二章 - 汇总数据

聚集函数

函数说明
AVG()返回某列的平均值
COUNT()返回某列的行数 COUNT(*)包含空值
MIN()返回某列的最小值
MAX()返回某列的最大值
SUM()返回某列的和
SELECT COUNT(*) AS num_items,
       MIN(prod_price) AS price_min,
       MAX(prod_price) AS price_max,
       AVG(prod_price) AS price_avg
       FROM products;
+-----------+-----------+-----------+-----------+
| num_items | price_min | price_max | price_avg |
+-----------+-----------+-----------+-----------+
|        14 |      2.50 |     55.00 | 16.133571 |
+-----------+-----------+-----------+-----------+

第十三章 - 分组数据

SELECT order_num, SUM(quantity * item_price) AS ordertotal
FROM orderitems
GROUP BY order_num
HAVING SUM(quantity * item_price) >= 50
ORDER BY ordertotal;
+-----------+------------+
| order_num | ordertotal |
+-----------+------------+
|     20006 |      55.00 |
|     20008 |     125.00 |
|     20005 |     149.87 |
|     20007 |    1000.00 |
+-----------+------------+

SELECT子语句的顺序:SELECT -> FROM -> WHERE -> GROUP BY -> HAVING -> ORDER BY -> LIMIT

第十四章 - 使用子查询

例子:列出订购TN2物品的所有客户

SELECT cust_name, cust_contact
FROM customers
WHERE cust_id IN (SELECT cust_id
                  FROM orders
                  WHERE order_num IN (SELECT order_num
                                        FROM orderitems
                                        WHERE prod_id = 'TNT2'));

+----------------+--------------+
| cust_name      | cust_contact |
+----------------+--------------+
| Coyote Inc.    | Y Lee        |
| Yosemite Place | Y Sam        |
+----------------+--------------+

第十五章 - 联结表

创建联结

SELECT vend_name, prod_name, prod_price
FROM vendors INNER JOIN products
ON vendors.vend_id = products.vend_id
ORDER BY vend_name, prod_name;
+-------------+----------------+------------+
| vend_name   | prod_name      | prod_price |
+-------------+----------------+------------+
| ACME        | Bird seed      | 10.00      |
| ACME        | Carrots        | 2.50       |
| ACME        | Detonator      | 13.00      |
| ACME        | Safe           | 50.00      |
| ACME        | Sling          | 4.49       |
| ACME        | TNT (1 stick)  | 2.50       |
| ACME        | TNT (5 sticks) | 10.00      |
| Anvils R Us | .5 ton anvil   | 5.99       |
| Anvils R Us | 1 ton anvil    | 9.99       |
| Anvils R Us | 2 ton anvil    | 14.99      |
| Jet Set     | JetPack 1000   | 35.00      |
| Jet Set     | JetPack 2000   | 55.00      |
| LT Supplies | Fuses          | 3.42       |
| LT Supplies | Oil can        | 8.99       |
+-------------+----------------+------------+

联结多个表

例子:显示编号为20005订单里的物品

SELECT prod_name, vend_name, prod_price, quantity
FROM products, vendors, orderitems
WHERE vendors.vend_id = products.vend_id
AND   orderitems.prod_id = products.prod_id
AND   orderitems.order_num = 20005;
+----------------+-------------+------------+----------+
| prod_name      | vend_name   | prod_price | quantity |
+----------------+-------------+------------+----------+
| .5 ton anvil   | Anvils R Us | 5.99       |       10 |
| 1 ton anvil    | Anvils R Us | 9.99       |        3 |
| TNT (5 sticks) | ACME        | 10.00      |        5 |
| Bird seed      | ACME        | 10.00      |        1 |
+----------------+-------------+------------+----------+

第十六章 - 创建高级联结

使用表别名

  • 可以缩短SQL语句
  • 可以在单条SELECT语句中多次使用相同的表

自联结

例子:加入发现ID为DTNTR存在问题,想知道提供该物品的供应商的别的物品是否也存在问题

SELECT p1.prod_id, p1.prod_name
FROM products AS p1 ,products AS p2
WHERE p1.vend_id = p2.vend_id
AND   p2.prod_id = 'DTNTR';
+---------+----------------+
| prod_id | prod_name      |
+---------+----------------+
| DTNTR   | Detonator      |
| FB      | Bird seed      |
| FC      | Carrots        |
| SAFE    | Safe           |
| SLING   | Sling          |
| TNT1    | TNT (1 stick)  |
| TNT2    | TNT (5 sticks) |
+---------+----------------+

外部联结

例子:检索所有客户的订单

SELECT c.cust_id, o.order_num
FROM customers AS c LEFT JOIN orders AS o
ON c.cust_id = o.cust_id;
+---------+-----------+
| cust_id | order_num |
+---------+-----------+
|   10001 |     20005 |
|   10001 |     20009 |
|   10002 |     NULL  |
|   10003 |     20006 |
|   10004 |     20007 |
|   10005 |     20008 |
+---------+-----------+

使用带聚集函数的联结

例子:检索所有客户及每个客户说下的订单数

SELECT c.cust_id, c.cust_name, COUNT(o.order_num) AS num_ord
FROM customers AS c LEFT JOIN orders AS o
ON c.cust_id = o.cust_id
GROUP BY c.cust_id;
+---------+----------------+---------+
| cust_id | cust_name      | num_ord |
+---------+----------------+---------+
|   10001 | Coyote Inc.    |       2 |
|   10002 | Mouse House    |       0 |
|   10003 | Wascals        |       1 |
|   10004 | Yosemite Place |       1 |
|   10005 | E Fudd         |       1 |
+---------+----------------+---------+

使用联结和联结条件

  • 保证正确的联结条件

第十七章 - 组合查询

例子:加入需要价格小于等于5的所有物品的一个列表,而且还想包括供应商1001和1002生产的物品(不包括价格)

SELECT vend_id, prod_name, prod_price
FROM products
WHERE prod_price <= 5
UNION ALL
SELECT vend_id, prod_name, prod_price
FROM products
WHERE vend_id IN (1001,1002)
ORDER BY vend_id, prod_price;
+---------+---------------+------------+
| vend_id | prod_name     | prod_price |
+---------+---------------+------------+
|    1001 | .5 ton anvil  | 5.99       |
|    1001 | 1 ton anvil   | 9.99       |
|    1001 | 2 ton anvil   | 14.99      |
|    1002 | Fuses         | 3.42       |
|    1002 | Fuses         | 3.42       |
|    1002 | Oil can       | 8.99       |
|    1003 | Carrots       | 2.50       |
|    1003 | TNT (1 stick) | 2.50       |
|    1003 | Sling         | 4.49       |
+---------+---------------+------------+

第十八章 - 全文本搜索

理解全文本搜索

  • MyISAM引擎支持全文本搜索
  • InnoDN不支持全文本搜索

进行全文本搜索

使用两个函数 Match()指定被搜索的列 和 Against()指定要用的搜索表达式

SELECT note_text
FROM productnotes
WHERE Match(note_text) Against('rabbit');
+---------------------------------------------------------------------------------------------------------------------+
| note_text                                                                                                           |
+---------------------------------------------------------------------------------------------------------------------+
| Customer complaint: rabbit has been able to detect trap, food apparently less effective now.                        |
| Quantity varies, sold by the sack load.
All guaranteed to be bright and orange, and suitable for use as rabbit bait. |
+---------------------------------------------------------------------------------------------------------------------+

布尔文本搜索(略)

感觉用的不是很多

第十九章 - 插入数据

插入完整的行

例子:插入一个新客户到customers表

+---------+----------------+---------------------+-----------+------------+----------+--------------+--------------+---------------------+
| cust_id | cust_name      | cust_address        | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email          |
+---------+----------------+---------------------+-----------+------------+----------+--------------+--------------+---------------------+
|   10001 | Coyote Inc.    | 200 Maple Lane      | Detroit   | MI         | 44444    | USA          | Y Lee        | ylee@coyote.com     |
|   10002 | Mouse House    | 333 Fromage Lane    | Columbus  | OH         | 43333    | USA          | Jerry Mouse  | NULL                |
|   10003 | Wascals        | 1 Sunny Place       | Muncie    | IN         | 42222    | USA          | Jim Jones    | rabbit@wascally.com |
|   10004 | Yosemite Place | 829 Riverside Drive | Phoenix   | AZ         | 88888    | USA          | Y Sam        | sam@yosemite.com    |
|   10005 | E Fudd         | 4545 53rd Street    | Chicago   | IL         | 54545    | USA          | E Fudd       | NULL                |
+---------+----------------+---------------------+-----------+------------+----------+--------------+--------------+---------------------+
INSERT INTO customers (cust_name,
cust_address,
cust_city,
cust_state,
cust_zip,
cust_country,
cust_contact,
cust_email)
VALUES ('Pep E.LaPew',
'100 Main street',
'Los Angeles',
'CA','90046',
'USA',
NULL,
NULL);
+---------+----------------+---------------------+-------------+------------+----------+--------------+--------------+---------------------+
| cust_id | cust_name      | cust_address        | cust_city   | cust_state | cust_zip | cust_country | cust_contact | cust_email          |
+---------+----------------+---------------------+-------------+------------+----------+--------------+--------------+---------------------+
|   10001 | Coyote Inc.    | 200 Maple Lane      | Detroit     | MI         | 44444    | USA          | Y Lee        | ylee@coyote.com     |
|   10002 | Mouse House    | 333 Fromage Lane    | Columbus    | OH         | 43333    | USA          | Jerry Mouse  | NULL                |
|   10003 | Wascals        | 1 Sunny Place       | Muncie      | IN         | 42222    | USA          | Jim Jones    | rabbit@wascally.com |
|   10004 | Yosemite Place | 829 Riverside Drive | Phoenix     | AZ         | 88888    | USA          | Y Sam        | sam@yosemite.com    |
|   10005 | E Fudd         | 4545 53rd Street    | Chicago     | IL         | 54545    | USA          | E Fudd       | NULL                |
|   10006 | Pep E.LaPew    | 100 Main street     | Los Angeles | CA         | 90046    | USA          | NULL         | NULL                |
+---------+----------------+---------------------+-------------+------------+----------+--------------+--------------+---------------------+

插入多个行

直接用多条INSERT 语句即可,中间用’;'隔开即可

插入检索出的数据

配合着SELECT语句即可

第二十章 - 更新和删除数据

更新数据

例子:10005客户现在有了电子邮件地址,因此需要更新

UPDATE customers
SET cust_email = 'elmer@fudd.com'
WHERE cust_id = 10005;
+---------+----------------+---------------------+-------------+------------+----------+--------------+--------------+---------------------+
| cust_id | cust_name      | cust_address        | cust_city   | cust_state | cust_zip | cust_country | cust_contact | cust_email          |
+---------+----------------+---------------------+-------------+------------+----------+--------------+--------------+---------------------+
|   10001 | Coyote Inc.    | 200 Maple Lane      | Detroit     | MI         | 44444    | USA          | Y Lee        | ylee@coyote.com     |
|   10002 | Mouse House    | 333 Fromage Lane    | Columbus    | OH         | 43333    | USA          | Jerry Mouse  | NULL                |
|   10003 | Wascals        | 1 Sunny Place       | Muncie      | IN         | 42222    | USA          | Jim Jones    | rabbit@wascally.com |
|   10004 | Yosemite Place | 829 Riverside Drive | Phoenix     | AZ         | 88888    | USA          | Y Sam        | sam@yosemite.com    |
|   10005 | E Fudd         | 4545 53rd Street    | Chicago     | IL         | 54545    | USA          | E Fudd       | elmer@fudd.com      |
|   10006 | Pep E.LaPew    | 100 Main street     | Los Angeles | CA         | 90046    | USA          | NULL         | NULL                |
+---------+----------------+---------------------+-------------+------------+----------+--------------+--------------+---------------------+

删除数据

DELETE FROM customers
WHERE cust_id = 10006;
+---------+----------------+---------------------+-----------+------------+----------+--------------+--------------+---------------------+
| cust_id | cust_name      | cust_address        | cust_city | cust_state | cust_zip | cust_country | cust_contact | cust_email          |
+---------+----------------+---------------------+-----------+------------+----------+--------------+--------------+---------------------+
|   10001 | Coyote Inc.    | 200 Maple Lane      | Detroit   | MI         | 44444    | USA          | Y Lee        | ylee@coyote.com     |
|   10002 | Mouse House    | 333 Fromage Lane    | Columbus  | OH         | 43333    | USA          | Jerry Mouse  | NULL                |
|   10003 | Wascals        | 1 Sunny Place       | Muncie    | IN         | 42222    | USA          | Jim Jones    | rabbit@wascally.com |
|   10004 | Yosemite Place | 829 Riverside Drive | Phoenix   | AZ         | 88888    | USA          | Y Sam        | sam@yosemite.com    |
|   10005 | E Fudd         | 4545 53rd Street    | Chicago   | IL         | 54545    | USA          | E Fudd       | elmer@fudd.com      |
+---------+----------------+---------------------+-----------+------------+----------+--------------+--------------+---------------------+

第二十一章

创建表

例子:以customers的创建为例

CREATE TABLE customers
(
        cust_id         int             NOT NULL AUTO_INCREMENT,
        cust_name       char(50)        NOT NULL,
        cust_address    char(50)        NULL,
        cust_city       char(50)        NULL,
        cust_state      char(5)         NULL,
        cust_zip        char(10)        NULL,
        cust_country    char(50)        NULL,
        cust_contact    char(50)        NULL,
        cust_email      char(255)       NULL,
        PRIMARY KEY (cust_id)
)ENGINE = InooDB;

更新表

ALTER TABLE vendors
ADD vend_phone CHAR(20);
+---------+----------------+-----------------+-------------+------------+----------+--------------+------------+
| vend_id | vend_name      | vend_address    | vend_city   | vend_state | vend_zip | vend_country | vend_phone |
+---------+----------------+-----------------+-------------+------------+----------+--------------+------------+
|    1001 | Anvils R Us    | 123 Main Street | Southfield  | MI         | 48075    | USA          | NULL       |
|    1002 | LT Supplies    | 500 Park Street | Anytown     | OH         | 44333    | USA          | NULL       |
|    1003 | ACME           | 555 High Street | Los Angeles | CA         | 90046    | USA          | NULL       |
|    1004 | Furball Inc.   | 1000 5th Avenue | New York    | NY         | 11111    | USA          | NULL       |
|    1005 | Jet Set        | 42 Galaxy Road  | London      | NULL       | N16 6PS  | England      | NULL       |
|    1006 | Jouets Et Ours | 1 Rue Amusement | Paris       | NULL       | 45678    | France       | NULL       |
+---------+----------------+-----------------+-------------+------------+----------+--------------+------------+
ALTER TABLE vendors
DROP vend_phone;
+---------+----------------+-----------------+-------------+------------+----------+--------------+
| vend_id | vend_name      | vend_address    | vend_city   | vend_state | vend_zip | vend_country |
+---------+----------------+-----------------+-------------+------------+----------+--------------+
|    1001 | Anvils R Us    | 123 Main Street | Southfield  | MI         | 48075    | USA          |
|    1002 | LT Supplies    | 500 Park Street | Anytown     | OH         | 44333    | USA          |
|    1003 | ACME           | 555 High Street | Los Angeles | CA         | 90046    | USA          |
|    1004 | Furball Inc.   | 1000 5th Avenue | New York    | NY         | 11111    | USA          |
|    1005 | Jet Set        | 42 Galaxy Road  | London      | NULL       | N16 6PS  | England      |
|    1006 | Jouets Et Ours | 1 Rue Amusement | Paris       | NULL       | 45678    | France       |
+---------+----------------+-----------------+-------------+------------+----------+--------------+

删除表

DROP TABLE customers2;

重命名表

RENAME TBALE customers2 TO customers;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值