MySQL optimization-MySQL basic operations and common skills

Mysql basic operation

mysql table replication:

1. Copy the table structure, create a student table, the structure of the user table structure, do not copy data
create table student like user;

2. Copy the contents of the table, copy the user table data to the student table
insert into student select * from user;

mysql index:

1. View the user table index

show index from user\G

2. Ordinary index
1) Create an ordinary index for the age field of the user table

create index i_age on user(age);

2) Delete the i_age index in the user table

drop index i_age on user;

3. Unique index
1) Create a unique index for the username field of the user table

create unique index u_username on user(username);

2) Delete the unique index u_username created by the user table

drop index u_username on user;

4. Primary key index
Primary key index is generally used in combination with auto-increment

1) Create the primary key index
alter
2) Delete the primary key index
alter

mysql view:

1. Create a view table for creating a linked table query between the user table and the 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. Delete the view named userclass

drop view userclass;

3. View the view
because the view also belongs to the table, but belongs to the virtual table

show tables;

4. View the view userclass table structure

desc usercclass;

5. Query view userclass data

select * from userclass;

6.The characteristics of
the viewWhen the data in the table changes, the view data will also change.

7. View the future auto-increment number in the user table in mysql: AUTO_INCREMENT=not from the increment number

show create table user;

Common MySQL skills

mysql string function:

1. String concatenation

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

2. Turn lowercase

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

3. Turn to uppercase:

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

4. length

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

5. Remove the space on the left

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

6. Remove the space on the right

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

7. Repeat

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

8. Replace

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

9. Interception

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

10.Space

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

mysql math functions:

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 date function:

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');

Rearrange the auto_increment method:

Sort the table from 1

1. After deleting the table data, reset the self-increasing sort weight 1 to start calculation

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

2.truncate

truncate user;

Help for commands in mysql:

1. Simple usage, add a question mark before the command

? create

2.More

? fun%

Use RAND() to extract random rows:
select * from user order by rand limit 3;

The use of regular expressions:

1.Data ending with php
select * from user where username regexp'php$';

2. Data ending in php or ending in linux
select * from user where username regexp'php ′ orusernameregexp ′ linux 'or username regexp'linux Orusernameregexplinux’;

3. Find data containing php or linux or user
select * from user where username regexp'php|linux|user';

Guess you like

Origin blog.csdn.net/weixin_39218464/article/details/112093596