本文主要介绍MySQL数据库的相关知识,同时通过操作示例介绍MySQL数据库的常用操作。
1 概述
引用MySQL官网中的描述信息,内容如下:
MySQL is the world's most popular open source database. Whether you are a fast growing web property, technology ISV or large enterprise, MySQL can cost-effectively help you deliver high performance, scalable database applications.
2 常用操作
1.1 创建数据库
创建一个名称为“testdb”的数据库,命令如下:
mysql>
mysql> create database testdb;
Query OK, 1 row affected (0.01 sec)
1.2 查询数据库信息
查询数据库信息,观察数据库“testdb”是否创建成功了,命令如下:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| testdb |
+--------------------+
4 rows in set (0.01 sec)
mysql>
上述查询结果表明,数据库“testdb”已经创建成功了。
1.3 创建表
在上面创建的数据库“testdb”中,创建一个名为“customers”的表,命令如下:
mysql>
mysql> use testdb;
Database changed
mysql>
mysql> create table customers (customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name TEXT, last_name TEXT);
Query OK, 0 rows affected (0.03 sec)
mysql>
1.4 查询表信息
使用命令“use testdb;”进入数据库“testdb”后,查询数据库“testdb”中的表信息,观察表“customers”是否创建成功了,命令如下:
mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| customers |
+------------------+
1 row in set (0.00 sec)
mysql>
上述查询结果表明,表“customers”已经创建成功了。
1.5 插入数据
在前面创建的数据库“testdb”中的表“customers”中,执行插入数据操作,命令如下:
mysql> insert into customers values(1, "liitdar", "me");
Query OK, 1 row affected (0.01 sec)
mysql>
1.6 查询数据
查询表“customers”中的数据信息,命令如下:
mysql> select * from customers;
+-------------+------------+-----------+
| customer_id | first_name | last_name |
+-------------+------------+-----------+
| 1 | liitdar | me |
+-------------+------------+-----------+
1 row in set (0.00 sec)
mysql>
从上述查询结果能够看到,1.5节中插入的数据已经在表“customers”中了。
1.7 修改(更新)数据
更新表“customers”中first_name的值为 “liitdar” 的记录,将该记录的last_name的值修改为“itsme”,命令如下:
mysql> update customers set last_name = 'itsme' where first_name = 'liitdar';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
查询表“customers”,观察上面的修改操作是否成功,命令如下:
mysql> select * from customers;
+-------------+------------+-----------+
| customer_id | first_name | last_name |
+-------------+------------+-----------+
| 1 | liitdar | itsme |
+-------------+------------+-----------+
1 row in set (0.01 sec)
mysql>
上述查询结果表明,前面的修改操作已经成功。
1.8 删除数据
删除表“customers”中first_name的值为 “liitdar”,命令如下:
mysql> delete from customers where first_name = 'liitdar';
Query OK, 1 row affected (0.00 sec)
mysql>
查询表“customers”,观察删除操作是否成功,命令如下:
mysql> select * from customers;
Empty set (0.01 sec)
mysql>
上述查询结果表明,删除操作已经成功了。
1.9 查询表属性
查询表的字段名称及属性,命令如下:
mysql> desc roles;
+------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+----------------+
| role_id | int(11) | NO | PRI | NULL | auto_increment |
| occupation | text | YES | | NULL | |
| camp | text | YES | | NULL | |
+------------+---------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
mysql>
1.10 UNION操作符
UNION操作符用于合并两个或多个SELECT语句的结果集。UNION操作符的结果集中不显示重复内容。
注意:UNION内部的每个SELECT语句必须拥有相同数量的列,对应的列也必须拥有相似的数据类型。同时,每个SELECT语句中的列的顺序必须相同。
UNION操作符的用法样式如下:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
例如,表roles和roles_2的内容如下:
mysql> select * from roles;
+---------+------------+----------+
| role_id | occupation | camp |
+---------+------------+----------+
| 1 | warrior | alliance |
| 2 | paladin | alliance |
+---------+------------+----------+
2 rows in set (0.01 sec)
mysql>
mysql> select * from roles_2;
+---------+------------+----------+
| role_id | occupation | camp |
+---------+------------+----------+
| 11 | Mage | alliance |
| 12 | Warlock | alliance |
| 13 | Warlock | Horde |
+---------+------------+----------+
3 rows in set (0.00 sec)
mysql>
现在想要获取这两个表中“occupation”字段的内容,并且不显示重复的内容,此时就可以使用UNION操作符了,命令如下:
mysql> select occupation from roles union select occupation from roles_2;
+------------+
| occupation |
+------------+
| warrior |
| paladin |
| Mage |
| Warlock |
+------------+
4 rows in set (0.01 sec)
mysql>
上述查询结果表明,通过使用UNION操作符,在最终的查询结果中合并了“occupation”字段的重复内容“Warlock”,使其只显示一次。
1.11 删除表
使用DROP命令删除表,语法格式如下:
DROP TABLE table_name;
例如,删除表“occupation_info”,命令如下:
mysql> drop table occupation_info;
Query OK, 0 rows affected (0.01 sec)
1.12 添加表字段
使用“ALTER TABLE”语句中的ADD命令,来向数据表中添加列,同时可以使用“AFTER 字段名”,来设定新添加的字段位于某个字段之后(AFTER语句要在命令的末尾)。
例如,在表mount_info中添加mount_name字段,包括定义其数据类型,以及该字段的位置。命令如下:
mysql> ALTER TABLE mount_info ADD COLUMN mount_name TEXT AFTER mount_id;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加后的mount_info表属性如下:
mysql> desc mount_info;
+------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+----------------+
| mount_id | int(11) | NO | PRI | NULL | auto_increment |
| mount_name | text | YES | | NULL | |
| role_id | int(11) | YES | | NULL | |
+------------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
1.13 查询结果排序(ORDER BY)
使用ORDER BY语句指定列名及排序规则,对查询结果进行排序。
ORDER BY语句默认按照升序(也可使用关键字ASC)对查询结果进行排序,命令如下:
mysql> SELECT * FROM mount_info ORDER BY role_id;
+----------+------------+---------+
| mount_id | mount_name | role_id |
+----------+------------+---------+
| 1 | horse | 1 |
| 3 | sheep | 2 |
| 2 | sheep | 4 |
+----------+------------+---------+
3 rows in set (0.00 sec)
mysql>
如果想要按照降序对查询结果进行排序,需要使用DESC关键字,命令如下:
mysql> SELECT * FROM mount_info ORDER BY role_id DESC;
+----------+------------+---------+
| mount_id | mount_name | role_id |
+----------+------------+---------+
| 2 | sheep | 4 |
| 3 | sheep | 2 |
| 1 | horse | 1 |
+----------+------------+---------+
3 rows in set (0.01 sec)
mysql>
1.14 获取字段的子串(SUBSTR)
使用SUBSTR函数可以获取数据库字段的子内容。
SUBSTR函数的用法如下:
SUBSTR(string, position, substring_length)
说明:position为正数(最小为“1”)时指从左边开始计算位置;position为负数时指从右边开始计算位置。
示例用法如下:
mysql> select camp from roles;
+----------+
| camp |
+----------+
| alliance |
| alliance |
| horde |
| alliance |
| horde |
| alliance |
| horde |
| horde |
+----------+
8 rows in set (0.00 sec)
mysql>
mysql> select SUBSTR(camp,1,3) as sub_camp from roles;
+----------+
| sub_camp |
+----------+
| all |
| all |
| hor |
| all |
| hor |
| all |
| hor |
| hor |
+----------+
8 rows in set (0.00 sec)
mysql>
mysql> select SUBSTR(camp,-3,3) as sub_camp from roles;
+----------+
| sub_camp |
+----------+
| nce |
| nce |
| rde |
| nce |
| rde |
| nce |
| rde |
| rde |
+----------+
8 rows in set (0.00 sec)
mysql>