示例所用表:
+----+--------------+---------------------------+-------+---------+ | 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 | +----+--------------+---------------------------+-------+---------+
从websites中选取所有记录:
SELECT * FROM websites;
从websites中选取name和country列:
SELECT name,country FROM websides;
SELECT DISTINCT country FROM websites;
下面的 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';
SELECT * FROM Websites ORDER BY alexa;
SELECT * FROM Websites ORDER BY alexa DESC;
SELECT * FROM Websites ORDER BY country,alexa;
INSERT INTO Websites (name, url, alexa, country) VALUES ('百度','https://www.baidu.com/','4','CN');
UPDATE Websites SET alexa='5000', country='USA' WHERE name='菜鸟教程';
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)
从 “Websites” 表中选取头两条记录:
SELECT * FROM Websites LIMIT 2;
从 websites 表中选取前面百分之 50 的记录:
SELECT TOP 50 PERCENT * FROM Websites;
SELECT name AS n, country AS c FROM Websites
SQL 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
运行结果:
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;
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 | +----+------------+-------------------------+---------+
SELECT country FROM Websites UNION SELECT country FROM apps ORDER BY country;
和 UNION 一样只是不用去重
SELECT country FROM Websites UNION ALL SELECT country FROM apps ORDER BY country;
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 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 newtable FROM table1 WHERE 1=0;
(使用本章节最开始的两张表)
INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中。目标表中任何已存在的行都不会受影响。
INSERT INTO Websites(name,country) SELECT app_name,country FROM apps;
INSERT INTO Websites(name, country) SELECT app_name, country FROM apps WHERE id=1;
CREATE DATABASE
用于创建数据库
CREATE DATABASE my_db // 创建一个名字为my_bd的数据库
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) );
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
规定没有给列赋值时的默认值。