1. 程式人生 > >SQL-SQL基礎

SQL-SQL基礎

這樣的 創建表 trac cad acl 標示 and anti having

SQL(Structured Query Language)是通用的數據庫查詢語言,各個數據庫廠商均對SQL-92標準做了支持,同一時候各家又再次基礎上做了相應擴展,比如oracle的PL/SLQ。

SQL主要分為四大類:
DDL:Data Defined Language,數據定義語言。
DML:Data Munipulation Language,數據改動語言。
DQL:Data Query Language,數據查詢須要。
DCL:Data Control Language,數據控制語言。

說明:
利用DDL對數據庫對象(數據庫、數據表、視圖、索引、序列、存儲過程、觸發器、事務)進行創建、改動、刪除。利用DML可對數據表中的數據進行增、刪、改操縱。

DQL提供對數據表中數據的查詢接口。DCL負責對數據庫中的權限等進行控制。

以下對這幾種類型的數據庫操作語言做簡要描寫敘述。

  • 首先,是DDL數據定義語言。


    對數據庫中對象的定義都屬於DDL,以創建表為例。
    1)創建表
    grammar schema :

create table table_name(
    column_name datatype [not null || null]
    ...
    [constraint]
);

註意:約束的類型主要有 主鍵約束、外鍵約束、檢查約束、非空約束、唯一約束

2)改動表
alter table table_name operation_type
eg:
加入列:alter table table_name add column column_name datatype;
改動列:alter table table_name modify column column_name datatype;
刪除列:alter table table_name drop column column_name;
重命名列:alter table table_name rename column oldname to newname;
重命名表:alter table table_name rename oldname to new name;

3)刪除表
drop table table_name;

外篇:
約束的使用:主鍵約束、外鍵約束、唯一約束、檢查約束、非空約束。

1)主鍵約束:主鍵約束在一個數據庫表中僅僅能有一個,一個主鍵能夠有一列或多列組成。

加入主鍵有三種方式
在列定義的時候指定主鍵(這樣的方式僅僅能指定一列為主鍵約束)

create table table_name(
          .....
          column_name data_type primary key,
          ......
);

在列聲明的後面加入主鍵約束的聲明(這樣的方式能夠聲明多列為主鍵約束)

create table table_name(
    ....
    column_name data_type,
    constraint constraint_name primary key(column1,column2...)
);

在表定義外加入主鍵約束

alter table table_name add constraint constraint_name primary key(column1,column2,column3...)

刪除主鍵約束

alter table table_name drop constraint constraint_name;

2)外鍵約束:能夠保證使用外鍵約束的列與所引用的主鍵約束的數據列一致。一個數據表中能夠有多個外鍵。

外鍵的加入方式與主鍵同樣,基本的語法為:

constraint constraint_name foreign key column(columen_name)
references table_name(column_name) [on delete cascade]

3)唯一約束:可設置在表中輸入的字段都是一味的,與主鍵相似,差別在於主鍵僅僅能有一個,而唯一約束能夠有多個。

唯一約束的加入方式與主鍵同樣。基本的語法為

constraint constraint_name unique(column_name)

4)檢查約束:檢查約束能夠規定每列能夠輸入的值,進而保證數據的正確性。

創建方式同上,基本的語法為。

constraint constraint_name check(condition)

eg:

constraint constraint_name check(age>=18 and age<=30)

5)非空約束:在創建表時。為列加入非空約束,保證該字段必須輸入值。

創建語法為:

create table table_name(
    ...
    column_name datatype not null,
);

移除非空約束:

alter table table_name modify column column_name null;
  • 另外一種是DML語句

    DML包含對數據的增、刪、改操作。


    1)加入數據
    ①向表中直接插入數據

insert into table_name(column1,column2...) values (data1,data2...)[,(data1,data2...)]

②通過還有一個查詢語句的結果向表中插入數據

insert into table_name1(column1,column2...) select data1,data2... from table_name2

③在創建表時。直接從一個源表中取出數據並插入

create table  table_name as select column1,column2... from source_table_name

2)改動數據

update table_name set column_name1=data1,column_name2=data2...
[where condition]

不寫where字句回向表中的全部數據更新。

3)刪除數據

delete from table_name [where condition]

不寫where字句時會刪除表中的全部數據。

4)其它數據操作語句
truncate:truncate語句和delete語句一樣都是為了完畢刪除數據表中的數據的。但兩者是有差別的。用truncate刪除表數據和沒有where字句的delete一樣都是刪除表中的全部數據,但使用truncate會更快一些。

delete語句每次刪除一行,並在事務日誌中為全部刪除的每個記錄一項,truncate通過釋放存儲表數據所用的數據頁來刪除數據。而且僅僅在事務日誌中記錄釋放的頁數,所以用truncate刪除表中全部數據能夠釋放存儲空間,delete則不會。

truncate刪除表中的全部行,但表結構及其列、約束、索引等保持不變。


truncate不能激活觸發器。不能用於參與了視圖的表。

delete刪除數據會產生碎片。truncate不會。

  • 第三章是DQL 查詢語句

select是查詢語句必備的關鍵字,select語句由一系列字句組成,終於檢索數來的數據是由字句決定的。


select語句依照復雜程度可分為:
1)簡單查詢
2)where條件查詢
3)多表查詢
4)子查詢

DQL-select有的基本的語法格式為

select [distinct] column_list from table_list
[where condition] [group by column_name]
[having condition] [ordre by column_name]

1)獲取指定字段的數據

select column1,column2,column3 from table_name

2)獲取全部字段的數據

select column1,column2, ... columnn from table_name
select * from table_name

註:在查詢全部字段的數據時,盡量不要用*,第一由於效率比較低,第二當表結構發生變化時,easy導致程序異常,第三用詳細字段能夠降低網絡開銷。

3)使用別名替代表中的字段名查詢

select column_name as ‘別名‘ from table_name

4)使用函數操作查詢字段

select substr(productid,1,6) from productinfo

5)去除檢索數據中的反復記錄

select distinct category from productinfo

註意:distinct後面假設跟著多個字段,那麽distinct會將這些字段看成一個總體來去除反復數據。

6)對檢索出來的數據進行排序

order by
{expr | position | c_alias} [ASC | DESC] [NULLS FIRST | NULLS LAST][,....]

能夠按字段名稱,位置。別名來排序,當中nulls first和nulls last是對空值的處理方式,ASC為降序排列(默認),DESC為升序排列。

WHERE查詢
where字句中使用的操作符主要有:關系操作符,比較操作符,邏輯操作符。
關系操作符有:<,>,<=,>=,=,!=,<>
比較操作符有:IS NULL,LIKE,BETWEEN…AND…,IN
邏輯操作符有:AND,OR。NOT

7)使用單一限制條件查詢

select productname,quantity from productioninfo where quantity > 20

8)使用多個限制條件查詢

select productname,quantity form productioninfo where
quantity > 20 and quantity < 50
select productname,quantity from productioninfo where 
quantity between 20 and 50

between…and為閉區間。

9)模糊查詢
“_”代表一個字符,“%”代表多個字符。

select productname,productprice from productioninfo where
productionname like ‘%三星%’

10)查詢條件在某個集合內 in

select productname productprice from productioninfo where
catagory in (‘010030002‘,‘0100010001‘)

使用字查詢
在非常多情況下,where後面的條件不是一個確定的值。而是從另外一個查詢語句中的查詢結果,這時就要用到子查詢。
子查詢不僅僅出如今select句子中。也出如今delete和update語句中。

11)子查詢返回單行記錄

select productname, productprice from productinfo where category = (select categoryid from categoryinfo where categoryname = ‘MP3‘);

12)子查詢返回多行數據(in,any some,all)
ANY:標示滿足子查詢結果中的隨意一個。與<,<= 或 >,>=連用
SOME:使用方法與ANY同樣
ALL:標示滿足子查詢中的全部結果
IN:等於子查詢中的隨意一個

eg:
in:

select productname, productprice from productioninfo
where category in (select categoryid from categoryinfo where categoryname = ‘TV‘ or categoryname = ‘MP3‘)

any:
重產品表中查詢出價格低於指定價格列表中的最大值

select productname, productprice from productioninfo
where productprice < any(select productprice from productionifno where category=‘010003002‘) and category
<> ‘010003002‘

some:

select productname, productprice from productioninfo
where productprice = some(selct productprice from productinfo where category = ‘010003002‘and category <> ‘010003002‘

all:檢索數比指定價格還低的數據

select productname, productprice from productioninfo where 
productprice < all(select productprice from productinfo where category = ‘010003002‘)
  • 最後。是連接查詢

    在數據庫設計時。我們一般會吧現實世界的數據依照某種規則拆分成獨立的數據,而這些獨立的數據會依照拆分規則進行聯結。當從數據庫中查詢現實世界須要的數據時,就要依照拆分規則進行查詢,而這樣的規則就是表與表之間的關系。
    實如今存在關系的表之間進行查詢。就須要連接查詢。

連接查詢的分類:內連接、外連接、全連接和自連接。

1)最簡單的內連接

select * from productinfo,categoryinfo

這樣的查詢結果是兩張表的笛卡爾ji,實際情況中。這樣的結果沒有太大的意義。

2)等值內連接
查詢出productinfo 和 categoryinfo中產品類型編碼一致的數據

select p.productname,p.productprice,c.categoryname from 
productinfo p, categoryinfo c where p.category = c.categoryid

或(inner join … on ..)

select p.productname, p.productprice,c.categoryname from 
productinfo p inner join categoryinfo c on p.category = p.categoryid

3)不等值內連接
使用方法與等值連接同樣,將=改為>,>=,<,<=,<>,!=

4)自連接
獲取表productinfo中數量同樣。不同產品的記錄

select p.productname,p.productprice,pr.productname,pr.productprice from productinfo p, productinfo pr where p.quality = pr.quality and p.rowid < pr.rowid

5)外連接

 左外連接:返回左表的全部記錄和右表符合條件的全部記錄
 右外連接:
 全外連接:返回全部匹配成功的記錄。並同一時候返回左、右表未匹配成功的記錄

eg:
左外連接
查詢出productinfo表中每個產品相應的產品類型

select p.productname, p.productprice, p.category, c.categoryid, c.categoryname from productinfo p
left join categoryinfo c on p.category = c.categoryid

右外連接:略

全外連接:用全外連接對產品類型編碼進行匹配

select p.productname, p.productprice p.category , c.categoryid ,c.categoryname from productinfo p full join categoryinfo on p.category = c.categoryid

SQL-SQL基礎