標籤:index ike 根據 gpo body 索引 運算 組合 長度
1. 建立資料庫CREATE DATABASE database-name
2. 刪除資料庫drop database dbname
3. 建立新表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
4. 增加一個列Alter table tabname add column col type
註:列增加後將不能刪除。DB2中列加上後資料類型也不能改變,唯一能改變的是增加varchar類型的長度。
5. 添加主鍵: Alter table tabname add primary key(col)
刪除主鍵: Alter table tabname drop primary key(col)
6. 建立索引:create [unique] index idxname on tabname(col….)
刪除索引:drop index idxname
註:索引是不可更改的,想更改必須刪除重建立。
7. 建立視圖:create view viewname as select statement
刪除視圖:drop view viewname
8. 幾個簡單的基本的mysql語句
選擇:select * from table1 where 範圍
插入:insert into table1(field1,field2) values(value1,value2)
刪除:delete from table1 where 範圍
更新:update table1 set field1=value1 where 範圍
尋找:select * from table1 where field1 like ’%value1%’ ---like的文法很精妙,查資料!
排序:select * from table1 order by field1,field2 [desc]
總數: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
9.幾個進階查詢運算詞
A: UNION 運算子UNION 運算子通過組合其他兩個結果表(例如 TABLE1 和 TABLE2)並消去表中任何重複行而派生出一個結果表。
當 ALL 隨 UNION 一起使用時 (即 UNION ALL),不消除重複行。兩種情況下,派生表的每一行不是來自 TABLE1 就是來自 TABLE2。
B: EXCEPT 運算子EXCEPT 運算子通過包括所有在 TABLE1 中但不在 TABLE2 中的行並消除所有重複行而派生出一個結果表。
當 ALL 隨 EXCEPT 一起使用時 (EXCEPT ALL),不消除重複行。
C: INTERSECT 運算子INTERSECT 運算子通過只包括 TABLE1 和 TABLE2 中都有的行並消除所有重複行而派生出一個結果表。
當 ALL 隨 INTERSECT 一起使用時 (INTERSECT ALL),不消除重複行。
註:使用運算詞的幾個查詢結果行必須是一致的。
10. 使用外串連
A、left (outer) join:
左外串連(左串連):結果集幾包括串連表的匹配行,也包括左串連表的所有行。
SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
B:right (outer) join:
右外串連(右串連):結果集既包括串連表的匹配串連行,也包括右串連表的所有行。
C:full/cross (outer) join:
全外串連:不僅包括符號串連表的匹配行,還包括兩個串連表中的所有記錄。
11. 分組:Group by:
一張表,一旦分組完成後,查詢後只能得到組相關的資訊。
組相關的資訊:(統計資訊) count,sum,max,min,avg 分組的標準)
在selecte統計函數中的欄位,不能和普通的欄位放在一起;
MySQL表的幾個簡單查詢語句