SQL语句的基本应用整理

SQL语句一些重要的命令

(sql对大小写不敏感:select与SELECT是相同的)

select 从数据库中提取数据
update 更新数据库中的数据
delete 从数据库中删除数据
insert init 向数据库插入数据
create database 创建数据库
alter database 修改数据库
create table 创建数据表
alter table 变更(修改)数据表
drop table 删除表
create index 创建索引(搜索键)
drop index 删除索引

SQL SELECT语法

select column_name,column_name from table_name;

select * from table_name;

实例
1、SQL语句从‘test’表中取出‘name’‘country’

select name,country from test;

2、SQL语句从‘test’表中选取所有列

select * from test;

SQL SELECT DISTINCT语句

select distinct 语句用于返回唯一不同的值
在表中,一个列可能包含多个重复的值,有时希望仅仅累出不同的(distinct)的值
DISTINCT关键词用于返回唯一不同的值

SQL SELECT DISTINCT语法

select distinct column_name,colimn_name from table_name;

实例
SQL语句仅从‘test’表的‘country’列中选取唯一不同的值,也就是去掉‘country’列重复值

select distinct country from test;

SQL WHERE子句

WHERE子句用于过滤记录
WHERE子句用于提取哪些曼珠指定条件发记录

SQL WHERE语法

select column_name,column_name from table_name where column_name operator value;

实例
SQL语句从‘test’表中选取国家位‘CN’的网站(如果是数值字段,请不要使用引号 )

select * from test where country'CN'

Where +条件(筛选行)
条件:列,比较运算符,值
比较运算符包涵:= > < >= ,<=, !=,<> 表示(不等于)

下列运算符号可以在where子句中使用

=         等于
<>        不等于
>         大于
<         小于
>=        大于等于
<=        小于等于
BETWEEN   在某个范围内
LIKE      搜索某种范围
IN        指定针对某个列的多个可能值

SQL AND&OR 运算符

(and就是和 or就是或者)
AND&OR运算符用于基于一个以上的条件对记录进行过滤
如果第一个条件哥第二个条件成立,AND运算符现实一条记录
如果第一个条件和第二个条件中只有一个成立,则OR运算符显示一条记录

AND运算符实例
1、SQL语句从‘test’表冲选取国家位‘CN’且alexa排名大于‘50’的所有

select * from test where counter='CN' and alexa > 50;

OR 运算符实例
SQL语句从‘test’表中选取国家为‘USA’或者‘CN’的客户

select * from test where country='USA' or country='CN' ;

结合AND和OR 实列
SQL语句 从‘test’表中选取alexa排名大于‘15’且国家为‘CN’‘USA’的

select * from test where alexa > 15 and (country='CN' or country='USA');

SQL ORDER BY关键字

order by 关键字用于对结果集排序
order by关键字用于对结果集按照一个或者多个列进行排序
order by关键字默认按照升序对记录进行排序,如果需要按照降序对记录进行排序,可以使用desc关键字

SQL ORDER BY语法

select conlumn_name,conlumn_name 
from table_name 
order by column_name,column_name asc|desc;

实例
1、SQL语句从‘test’表中选取所有网站,并按照‘alexa’列排序

select  * from test order by alexa;

2、SQL语句从‘test’表中选取所有网站,并按照‘alexa’列降序排序

select * from test order by alexa desc;

3、SQL语句从‘test’表中选取所有网站,并按照‘alexa’和‘country’列排序

select * from test order by country,alexa;

SQL INSERT INTO 语句

insert into 语句用于向表中插入新记录
SQL INSERT INTO 语法
insert into语句可以有两种编写形式
1、第一种形式无需指定要插入数据的列名,只需要提供被插入到值即可

insert into table_name values (value1,value2,value3,...)

2、第二种形式需要指定列名及被插入的值

insert into table_name (column1,column2,column3,...)values (value1,value2,value3...)

实例
1、向test中插入新的行

insert into test (name,url,alexa,counter) values ('百度','www.baidu.com','2','','CN');

2、SQL 语句将插入一个新行,但是只在 “name”、“url” 和 “country” 列插入数据(id 字段会自动更新)

insert into test (name,url,counter) values ('谷歌', 'www.google.com', 'USA');

insert into select 和select into from 的区别
insert into scorebak select * from socre where neza=‘neza’ --插入一行,要求表scorebak 必须存在
select * into scorebak from score where neza=‘neza’ --也是插入一行,要求表scorebak 不存在

SQL UPDATE语句

update 语句用于更新表中已存在的记录
SQL UPDATE语法

update table_name set column1=value1,column2=value2,... where some_column=some_value;

注意 : where子句规定哪条记录或者哪些记录需要更新,省略where子句,所有的记录都会被更新
实例
把test表中 “谷歌” 的 alexa 排名更新为 5000,country 改为 CN(原 排名为1,country 为 USA)

update test ste alexa=5000,country=CN where name=谷歌;

SQL DELETE语句

delete 语句用于删除表中的记录
SQL DELETE语法

delete from table_name where some_column=some_value;

注意:where子句规定哪条记录或者哪些记录需要删除,省略where子句,所有的记录都会被删除
实例
从‘test’表中删除‘谷歌’且国家为USA

delete from test where name='谷歌',COUNTER='USA'

SQL SELECT TOP,LIMIT,ROWNUM子句

select top 子句用于规定要返回的记录 的数目
select top 子句用于拥有数千条的大型表来说,是非常有用的
注意:并非所有的数据库系统都支持select top 语句 ,Mysql支持limit语句来选取指定的数条数据,Oracle可以使用rownum来选取

SQL Server/MS Access 语法

select top number|percent column_name(s) from table_name;

MYSQL语法

select column_name(s) from table_name limit number;

实例

select * from persons limit 5;

Oracle语法

select * from table_name where rownum <= number;
select column_name(s) from table_name where rownum <= 5;

MySQL SELECT LIMIT 实例
选取test表中头两条记录

select * from test limit 2;
select  * from test limit 2 order by desc 
查看后最后两行--desc 表示降序排列 asc 表示升序

SQL SELECT TOP PERCENT 实例
从test表中选取前面百分之 50 的记录

select top 50 percent * from test;
select top 50 * from test order by desc 
查看后最后两行 --desc 表示降序排列 asc 表示升序

SQL LIKE操作符

LIKE操作符用于在where子句中搜索列表中的指定模式

SQL LIME语法

select column_name(s) from table_name where column_name like pattern;

实例:
1、SQL 语句选取 name 以字母 “G” 开始的所有客户

select * from test where name like 'G%';

提示:‘%’符号用于在模式的前后定义通配符(默认字母)
2、SQL 语句选取 name 以字母 “k” 结尾的所有客户

select * from test where name like '%k';

3、SQL 语句选取 name 包含模式 “oo” 的所有客户

select * from test where name like '%oo%'

提示:通过使用 NOT 关键字,您可以选取不匹配模式的记录
4、SQL 语句选取 name 不包含模式 “oo” 的所有客户

select * from test where name not like '%oo%';

SQL 通配符
通配符可以用于代替字符中的任何其他字符
在SQL中,通配符与SQL LIKE操作符一起使用
SQL,可以与一下通配符使用:

%          代替o或者多个字符
_          代替一个字符
[]         字符列表中的任何单一字符
[^]或者[!] 不再字符表中的任何单一字符

将在test表中实验

+----+---------------+---------------------------+-------+---------+
| 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     |
+----+---------------+---------------------------+-------+---------+

实列
使用 SQL % 通配符
SQL 语句选取 url 以字母 “https” 开始的所有网站

select * from test where url like 'https';
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |
+----+---------------+---------------------------+-------+---------+

在这里插入图片描述

SQL 语句选取 url 包含模式 “oo” 的所有网站

select * from test where url like '%oo%';
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  3 | 菜鸟教程       | http://www.runoob.com/    |  5000 | USA     |
|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |
+----+---------------+---------------------------+-------+---------+

使用 SQL _ 通配符
SQL 语句选取 name 以一个任意字符开始,然后是 “oogle” 的所有客户

select * from test where name like '_oogle';
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
+----+---------------+---------------------------+-------+---------+

SQL 语句选取 name 以 “G” 开始,然后是一个任意字符,然后是 “o”,然后是一个任意字符,然后是 “le” 的所有网站

select * from test where name like 'G_o_le';
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
+----+---------------+---------------------------+-------+---------+

使用 SQL [charlist] 通配符
MySQL 中使用 REGEXP 或 NOT REGEXP 运算符 (或 RLIKE 和 NOT RLIKE) 来操作正则表达式。
SQL 语句选取 name 以 “G”、“F” 或 “s” 开始的所有网站

select * from test where name rlike(或者regexp) '^[G,F,s]';
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

SQL 语句选取 name 以 A 到 H 字母开头的网站

select * from test where name rlike(或者regexp) '^[A-H]';
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |
+----+---------------+---------------------------+-------+---------+

SQL 语句选取 name 不以 A 到 H 字母开头的网站

select * from test where name  rlike(或者regexp) '^[^A-H]';
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 | 菜鸟教程       | http://www.runoob.com/    |  5000 | USA     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

SQL IN操作符

in操作符允许在where子句中规定多个值
SQL IN语法

select column_name(s) from table_name where column_name in (value1,value2,...);

演示

+----+---------------+---------------------------+-------+---------+
| 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     |
+----+---------------+---------------------------+-------+---------+

实例
SQL语句选取test name为‘google’‘微博’的所有网站

select * from test where name in ('google','微博')+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |

SQL BETWEEN操作符

BETWEEN 操作符用于选取介于两个值之间的数据范围的值,这个值可以是数值,文本,日期
SQL BETWEEN语法

select column_name(s) from table_name where column_name between value1 and value2;

演示

+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 |百度           | http://www.baidu.com/    |  5000 | CN     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  5 | bilibili      | https://www.bilibili.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

实例
SQL 语句选取 alexa介于 0和3之间的所有网站

select * from test where alexa between 0 and 3;
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  5 | bilibili      | https://www.bilibili.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

NOT BETWEEN 操作符
显示不在上面实例范围内的网站

select * from test where alexa not between 0 and 3;
 +----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 |百度           | http://www.baidu.com/    |  5000 | CN     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
+----+---------------+---------------------------+-------+---------+

带有IN的netween操作符
SQL 语句选取 alexa介于1和20之间但是country不为USA和IND的所有网站

select * from test where (alexa between 1 and 20) and (country not in 'USA','IND');
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
+----+---------------+---------------------------+-------+---------+

带有文本值的BETWEEN操作符
SQL语句 选取neme 以介于’A’ 和’H’之间的字母开始的所有网站

select * from test where name between 'A' and 'H';
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  5 | bilibili      | https://www.bilibili.com/ |     3 | USA     |
+----+---------------+---------------------------+-------+---------+

带有文本值的NOT BETWEEN 操作符
SQL语句 选取name 不介于’A‘’H‘ 之间字母开始的所有网站

select * from test where name not between 'A' and 'H'; 
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 |百度           | http://www.baidu.com/    |  5000 | CN     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

带有日期值的BETWEEN操作符

+-----+---------+-------+------------+
| 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 |
+-----+---------+-------+------------+

SQL语句选取date介于’2016-05-10‘’2016-05-14‘之间的所有访问记录

select * from test where date between '2016-05-10' and '2016-05-14';
+-----+---------+-------+------------+
| aid | site_id | count | date       |
+-----+---------+-------+------------+
|   2 |       3 |   100 | 2016-05-13 |
|   3 |       1 |   230 | 2016-05-14 |
|   4 |       2 |    10 | 2016-05-14 |
|   5 |       5 |   205 | 2016-05-14 |

请注意,在不同的数据库中,BETWEEN 操作符会产生不同的结果!
在某些数据库中,BETWEEN 选取介于两个值之间但不包括两个测试值的字段。
在某些数据库中,BETWEEN 选取介于两个值之间且包括两个测试值的字段。
在某些数据库中,BETWEEN 选取介于两个值之间且包括第一个测试值但不包括最后一个测试值的字段。
因此,请检查您的数据库是如何处理 BETWEEN 操作符!

SQL 别名
通过使用SQL别名 可以为表明称或者列表名称指定别名
基本上,创建别名时为了王列名称的可读性更强
列的SQL别名语法

select column_name as alias_name from table_name;

表的SQL别名语法

select column_name(s) from table_name as alias_name ;

演示
test

+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 |百度           | http://www.baidu.com/    |  5000 | CN     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  5 | bilibili      | https://www.bilibili.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

下面是 text 网站访问记录表数据

+-----+---------+-------+------------+
| 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 |
+-----+---------+-------+------------+

实例
SQL语句指定两个别名,一个是name列的别名,一个是country列的别名
提示:如果列名称包含空格,要求使用双引号或者方括号

select name as n,country c from test;
+---------------+------+
| n          | c       |
+---------------+---------+
| Google        | USA     |
| 淘宝         | CN      |
|百度           | CN     |
| 微博          | CN      |
| bilibili      | USA     |
| stackoverflow | IND     |
+----+---------------+--------+

SQL语句中,把三个列(url,alexa和country)结合在一起,并创建一个命名为’test_info‘

select name as concat('url','alexa','country') as test_info from test;
+---------------+---------------------------+-------+---------+
| name          | test_info                                   |
+---------------+---------------------------+-------+---------+
| Google        | https://www.google.cm/    ,     1 , USA     |
| 淘宝          | https://www.taobao.com/   ,    13 , CN      |
|百度           | http://www.baidu.com/    ,  5000 , CN     |
| 微博           | http://weibo.com/         ,    20 , CN      |
| bilibili      | https://www.bilibili.com/ ,     3 , USA     |
| stackoverflow | http://stackoverflow.com/ ,     0 , IND     |
+----+---------------+---------------------------+-------+---------+

表的别名
SQL语句选取’淘宝‘的所有访问记录,使用test和text表,并分别为他们指定表别名’w‘’a‘(通过使用别名让SQL更简短)

select w.name,w.url,a.count,a.date from test as w,text as a where a.site_id = w.id and w,name='淘宝';
+-----+---------+------------------------------------------+--
| name  |        url                  | count  |      date  |
+-----+---------+------------------------------------------+---
|   淘宝 |     https://www.taobao.com/|    45 | 2016-05-10 |
|   淘宝 |      https://www.taobao.com/ |   100 | 2016-05-13 |
|   淘宝 |       https://www.taobao.com/ |   230 | 2016-05-14 |
|   淘宝 |       https://www.taobao.com/ |    10 | 2016-05-14 |
|   淘宝 |       https://www.taobao.com/ |   205 | 2016-05-14 |
select w.name,w.url,a.dount,a.data 
from test as w ,text as a 
where w.id=a.site_id and w.name = '淘宝';

SQL 连接(JOIN)

SQL join 用于吧来自两个过着多个表的行结合起来,基于这些表之间的共同字段
最长见的JOIN类型:SQL INNER JOIN(简单的JOIN),SQL INNER JOIN 从大哥表中返回
满足JOIN条件的所有行
LEFT JOIN ,RIGHT JOIN ,INNER JOIN ,OUTER JOIN 7种用法

在这里插入图片描述
用一下test数据演示

+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 |百度           | http://www.baidu.com/    |  5000 | CN     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  5 | bilibili      | https://www.bilibili.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

下面是 text网站访问记录表的数据

+-----+---------+-------+------------+
| 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 |
+-----+---------+-------+------------+
select test.id,test,name,text.count,text.date
from test inner join text
on test.id=text.site_id;
或者
select t.id,t,name,x.count,x.date
from test t inner join text x
on t.id=x.site_id;
+-----+---------+----------------------+--
| id  |  name  | count  |      date  |
+-----+---------+----------------------+
| 1   | Google|    45 | 2016-05-10 |
| 1   | Google|   230 | 2016-05-14 |
|   2 |   淘宝|   10 | 2016-05-14 |
|   3 |  百度|   100 | 2016-05-134 |
|   3 |   百度|   220 | 2016-05-154 |
|   3 |   百度|   201 | 2016-05-17 |
|   4 |   微博|   13 | 2016-05-15 |
|   5 |   bilibili|   205 | 2016-05-14 |
|   5 |   bilibili|   545 | 2016-05-16 |

不同的SQL JOIN
列出可以使用的不同的SQL JOIN类型:

  • inner join :如果表中有至少一个匹配,则返回
  • left join :即使用右表中没有匹配,也从左表返回所有的行
  • right join :即使左表中没有匹配,也从右表返回所有的行
  • full join : 只要其中一个表中存在匹配,则返回行

SQL INNER JOIN 关键字

INNER JOIN 关键字在表中存在至少一个匹配返回行
SQL INNER JOIN 语法

select colimn_name(s) 
from table_name1 inner join table_name2
on table_name1.column_name=table2.column_name;

或者

select column_name(s)
from table1
join table2
on table1.column_name=table2.column_name;

注释:inner join和join是相同的

用一下test数据演示

+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.cm/    |     1 | USA     |
|  2 | 淘宝          | https://www.taobao.com/   |    13 | CN      |
|  3 |百度           | http://www.baidu.com/    |  5000 | CN     |
|  4 | 微博           | http://weibo.com/         |    20 | CN      |
|  5 | bilibili      | https://www.bilibili.com/ |     3 | USA     |
|  7 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+

下面是 text网站访问记录表的数据

+-----+---------+-------+------------+
| 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 |
+-----+---------+-------+------------+
select test.name,text.count,text.date
from test inner join text
on test.id=test.site_id
order by text.count;(排序可用可不用)

SQL LEFT JOIN关键字

LEFT JOIN 关键字 从左表(table1)返回所有的行,即使右表(table2)中没有匹配,也会返回结果,结果为NULL

DQL LEFT JOIN 语法
select column_name(s)
from table1
left join table2
on table1.column_name=table2.column_name;

或者

select column_name(s)
from table1
left outer join table2
on table1.column_name=table2.column_name;

注释:在一些数据库中。left join成为left outer join

用一下test数据演示

+----+--------------+---------------------------+-------+---------+
| 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     |
+----+---------------+---------------------------+-------+---------+

下面是 text网站访问记录表的数据

+-----+---------+-------+------------+
| 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 |
+-----+---------+-------+------------+

SQL 语句将返回所有网站及他们的访问量(如果有的话)。以下实例中我们把 test 作为左表,text 作为右表:

select test.name,text.count,text.date
from test
left join text
on test.id=text.site_id
order by text.count desc;

在这里插入图片描述

SQL FULL OUTER JOIN 关键字

FULL OUTER JOIN关键字只要左表(table1)和右表(table2)其中一个表存在匹配,则返回行
FULL OUTER JOIN关键字结合了LIFT JOIN 和RIGHT JOIN 的结果
SQL FULL OUTER JOIN语法

select column_name from table1 full outer join table2 on table1.column_name=table2.column_name;

下面是选自 test 表的数据:

+----+--------------+---------------------------+-------+---------+
| 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     |
+----+---------------+---------------------------+-------+---------+

下面是 text 网站访问记录表的数据:

+-----+---------+-------+------------+
| 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 |
+-----+---------+-------+------------+

MySQL中不支持 FULL OUTER JOIN,你可以在 SQL Server 测试以下实例

select test.name ,text.count,text.date
from test
full outer join text
on test.id=text.site_id
order by text.count desc;

A inner join B 取交集。
A left join B 取 A 全部,B 没有对应的值为 null。
A right join B 取 B 全部 A 没有对应的值为 null。
A full outer join B 取并集,彼此没有对应的值为 null。
对应条件在 on 后面填写。

SQL UNION操作符

SQL UNION操作符合并两或者多个SELECT语句的结果
注意,UNION内部的每个SELECT语句必须拥有相同数量的列,列也必须拥有类似的数据类型,哦那个是,每个SELECT语句中的列的顺序必须相同
SQL UNION语法

select column_name from table1
union
select column_name from table2

注释:默认union操作符选取不同的值,如果允许重复的值,使用union all

SQL UNION ALL 语法

select column_name from table1
union all 
select column_name from table2

下面是选自 test 表的数据:

+----+--------------+---------------------------+-------+---------+
| 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     |
+----+---------------+---------------------------+-------+---------+

下面是 “apps” APP 的数据:

+----+------------+-------------------------+---------+
| 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      |
+----+------------+-------------------------+---------+

SQL UNION 实例
SQL 语句从 “test” 和 “apps” 表中选取所有不同的country(只有不同的值)

select country from test
union 
select country from apps
order by country;

在这里插入图片描述

SQL UNION ALL 实例

SQL 语句使用 UNION ALL 从 “tes” 和 “apps” 表中选取所有的country(也有重复的值)

select country from test
union all
select countr from apps
order by country;

在这里插入图片描述

带有 WHERE 的 SQL UNION ALL

SQL 语句使用 UNION ALL 从 “test” 和 “apps” 表中选取所有的中国(CN)的数据(也有重复的值)

select name,country from test
where country='CN'
union all
select name,country from apps
where country='CN'
order by country;

在这里插入图片描述

SQL SELECT INOT 语句

通过SQL可以从一个表复制信息到另外一个表,然后把数据插入到另外一个新表中
注意:MYSQL数据库不支持SELECT …INTO语句,但支持INSER INTO …SELECT
也可以用一下语句来拷贝表结构及数据

CREATE TABLE 新表
AS
SELECT * FROM 旧表

SQL SELECT INTO 语法
可以父子和所有的列插入到新表中

select * into newtable [IN externaldb] from table;

或者只复制希望的列插入到新表中:

select column_name into newtable [in externaldb] feom table;

提示:新表将会使用SELECT 语句中定义的列名称和类型进行创建。可以使用AS 子句来应用新名称

SQL SELECT INTO 实例
创建test 的备份复件

select * into test2 from test;

只复制一些列插入到新表中

select name , url into test2 from test;

只复制中国的网站插入到新表中

select * into test2 from test where country='CN'';

复制多个表中的数据插入到新表中

select test.name,text.count,text.date 
into test2 
from test 
left join text 
on test.id=text.site_id;

SQL INSERT INTO SELECT 语句

INSERT INTO SELECT语句可以从一个表复制数据,然后把数据插入到一个已经的表中
目标表中任何已存在的行都不会受到影响
SQL INSERT INTO SELECT 语法
可以从一个表复制所有的列插入到另一个已存在的表中

insert into table2 select * from table1;

或者可以只复制希望的列插入到另一个已存在的表中

insert into table2 (column_name) select column_name from table1;

下面是选自 “test” 表的数据:

+----+--------------+---------------------------+-------+---------+
| 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     |
+----+---------------+---------------------------+-------+---------+

下面是 “apps” APP 的数据:

+----+------------+-------------------------+---------+
| 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      |
+----+------------+-------------------------+---------+

复制apps中的数据插入到test中

insert into test (name,country) select apps.name,country from apps 

只复制qq的app到test中

inster into tets (name,country) select apps.name,apps.country from apps where id=1
SQL CREATE DAABASE语句

CREATE DATABASE 语句用于创建数据库
SQL CREATE DATABASE语法

create database dbname;

实例
创建一个名为 my.db的数据库

create database my.db

SQL CREATE TABLE语句

CREATE TABLE 语句用于创建数据库中的表
表由航和列组成,每个表都必须拥有表名
SQL CREATE TABLE 语法

create table table_name
(column_name data_type(size),
column_name data_type(size),
column_name data_type(size),
...
);

column_name 参数规定表中列的名称
data_type 参数规定列的数据类型(列入varchar intger decimal date等)
size 参数规定表中的最大长度

实列
创建一个名为‘test’的表 包含五列 personid lastname firstname address city

create table test
(personid int,
lastname varchar(255),
firstname varchar(255),
daaress varchar(255),
city varchar(255)
);

PersonID 列的数据类型是 int,包含整数。
LastName、FirstName、Address 和 City 列的数据类型是 varchar,包含字符,且这些字段的最大长度为 255 个字符

SQL 约束(Constraints)

SQL约束用于规定表中的数据规则
如果存在违反约束的数据行为,行为会被约束终止
约束可以再擦黄健表时规定(通过CRERTE TABLE语句)或者再表创建之后规定(通过ALTER TABLE 语句)
SQL CREATE TABLE + CONSTRAINT 语法

create table table_name
(column_name1 data_trye(size) constraint_name,
column_name2 data_trye(size) constraint_name,
column_name3 data_trye(size) constraint_name,
...
);
  • NOY NULL 指示某列不能存储NULL值
  • UNIQUE 保证某列的每行必须由唯一的值
  • PRIMARY KEY NOT NULL
    和UNIQUE的结合,确保某列(或者两个列多个列的结合)由唯一标识,有助于更容易更快速的找到表中的一个特定的记录
  • FOREIGN KEY 保证一个表中的数据匹另一个表中的值的参照完整性
  • CHECK 保证列中的值符合指定的条件
  • DEFAULT 规定没有给列赋值时的默认值
PRIMARY KEY 约束的实例
CREATE TABLE Persons
(
    Id_P int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255),
    PRIMARY KEY (Id_P)  //PRIMARY KEY约束
)
CREATE TABLE Persons
(
    Id_P int NOT NULL PRIMARY KEY,   //PRIMARY KEY约束
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255)
)

SQL NOT NULL约束

NOT NULL 约束强制列不接受 NULL 值
NOT NULL 确属强制字段始终包含值,如果不向字段添加值,就无法插入新记录或者更新记录

实例:
SQL 强制 “ID” 列、 “LastName” 列以及 “FirstName” 列不接受 NULL 值

create table persons(
id int not null,
lastname varchar(255) not null,
firstname varchar(255) not null,
age int
);

添加NOT NULL约束
在已经创建的表的‘age’字段中添加not null 约束

alter table persons modify age int not null

删除NOT NULL 约束
在已经创建的表的‘age’字段中删除not null 约束

alter table persons modify age int null;

SQL UNIQUE约束

UNIQUE 约束唯一表示数据库中的每一条记录
UNIQUE和PRIMARY KEY约束均为列集合提供了唯一性的保证
PRIMARY KEY约束拥有自动定义的UNIQUE约束
注意:每个表可以拥有多个UNIQUE 约束 ,但是只能拥有一个PRIMART KEY约束

CREATE TABLE 时的SQL UNIQUE约束
下面的 SQL 在 “Persons” 表创建时在 “P_Id” 列上创建 UNIQUE 约束
MYSQL

create table persons(
P_ID int not null,
lastname varchar (255),
firstname varchar (255),
address varchar (255),
city varchar (255),
unique (P_ID)
);

SQL Server / Oracle / MS Access:

create table persons(
P_ID int not null unique,
lastname varchar (255),
firstname varchar (255),
address varchar (255),
city varchar (255),
);

ALTER TABLE 时的SQL UNIQUE约束
当表已被创建时,需在‘p_id’列创建UNIQUE约束
MySQL / SQL Server / Oracle / MS Access

alter table persons add unique (p_id);

如需命名 UNIQUE 约束,并定义多个列的 UNIQUE 约束,请使用下面的 SQL 语法:

alter table persons add constraint uc_persons unique (p_id,lastname)

撤销UNIQUE约束
MYSQL

alter table persons drop index uc_personsid;

SQL Server / Oracle / MS Access:

alter table persons drop constraint uc_personid;

SQL PRIMARY KEY 约束

PRIMARY KEY 约束唯一标识数据库表中的每条记录
主键必须包含唯一的值
主键列不能包含NULL值
每个表都应该有一个主键,并且每个表只能有一个主键
CREATE TABLE 时的SQL PRIMARY KEY 约束

下面的 SQL 在 “Persons” 表创建时在 “P_Id” 列上创建 PRIMARY KEY 约束
MYSQL

create table persons
(
p_id int not null,
lastname varchar(255) not null ,
firstname varchar(255),
address varchar (255)
primary key (p_id)
);

SQL Server / Oracle / MS Access:

create table persons
(
p_id not null primary key, 
lastname varchar(255) not null ,
firstname varchar(255),
address varchar (255)
);

如需命名 PRIMARY KEY 约束,并定义多个列的 PRIMARY KEY 约束,请使用下面的 SQL 语法:
MySQL / SQL Server / Oracle / MS Access

CREATE TABLE Persons
(
P_Id int NOT NULL,
lastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
constraint pk_personsid primary key (p_id,lastername)
);

注释:在上面的实例中,只有一个主键 PRIMARY KEY(pk_PersonID)。然而,pk_PersonID 的值是由两个列(P_Id 和 LastName)组成的。

ALTER TABLE 时的SQL PRIMARY KEY约束
当表已被创建时,如需在 “P_Id” 列创建 PRIMARY KEY 约束,请使用下面的 SQL:
MySQL / SQL Server / Oracle / MS Access:

alter table persons
add primary key (p_id);

如需命名 PRIMARY KEY 约束,并定义多个列的 PRIMARY KEY 约束,请使用下面的 SQL 语法:
MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)

注释:如果您使用 ALTER TABLE 语句添加主键,必须把主键列声明为不包含 NULL 值(在表首次创建时)。

撤销PRIMARY KEY 约束

MYQL

alter table persons
drop primary key;

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID;

SQL FOREIGN KEY约束

一个表中FOREIGN KEY 指向另一个表中的UNIQUE KEY(唯一约束的键)
persons表

在这里插入图片描述
oeders表
在这里插入图片描述
注意:orders表中的‘p_id’列只想‘persons’表中的‘p_id’列
“Persons” 表中的 “P_Id” 列是 “Persons” 表中的 PRIMARY KEY。
“Orders” 表中的 “P_Id” 列是 “Orders” 表中的 FOREIGN KEY。
FOREIGN KEY 约束用于预防破坏表之间连接的行为。
FOREIGN KEY 约束也能防止非法数据插入外键列,因为它必须是它指向的那个表中的值之一。

CREATE TABLE 时的SQL FORELGN KEY约束

sql在‘orders’表创建时在‘p_id’列上创建FOREIGN KEY 约束
MYSQL

create table orders
(
o_id int not null,
orderno int not null,
p_id int
primary key (o_id),
foreign key (p_id) references persons (p_id)
);

SQL Server / Oracle / MS Access:

create table orders
(
o_id int not null primary key,
orderno int not null,
p_id int foreign key (p_id) references persons (p_id)
);

如需命名 FOREIGN KEY 约束,并定义多个列的 FOREIGN KEY 约束,请使用下面的 SQL 语法:
MySQL / SQL Server / Oracle / MS Access:

create table orders
(
o_id int not null,
ordersno int not null,
p_id int,
primary key (o_id),
constraint fk_perorders foreign key (p_id)
references persons (p_id)
);

alter table 时的SQL FOREIGN KEY 约束
当 “Orders” 表已被创建时,如需在 “P_Id” 列创建 FOREIGN KEY 约束,请使用下面的 SQL
MySQL / SQL Server / Oracle / MS Access

alter table orders
add foreign key (p_id)
references persons (o_id)

如需命名 FOREIGN KEY 约束,并定义多个列的 FOREIGN KEY 约束,请使用下面的 SQL 语法:
MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)

撤销 FOREIGN KEY 约束
MYSQL

alter table orders
drop foreign key fk_perorders

SQL Server / Oracle / MS Access:

alters table orders
drop constraint fk_perorders

SQL CRETAE INDEX 语句

CREATE INDEX语句用于在表中创建索引
在不读取整个表的情况下,索引使数据库应用程序可以更快地查找数据
注解:更新一个包含索引的表需要比更新一个没有索引的表花费更多的实践,这时由于索引本身也需要更新,因此,理想的做法是仅仅在常常被搜索的列上面创建索引

SQL CREATE INDEX语法
在变上创建一个简单的索引,允许使用重复的值

create index index_name
on table_name (column_name)

SQL CREATE UNIQUE INDEX语法
在表上创建一个唯一的索引。不允许使用重复的值:唯一的索引意味着两个不能拥有相同的索引值

cretabe unique index index_name
on table_nem (column_name)

注释:用于创建索引的语法在不同的数据库中不一样,因此,检查数据库中创建索引的语言

CREATE INDEX实例
下面的 SQL 语句在 “test” 表的 “name” 列上创建一个名为 “PIndex” 的索引

create index PIndex
on test (name);

如果您希望索引不止一个列,您可以在括号中列出这些列的名称,用逗号隔开

create index PIndex
on test (name,name2);

SQL 撤销索引,撤销表以及册小数据库

使用DROP语句,可以删除索引,表和数据库
DROP INDEX语句
DROP INDEX语句用于删除表中的索引

alter table table_name drop index index_name

DROP TABLE 语句

DROP TABLE语句用于删除表

drop table table _name

DROP DATABASE语句

DROP DATABASE语句用于删除数据库

drop table database_name

TRUNCATE TABLE 语句

仅仅删除表内的数据,但是并不删除表本身

truncate table table_name

SQL ALTER TABLE 语句

ALTER TABLE 语句用于在已有的表中添加,删除或者修改
在列表中添加列

alter table table_name add column_name datatype

删除表中的列

alter table table_name drop column column_name 

改变表中列的数据类型

SQL Server / MS Access:
alter table table_name alter column column_name datatype

My SQL / Oracle:

alter table table_name modify column column_name datatype

实例
表test
在这里插入图片描述
在表test中添加一个dateofbirth的列

alter table test add dateofbirth date;

注意:新列dateofbirth的类型是date,可以存放日期
现在的test表在这里插入图片描述

改变数据类型实例

改变test表中的dateofbirth列的数据类型

alter table test alter column dateofbirth year

注意:现在dateofbirth列的类型是year。可以存放2位或者4位格式的年份
drop column 实例
删除 test表中的dateofbirth列

alter table test drop column dateofbirth

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值