MySQLの最適化-MySQLの基本操作と一般的なスキル

Mysqlの基本操作

mysqlテーブルレプリケーション:

1.テーブル構造をコピーし、学生テーブル、ユーザーテーブル構造の構造を
作成します。データコピーしないでください。ユーザーのようなテーブル学生を作成します。

2.テーブルの内容をコピーし、ユーザーテーブルデータを学生テーブルにコピーします
。studentselect* fromuserに挿入します。

mysqlインデックス:

1.ユーザーテーブルのインデックスを表示する

show index from user\G

2.通常のインデックス
1)ユーザーテーブルの年齢フィールドの通常のインデックスを作成します

create index i_age on user(age);

2)ユーザーテーブルのi_ageインデックスを削除します

drop index i_age on user;

3.一意のインデックス
1)ユーザーテーブルのユーザー名フィールドに一意のインデックスを作成します

create unique index u_username on user(username);

2)ユーザーテーブルによって作成された一意のインデックスu_usernameを削除します

drop index u_username on user;

4.主キーインデックス
主キーインデックスは通常、自動インクリメントと組み合わせて使用​​されます

1)主キーインデックスの
変更を作成します
2)主キーインデックスの
変更を削除します

mysqlビュー:

1.ユーザーテーブルとクラスの間にリンクテーブルクエリを作成するためのビューテーブルを作成します

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.ビューのユーザークラステーブル構造を表示します

desc usercclass;

5.クエリビューのユーザークラスデータ

select * from userclass;

6.
ビューの特性表のデータが変わると、ビューのデータも変わります。

7.mysqlのユーザーテーブルで将来の自動インクリメント番号を表示します。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分 ();

取最小数
例子: 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.週(日付);

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

7.年(日付);

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

8.datediff();

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

auto_incrementメソッドを再配置します。

テーブルを1から並べ替えます

1.テーブルデータを削除した後、自己増加ソートウェイト1をリセットして計算を開始します

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

2.切り捨て

truncate user;

mysqlのコマンドのヘルプ:

1.簡単な使用法、コマンドの前に疑問符を追加します

? create

2以上

? fun%

RAND()を使用して、ランダムな行を抽出
します。ランド制限3によるユーザーの順序から*を選択します。

正規表現の使用:

1.phpで終わるデータ
select * from user where username regexp'php $ ';

2.php
終わるデータまたはlinuxで終わるデータselect * from user where username regexp'php 'orusernameregexp'linux' or username regexp'linux Orusernameregexp linux’;

3. phpまたはlinuxを含むデータを検索するか、user
select * from user where username regexp'php | linux | user ';

おすすめ

転載: blog.csdn.net/weixin_39218464/article/details/112093596