入门语法
示例所用表:
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
SELECT
-
从websites中选取所有记录:
SELECT * FROM websites;
-
从websites中选取name和country列:
SELECT name,country FROM websides;
SELECT DISTINCT
- 仅从 “Websites” 表的 “country” 列中选取唯一不同的值,也就是去掉 “country” 列重复值(去重):
SELECT DISTINCT country FROM websites;
WHERE
-
下面的 SQL 语句从 “Websites” 表中选取国家为 “CN” 的所有网站:
SELECT * FROM websites WHERE country = 'CN'
-
where
中特殊运算符:
BETWEEN
在某个范围内Select * from emp where sal between 1500 and 3000;
LIKE
搜索某种模式Select * from emp where ename like 'M%';
IN
指定针对某个列的多个可能值Select * from emp where sal in (5000,3000,1500);
AND
与条件SELECT * FROM Websites WHERE country='CN' AND alexa > 50;
OR
或条件SELECT * FROM Websites WHERE country='USA' OR country='CN';
ORDER BY
- 将所规定的数据按照排列后顺序输出(默认升序)
SELECT * FROM Websites ORDER BY alexa;
- 降序排列
SELECT * FROM Websites ORDER BY alexa DESC;
- 多列(先按照第一个名字,然后第二个)
SELECT * FROM Websites ORDER BY country,alexa;
INSERT INTO
- 插入操作(插入一组数据)
INSERT INTO Websites (name, url, alexa, country) VALUES ('百度','https://www.baidu.com/','4','CN');
UPDATE
- 一定要加where
UPDATE Websites SET alexa='5000', country='USA' WHERE name='菜鸟教程';
DELETE
- 删除表中的行
DELETE FROM Websites
WHERE name='Facebook' AND country='USA';
- 删除全部表格
DELETE FROM table_name;
//or
DELETE * FROM table_name;
高级语法
演示所用表:
mysql> SELECT * FROM Websites;
+----+---------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+---------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 5000 | USA |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
mysql> SELECT * FROM access_log;
+-----+---------+-------+------------+
| aid | site_id | count | date |
+-----+---------+-------+------------+
| 1 | 1 | 45 | 2016-05-10 |
| 2 | 3 | 100 | 2016-05-13 |
| 3 | 1 | 230 | 2016-05-14 |
| 4 | 2 | 10 | 2016-05-14 |
| 5 | 5 | 205 | 2016-05-14 |
| 6 | 4 | 13 | 2016-05-15 |
| 7 | 3 | 220 | 2016-05-15 |
| 8 | 5 | 545 | 2016-05-16 |
| 9 | 3 | 201 | 2016-05-17 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)
SELECT TOP,LIMIT,ROWNUM
从 “Websites” 表中选取头两条记录:
SELECT * FROM Websites LIMIT 2;
从 websites 表中选取前面百分之 50 的记录:
SELECT TOP 50 PERCENT * FROM Websites;
SQL 别名
- 下面的 SQL 语句指定了两个别名,一个是 name 列的别名,一个是 country 列的别名:
SELECT name AS n, country AS c
FROM Websites
SQL 连接(JOIN)----- IMPORTANT
SQL JOIN 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段。
INNER JOIN
INNER JOIN 关键字在表中存在至少一个匹配时返回行(返回所有重叠部分)
- 返回所有网站的访问记录
SELECT Websites.name, access_log.count, access_log.date
FROM Websites
INNER JOIN access_log
ON websites.id = access_log.site_id
order by access_log.count
运行结果:
LEFT JOIN
- 把 Websites 作为左表,access_log 作为右表
SELECT Websites.name, access_log.count, access_log.date
FROM Websites
LEFT JOIN access_log
ON Websites.id=access_log.site_id
ORDER BY access_log.count DESC;
RIGHT JOIN
- 和LEFT JOIN 相反,如果左边没有匹配,则和左边表相关的属性的值为空
FULL JOIN
- FULL JOIN 结合了 LEFT JOIN 和 RIGHT JOIN 共同的结果
UNION 操作符
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
UNION 内部的每个 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 SELECT 语句中的列的顺序必须相同。
演示数据库:
mysql> SELECT * FROM Websites;
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
mysql> SELECT * FROM apps;
+----+------------+-------------------------+---------+
| id | app_name | url | country |
+----+------------+-------------------------+---------+
| 1 | QQ APP | http://im.qq.com/ | CN |
| 2 | 微博 APP | http://weibo.com/ | CN |
| 3 | 淘宝 APP | https://www.taobao.com/ | CN |
+----+------------+-------------------------+---------+
UNION
SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;
UNION ALL
和 UNION 一样只是不用去重
SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;
带有 WHERE 的 SQL UNION ALL
SELECT country, name FROM Websites
WHERE country='CN'
UNION ALL
SELECT country, app_name FROM apps
WHERE country='CN'
ORDER BY country;
SELECT INTO
SELECT INTO 语句从一个表复制数据,然后把数据插入到另一个新表中。
实例:
- 创建 Websites 的备份复件:
SELECT * INTO WebsiteBackup2016 FROM Websites;
- 只复制一些列插入到新表中:
SELECT name, url INTO WebsitesBackup2016 FROM Websites;
- 只复制中国的网站插入到新表中:
SELECT * INTO WebsitesBackup2016 FROM Websites WHERE country ='CN'
- 复制多个表的数据插入到新表中
SELECT Websites.name, access_log.count, access_log.date INTO WebsitesBackup2016 FROM Websites LEFT JOIN access_log ON Websites.id=access_log.site_id;
- SELECT INTO 语句可用于通过另一种模式创建一个新的空表。只需要添加促使查询没有数据返回的 WHERE 子句即可:
SELECT * INTO newtable FROM table1 WHERE 1=0;
INERT INTO SELECT
(使用本章节最开始的两张表)
INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中。目标表中任何已存在的行都不会受影响。
- 复制 “apps” 中的数据插入到 “Websites” 中:
INSERT INTO Websites(name,country) SELECT app_name,country FROM apps;
- 只复 QQ 的 APP 到 “Websites” 中:
INSERT INTO Websites(name, country) SELECT app_name, country FROM apps WHERE id=1;
CREATE DATABASE
CREATE DATABASE
用于创建数据库
CREATE DATABASE my_db // 创建一个名字为my_bd的数据库
CREATE TABLE
CREATE TABLE table_name//表名
(
column_name1 data_type(size),//列名 | 数据类型
column_name2 data_type(size),
column_name3 data_type(size),
....
);
例如
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
- 在创建数据库时,可以在每一条待创建的列后面加上SQL 约束(Constraints),来约束数据规则:
CREATE TABLE table_name
(
column_name1 data_type(size) [constraint_name],
column_name2 data_type(size) [constraint_name],
column_name3 data_type(size) [constraint_name],
....
);
在此,[constraint_name] 有:
NOT NULL
某列不能储存NULL值,强制不接受NULL值
例如:
ALTER TABLE persons
MODIFY Age int NOT NULL;//在一个已建的表中添加NOT NULL约束
ALTER TABLE Persons
MODIFY Age int NULL;//在一个已建的表中删除NOT NULL约束
ALTER TABLE persons
UNIQUE
某列必须有唯一值??????????
PRIMARY KEY
NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。
FOREIGN KEY
保证一个表中的数据匹配另一个表中的值的参照完整性。
CHECK
保证列中的值符合指定的条件。
DEFAULT
规定没有给列赋值时的默认值。