MySQL优化-MySQL基础操作与常用技巧

Mysql基础操作

mysql表复制:

1.复制表结构,创建一张student表,结构为user的表结构,不复制数据
create table student like user;

2.复制表内容,将user表数据复制到student表中
insert into student select * from user;

mysql索引:

1.查看user表索引

show index from user\G

2.普通索引
1)为user表age字段创建普通索引

create index i_age on user(age);

2)删除user表中i_age索引

drop index i_age on user;

3.唯一索引
1)为user表username字段创建唯一索引

create unique index u_username on user(username);

2)删除user表创建的唯一索引u_username

drop index u_username on user;

4.主键索引
主键索引一般与自增结合使用

1)创建主键索引
alter
2)删除主键索引
alter

mysql视图:

1.为创建user表与class的连表查询创建视图表

select user.username,user.age,class.name from user,class where user.class_id=class.id;
create view userclass as select user.username,user.age,class.name from user,class where user.class_id=class.id;

2.删除名字为userclass的视图

drop view userclass;

3.查看视图
因为视图也属于表,但是是属于虚拟表

show tables;

4.查看视图userclass表结构

desc usercclass;

5.查询视图userclass数据

select * from userclass;

6.视图的特性
当表中数据发生变化时视图数据也会随着发生变化.

7.mysql中查看user表中未来的自增数:AUTO_INCREMENT=未来自增数

show create table user;

常用的MySQL技巧

mysql字符串函数:

1.字符串连接

concat();
例子: select concat('php','linux');

2.转小写

lcase();
例子: select lcase('PHP IS VERY MUCH!');

3.转大写:

ucase();
例子: select id,ucase(username),age from user;

4.长度

length();
例子: select length('linux');

5.取除左边的空格

ltrim();
例子: select length(ltrim('   linux'));

6.取除右边的空格

rtrim();
例子: select length(rtrim('linux   '));

7.重复

repeat();
例子: select concat(repeat('-',20),'linux');

8.替换

replace();将linux替换成php
例子: select replace('linux and java','linux','php');

9.截取

substring();从1开始往左数从6开始截取
例子: select substring('/usr/local/src',6,5);

10.空格

space();
例子: select concat('linux',space(20),'php');

mysql数学函数:

1.bin();
十进制转2进制,将10转为1010
例子: select bin(10);

2.ceiling();

取上一个整数,结果为:11
例子: select ceiling(10.5);

3.floor();

取下一个整数结果为:10
例子: select floor(10.5);

4.max();

取最大数
例子: select max(id) from user;

5.min();

取最小数
例子: select min(id) from user;

6.sqrt();

开平方,100开平方为10
例子: select sqrt(100);

7.rand();

求随机数
例子: select * from user order by rand();

mysql日期函数:

1.curdate();
当前日期
例子: select curdate();

2.curtime();

当前时间,时分秒
例子: select curtime();

3.now();

当前日期和时间
例子: select now();

4.unix_timestamp();

当前时间戳
例子: select unix_timestamp();

5.from_unixtime();

时间戳转日期
例子: select from_unixtime(1492176896);

6.week(date);

一年中的第几周
例子: select week('2017-1-8');

7.year(date);

日期中的年部分
例子: select year('2017-4-14');

8.datediff();

日期差值
例子: select datediff('2017-4-14','2017-4-10');

重排auto_increment方法:

将表的排序从1开始排序

1.delete表数据后重置自增排序重1开始计算

1)delete from user;
2)alter table user auto_increment=1;

2.truncate

truncate user;

mysql中命令的帮助:

1.简单用法,命令前加问号

? create

2.更多

? fun%

巧用RAND()提取随机行:
select * from user order by rand limit 3;

正则表达式的使用:

1.以php结尾的数据
select * from user where username regexp ‘php$’;

2.以php结尾或以linux结尾的数据
select * from user where username regexp ‘php ′ o r u s e r n a m e r e g e x p ′ l i n u x ' or username regexp 'linux orusernameregexplinux’;

3.查找包含php或linux或user的数据
select * from user where username regexp ‘php|linux|user’;

猜你喜欢

转载自blog.csdn.net/weixin_39218464/article/details/112093596
今日推荐