CREATE DATABASE 数据库名;
例如:
mysql> CREATE DATABASE stu;
Query OK, 1 row affected (0.01 sec)
drop database 数据库名;
例如:
mysql> DROP DATABASE stu;
Query OK, 0 rows affected (0.02 sec)
USE 数据库名;
例如:
mysql> USE test;
Database changed
MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。
创建MySQL数据表需要以下信息:表名、表字段名、定义每个表字段
CREATE TABLE 表名 (列名 列的类型);
例如:
mysql> CREATE TABLE test1(
-> id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> title VARCHAR(100) NOT NULL,
-> author VARCHAR(20) NOT NULL,
-> sub_date DATE);
Query OK, 0 rows affected (0.03 sec)
注意:MySQL命令终止符为分号 ;
注意: -> 是换行符标识,不要复制
DROP TABLE 表名;
例如:
mysql> DROP TABLE test1;
Query OK, 0 rows affected (0.05 sec)
INSERT 表名 ( 列1, 列2,...列N )VALUES ( 值1,值2,...值N );
例如:
mysql> INSERT test1(
-> title,author,sub_date)
-> VALUES
-> ("Java","Jom",NOW());
Query OK, 1 row affected, 1 warning (0.00 sec)
注意: 使用箭头标记 -> 不是 SQL 语句的一部分,它仅仅表示一个新行,如果一条SQL语句太长,我们可以通过回车键来创建一个新行来编写 SQL 语句,SQL 语句的命令结束符为分号 ;
SELECT * FROM 表名;
例如:
mysql> SELECT * FROM test1;
+----+-------+--------+------------+
| id | title | author | sub_date |
+----+-------+--------+------------+
| 1 | Java | Jom | 2019-03-27 |
+----+-------+--------+------------+
1 row in set (0.00 sec)
使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据
SELECT 列1,列2,..列N FROM 表1, 表2...
[WHERE 条件1 [AND [OR]] 条件2.....
例如:
mysql> SELECT * FROM test1 WHERE author ='Jom';
+----+-------+--------+------------+
| id | title | author | sub_date |
+----+-------+--------+------------+
| 1 | Java | Jom | 2019-03-27 |
+----+-------+--------+------------+
1 row in set (0.01 sec)
以下为操作符列表,可用于 WHERE 子句中。
下表中实例假定 A 为 10, B 为 20
BINARY 关键字
MySQL 的 WHERE 子句的字符串比较是不区分大小写的。 你可以使用 BINARY 关键字来设定 WHERE 子句的字符串比较是区分大小写的。
例如:
mysql> SELECT * FROM test1 WHERE BINARY title='java';
Empty set (0.01 sec)
mysql> SELECT * FROM test1 WHERE BINARY title='Java';
+----+-------+--------+------------+
| id | title | author | sub_date |
+----+-------+--------+------------+
| 1 | Java | Jom | 2019-03-27 |
+----+-------+--------+------------+
1 row in set (0.00 sec)
如果我们需要修改或更新 MySQL 中的数据,我们可以使用 SQL UPDATE 命令来操作
UPDATE 表名 SET 列1=new-value1, 列2=new-value2 [WHERE 条件]
例如:
mysql> UPDATE test1 SET title='C++' WHERE id=3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查看test1表:发现id为3的title从MySQL改为iOS
mysql> SELECT * FROM test1;
+----+-------+--------+------------+
| id | title | author | sub_date |
+----+-------+--------+------------+
| 1 | Java | Jom | 2019-03-27 |
| 2 | PHP | Tom | 2019-03-27 |
| 3 | C++ | LiSa | 2019-03-27 |
| 4 | iOS | Lili | 2019-03-27 |
+----+-------+--------+------------+
4 rows in set (0.00 sec)
可以使用 SQL 的 DELETE FROM 命令来删除 MySQL 数据表中的记录
DELETE FROM 表名 [WHERE 条件]
例如:
mysql> DELETE FROM test1 WHERE id=3;
Query OK, 1 row affected (0.01 sec)
查询一下发现id为3的记录被删除了
mysql> SELECT * FROM test1;
+----+-------+--------+------------+
| id | title | author | sub_date |
+----+-------+--------+------------+
| 1 | Java | Jom | 2019-03-27 |
| 2 | PHP | Tom | 2019-03-27 |
| 4 | iOS | Lili | 2019-03-27 |
+----+-------+--------+------------+
3 rows in set (0.00 sec)
SELECT 列1,列2,...列N FROM 表名 WHERE 列1 LIKE 条件1 [AND [OR]] filed2 = 'somevalue'
例如:
mysql> SELECT * FROM test1 WHERE author LIKE "%m";
+----+-------+--------+------------+
| id | title | author | sub_date |
+----+-------+--------+------------+
| 1 | Java | Jom | 2019-03-27 |
| 2 | PHP | Tom | 2019-03-27 |
+----+-------+--------+------------+
2 rows in set (0.00 sec)
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。
语法
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
参数
expression1, expression2, ... expression_n: 要检索的列。
tables: 要检索的数据表。
WHERE conditions: 可选, 检索条件。
DISTINCT: 可选,删除结果集中重复的数据。默认情况下 UNION 操作符已经删除了重复数据,所以 DISTINCT 修饰符对结果没啥影响。
ALL: 可选,返回所有结果集,包含重复数据。
例子:
"Websites" 表的数据:
mysql> SELECT * FROM Websites;
+----+---------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+---------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.google | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 6 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
6 rows in set (0.00 sec)
"apps" APP 的数据:
mysql> SELECT * FROM apps;
+----+------------+-------------------------+---------+
| id | name | url | country |
+----+------------+-------------------------+---------+
| 1 | QQ APP | http://im.qq.com/ | CN |
| 2 | WebBO APP | http://weibo.com/ | CN |
| 3 | TaoBao APP | http://www,taobao.com/ | CN |
+----+------------+-------------------------+---------+
3 rows in set (0.00 sec)
从 "Websites" 和 "apps" 表中选取所有不同的country(只有不同的值):
mysql> SELECT country FROM Websites
-> UNION
-> SELECT country FROM apps
-> ORDER BY country;
+---------+
| country |
+---------+
| CN |
| IND |
| USA |
+---------+
3 rows in set (0.00 sec)
注释:UNION 不能用于列出两个表中所有的country。如果一些网站和APP来自同一个国家,每个国家只会列出一次。UNION 只会选取不同的值。请使用 UNION ALL 来选取重复的值!
UNION ALL 的例子:
mysql> SELECT country FROM Websites UNION ALL SELECT country FROM apps ORDER BY country;
+---------+
| country |
+---------+
| CN |
| CN |
| CN |
| CN |
| CN |
| CN |
| IND |
| USA |
| USA |
+---------+
9 rows in set (0.00 sec)
带有 WHERE 的 SQL UNION AL
使用 UNION ALL 从 “Websites” 和 “apps” 表中选取所有的中国(CN)的数据(也有重复的值):
mysql> SELECT country,name FROM Websites WHERE country='CN'
-> UNION ALL
-> SELECT country,name FROM apps WHERE country='CN'
-> ORDER BY country;
+---------+--------------+
| country | name |
+---------+--------------+
| CN | 淘宝 |
| CN | 菜鸟教程 |
| CN | 微博 |
| CN | QQ APP |
| CN | WebBO APP |
| CN | TaoBao APP |
+---------+--------------+
6 rows in set (0.00 sec)
ORDER BY 子句来设定你想按哪个字段哪种方式来进行排序,再返回搜索结果
SELECT 列1, 列2,...列N FROM 表1, 表2...ORDER BY 列1, 列2...] [ASC [DESC]]
例子:
mysql> SELECT * FROM test1 WHERE author LIKE '%m' ORDER BY sub_date DESC;
+----+-------+--------+------------+
| id | title | author | sub_date |
+----+-------+--------+------------+
| 1 | Java | Jom | 2019-03-27 |
| 2 | PHP | Tom | 2019-03-27 |
+----+-------+--------+------------+
2 rows in set (0.00 sec)