1 #登錄數(shù)據(jù)庫(kù) 2 mysql - hlocalhost - uroot - p; 3 #修改密碼 4 mysqladmin - uroot - pold password new; 5 6 7 #顯示數(shù)據(jù)庫(kù) 8 show databases; 9 #顯示數(shù)據(jù)表 10 show tables; 11 #選擇數(shù)據(jù)庫(kù) 12 use examples; 13 #創(chuàng)建數(shù)據(jù)庫(kù)并設(shè)置編碼utf - 8 多語(yǔ)言 14 create database `examples` default character set utf8 collate utf8_general_ci; 15 #刪除數(shù)據(jù)庫(kù) 16 drop database examples; 17 #創(chuàng)建表 18 create table test( 19 id int ( 10 ) unsigned zerofill not null auto_increment, 20 email varchar ( 40 ) not null , 21 ip varchar ( 15 ) not null , 22 state int ( 10 ) not null default ' -1 ' , 23 primary key (id) 24 )engine = InnoDB; 25 #顯示表結(jié)構(gòu) 26 describe 27 #刪除表 28 drop table test; 29 #重命名表 30 alter table test_old rename test_new; 31 #添加列 32 alter table test add cn int ( 4 ) not null ; 33 #修改列 34 alter table test change id id1 varchar ( 10 ) not null ; 35 #刪除列 36 alter table test drop cn; 37 #創(chuàng)建索引 38 alter table test add index (cn,id); 39 #刪除索引 40 alter table test drop index cn 41 #插入數(shù)據(jù) 42 insert into test (id,email,ip,state) values ( 2 , ' qq@qq.com ' , ' 127.0.0.1 ' , ' 0 ' ); 43 #刪除數(shù)據(jù) 44 delete from test where id = 1 ; 45 #修改數(shù)據(jù) 46 update test set id = ' 1 ' ,email = ' q@qq.com ' where id = 1 ; 47 #查數(shù)據(jù) 48 select * from test; #取所有數(shù)據(jù) 49 select * from test limit 0 , 2 ; #取前兩條數(shù)據(jù) 50 select * from test email like ' %qq% ' #查含有qq字符 _表示一個(gè) % 表示多個(gè) 51 select * from test order by id asc ;#降序desc 52 select * from test id not in ( ' 2 ' , ' 3 ' );#id不含2,3或者去掉not表示含有 53 select * from test timer between 1 and 10 ;#數(shù)據(jù)在1,10之間 54 55 # -- -------------------------表連接知識(shí)------------------------------ 56 #等值連接又叫內(nèi)鏈接 inner join 只返回兩個(gè)表中連接字段相等的行 57 select * from A inner join B on A.id = B.id; #寫法1 58 select * from A,B where A.id = B.id; #寫法2 59 select a.id,a.title from A a inner join B b on a.id = b.id and a.id = 1 ;#寫法3 表的臨時(shí)名稱 60 select a.id as ID,a.title as 標(biāo)題 from A inner join B on A.id = B.id;#添加as字句 61 62 #左連接又叫外連接 left join 返回左表中所有記錄和右表中連接字段相等的記錄 63 select * from A left join B on A.id = B.id; 64 65 select * from A left join (B,C,D) on (B.i1 = A.i1 and C.i2 = A.i2 and D.i3 = A.i3);#復(fù)雜連接 66 67 #右連接又叫外連接 right join 返回右表中所有記錄和左表中連接字段相等的記錄 68 select * from A right join B on A.id = B.id; 69 70 #完整外部鏈接 full join 返回左右表中所有數(shù)據(jù) 71 select * from A full join B on A.id = B.id; 72 73 #交叉連接 沒(méi)有where字句 返回卡迪爾積 74 select * from A cross join B; 75 -- -----------------------表連接結(jié)束------------------------------------------------------------ 76 -- ---------------索引創(chuàng)建------------------------------------------------ 77 show index from A #查看索引 78 alter table A add primary key (id) #主鍵索引 79 alter table A add unique (name) #唯一索引 80 alter table A add index name(name) #普通索引 81 alter table A add fulltext(name) #全文索引 82 alter table A add index name(id,name) #多列索引 83 84 #常用函數(shù) 85 abs ( - 1 )#絕對(duì)值 86 pi ()#pi值 87 sqrt ( 2 )#平方根 88 mod( - 5 , 3 )#取余 - 2 89 ceil( 10.6 )#進(jìn)位 + 1 結(jié)果11 ceil( 10.0 )結(jié)果10 90 floor ( 10.6 )#取整 10 91 round ( 2.5 )#四舍五入到整數(shù) 結(jié)果3 92 round ( 2.5 , 2 )#保留兩位小數(shù) 結(jié)果2. 50 93 truncate ( 2.5234 , 3 )#取小數(shù)后3位不四舍五入 2.523 94 sign ( - 2 );#符號(hào)函數(shù) 返回 - 1 0還是0 正數(shù)返回1 95 pow( 2 , 3 ), exp ( 2 );#2的3次冪 或e的2次冪 96 log ( 2 ), log10 ( 2 );#求對(duì)數(shù) 97 radians ( 180 ), degrees ( 0.618 );#角度弧度轉(zhuǎn)換 98 sin ( 0.5 ), asin ( 0.5 )#正弦和反正弦 類似cos acos tan atan 99 length( ' hi ' )#計(jì)算字符長(zhǎng)度 100 concat( ' 1 ' , 1 , ' hi ' )#合并字符串 101 insert ( ' 12345 ' , 1 , 0 , ' 7890 ' );#從開(kāi)頭第1個(gè)字符開(kāi)始到0個(gè)結(jié)束,替換成后邊字符串,0表示在最前邊插入 102 ucase( ' a ' ),lcase( ' A ' )#轉(zhuǎn)成大寫和小寫 103 left ( ' abcd ' , 2 ), right ( ' abcd ' , 2 );#返回前兩個(gè)字符和后兩個(gè)字符 104 ltrim ( ' 0 ' ), rtrim ( ' 0 ' ),trim( ' 0 ' )#刪除空格 105 replace ( ' 1234567890 ' , ' 345678 ' , ' 0 ' );#替換輸出12090 106 substring ( ' 12345 ' , 1 , 2 )#取字符 輸出12 1是位置 2是長(zhǎng)度 107 instr( ' 1234 ' , ' 234 ' );#取得234位置是2 108 reverse ( ' 1234 ' );#反序輸出4321 109 current ()#返回日期 110 curtime()#返回時(shí)間 111 now()#返回日期時(shí)間 112 month (now())#當(dāng)前月份 monthname 英文月份 113 dayname(now())#星期英文 dayofweek()1是星期天 weekday()1是星期二 114 week(now())#本年第多少周 115 dayofyear(now()),dayofmonth(now())#今天是本年第多少天 今天是本月第多少天 116 year (now()), month (now()), day (now()),hour(now()),minute(now()),second(now())#返回年月日 時(shí)分秒 117 time_to_sec(now()),sec_to_time( 3600 * 8 );#轉(zhuǎn)換時(shí)間為秒和還原 118 version()#mysql版本 119 database ()#當(dāng)前連接的數(shù)據(jù)庫(kù) 沒(méi)有為null 120 user ()#獲取用戶名 121 md5( ' a ' )#加密字符串 122 ascii ( ' a ' )#ascii值97 123 bin( 100 ),hex( 100 ),oct( 100 )#返回二進(jìn)制 十六進(jìn)制 八進(jìn)制 124 conv( 10001 , 2 , 8 );#各種進(jìn)制相互轉(zhuǎn)換 125 rand ()#生成0到1之間隨機(jī)數(shù) 126 sleep( 0.02 )#暫停秒數(shù) 127 128 數(shù)據(jù)庫(kù)優(yōu)化 129 .開(kāi)啟緩存,盡量使用php函數(shù)而不是mysql 130 . explain select 語(yǔ)句可以知道性能 131 .一行數(shù)據(jù)使用 limit 1 ; 132 .為搜索字段重建索引 比如關(guān)鍵字 標(biāo)簽 133 .表連接join保證字段類型相同并且有其索引 134 .隨機(jī)查詢使用php $r = mysql_query(" SELECT count ( * ) FROM user "); 135 $d = mysql_fetch_row($r); 136 $ rand = mt_rand( 0 ,$d [ 0 ] - 1 ); 137 $r = mysql_query(" SELECT username FROM user LIMIT $ rand , 1 "); 138 .避免使用select * 應(yīng)該使用具體字段 139 .每張表都是用id主鍵,并且是unsigned int 140 .對(duì)于取值有限而固定使用enum類型,如性別 國(guó)家 名族 部門 狀態(tài) 141 .盡可能使用not null ip存儲(chǔ)使用int( 4 ),使用ip 轉(zhuǎn)化函數(shù)ip2long()相互long2ip() 142 .delete和insert語(yǔ)句會(huì)鎖表,所以可以采用分拆語(yǔ)句操作 143 while ( 1 ){操作語(yǔ)句;usleep( 2000 );} 144 .選擇正確的存儲(chǔ)引擎;MyISAM適合大量查詢 寫操作多用InnoDB支持事務(wù) 145 146 #存儲(chǔ)過(guò)程 147 #存儲(chǔ)程序 148 delimiter #定義存儲(chǔ)程序 149 create procedure getversion(out params varchar ( 20 )) #params是傳出參數(shù) in傳進(jìn) out傳出 inout傳回 150 begin 151 select version() into params; #版本信息賦值params 152 end 153 call getversion( @a ); #調(diào)用存儲(chǔ)過(guò)程 154 select @a ; 155 delimiter #定義存儲(chǔ)函數(shù) 156 create function display(w varchar ( 20 )) returns varchar ( 20 ) 157 begin 158 return concat( ' hello ' ,w); 159 end 160 select display( ' world ' ); 161 162 drop procedure if exists spName; #刪除一個(gè)存儲(chǔ)過(guò)程 163 alter function spName [] ;#修改一個(gè)存儲(chǔ)過(guò)程 164 show create procedure spName;#顯示存儲(chǔ)過(guò)程信息 165 declare varName type default value;#聲明局部變量 166 #if語(yǔ)句 167 if 條件 then 語(yǔ)句 168 elseif 條件 then 語(yǔ)句 169 else 語(yǔ)句 170 end if 171 #case語(yǔ)句 172 case 條件 173 when 條件 then 語(yǔ)句 174 when 條件 then 語(yǔ)句 175 else 語(yǔ)句 176 end case 177 #loop語(yǔ)句 178 fn:loop 179 語(yǔ)句 180 end loop fn; 181 leave fn #退出循環(huán) 182 #while語(yǔ)句 183 fn: while 條件 do 184 語(yǔ)句 185 end while fn 186 187 188 #mysql使用幫助資料 189 ? contents; #列出幫助類型 190 ? data types;#列出數(shù)據(jù)類型 191 ? int ;#列出具體類型 192 ? show;#show語(yǔ)句 193 ? create table ;# 194 #常見(jiàn)表的比較 195 Myisam BDB Memory InnoDB Archive 196 存儲(chǔ)限制 no no yes 64T no 197 事物安全 支持 支持 198 鎖機(jī)制 表鎖 頁(yè)鎖 表鎖 行鎖 行鎖 199 全文索引 支持 200 外鍵支持 支持 201 myisam frm存儲(chǔ)表定義 MYD存儲(chǔ)數(shù)據(jù) MYI存儲(chǔ)索引 202 InnoDB 用于事務(wù)處理 203 char 和 varchar保存和索引都不相同 204 浮點(diǎn)數(shù)float( 10 , 2 ) 定點(diǎn)數(shù)decimal( 10 , 2 ) 205 長(zhǎng)度一定下,浮點(diǎn)數(shù)表示更大數(shù)據(jù)范圍,缺點(diǎn)是引起精度丟失,貨幣等使用定點(diǎn)數(shù)存儲(chǔ) 206 索引適合于where字句或者連接字句列 207 對(duì)于唯一值使用唯一索引 208 209 添加新用戶 grant select , insert , update , delete on * . * to Yoby @localhost identified by ' mysql ' ; 210 # * . * 數(shù)據(jù)庫(kù)名.表名,限制登錄某一個(gè)數(shù)據(jù)庫(kù) test. * localhost是本地主機(jī) 網(wǎng)絡(luò)可以使用 ' % ' 代替所有主機(jī) ' mysql ' 是密碼 Yoby是用戶名 所有權(quán)限可以用 all代替 211 查看用戶權(quán)限 show grants for ' root ' @ ' localhost ' ; 212 移除權(quán)限 revoke all on * . * from root @localhost ; 213 group by id 分組 214 having 限制字句 215 select1 union select2 聯(lián)合查詢有重復(fù)去掉保留一行 216 select2 union all select2 所有行合并到結(jié)果集中去
?
#登錄數(shù)據(jù)庫(kù)
mysql -hlocalhost -uroot -p;
#修改密碼
mysqladmin -uroot -pold password new;
#顯示數(shù)據(jù)庫(kù)
show databases;
#顯示數(shù)據(jù)表
show tables;
#選擇數(shù)據(jù)庫(kù)
use examples;
#創(chuàng)建數(shù)據(jù)庫(kù)并設(shè)置編碼utf-8 多語(yǔ)言
create database `examples` default character set utf8 collate utf8_general_ci;
#刪除數(shù)據(jù)庫(kù)
drop database examples;
#創(chuàng)建表
create table test(
??? id int(10) unsigned zerofill not null auto_increment,
??? email varchar(40) not null,
??? ip varchar(15) not null,
??? state int(10) not null default '-1',
??? primary key (id)
)engine=InnoDB;
#顯示表結(jié)構(gòu)
describe
#刪除表
drop table test;
#重命名表
alter table test_old rename test_new;
#添加列
alter table test add cn int(4) not null;
#修改列
alter table test change id id1 varchar(10) not null;
#刪除列
alter table test drop cn;
#創(chuàng)建索引
alter table test add index (cn,id);
#刪除索引
alter table test drop index cn
#插入數(shù)據(jù)
insert into test (id,email,ip,state) values(2,'qq@qq.com','127.0.0.1','0');
#刪除數(shù)據(jù)
delete from test where id = 1;
#修改數(shù)據(jù)
update test set id='1',email='q@qq.com' where id=1;
#查數(shù)據(jù)
select * from test;? #取所有數(shù)據(jù)
select * from test limit 0,2;? #取前兩條數(shù)據(jù)
select * from test email like '%qq%' #查含有qq字符 _表示一個(gè) %表示多個(gè)
select * from test order by id asc;#降序desc
select * from test id not in('2','3');#id不含2,3或者去掉not表示含有
select * from test timer between 1 and 10;#數(shù)據(jù)在1,10之間
#---------------------------表連接知識(shí)------------------------------
#等值連接又叫內(nèi)鏈接 inner join 只返回兩個(gè)表中連接字段相等的行
select * from A inner join B on A.id = B.id; #寫法1
select * from A,B where A.id = B.id; #寫法2
select a.id,a.title from A a inner join B b on a.id=b.id and a.id=1;#寫法3 表的臨時(shí)名稱
select a.id as ID,a.title as 標(biāo)題 from A inner join B on A.id=B.id;#添加as字句
#左連接又叫外連接 left join 返回左表中所有記錄和右表中連接字段相等的記錄
select * from A left join B on A.id = B.id;
select * from A left join (B,C,D) on (B.i1=A.i1 and C.i2=A.i2 and D.i3 = A.i3);#復(fù)雜連接
#右連接又叫外連接 right join 返回右表中所有記錄和左表中連接字段相等的記錄
select * from A right join B on A.id = B.id;
#完整外部鏈接 full join 返回左右表中所有數(shù)據(jù)
select * from A full join B on A.id = B.id;
#交叉連接 沒(méi)有where字句 返回卡迪爾積
select * from A cross join B;
-------------------------表連接結(jié)束------------------------------------------------------------
-----------------索引創(chuàng)建------------------------------------------------
show index from A #查看索引
alter table A add primary key(id) #主鍵索引
alter table A add unique(name) #唯一索引
alter table A add index name(name) #普通索引
alter table A add fulltext(name) #全文索引
alter table A add index name(id,name) #多列索引
#常用函數(shù)
abs(-1)#絕對(duì)值
pi()#pi值
sqrt(2)#平方根
mod(-5,3)#取余-2
ceil(10.6)#進(jìn)位+1 結(jié)果11 ceil(10.0)結(jié)果10
floor(10.6)#取整 10
round(2.5)#四舍五入到整數(shù) 結(jié)果3
round(2.5,2)#保留兩位小數(shù) 結(jié)果2.50
truncate(2.5234,3)#取小數(shù)后3位不四舍五入 2.523
sign(-2);#符號(hào)函數(shù) 返回-1 0還是0 正數(shù)返回1
pow(2,3),exp(2);#2的3次冪 或e的2次冪
log(2),log10(2);#求對(duì)數(shù)
radians(180),degrees(0.618);#角度弧度轉(zhuǎn)換
sin(0.5),asin(0.5)#正弦和反正弦 類似cos acos tan atan
length('hi')#計(jì)算字符長(zhǎng)度
concat('1',1,'hi')#合并字符串
insert('12345',1,0,'7890');#從開(kāi)頭第1個(gè)字符開(kāi)始到0個(gè)結(jié)束,替換成后邊字符串,0表示在最前邊插入
ucase('a'),lcase('A')#轉(zhuǎn)成大寫和小寫
left('abcd',2),right('abcd',2);#返回前兩個(gè)字符和后兩個(gè)字符
ltrim('? 0? '),rtrim(' 0 '),trim('? 0? ')#刪除空格
replace('1234567890','345678','0');#替換輸出12090
substring('12345',1,2)#取字符 輸出12 1是位置 2是長(zhǎng)度
instr('1234','234');#取得234位置是2
reverse('1234');#反序輸出4321
current()#返回日期
curtime()#返回時(shí)間
now()#返回日期時(shí)間
month(now())#當(dāng)前月份 monthname 英文月份
dayname(now())#星期英文 dayofweek()1是星期天 weekday()1是星期二
week(now())#本年第多少周
dayofyear(now()),dayofmonth(now())#今天是本年第多少天 今天是本月第多少天
year(now()),month(now()),day(now()),hour(now()),minute(now()),second(now())#返回年月日 時(shí)分秒
time_to_sec(now()),sec_to_time(3600*8);#轉(zhuǎn)換時(shí)間為秒和還原
version()#mysql版本
database()#當(dāng)前連接的數(shù)據(jù)庫(kù) 沒(méi)有為null
user()#獲取用戶名
md5('a')#加密字符串
ascii('a')#ascii值97
bin(100),hex(100),oct(100)#返回二進(jìn)制 十六進(jìn)制 八進(jìn)制
conv(10001,2,8);#各種進(jìn)制相互轉(zhuǎn)換
rand()#生成0到1之間隨機(jī)數(shù)
sleep(0.02)#暫停秒數(shù)
數(shù)據(jù)庫(kù)優(yōu)化
.開(kāi)啟緩存,盡量使用php函數(shù)而不是mysql
. explain select 語(yǔ)句可以知道性能
.一行數(shù)據(jù)使用 limit 1;
.為搜索字段重建索引 比如關(guān)鍵字 標(biāo)簽
.表連接join保證字段類型相同并且有其索引
.隨機(jī)查詢使用php $r = mysql_query("SELECT count(*) FROM user");
??????????????????? $d = mysql_fetch_row($r);
??????????????????? $rand = mt_rand(0,$d[0] - 1);
??????????????????? $r = mysql_query("SELECT username FROM user LIMIT $rand, 1");
.避免使用select * 應(yīng)該使用具體字段
.每張表都是用id主鍵,并且是unsigned int
.對(duì)于取值有限而固定使用enum類型,如性別 國(guó)家 名族 部門 狀態(tài)
.盡可能使用not null ip存儲(chǔ)使用int(4),使用ip 轉(zhuǎn)化函數(shù)ip2long()相互long2ip()
.delete和insert語(yǔ)句會(huì)鎖表,所以可以采用分拆語(yǔ)句操作
??? while(1){操作語(yǔ)句;usleep(2000);}
.選擇正確的存儲(chǔ)引擎;MyISAM適合大量查詢 寫操作多用InnoDB支持事務(wù)
#存儲(chǔ)過(guò)程
#存儲(chǔ)程序
delimiter #定義存儲(chǔ)程序
create procedure getversion(out params varchar(20)) #params是傳出參數(shù) in傳進(jìn) out傳出 inout傳回
begin
select version() into params; #版本信息賦值params
end
call getversion(@a); #調(diào)用存儲(chǔ)過(guò)程
select @a;
delimiter #定義存儲(chǔ)函數(shù)
create function display(w varchar(20)) returns varchar(20)
begin
return concat('hello',w);
end
select display('world');
drop procedure if exists spName; #刪除一個(gè)存儲(chǔ)過(guò)程
alter function spName [];#修改一個(gè)存儲(chǔ)過(guò)程
show create procedure spName;#顯示存儲(chǔ)過(guò)程信息
declare varName type default value;#聲明局部變量
#if語(yǔ)句
if 條件 then 語(yǔ)句
elseif 條件 then 語(yǔ)句
else 語(yǔ)句
end if
#case語(yǔ)句
case 條件
when 條件 then 語(yǔ)句
when 條件 then 語(yǔ)句
else 語(yǔ)句
end case
#loop語(yǔ)句
fn:loop
語(yǔ)句
end loop fn;
leave fn #退出循環(huán)
#while語(yǔ)句
fn:while 條件 do
語(yǔ)句
end while fn
#mysql使用幫助資料
? contents; #列出幫助類型
? data types;#列出數(shù)據(jù)類型
? int;#列出具體類型
? show;#show語(yǔ)句
? create table;#
#常見(jiàn)表的比較
??????????????????? Myisam?? BDB??? Memory??? InnoDB??? Archive
存儲(chǔ)限制??????? no?????????? no????? yes??????????????? 64T??????? no
事物安全????????????????????? 支持???????????????????????? 支持??????????????????????? ?
鎖機(jī)制???????? 表鎖?????????? 頁(yè)鎖??? 表鎖???????????? 行鎖????????? 行鎖
全文索引?????? 支持
外鍵支持??????????????????????????????????????????????????????? 支持
myisam? frm存儲(chǔ)表定義 MYD存儲(chǔ)數(shù)據(jù) MYI存儲(chǔ)索引
InnoDB 用于事務(wù)處理
char 和 varchar保存和索引都不相同
浮點(diǎn)數(shù)float(10,2) 定點(diǎn)數(shù)decimal(10,2)
長(zhǎng)度一定下,浮點(diǎn)數(shù)表示更大數(shù)據(jù)范圍,缺點(diǎn)是引起精度丟失,貨幣等使用定點(diǎn)數(shù)存儲(chǔ)
??????? 索引適合于where字句或者連接字句列
??????? 對(duì)于唯一值使用唯一索引
添加新用戶 grant select,insert,update,delete on *.* to Yoby@localhost identified by 'mysql';
#?????????? *.* 數(shù)據(jù)庫(kù)名.表名,限制登錄某一個(gè)數(shù)據(jù)庫(kù) test.*?????????????????????????? localhost是本地主機(jī) 網(wǎng)絡(luò)可以使用 '%'代替所有主機(jī)??????? 'mysql'是密碼 Yoby是用戶名? 所有權(quán)限可以用 all代替
查看用戶權(quán)限 show grants for 'root'@'localhost';
移除權(quán)限? revoke all on *.* from root@localhost;
group by id 分組
having 限制字句
select1 union select2 聯(lián)合查詢有重復(fù)去掉保留一行
select2 union all select2 所有行合并到結(jié)果集中去
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
