天天看點

MYSQL學習筆記_1

一、基本SQL語句

1. 增、删、改、查及排序:

① 插入:insert into table(field1,field2) values(value1,value2)

② 删除:delete from table where 範圍

③ 更新:update table set field1=value1 where 範圍

④ 查詢:select * from table where 範圍

⑤ 排序:select * from table1 order by field1,field2 [desc]   -- desc為降序

2. 聚合函數:

① 總數:select count * as totalcount from table1

② 求和:select sum(field1) as sumvalue from table1

③ 平均:select avg(field1) as avgvalue from table1

④ 最大:select max(field1) as maxvalue from table1

⑤ 最小:select min(field1) as minvalue from table1

3. 基礎語句:

① 建立資料庫:CREATE DATABASE database-name

② 删除資料庫:drop database dbname

③備份sql server,建立 備份資料的 device

USE master

EXEC sp_addumpdevice \'disk\', \'testBack\', \'c:\mssql7backup\MyNwind_1.dat\'

開始備份:BACKUP DATABASE pubs TO testBack

④ 建立新表:create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

根據已有的表建立新表:

A:create table tab_new like tab_old (使用舊表建立新表)

B:create table tab_new as select col1,col2… from tab_old definition only

⑤ 删除新表:drop table tabname

⑥ 增加一個列:Alter table tabname add column col type

注:列增加後将不能删除。DB2中列加上後資料類型也不能改變,唯一能改變的是增加varchar類型的長度。

⑦主鍵:

添加主鍵:Alter table tabname add primary key(col)

删除主鍵:Alter table tabname drop primary key(col)

⑧ 索引:

建立索引:create [unique] index idxname on tabname(col….)

删除索引:drop index idxname

注:索引是不可更改的,想更改必須删除重建立。

⑨視圖:

建立視圖:create view viewname as select statement

删除視圖:drop view viewname

二、特殊查詢

1. 帶in關鍵字查詢:select 字段1,字段2 frome 表名 where 字段 [not]in(元素1,元素2);

例:select * from t_student where age in (18,20);

  select * from t_student where age not in (18,20);

2. 帶between and的範圍查詢:select 字段1,字段2 frome 表名 where 字段 [not]between 取值1 and 取值2;

例:select * frome t_student where age between 21 and 29;

select * frome t_student where age not between 21 and 29;

3. 帶like的模糊查詢:select 字段1,字段2… frome 表名 where 字段 [not] like ‘字元串’;

“%”代表任意字元;

“_”代表單個字元;

例:select * frome t_student where stuName like ‘張三”;

select * frome t_student where stuName like ‘張三%”;

select * frome t_student where stuName like ‘%張三%”;//含有張三的任意字元

select * frome t_student where stuName like ‘張三_”

4. 空值查詢:select 字段1,字段2…frome 表名 where 字段 is[not] null;

5. 帶and的多條件查詢:

select 字段1,字段2…frome 表名 where 條件表達式1 and 條件表達式2 [and 條件表達式n]

例:select * frome t_student where gradeName=’一年級’ and age=23;

6. 帶or的多條件查詢

select 字段1,字段2…frome 表名 where 條件表達式1 or 條件表達式2 [or 條件表達式n]

例:select * frome t_student where gradeName=’一年級’ or age=23;//或者,條件隻要滿足一個

7. distinct去重複查詢:select distinct 字段名 from 表名;

8. 對查詢結果排序order by:select 字段1,字段2…from 表名 order by 屬性名 [asc|desc]

例:select * frome t_student order by age desc;//降序,從大到小

select * frome t_student order by age asc;//升序,asc預設可以不寫

9.limit 分頁查詢:select 字段1,字段2,…from 表名 limit 初始位置,記錄數;

例:select * from t_student limit 0,5;

10. 分組查詢:group by (先排序後分組)

① 舉例說明:如果要用到group by 一般用到的就是“每這個字” 例如說明現在有一個這樣的表:每個部門有多少人 就要用到分組的技術

例:select DepartmentID as \'部門名稱\' COUNT(*) as \'個數\' from BasicDepartment group by DepartmentID

② 當同時含有where子句、group by 子句 、having子句及聚集函數時,執行順序如下:

-- 執行where子句查找符合條件的資料;

-- 使用group by 子句對資料進行分組;對group by 子句形成的組運作聚集函數計算每一組的值;最後用having 子句去掉不符合條件的組。

-- having 子句中的每一個元素也必須出現在select清單中。有些資料庫例外,如oracle.

-- having子句和where子句都可以用來設定限制條件以使查詢結果滿足一定的條件限制。

-- having子句限制的是組,而不是行。where子句中不能使用聚集函數,而having子句中可以

③ having 和 where的差別:

where :是在對查詢結果進行分組前,将不符合where條件的行去掉,即在分組之前過濾資料,where條件中不能包含聚合函數,使用where條件過濾出特定的行

having :是篩選滿足條件的組,即在分組之後過濾資料,條件中經常包含聚合函數,使用having 條件過濾出特定的組,也可以使用多個分組标準進行分組

11. 特殊語句:

① 随機取出10條資料:select top 10 * from tablename order by newid()

② 随機選擇記錄:select newid()

③ 删除重複記錄:Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)

④ 列出資料庫裡所有的表名:select name from sysobjects where type=\'U\'

⑤ 列出表裡的所有的:select name from syscolumns where id=object_id(\'TableName\')

⑥ 選擇從10到15的記錄:select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc

三、多表連接配接查詢

注意:多表查詢時,一定要找到兩個表中互相關聯的字段,并且作為條件使用

文法:select field1,field2 from table1 inner | left | right join table2 on table1.field1 = table2.field2 and 其他條件;

① 内連接配接查詢:内連接配接查詢和多表連接配接查詢效果是一樣的

② 左外連接配接查詢:左邊表中的資料會優先顯示,右邊表中的資料符合條件才會顯示,不符合條件的會以null進行填充

③ 右外連接配接查詢:右邊表中的資料優先全部顯示,與左連接配接正好相反

④ 全連接配接查詢:顯示左右表中全部資料,是在内連接配接的基礎上增加“左右兩邊沒有顯示的資料”(Mysql不提供full JOIN關鍵字,使用UNION實作)

四、合并查詢

1.union:使用union關鍵字是,資料庫系統會将所有的查詢結果合并到一起,然後去掉相同的記錄;

2.union all:使用union all,不會去除掉重複的記錄;

六、子查詢

1.帶in關鍵字的子查詢(一個查詢語句的條件可能落在另一個select語句的查詢結果中)

2.帶比較運算符的子查詢(子查詢可以使用比較運算符:> = <)

3.帶exists關鍵字的子查詢(加入子查詢查詢到記錄,則進行外層查詢,否則,不執行外層查詢)

4.帶any關鍵字的子查詢(any關鍵字表示滿足其中任一條件)

5.帶all關鍵字的子查詢(all關鍵字表示滿足所有條件)

MYSQL學習筆記_1