mysql表復制
- 復制表結構
create table student like user; - 復制表數(shù)據(jù)
insert into student select * from user;
mysql索引
- 查看索引
show index from user\G
1.普通索引
創(chuàng)建
create index i_age on user(age)
刪除
drop index i_age on user
2.唯一索引
創(chuàng)建
create unique index u_username on user(username)
刪除
drop index u_username on user
mysql視圖
- 創(chuàng)建
create view userclass as select user.username,user.age,class.name from user,class where user.class_id=class.id - 刪除
drop view userclass - 查看
show tables - 查看視圖數(shù)據(jù)
select * from userclass - 視圖的特性
當表中數(shù)據(jù)發(fā)生變化時視圖數(shù)據(jù)也會隨著發(fā)生變化慢逾。
mysql中查看表中未來的自贈數(shù)
show create table user;
MySQL中的內置函數(shù)
1.字符串函數(shù)
- concat(string [...]) 連接字符串
- lcase(string) 轉換成小寫
- ucase(string) 轉換成大寫
- length(string) string長度
- ltrim(string) 去除前端空格
- rtrim(string) 去除后端空格
- repeat(string,count) 重復count次
- replace(str,search_str,replace_str) 在str中用replace_str替換search_str
- substring(str,position [,length]) position開始携丁,取length個字符
- space(count) 生成count個空格
- 數(shù)學函數(shù)
- bin() 十進制轉二進制
- ceiling() 取上一個整數(shù)
- floor() 取下一個整數(shù)
- max() 取最大整數(shù)
- min() 取最小整數(shù)
- sqrt() 開平方
rand() 求0-1隨機數(shù)
- 日期函數(shù)
- curdate() 當前日期
- curtime() 當前時間
- now() 當前時間和日期
- unix_timestamp() 當前時間戳
- from_unixtime() 時間戳轉日期
- week(date) 一年中的第幾周
- year(date) 日期中的年部分
- datediff() 日期的差值
- 重排auto_increment方法
- delete
- delete from user
- alter table user auto_increment=2
- truncate
truncate user
mysql中的命令的幫助
? create 簡單
? da% 更多
巧用rand()提取隨即行
select * from user order by rand limit 3
正則表達式的使用
- 以php結尾的數(shù)據(jù)
select * from user where username regexp 'php$' - 以php結尾或以linux結尾的數(shù)據(jù)
select * from user where username regexp 'php$' or username regexp 'linux$' - 查找包含php或linux或user的數(shù)據(jù)
select * from user where username regexp 'php|linux|user'
檢查服務器增、刪、改和查的使用頻次(本次連接以來)
show status like "%Com_update%";
show status like "%Com_insert%";
show status like "%Com_select%";
show status like "%Com_delete%";
檢查服務器增绣夺、刪、改和查的使用頻次(本次服務器啟動以來)
show global status like "%Com_update%";
show global status like "%Com_insert%";
show global status like "%Com_select%";
show global status like "%Com_delete%";
查innodb自連接以來的影響行數(shù)
show status like "%InnoDB_rows%"
定位執(zhí)行效率較低的sql語句
explain或desc定位一條sql語句的影響行數(shù)
desc select * from user where username='user8'\G
查看mysql的慢查詢日志
show variables like "%quer%"
slow_query_log | on
查看慢查詢的次數(shù)
show status like "%quer%"
slow_queries | 0
修改慢查詢的時間(my.ini)
long_query_time=n
優(yōu)化表空間
optimize table sales
check表檢查
check table v_user