MySQL命令整理

注:下文中的$:僅僅是一行的開始,不是命令的一部分(雖然這是一句廢話)

1.可以使用多種方式連接上數據庫

2.在進行數據庫操作之前需要先選擇數據庫這個在命令行需要單獨輸入use*命令,但是在java中,在第一步中已經選擇好數據庫了

3.Mysql命令集合

$: show databases;

$: show tables;

$: show columns from myTable;

$: desc myTable;

$: select col1 from myTable;

$: SQL語句不區分大小寫,通常的寫法是將SQL的關鍵字大寫,其他標識符小寫,忽略本文的大小寫習慣

$: select col1,col2 from myTable;

$: select * from myTable;

$: select myTable.col1 from myDatabase.myTable;

$: select col1,col2,col3 from MyTable order by col1,col2;

Order by應放到SQL結果處理語句的最後

$: select col1 from myTable where id > 0;

where過濾行

$: SQL語句中,所有非數字型字面值需要使用單引號擴起來

$: select * from myTable where id > 0 and name is not null;

and優先級高於or,可使用括號避免優先級混亂

4.Mysql進階語句

$: select distinct col1,col2 from myTable;

distinct應用於所有列

$: select col1 from myTable limit 5;

從行0開始的5

$: select col1 from myTable limit 5,3;

從行5開始的3行

$: select col1 from myTable limit 5 offset 3;

從行3開始的5行

$: select col1,col2 from myTable order by col1 desc,col2 asc;

desc降序,默認asc

$: select col1 from myTable where id between 5 and 10;

優於>and<複合語句

$: select col1 from myTable where name is null;

空值檢驗

$: select col1 from myTable where name is not null;

非空檢驗

$: update myTable set col1 = null where name is null;

設置空值,不同於0或 ''

$: select col1 from myTable where id in(exVal1,exVal2);

In子句,通常結合sql子句使用

$: select col1 from myTable where id not in(exVal1,exVal2);

Not in子句

$: select col1 from myTable where name like 'James%';

like子句和多字符通配符%

$: select col1 from myTable where name like 'j_ke';

like子句和單字符通配符_

$:使用Mysql的正則表達式

$: select * from myTable where name regexp 'jack';

name中含有jack,regexp行內匹配like整行匹配

$: select * from myTable where name regexp 'j.ck';

.匹配任意一個字符

$: select * from myTable where name regexp 'jack|jerk';

or匹配

$: select * from myTable where name regexp '[123] to';

匹配多個字符中的一個

$: select * from myTable where name regexp '[^123]to';

字符排除匹配

$: select * from myTable where name regexp '[1-3a-z]t';

匹配字符範圍

$: select * from myTable where name regexp '\\.';

匹配特定字符

$: select * from myTable where name regexp '[123]{2,}t';

*,+,?,{n},{m,},{m,n}多匹配

$: select * from myTable where name regexp '^jack$';

定位元字符,^位於集合內部表示爲否定集合,位於此表示字符串的開始,$表示結束

$:創建計算字段

$: select concat(firstName,' ',lastName) as name  from myTable;

concat函數鏈接字符串,不支持+或 ||

$: select trim(firstName) from myTable;

還支持ltrimrtirm函數

$: select price*num as accounts from myTable;

支持數值計算,as重命名

$:文本處理函數(以下所有表格均爲部分函數)

$: upper()

大寫轉換

$: left()

返回最左字符

$: right()

返回最右字符

$: length()

返回字符串長度

$: lower()

小寫轉換

$: subString()

返回子串

$: locate()

找出子串

$:時間處理函數

$: now()

當前日期和時間

$: curDate()

當前日期

$: curTime()

當前時間

$: date()

返回一個日期時間的日期部分(數值)

$: time()

返回一個日期時間的時間部分(數值)

$: day()

返回一個日期時間的天部分(數值)

$: dayOfWeek()

返回一個日期時間的星期(數值)

$:hour()

返回一個日期時間的小時(數值)

$: minute()

返回一個日期時間的分鐘(數值)

$: month()

返回一個日期時間的月份(數值)

$: second()

返回一個日期時間的秒數(數值)

$: year()

返回一個日期時間的年份(數值)

$: select * from myTable where date(tradeDate)='2016-01-02';

date的文本格式爲yyyy-mm-dd

$: select * from myTable where time(tradeDate)='11:20:10';

time的文本格式爲hh-mm-ss

$:數值處理函數

$: abs()

絕對值

$: cos()

餘弦

$: exp()

指數值

$: pi()

返回圓周率

$: rand()

返回一個隨機數

$: tan()

正切值

$: sin()

正弦值

$: sqrt()

平方根

$: mod()

取餘值

$: 彙總處理函數,可結合distinct關鍵字只計算不重複列,而此時distinct就做用於個別列

$: avg()

取平均

$: count()

行數

$: max()

最大值,支持文本列

$: min()

最小值,支持文本列

$: sum()

求和

$:數據分組

$: select groupId, count(*) as accs from myTable having  accs > 0;

分組伴隨彙總函數,having過濾分組

$: group by,order by是中間結果處理語句,放到中間結果後面

limit爲結果處理語句,放到最後

$:創建表聯結,可用複合查詢解決,但使用聯結語法更簡潔

$: select * from tab1,tab2 where tab1.col2=tab2.col3;

返回表笛卡爾積的where條件子集

$: select * from tab1,tab2,tab3;

返回表笛卡爾積的行集合

$: select * from tab1 inner join tab2 on tab1.col2=tab2.col3;

內部聯結(等值聯結/叉聯結)

$: select * from tab1 t1,tab1,t2 where t1.col1=t2.col1 and t1.col2='jack';

自聯結

$: select * from tab1 left outer join tab2 on tab1.col1 = tab2.col2;

外部聯結

$:使用外部聯結需要指明左右,以指明要輸出所有列的表

無匹配行的其他列爲null值

$:使用Union組合結果,可用複合查詢代替

$:注意所有被組合的查詢結果的欄目列表必須相同

使用order by的話要放倒最後,只能有一個order by

$:使用全文索引

$:對中文的支持需要插件

也可以使用mysql+lucene的做法實現

$: Insert進階

$:利用批處理優化巨量數據插入到(遠程)數據庫

另外可以利用preparedstatement優化

$:插入檢索出的數據即 insert | select

使用的是列位置對應填充

$:修改表結構

$: alter myTable add newCol varchar(10);

添加新列

$: alter myTable drop column certainCol;

刪除某列

$: alter myTable add constraint csName foreign key(col1) references myTable2(col2);

添加外鍵

$: rename table tab1 to tab2;

表重命名

$:使用視圖

$:視圖並不是物理存在使用視圖的目的是爲了查詢結果重用但是每次使用視圖的時候都要重新查詢

$: create view viewName asselect子句

創建視圖

$: drop view viewName;

刪除視圖

$: create or replace view viewName as select子句

更新視圖

$:事務支持

$:可以使用mysql內置的事務支持但是其靈活度不高而且實際直接用的時候也不多

$:java的語句執行默認是一句一個事務之後默認自動提交可以先關閉自動提交然後手動提交和回滾就是實現事務了其內部實現用的就是mysql內置的事務支持

$:用戶管理

$: create user userName@hostIP identified by yourPwd;

創建用戶

$: grant villageName on dataBase.tableName to userdName;

可使用*通配數據庫和數據庫表,賦權

$: show grants for userName;

查看用戶的權限

$: revoke villageName on dataBase.tableName to userName;

可使用*通配數據庫和數據庫表,去權

$: set password for userName = password(yourPwd);

重置密碼

$:其他

1:mysql的innodb存儲引擎支持全文搜索

2:mysql支持存儲過程,存儲過程即使用sql語句編寫的函數

3:mysql支持遊標,只不過遊標僅僅支持在存儲過程中使用

4:mysql支持觸發器,觸發器是在update,delete,insert語句執行前/後觸發執行的mysql語句集合

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章