自己學(xué)習(xí)記錄用
show databases;
use mysql;
show tables;
show columns from customers;//查看customers表的表列
show status;
show create database; //查看創(chuàng)建的特定的數(shù)據(jù)庫(kù)
show grants; //查看授權(quán)用戶安全權(quán)限
show errors;或show warnings;//查看服務(wù)器錯(cuò)誤或警告信息
selecl語(yǔ)句? 用于檢索數(shù)據(jù)
select a from aa;//從aa表檢索名為a的列
select a,b,c from aa;//檢索多個(gè)列,從aa表檢索名為a,b,c的列
select * from aa;//檢索aa表的所有列
select distinct a from aa;//使用DISTINCT關(guān)鍵字伯顶,可以只返回每行唯一的值,即若值相同的話,不會(huì)在檢索結(jié)果出現(xiàn)第二次
limit字句
select a from aa limit 5;//LIMIT 5指示檢索結(jié)果只返回5行的內(nèi)容
select a from aa limit 5,5;//LIMIT 5, 5指示MySQL返回從行5開始的5行。第一個(gè)數(shù)為開始位置赂韵,第二個(gè)數(shù)為要檢索的行數(shù),即從第五行開始挠蛉,檢索出第六到第十行的內(nèi)容
select aa.a from aa;//使用完全限定的表名
order by 字句? 用于給檢索出的結(jié)果排序
select a from aa order by a;//order by a為給列名為a的列排序
select a,b,c from aa order by b,c;//選中a,b,c列祭示,給b,c列排序
select a,b form aa order by a desc,b;//默認(rèn)情況為升序排序,使用desc關(guān)鍵字可以降序排序谴古,desc只應(yīng)用于位于其前的列质涛,所以該語(yǔ)句只給a降序稠歉,b照常排序
select a from aa order by a desc limit 1;//將order by于limit結(jié)合,檢索出值最大的一行
where字句 用于數(shù)據(jù)過濾
select a,b from aa where num=2;//檢索出a,b列中num值為2的行
where字句的操作符:=汇陆、<>(不等于)怒炸、!=(不等于)、 <瞬测、 >横媚、 <=、 >=月趟、 between(指定的兩個(gè)值之間)
select a,b from aa where num between 5 and 10;//檢索出介于5與10之間的值
select a form aa where num is null;//檢索列中存在null值的行
select a,b from aa where num=3 and age=10;//檢索出同時(shí)滿足num=3灯蝴,age=10條件的行
select a,b from aa where num=3 or num=5;//檢索出只要滿足其中一個(gè)條件的行
select a,b from aa where (num=3 or num=5) and age =10;//and操作符在計(jì)算次序中優(yōu)先級(jí)高,所以要加()讓()的先條件過濾孝宗,不然它會(huì)先過濾and后的條件
select a,b from aa where num in (3,5);//該語(yǔ)句功能等同于使用or時(shí)的功能穷躁,檢索出num=3和num=5的所有行
select a,b from aa where num not in (3,5);//檢索出num不等于3或5的所有行
***
利用like操作符 配合通配符 %(匹配多個(gè)字符) 和 _ (匹配單個(gè)字符)來過濾條件
***
select name,age from aa where name like 'aa%';//匹配name列中以aa開頭的所有行
select name,age from aa where name like '%abc%';//匹配name列中只要存在abc這個(gè)值的所有行
select name,age from aa where name like '_ aa';//匹配出如包含1 aa,2 aa,3 aa值的行
select name,age from aa where name like '% aa';//若是%的話,可能匹配出 .1 aa值的行
在where字句中使用regexp關(guān)鍵字? 用正則表達(dá)式來過濾搜索
select num from aa where num regexp '10';//過濾出num列中包含10的行
select num from aa where num regexp '.10';//過濾出num列中包含".10"的行
select num from aa where num regexp '10|20';//正則表達(dá)式的or操作
select name from aa where name regexp '\\.';//過濾出以點(diǎn)結(jié)尾的行因妇,\\.表示查找點(diǎn)
在正則中用\\來轉(zhuǎn)義那些具有特殊意義的字符问潭,比如點(diǎn),|婚被,狡忙?,\什么的
\\f(換頁(yè)) \\n(換行) \\r(回車) \\t(制表) \\v(縱向制表)
****正則中的字符類的匹配*****
[:alnum:] 任意字母和數(shù)字(同[a-zA-Z0-9])
[:alpha:] 任意字符(同[a-zA-Z])
[:blank:] 空格和制表(同[\\t])
[:cntrl:] ASCII控制字符(ASCII 0到31和127)
[:digit:] 任意數(shù)字(同[0-9])
[:graph:] 與[:print:]相同址芯,但不包括空格
[:lower:] 任意小寫字母(同[a-z])
[:print:] 任意可打印字符
[:punct:] 既不在[:alnum:]又不在[:cntrl:]中的任意字符
[:space:] 包括空格在內(nèi)的任意空白字符(同[\\f\\n\\r\\t\\v])
[:upper:] 任意大寫字母(同[A-Z])
[:xdigit:] 任意十六進(jìn)制數(shù)字(同[a-fA-F0-9])
******************************
***********正則中的重復(fù)元字符********
元 字 符 說 明
* 0個(gè)或多個(gè)匹配
+ 1個(gè)或多個(gè)匹配(等于{1,})
? 0個(gè)或1個(gè)匹配(等于{0,1})
{n} 指定數(shù)目的匹配
{n,} 不少于指定數(shù)目的匹配
{n,m} 匹配數(shù)目的范圍(m不超過255)
*****************************************
例子:
select name from producets where name regexp 'stacks?';//匹配包含stack或stacks的行灾茁。stacks的s后面接?,表示s會(huì)出現(xiàn)0或1次
select name from producets where name regexp 'a{4}';//匹配a出現(xiàn)了四次的行
select name from producets where name regexp 'a{4谷炸,}';//匹配a出現(xiàn)了不少于四次的行
select name from producets where name regexp 'a{4北专,6}';//匹配a出現(xiàn)了四次到六次的行
*****正則中的定位符*****
元 字 符 說 明
^ 文本的開始
$ 文本的結(jié)尾
[[:<:]] 詞的開始
[[:>:]] 詞的結(jié)尾
*****************************
計(jì)算字段? <--->? 將位于不同表列的同一行字段拼接在一起,形成一個(gè)新的列旬陡,并用as關(guān)鍵字給其創(chuàng)建別名
字符串拼接用Concat()函數(shù)
例如:
select Concat(vend_name,' (',vend_country,')') from vendors order by vend_name;
//Concat()拼接串拓颓,即把多個(gè)串連接起來形成一個(gè)較長(zhǎng)的串。上面的SELECT語(yǔ)句連接以下4個(gè)元素: 存儲(chǔ)在vend_name列中的名字描孟;包含一個(gè)空格和一個(gè)左圓括號(hào)的串驶睦;存儲(chǔ)在vend_country列中的國(guó)家;包含一個(gè)右圓括號(hào)的串匿醒。
select Concat(RTrim(vend_name),' (',vend_country,')') from vendors order by vend_name;
//使用RTrim()函數(shù)可以去掉值右邊的所有空格
別名alias 用AS關(guān)鍵字
select Concat(vend_name,' (',vend_country,')') as vent_title from vendors;//給拼接的字符串指定了一個(gè)別名vent_title
***執(zhí)行算術(shù)計(jì)算***
select num1,num2,num3,num2*num3 as num2*3 form num;//匹配num1,num2,num3,并將num2,num3相乘并設(shè)置別名
****************函數(shù)******************
##########常用的文本處理函數(shù)###########
函 數(shù) 說 明
Left()? ? ? ? ? ? ? 返回串左邊的字符
Length()? ? ? ? 返回串的長(zhǎng)度
Locate()? ? ? ? ? 找出串的一個(gè)子串
Lower()? ? ? ? ? 將串轉(zhuǎn)換為小寫
LTrim()? ? ? ? ? ? 去掉串左邊的空格
Right()? ? ? ? ? ? 返回串右邊的字符
RTrim()? ? ? ? ? 去掉串右邊的空格
Soundex()? ? ? 返回串的SOUNDEX值场航,SOUNDEX考慮了類似的發(fā)音字符和音節(jié),使得能對(duì)串進(jìn)行發(fā)音比較而不是字母比較
SubString()? ? 返回子串的字符
Upper()? ? ? ? ? 將串轉(zhuǎn)換為大寫
#########################
Soundex()函數(shù)例子:
select cust_name,cust_contact from customers where Soundex(cust_contact)=Soundex('Y Lie');
//假設(shè)現(xiàn)在cust_contact列中存在一行的值為“Y Lee”青抛,若不加Soundex()函數(shù)的話旗闽,因?yàn)槠ヅ溴e(cuò)誤,
將不檢索出該行;而若加上Soundex()函數(shù)适室,由于Lee與Lie發(fā)音相似嫡意,則將該行檢索出來
#############常用日期和時(shí)間處理函數(shù)#############
函 數(shù) 說 明
AddDate() 增加一個(gè)日期(天、周等)
AddTime() 增加一個(gè)時(shí)間(時(shí)捣辆、分等)
CurDate() 返回當(dāng)前日期
CurTime() 返回當(dāng)前時(shí)間
Date() 返回日期時(shí)間的日期部分
DateDiff() 計(jì)算兩個(gè)日期之差
Date_Add() 高度靈活的日期運(yùn)算函數(shù)
Date_Format() 返回一個(gè)格式化的日期或時(shí)間串
Day() 返回一個(gè)日期的天數(shù)部分
DayOfWeek() 對(duì)于一個(gè)日期蔬螟,返回對(duì)應(yīng)的星期幾
Hour() 返回一個(gè)時(shí)間的小時(shí)部分
Minute() 返回一個(gè)時(shí)間的分鐘部分
Month() 返回一個(gè)日期的月份部分
Now() 返回當(dāng)前日期和時(shí)間
Second() 返回一個(gè)時(shí)間的秒部分
Time() 返回一個(gè)日期時(shí)間的時(shí)間部分
Year() 返回一個(gè)日期的年份部分
##########################################
例子:下列的兩個(gè)方法檢索出2005年9月下的所有數(shù)據(jù)
select cust_id,order_num from orders where Data(order_data) between '2005-09-01' and '2005-09-30';
select cust_id,order_num from orders where Year(order_data)=2005 and Month(order_data)=9;
##############常用數(shù)值處理函數(shù)
函 數(shù) 說 明
Abs() 返回一個(gè)數(shù)的絕對(duì)值
Cos() 返回一個(gè)角度的余弦
Exp() 返回一個(gè)數(shù)的指數(shù)值
Mod() 返回除操作的余數(shù)
Pi() 返回圓周率
Rand() 返回一個(gè)隨機(jī)數(shù)
Sin() 返回一個(gè)角度的正弦
Sqrt() 返回一個(gè)數(shù)的平方根
Tan() 返回一個(gè)角度的正切
##########################
匯總數(shù)據(jù)
################SQL聚集函數(shù)#########
函 數(shù) 說 明
AVG() 返回某列的平均值
COUNT() 返回某列的行數(shù),兩個(gè)用法:COUNT(*)汽畴,返回包含null值的行總數(shù)數(shù)旧巾;COUNT(列名),返回不包含null值的行總數(shù)
MAX() 返回某列的最大值
MIN() 返回某列的最小值
SUM() 返回某列值之和
##############################
select AVG(distinct prod_price) as avg_price from products where vend_id =1003;//加入distinct參數(shù)忍些,在計(jì)算平均值時(shí)則只考慮將不同的值進(jìn)行平均計(jì)算
組合聚集函數(shù):
select COUNT(*) as num_items,MIN(prod_price) as price_min,MAX(prod_price) as price_max,AVG(prod_price) as price_vag from products;
**************分組數(shù)據(jù)*************
group by字句
select vend_id,COUNT(*) as num_prods from products group by vend_id;//按vend_id分組鲁猩,組中的每個(gè)vend_id都對(duì)應(yīng)著自己的行數(shù)
利用having字句進(jìn)行過濾分組
select cust_id,COUNT(*) as order from orders group by cust_id having COUNT(*)>=2;//給cust_id分組,并過濾出行數(shù)大于2的那些分組
select vend_id,COUNT(*) as num_prods from products where prod_price >=10 group by vend_id having COUNT(*) >=2;//先過濾出prod_price>=10的行罢坝,按vend_id分組后廓握,再過濾出組中>=2的分組
利用group by分組后,再語(yǔ)句最后加上order by字句進(jìn)行排序
*************SELECT子句及其順序
子 句 說 明 是否必須使用
SELECT 要返回的列或表達(dá)式 是
FROM 從中檢索數(shù)據(jù)的表 僅在從表選擇數(shù)據(jù)時(shí)使用
WHERE 行級(jí)過濾 否
GROUP BY 分組說明 僅在按組計(jì)算聚集時(shí)使用
HAVING 組級(jí)過濾 否
ORDER BY 輸出排序順序 否
LIMIT 要檢索的行數(shù) 否
******************************************************************
drop命令 例如: drop database aaa;drop tables bbb;利用drop命令可以刪除創(chuàng)建的數(shù)據(jù)庫(kù)或表
表操作命令:select嘁酿、insert update delete ---->這四條命令用于表內(nèi)容操作
insert into aa(aa,bb,cc) values('1aa','2bb','3cc');//往aa表插入數(shù)據(jù)隙券,定義aa,bb,cc列名,在values里寫入對(duì)應(yīng)的值
update aa set name='aa' where age='10';//當(dāng)age=10時(shí)闹司,更改name為aa
delete from aa;//刪除aa表所有行娱仔,當(dāng)aa表還是存在的
delete from aa where age='10';//刪除aa表中age=10的那一行
表創(chuàng)建
create table customers
(
? id int not null auto_increment,
? name char(50) not null ,
? address char(50) null ,
? city char(50) null ,
? primary key (id)
)engine=innodb
//創(chuàng)建customers表,定義各個(gè)列名游桩、數(shù)據(jù)類型牲迫,通過primary key指定表的主鍵,通過engine=innodb選擇表的引擎類型
安全管理
創(chuàng)建用戶賬號(hào)
create user aaa identified by '000000'; //創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)用戶aaa,設(shè)置它的密碼為000000
rename user aaa to bbb;//將aaa用戶名更改為bbb
drop user bbb;? //將bbb這個(gè)賬號(hào)刪除(包括相關(guān)的權(quán)限)
show grant for aaa;//通過該條命令可以查看創(chuàng)建的用戶有什么權(quán)限
grant select on aa.* to bbb;//授權(quán)給bbb用戶具有aa數(shù)據(jù)庫(kù)下所有表的select訪問權(quán)限众弓,即只讀訪問權(quán)限
revoke select on aa.* from bbb;//取消賦予bbb用戶的select權(quán)限(該權(quán)限必須存在恩溅,不然會(huì)報(bào)錯(cuò))
grant select,delete,update,create on *.* to examuser@localhost identified by '000000'
更改用戶口令(密碼)
set password for bbb = Password('111111'); //將用戶bbb的密碼更改為111111
set password = Password('123456');? //若不指定用戶隔箍,則更改當(dāng)前用戶密碼