MySQL笔记基于教材,笔记中用到的数据库可以点击该链接下载
术语
- 数据库 (database):保存有组织的数据的容器
- DBMS (数据库管理系统)
- 表 (table):某种特定类型数据的结构化清单
- 模式 (schema):关于数据库和表的布局及特性的信息
- 列 (column):表中的一个字段。所有表都是由一个或多个列组成的
- 数据类型 (datatype):所容许的数据的类型
- 行 (row):表中的一个记录 (record)
- 主键 (primary key):一列(或一组列),其值能够唯一区分表中的每个行
- SQL (Structured Query Language):结构化查询语言。SQL是一种专门用来与数据库通信的语言
要点
表中任何列都可以作为主键,列成为主键的条件:
- 任意两行都不具有相同的主键值
- 每个行都必须具有一个主键值(主键不允许NULL值)
- MySQL不区分大小写,但建议将所有关键字大写,将数据库的名称小写
- MySQL忽略所有换行符,但建议分行以方便阅读
命令
USE
通过USE
关键字选择一个数据库进行操作,数据库选择成功后显示Database changed
消息。
mysql> USE crashcourse;
Database changed
记住,必须先使用USE
打开数据库,才能读取其中的数据。
SHOW
(1) SHOW DATABASES
返回可用数据库的一个列表
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| crashcourse |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)
(2) SHOW TABLES
返回当前选择的数据库内可用表的列表
mysql> SHOW TABLES;
+-----------------------+
| Tables_in_crashcourse |
+-----------------------+
| customers |
| orderitems |
| orders |
| productnotes |
| products |
| vendors |
+-----------------------+
6 rows in set (0.00 sec)
(3) SHOW COLUMNS
要求给出一个表名(在例子中对应 FROM customers
),它对每个字段返回一行。行中包含字段名、数据类型、是否允许NULL、键信息、默认值以及其他信息(如字段cust_id
的auto_increment
)
自动增量,即
auto_increment
是MySQL保持键值唯一的一个功能。
mysql> SHOW COLUMNS FROM customers;
+--------------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------+------+-----+---------+----------------+
| cust_id | int(11) | NO | PRI | NULL | auto_increment |
| cust_name | char(50) | NO | | NULL | |
| cust_address | char(50) | YES | | NULL | |
| cust_city | char(50) | YES | | NULL | |
| cust_state | char(5) | YES | | NULL | |
| cust_zip | char(10) | YES | | NULL | |
| cust_country | char(50) | YES | | NULL | |
| cust_contact | char(50) | YES | | NULL | |
| cust_email | char(255) | YES | | NULL | |
+--------------+-----------+------+-----+---------+----------------+
9 rows in set (0.01 sec)
SHOW COLUMNS FROM
语句等价于DESCRIBE
语句。可以用DESCRIBE customers
达到例子中同样的效果。
(4) 其他SHOW语句
SHOW STATUS; 用于显示广泛的服务器状态信息
SHOW CREATE DATABASE; 显示创建特定数据库
SHOW CREATE TABLE; 显示创建特定表
SHOW GRANTS; 显示授予用户的安全权限
SHOW ERRORS; 显示服务器错误消息
SHOW WARNINGS; 显示服务器警告消息
SELECT
(1) 用SELECT检索单个/多个/所有列
多个列的名称用逗号分隔
检索所有列时需要使用通配符*
单个列
mysql> 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) |
+----------------+
14 rows in set (0.00 sec)
多个列
mysql> 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 |
+---------+----------------+------------+
14 rows in set (0.00 sec)
所有列
mysql> 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 |
+---------+---------+----------------+------------+----------------------------------------------------------------+
14 rows in set (0.00 sec)
(2) 用DISTINCT对结果初步合并
使用SELECT DISTINCT
后结果中不同的值只出现一次
mysql> SELECT DISTINCT vend_id
-> FROM products;
+---------+
| vend_id |
+---------+
| 1001 |
| 1002 |
| 1003 |
| 1005 |
+---------+
4 rows in set (0.01 sec)
不能在一次SELECT中部分使用DISTINCT合并
(3) 用LIMIT限制检索结果
SELECT ...
FROM ...
LIMIT 参数1, 参数2
用法1:仅限制结果数(作为参数1)
用法2:限制检索开始行数(作为参数1)和检索结果数(作为参数2)
用法1
mysql> SELECT prod_name
-> FROM products
-> LIMIT 5;
+--------------+
| prod_name |
+--------------+
| .5 ton anvil |
| 1 ton anvil |
| 2 ton anvil |
| Detonator |
| Bird seed |
+--------------+
5 rows in set (0.00 sec)
用法2
mysql> SELECT prod_name
-> FROM products
-> LIMIT 5, 5; 从第5(arg1)行开始的5(arg2)行结果
+--------------+
| prod_name |
+--------------+
| Carrots |
| Fuses |
| JetPack 1000 |
| JetPack 2000 |
| Oil can |
+--------------+
5 rows in set (0.00 sec)
第一行为行0
MySQL系列笔记