欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mysql使用大全 從基礎(chǔ)到存儲(chǔ)過(guò)程

 更新時(shí)間:2012年05月28日 00:56:05   作者:  
看到園子里總結(jié)的Mysql用法,我覺(jué)得沒(méi)有我的全面,我的從登錄到高級(jí)的存儲(chǔ)過(guò)程都涉及到,這部分是我平常不會(huì)或是出現(xiàn)問(wèn)題都會(huì)拿來(lái)看,不過(guò)現(xiàn)在就和我一起來(lái)使用命令模式學(xué)習(xí)一下數(shù)據(jù)庫(kù)最基本的吧
平常習(xí)慣了phpmyadmin等其他工具的的朋友有的根本就不會(huì)命令,如果讓你筆試去面試我看你怎么辦,所以,學(xué)習(xí)一下還是非常有用的,也可以知道你通過(guò)GUI工具的時(shí)候工具到底做了什么。Mysql用處很廣,是php最佳拍檔,Java中使用也很方便。

    我是通過(guò)Windows 7 操作的,所以打開(kāi)運(yùn)行-輸入cmd吧,然后輸入mysql -hlocalhost -uroot -p;回車(chē)后就可以輸入密碼了,這里可以*號(hào)顯示,當(dāng)然也可以和-p連寫(xiě)的,這就是登錄mysql。修改密碼mysqladmin -uroot -pold password new;這里的root是用戶(hù)名 new是你的新密碼。退出是什么命令,曾有人問(wèn)我,我說(shuō)你直接點(diǎn)X好了,不過(guò)命令是quit;退出到cmd環(huán)境,退出cmd環(huán)境命令是exit;接著就是操作mysql的增刪改查,常稱(chēng)為CURD操作。
復(fù)制代碼 代碼如下:

#登錄數(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; #寫(xiě)法1
select * from A,B where A.id = B.id; #寫(xiě)法2
select a.id,a.title from A a inner join B b on a.id=b.id and a.id=1;#寫(xiě)法3 表的臨時(shí)名稱(chēng)
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)#正弦和反正弦 類(lèi)似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)成大寫(xiě)和小寫(xiě)
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()#獲取用戶(hù)名
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保證字段類(lèi)型相同并且有其索引
.隨機(jī)查詢(xún)使用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類(lèi)型,如性別 國(guó)家 名族 部門(mén) 狀態(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適合大量查詢(xún) 寫(xiě)操作多用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; #列出幫助類(lèi)型
? data types;#列出數(shù)據(jù)類(lèi)型
? int;#列出具體類(lèi)型
? 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ì)于唯一值使用唯一索引

添加新用戶(hù) 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是用戶(hù)名 所有權(quán)限可以用 all代替
查看用戶(hù)權(quán)限 show grants for 'root'@'localhost';
移除權(quán)限 revoke all on *.* from root@localhost;
group by id 分組
having 限制字句
select1 union select2 聯(lián)合查詢(xún)有重復(fù)去掉保留一行
select2 union all select2 所有行合并到結(jié)果集中去

這是一份最完整的mysql筆記,需要的可以復(fù)制保存了!

(原創(chuàng) Yoby)

相關(guān)文章

最新評(píng)論