数据库

2019-11-22  本文已影响0人  allen151

一些最重要的 SQL 命令

指定字段查找

select name,country from Websites;

有重复值的只先一个出来

select distinc country from Websites;

条件查找

select * from Websites where country='CN';

查找的字符

select * from Websites where id=1;

order by 关键字用于对结果集进行排序。

order by 关键字用于对结果集按照一个列或者多个列进行排序。

order by 关键字默认按照升序对记录进行排序。如果需要按照降序对记录进行排序,您可以使用 desc 关键字。

select * from Websites order by alexa desc;
select * from Websites order by country,alexa;

SQL insert into 语句

insert into 语句用于向表中插入新记录。

1.第一种形式无需指定要插入数据的列名,只需提供被插入的值即可:

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

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

insert into Websites (name, url, alexa, country)
values ('百度','https://www.baidu.com/','4','CN');

SQL update 语句

请注意 SQL update 语句中的 where 子句!
where 子句规定哪条记录或者哪些记录需要更新。如果您省略了 where 子句,所有的记录都将被更新!

update Websites set alexa='5000', country='USA' where name='菜鸟教程';

Update 警告!
在更新记录时要格外小心!在上面的实例中,如果我们省略了 where子句,如下所示:

update Websites set alexa='5000', country='USA'

执行以上代码会将 Websites 表中所有数据的 alexa 改为 5000,country 改为 USA。

执行没有 where子句的 update 要慎重,再慎重。

delete 语句

请注意 SQL delete 语句中的 where 子句!
where 子句规定哪条记录或者哪些记录需要删除。如果您省略了 where 子句,所有的记录都将被删除!

delete from Websites
where name='百度' AND country='CN';

SQL select top 子句

select top 子句用于规定要返回的记录的数目。

select top 子句对于拥有数千条记录的大型表来说,是非常有用的。

select * from Persons
where rownum <=5;

SQL like 操作符

like 操作符用于在 where 子句中搜索列中的指定模式。
下面的 SQL 语句选取 name 以字母 "G" 开始的所有客户:

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

提示:%符号用于在模式的前后定义通配符(默认字母)。

下面的 SQL 语句选取 name 以字母 "k" 结尾的所有客户:

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

下面的 SQL 语句选取 name 包含模式 "oo" 的所有客户:

select * from Websites
where name like '%oo%';

通过使用 not 关键字,您可以选取不匹配模式的记录。

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

in 操作符

in 操作符允许您在 where 子句中规定多个值。
in 操作符实例
下面的 SQL 语句选取 name 为 "Google" 或 "菜鸟教程" 的所有网站:

select * from Websites
where name IN ('Google','菜鸟教程');

SQL between 操作符

between 操作符用于选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或者日期。

select * from Websites
where alexa between 1 and 20;

not between 操作符实例

如需显示不在上面实例范围内的网站,请使用 not between:

select * from Websites
where alexa not between 1 and 20;

带有 in 的 between 操作符实例
下面的 SQL 语句选取 alexa 介于 1 和 20 之间但 country 不为 USA 和 IND 的所有网站:

select * from Websites
where (alexa between 1 and 20)
and country not in ('USA', 'IND');

带有文本值的 between 操作符实例
下面的 SQL 语句选取 name 以介于 'A' 和 'H' 之间字母开始的所有网站:

select * from Websites
where name between 'A' and 'H';
select * from Websites
where name not between 'A' and 'H';

带有日期值的 between 操作符实例
下面的 SQL 语句选取 date 介于 '2016-05-10' 和 '2016-05-14' 之间的所有访问记录:

SELECT * FROM access_log
WHERE date BETWEEN '2016-05-10' AND '2016-05-14';

SQL 别名

通过使用 SQL,可以为表名称或列名称指定别名。

select name as n, country as c
from Websites;

在下面的 SQL 语句中,我们把三个列(url、alexa 和 country)结合在一起,并创建一个名为 "site_info" 的别名:

select name, concat(url, ', ', alexa, ', ', country) as site_info
from Websites;

表的别名实例

下面的 SQL 语句选取 "菜鸟教程" 的所访问记录。我们使用 "Websites" 和 "access_log" 表,并分别为它们指定表别名 "w" 和 "a"(通过使用别名让 SQL 更简短):

select w.name, w.url, a.count, a.date 
from Websites as w, access_log as a 
where a.site_id=w.id and w.name="菜鸟教程";

别名应用情景:

SQL join

SQL join 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段。
最常见的 join 类型:SQL inner join(简单的 join)。 SQL inner join 从多个表中返回满足 join 条件的所有行。

select Websites.id, Websites.name, access_log.count, access_log.date
from Websites
inner join access_log
on Websites.id=access_log.site_id;

不同的 SQL JOIN

在我们继续讲解实例之前,我们先列出您可以使用的不同的 SQL JOIN 类型:

SQL left join 实例

下面的 SQL 语句将返回左边的所有记录,即使没有右表的匹配,右表没法有匹配,就以NULL填充:
有的库是left join,有的库是left outer join。 如果是right join ,与其相同,左右交换

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;

SQL full outer join 实例

MySQL中不支持 full outer join,你可以在 SQL Server 测试以下实例。

select Websites.name, access_log.count, access_log.date
from Websites
full outer join access_log
on Websites.id=access_log.site_id
order by access_log.count dese;

SQL UNION 操作符

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 SELECT 语句中的列的顺序必须相同。
有多个同样的值,只取出一个。

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

有多个同样的值,如果希望都取出来,则用union all。

select country from Websites
union all
select country from apps
order by country;

带有 where的 SQL union all

下面的 SQL 语句使用 union all 从 "Websites" 和 "apps" 表中选取所有的中国(CN)的数据(也有重复的值):

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

SQL select into实例

select *
into WebsitesBackup2016
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;

SQL insert into select 语句

insert into select 语句从一个表复制数据,然后把数据插入到一个已存在的表中。目标表中任何已存在的行都不会受影响。

insert into table2
select * from table1;
insert into table2
(column_name(s))
select column_name(s)
from table1;

SQL insert into select 实例

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;

SQL create database语句

create database 语句用于创建数据库。

create database my_db;

数据库表可以通过 create table 语句来添加。

SQL create table语句

create table语句用于创建数据库中的

表由行和列组成,每个表都必须有个表名。

提示:如需了解 MS Access、MySQL 和 SQL Server 中可用的数据类型,请访问我们完整的 数据类型参考手册


SQL create table 实例

现在我们想要创建一个名为 "Persons" 的表,包含五列:PersonID、LastName、FirstName、Address 和 City。

我们使用下面的 create table语句:

实例

create table Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

提示:可使用 INSERT INTO 语句向空表写入数据。

上一篇 下一篇

猜你喜欢

热点阅读