Mysql使用大全 從基礎(chǔ)到存儲過程
更新時間:2012年05月28日 00:56:05 作者:
看到園子里總結(jié)的Mysql用法,我覺得沒有我的全面,我的從登錄到高級的存儲過程都涉及到,這部分是我平常不會或是出現(xiàn)問題都會拿來看,不過現(xiàn)在就和我一起來使用命令模式學(xué)習(xí)一下數(shù)據(jù)庫最基本的吧
平常習(xí)慣了phpmyadmin等其他工具的的朋友有的根本就不會命令,如果讓你筆試去面試我看你怎么辦,所以,學(xué)習(xí)一下還是非常有用的,也可以知道你通過GUI工具的時候工具到底做了什么。Mysql用處很廣,是php最佳拍檔,Java中使用也很方便。
我是通過Windows 7 操作的,所以打開運(yùn)行-輸入cmd吧,然后輸入mysql -hlocalhost -uroot -p;回車后就可以輸入密碼了,這里可以*號顯示,當(dāng)然也可以和-p連寫的,這就是登錄mysql。修改密碼mysqladmin -uroot -pold password new;這里的root是用戶名 new是你的新密碼。退出是什么命令,曾有人問我,我說你直接點(diǎn)X好了,不過命令是quit;退出到cmd環(huán)境,退出cmd環(huán)境命令是exit;接著就是操作mysql的增刪改查,常稱為CURD操作。
#登錄數(shù)據(jù)庫
mysql -hlocalhost -uroot -p;
#修改密碼
mysqladmin -uroot -pold password new;
#顯示數(shù)據(jù)庫
show databases;
#顯示數(shù)據(jù)表
show tables;
#選擇數(shù)據(jù)庫
use examples;
#創(chuàng)建數(shù)據(jù)庫并設(shè)置編碼utf-8 多語言
create database `examples` default character set utf8 collate utf8_general_ci;
#刪除數(shù)據(jù)庫
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字符 _表示一個 %表示多個
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之間
#---------------------------表連接知識------------------------------
#等值連接又叫內(nèi)鏈接 inner join 只返回兩個表中連接字段相等的行
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 表的臨時名稱
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;
#交叉連接 沒有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)#絕對值
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);#符號函數(shù) 返回-1 0還是0 正數(shù)返回1
pow(2,3),exp(2);#2的3次冪 或e的2次冪
log(2),log10(2);#求對數(shù)
radians(180),degrees(0.618);#角度弧度轉(zhuǎn)換
sin(0.5),asin(0.5)#正弦和反正弦 類似cos acos tan atan
length('hi')#計(jì)算字符長度
concat('1',1,'hi')#合并字符串
insert('12345',1,0,'7890');#從開頭第1個字符開始到0個結(jié)束,替換成后邊字符串,0表示在最前邊插入
ucase('a'),lcase('A')#轉(zhuǎn)成大寫和小寫
left('abcd',2),right('abcd',2);#返回前兩個字符和后兩個字符
ltrim(' 0 '),rtrim(' 0 '),trim(' 0 ')#刪除空格
replace('1234567890','345678','0');#替換輸出12090
substring('12345',1,2)#取字符 輸出12 1是位置 2是長度
instr('1234','234');#取得234位置是2
reverse('1234');#反序輸出4321
current()#返回日期
curtime()#返回時間
now()#返回日期時間
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())#返回年月日 時分秒
time_to_sec(now()),sec_to_time(3600*8);#轉(zhuǎn)換時間為秒和還原
version()#mysql版本
database()#當(dāng)前連接的數(shù)據(jù)庫 沒有為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ù)庫優(yōu)化
.開啟緩存,盡量使用php函數(shù)而不是mysql
. explain select 語句可以知道性能
.一行數(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
.對于取值有限而固定使用enum類型,如性別 國家 名族 部門 狀態(tài)
.盡可能使用not null ip存儲使用int(4),使用ip 轉(zhuǎn)化函數(shù)ip2long()相互long2ip()
.delete和insert語句會鎖表,所以可以采用分拆語句操作
while(1){操作語句;usleep(2000);}
.選擇正確的存儲引擎;MyISAM適合大量查詢 寫操作多用InnoDB支持事務(wù)
#存儲過程
#存儲程序
delimiter #定義存儲程序
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)用存儲過程
select @a;
delimiter #定義存儲函數(shù)
create function display(w varchar(20)) returns varchar(20)
begin
return concat('hello',w);
end
select display('world');
drop procedure if exists spName; #刪除一個存儲過程
alter function spName [];#修改一個存儲過程
show create procedure spName;#顯示存儲過程信息
declare varName type default value;#聲明局部變量
#if語句
if 條件 then 語句
elseif 條件 then 語句
else 語句
end if
#case語句
case 條件
when 條件 then 語句
when 條件 then 語句
else 語句
end case
#loop語句
fn:loop
語句
end loop fn;
leave fn #退出循環(huán)
#while語句
fn:while 條件 do
語句
end while fn
#mysql使用幫助資料
? contents; #列出幫助類型
? data types;#列出數(shù)據(jù)類型
? int;#列出具體類型
? show;#show語句
? create table;#
#常見表的比較
Myisam BDB Memory InnoDB Archive
存儲限制 no no yes 64T no
事物安全 支持 支持
鎖機(jī)制 表鎖 頁鎖 表鎖 行鎖 行鎖
全文索引 支持
外鍵支持 支持
myisam frm存儲表定義 MYD存儲數(shù)據(jù) MYI存儲索引
InnoDB 用于事務(wù)處理
char 和 varchar保存和索引都不相同
浮點(diǎn)數(shù)float(10,2) 定點(diǎn)數(shù)decimal(10,2)
長度一定下,浮點(diǎn)數(shù)表示更大數(shù)據(jù)范圍,缺點(diǎn)是引起精度丟失,貨幣等使用定點(diǎn)數(shù)存儲
索引適合于where字句或者連接字句列
對于唯一值使用唯一索引
添加新用戶 grant select,insert,update,delete on *.* to Yoby@localhost identified by 'mysql';
# *.* 數(shù)據(jù)庫名.表名,限制登錄某一個數(shù)據(jù)庫 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é)果集中去
這是一份最完整的mysql筆記,需要的可以復(fù)制保存了!
(原創(chuàng) Yoby)
我是通過Windows 7 操作的,所以打開運(yùn)行-輸入cmd吧,然后輸入mysql -hlocalhost -uroot -p;回車后就可以輸入密碼了,這里可以*號顯示,當(dāng)然也可以和-p連寫的,這就是登錄mysql。修改密碼mysqladmin -uroot -pold password new;這里的root是用戶名 new是你的新密碼。退出是什么命令,曾有人問我,我說你直接點(diǎn)X好了,不過命令是quit;退出到cmd環(huán)境,退出cmd環(huán)境命令是exit;接著就是操作mysql的增刪改查,常稱為CURD操作。
復(fù)制代碼 代碼如下:
#登錄數(shù)據(jù)庫
mysql -hlocalhost -uroot -p;
#修改密碼
mysqladmin -uroot -pold password new;
#顯示數(shù)據(jù)庫
show databases;
#顯示數(shù)據(jù)表
show tables;
#選擇數(shù)據(jù)庫
use examples;
#創(chuàng)建數(shù)據(jù)庫并設(shè)置編碼utf-8 多語言
create database `examples` default character set utf8 collate utf8_general_ci;
#刪除數(shù)據(jù)庫
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字符 _表示一個 %表示多個
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之間
#---------------------------表連接知識------------------------------
#等值連接又叫內(nèi)鏈接 inner join 只返回兩個表中連接字段相等的行
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 表的臨時名稱
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;
#交叉連接 沒有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)#絕對值
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);#符號函數(shù) 返回-1 0還是0 正數(shù)返回1
pow(2,3),exp(2);#2的3次冪 或e的2次冪
log(2),log10(2);#求對數(shù)
radians(180),degrees(0.618);#角度弧度轉(zhuǎn)換
sin(0.5),asin(0.5)#正弦和反正弦 類似cos acos tan atan
length('hi')#計(jì)算字符長度
concat('1',1,'hi')#合并字符串
insert('12345',1,0,'7890');#從開頭第1個字符開始到0個結(jié)束,替換成后邊字符串,0表示在最前邊插入
ucase('a'),lcase('A')#轉(zhuǎn)成大寫和小寫
left('abcd',2),right('abcd',2);#返回前兩個字符和后兩個字符
ltrim(' 0 '),rtrim(' 0 '),trim(' 0 ')#刪除空格
replace('1234567890','345678','0');#替換輸出12090
substring('12345',1,2)#取字符 輸出12 1是位置 2是長度
instr('1234','234');#取得234位置是2
reverse('1234');#反序輸出4321
current()#返回日期
curtime()#返回時間
now()#返回日期時間
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())#返回年月日 時分秒
time_to_sec(now()),sec_to_time(3600*8);#轉(zhuǎn)換時間為秒和還原
version()#mysql版本
database()#當(dāng)前連接的數(shù)據(jù)庫 沒有為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ù)庫優(yōu)化
.開啟緩存,盡量使用php函數(shù)而不是mysql
. explain select 語句可以知道性能
.一行數(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
.對于取值有限而固定使用enum類型,如性別 國家 名族 部門 狀態(tài)
.盡可能使用not null ip存儲使用int(4),使用ip 轉(zhuǎn)化函數(shù)ip2long()相互long2ip()
.delete和insert語句會鎖表,所以可以采用分拆語句操作
while(1){操作語句;usleep(2000);}
.選擇正確的存儲引擎;MyISAM適合大量查詢 寫操作多用InnoDB支持事務(wù)
#存儲過程
#存儲程序
delimiter #定義存儲程序
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)用存儲過程
select @a;
delimiter #定義存儲函數(shù)
create function display(w varchar(20)) returns varchar(20)
begin
return concat('hello',w);
end
select display('world');
drop procedure if exists spName; #刪除一個存儲過程
alter function spName [];#修改一個存儲過程
show create procedure spName;#顯示存儲過程信息
declare varName type default value;#聲明局部變量
#if語句
if 條件 then 語句
elseif 條件 then 語句
else 語句
end if
#case語句
case 條件
when 條件 then 語句
when 條件 then 語句
else 語句
end case
#loop語句
fn:loop
語句
end loop fn;
leave fn #退出循環(huán)
#while語句
fn:while 條件 do
語句
end while fn
#mysql使用幫助資料
? contents; #列出幫助類型
? data types;#列出數(shù)據(jù)類型
? int;#列出具體類型
? show;#show語句
? create table;#
#常見表的比較
Myisam BDB Memory InnoDB Archive
存儲限制 no no yes 64T no
事物安全 支持 支持
鎖機(jī)制 表鎖 頁鎖 表鎖 行鎖 行鎖
全文索引 支持
外鍵支持 支持
myisam frm存儲表定義 MYD存儲數(shù)據(jù) MYI存儲索引
InnoDB 用于事務(wù)處理
char 和 varchar保存和索引都不相同
浮點(diǎn)數(shù)float(10,2) 定點(diǎn)數(shù)decimal(10,2)
長度一定下,浮點(diǎn)數(shù)表示更大數(shù)據(jù)范圍,缺點(diǎn)是引起精度丟失,貨幣等使用定點(diǎn)數(shù)存儲
索引適合于where字句或者連接字句列
對于唯一值使用唯一索引
添加新用戶 grant select,insert,update,delete on *.* to Yoby@localhost identified by 'mysql';
# *.* 數(shù)據(jù)庫名.表名,限制登錄某一個數(shù)據(jù)庫 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é)果集中去
這是一份最完整的mysql筆記,需要的可以復(fù)制保存了!
(原創(chuàng) Yoby)
您可能感興趣的文章:
- 基于Php mysql存儲過程的詳解
- 基于mysql事務(wù)、視圖、存儲過程、觸發(fā)器的應(yīng)用分析
- jdbc調(diào)用mysql存儲過程實(shí)現(xiàn)代碼
- mysql存儲過程 游標(biāo) 循環(huán)使用介紹
- mysql存儲過程事務(wù)管理簡析
- 如何測試mysql觸發(fā)器和存儲過程
- 詳解MySQL存儲過程參數(shù)有三種類型(in、out、inout)
- mysql存儲過程詳解
- MySql存儲過程異常處理示例代碼分享
- MySql的存儲過程學(xué)習(xí)小結(jié) 附pdf文檔下載
- MSSQL MySQL 數(shù)據(jù)庫分頁(存儲過程)
- MySQL動態(tài)創(chuàng)建表,數(shù)據(jù)分表的存儲過程
- mysql多次調(diào)用存儲過程的問題
- mysql 讓一個存儲過程定時作業(yè)的代碼
- mysql中用于數(shù)據(jù)遷移存儲過程分享
- mysql 查詢數(shù)據(jù)庫中的存儲過程與函數(shù)的語句
- 深入mysql存儲過程中表名使用參數(shù)傳入的詳解
相關(guān)文章
mysql mysqldump只導(dǎo)出表結(jié)構(gòu)或只導(dǎo)出數(shù)據(jù)的實(shí)現(xiàn)方法
mysql mysqldump只導(dǎo)出表結(jié)構(gòu)或只導(dǎo)出數(shù)據(jù)的實(shí)現(xiàn)方法,需要的朋友可以參考下。2011-11-11mysql修改數(shù)據(jù)庫編碼(數(shù)據(jù)庫字符集)和表的字符編碼的方法
Mysql數(shù)據(jù)庫是一個開源的數(shù)據(jù)庫,應(yīng)用非常廣泛。以下是修改mysql數(shù)據(jù)庫的字符編碼的操作過程和將表的字符編碼轉(zhuǎn)換成utf-8的方法,需要的朋友可以參考下2014-03-03MySQL多表查詢與7種JOINS的實(shí)現(xiàn)舉例
最近學(xué)習(xí)了多表查詢,對此做一些筆記的整理,下面這篇文章主要給大家介紹了關(guān)于MySQL多表查詢與7種JOINS實(shí)現(xiàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02mysql獲取指定時間段中所有日期或月份的語句(不設(shè)存儲過程,不加表)
最近需要用mysql獲取一個時間段中的所有月份,網(wǎng)上查都是要設(shè)置存儲過程或者加一個日期表的,不滿足我的需求,翻墻找資料加上自己試驗(yàn),如下代碼分享給大家2021-06-06Mysql以utf8存儲gbk輸出的實(shí)現(xiàn)方法提供
Mysql以utf8存儲gbk輸出的實(shí)現(xiàn)方法提供...2007-11-11