一些最重要的 SQL 命令
- select - 從數(shù)據(jù)庫中提取數(shù)據(jù)
- update - 更新數(shù)據(jù)庫中的數(shù)據(jù)
- delete - 從數(shù)據(jù)庫中刪除數(shù)據(jù)
- insert into - 向數(shù)據(jù)庫中插入新數(shù)據(jù)
- create database - 創(chuàng)建新數(shù)據(jù)庫
- alter database - 修改數(shù)據(jù)庫
- create table - 創(chuàng)建新表
- alter table - 變更(改變)數(shù)據(jù)庫表
- drop table - 刪除表
- create index - 創(chuàng)建索引(搜索鍵)
- drop index - 刪除索引
指定字段查找
select name,country from Websites;
有重復(fù)值的只先一個出來
select distinc country from Websites;
條件查找
select * from Websites where country='CN';
查找的字符
- 文本字段 vs. 數(shù)值字段
如果是文本就要單引號,如果是數(shù)值就不用單引號。
select * from Websites where id=1;
order by 關(guān)鍵字用于對結(jié)果集進(jìn)行排序。
order by 關(guān)鍵字用于對結(jié)果集按照一個列或者多個列進(jìn)行排序。
order by 關(guān)鍵字默認(rèn)按照升序?qū)τ涗涍M(jìn)行排序。如果需要按照降序?qū)τ涗涍M(jìn)行排序,您可以使用 desc 關(guān)鍵字。
select * from Websites order by alexa desc;
select * from Websites order by country,alexa;
SQL insert into 語句
insert into 語句用于向表中插入新記錄。
- SQL insert into 語法
insert into 語句可以有兩種編寫形式。
1.第一種形式無需指定要插入數(shù)據(jù)的列名,只需提供被插入的值即可:
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 子句規(guī)定哪條記錄或者哪些記錄需要更新。如果您省略了 where 子句,所有的記錄都將被更新!
update Websites set alexa='5000', country='USA' where name='菜鳥教程';
Update 警告!
在更新記錄時要格外小心!在上面的實例中,如果我們省略了 where子句,如下所示:
update Websites set alexa='5000', country='USA'
執(zhí)行以上代碼會將 Websites 表中所有數(shù)據(jù)的 alexa 改為 5000,country 改為 USA。
執(zhí)行沒有 where子句的 update 要慎重,再慎重。
delete 語句
請注意 SQL delete 語句中的 where 子句!
where 子句規(guī)定哪條記錄或者哪些記錄需要刪除。如果您省略了 where 子句,所有的記錄都將被刪除!
delete from Websites
where name='百度' AND country='CN';
SQL select top 子句
select top 子句用于規(guī)定要返回的記錄的數(shù)目。
select top 子句對于擁有數(shù)千條記錄的大型表來說,是非常有用的。
select * from Persons
where rownum <=5;
SQL like 操作符
like 操作符用于在 where 子句中搜索列中的指定模式。
下面的 SQL 語句選取 name 以字母 "G" 開始的所有客戶:
select * from Websites
where name like 'G%';
提示:%符號用于在模式的前后定義通配符(默認(rèn)字母)。
下面的 SQL 語句選取 name 以字母 "k" 結(jié)尾的所有客戶:
select * from Websites
where name like '%k';
下面的 SQL 語句選取 name 包含模式 "oo" 的所有客戶:
select * from Websites
where name like '%oo%';
通過使用 not 關(guān)鍵字,您可以選取不匹配模式的記錄。
select * from Websites
where name not like '%oo%';
in 操作符
in 操作符允許您在 where 子句中規(guī)定多個值。
in 操作符實例
下面的 SQL 語句選取 name 為 "Google" 或 "菜鳥教程" 的所有網(wǎng)站:
select * from Websites
where name IN ('Google','菜鳥教程');
SQL between 操作符
between 操作符用于選取介于兩個值之間的數(shù)據(jù)范圍內(nèi)的值。這些值可以是數(shù)值、文本或者日期。
select * from Websites
where alexa between 1 and 20;
not between 操作符實例
如需顯示不在上面實例范圍內(nèi)的網(wǎng)站,請使用 not between:
select * from Websites
where alexa not between 1 and 20;
帶有 in 的 between 操作符實例
下面的 SQL 語句選取 alexa 介于 1 和 20 之間但 country 不為 USA 和 IND 的所有網(wǎng)站:
select * from Websites
where (alexa between 1 and 20)
and country not in ('USA', 'IND');
帶有文本值的 between 操作符實例
下面的 SQL 語句選取 name 以介于 'A' 和 'H' 之間字母開始的所有網(wǎng)站:
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)結(jié)合在一起,并創(chuàng)建一個名為 "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="菜鳥教程";
別名應(yīng)用情景:
- 在查詢中涉及超過一個表
- 在查詢中使用了函數(shù)
- 列名稱很長或者可讀性差
- 需要把兩個列或者多個列結(jié)合在一起
SQL join
SQL join 子句用于把來自兩個或多個表的行結(jié)合起來,基于這些表之間的共同字段。
最常見的 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
在我們繼續(xù)講解實例之前,我們先列出您可以使用的不同的 SQL JOIN 類型:
- INNER JOIN:如果表中有至少一個匹配,則返回行
- LEFT JOIN:即使右表中沒有匹配,也從左表返回所有的行
- RIGHT JOIN:即使左表中沒有匹配,也從右表返回所有的行
- FULL 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 語句的結(jié)果集。
請注意,UNION 內(nèi)部的每個 SELECT 語句必須擁有相同數(shù)量的列。列也必須擁有相似的數(shù)據(jù)類型。同時,每個 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)的數(shù)據(jù)(也有重復(fù)的值):
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實例
- 創(chuàng)建 Websites 的備份復(fù)件:
select *
into WebsitesBackup2016
from Websites;
- 只復(fù)制一些列插入到新表中:
select name, url
into WebsitesBackup2016
from Websites;
- 只復(fù)制中國的網(wǎng)站插入到新表中:
select *
into WebsitesBackup2016
from Websites
where country='CN';
- 復(fù)制多個表中的數(shù)據(jù)插入到新表中:
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 語句可用于通過另一種模式創(chuàng)建一個新的空表。只需要添加促使查詢沒有數(shù)據(jù)返回的 where 子句即可:
select *
into newtable
from table1
where 1=0;
SQL insert into select 語句
insert into select 語句從一個表復(fù)制數(shù)據(jù),然后把數(shù)據(jù)插入到一個已存在的表中。目標(biāo)表中任何已存在的行都不會受影響。
- SQL insert into select 有兩種作用
我們可以從一個表中復(fù)制所有的列插入到另一個已存在的表中:
insert into table2
select * from table1;
- 或者我們可以只復(fù)制希望的列插入到另一個已存在的表中:
insert into table2
(column_name(s))
select column_name(s)
from table1;
SQL insert into select 實例
- 復(fù)制 "apps" 中的數(shù)據(jù)插入到 "Websites" 中:
insert into Websites (name, country)
select app_name, country from apps;
只復(fù) QQ 的 APP 到 "Websites" 中:
insert into Websites (name, country)
select app_name, country from apps
where id=1;
SQL create database語句
create database 語句用于創(chuàng)建數(shù)據(jù)庫。
- SQL create database實例
create database my_db;
數(shù)據(jù)庫表可以通過 create table 語句來添加。
SQL create table語句
create table語句用于創(chuàng)建數(shù)據(jù)庫中的表。
表由行和列組成,每個表都必須有個表名。
提示:如需了解 MS Access、MySQL 和 SQL Server 中可用的數(shù)據(jù)類型,請訪問我們完整的 數(shù)據(jù)類型參考手冊。
SQL create table 實例
現(xiàn)在我們想要創(chuàng)建一個名為 "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)
);
PersonID 列的數(shù)據(jù)類型是 int,包含整數(shù)。
LastName、FirstName、Address 和 City 列的數(shù)據(jù)類型是 varchar,包含字符,且這些字段的最大長度為 255 個字符。
提示:可使用 INSERT INTO 語句向空表寫入數(shù)據(jù)。