溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL中簡單語句如何應(yīng)用

發(fā)布時間:2020-06-03 16:19:31 來源:網(wǎng)絡(luò) 閱讀:253 作者:三月 欄目:MySQL數(shù)據(jù)庫

下面講講關(guān)于MySQL中簡單語句如何應(yīng)用,文字的奧妙在于貼近主題相關(guān)。所以,閑話就不談了,我們直接看下文吧,相信看完MySQL中簡單語句如何應(yīng)用這篇文章你一定會有所受益。

1.mysql多實(shí)例登陸
mysql -uroot -p'123456' -S /data/3306/mysql.sock

2.查看版本及登陸用戶
select version();
select user();

3.創(chuàng)建GBK字符集數(shù)據(jù)庫rich3
create database rich3 CHARACTER SET = gbk COLLATE = gbk_chinese_ci;
show create database rich3;   //查看創(chuàng)建的數(shù)據(jù)庫rich3

4.創(chuàng)建用戶rich3并管理數(shù)據(jù)庫rich3所有權(quán)限
grant all on . to rich3@localhost identified by '123456';

5.查看當(dāng)前數(shù)據(jù)庫里有哪些用戶
select user,host from mysql.user;

6.進(jìn)入rich3數(shù)據(jù)庫
use rich3;

7.創(chuàng)建innodb引擎字符集為GBK表test,字段為id和name varchar(16),查看建表結(jié)構(gòu)及SQL語句
CREATE TABLE test (
id int(4) DEFAULT NULL,
name varchar(16) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk

show create table test; //查找創(chuàng)建的表test

8.插入一條數(shù)據(jù)1,abcd
insert into test values(1,'abcd');

9.批量插入數(shù)據(jù)2,你好,3,richrich
insert into test values(2,'你好'),(3,'richrich');
select * from test;                       //查看test表所有數(shù)據(jù)

10.查詢名為richrich的記錄,id大于1的記錄
select  from test where name=richrich';
select  from test where id>1;

11.將id大于1的名字richrich改為hello
update test set name='hello' where id=1;
select * from test;

12.在字段name前插入age字段,類型為int(4)
desc test;         //查看表結(jié)構(gòu)
alter table test add age int(4) after id;

13.備份rich3庫及mysql庫
mysqldump -uroot -p'123456' -S /data/3306/mysql.sock --events -B rich3 mysql >/opt/bak_db_rich3_mysql.sql

14.刪除表中的所有數(shù)據(jù)并查看
truncate table test;   //清空表
desc test;   //查看表結(jié)構(gòu)
select * from test;//查看表test中所有的內(nèi)容
show tables;//查看當(dāng)前庫中所有的表

15.刪除數(shù)據(jù)庫rich3并查看
show databases;
drop database richy2;

16.在命令行恢復(fù)以上刪除的數(shù)據(jù)
mysql -uroot -p'123456' -S /data/3306/mysql.sock </opt/bak_db_rich3_mysql.sql

17.把GBK字符集修改為UTF8
將備份的數(shù)據(jù)文件里面的gkb替換為utf8在還原到數(shù)據(jù)庫中,命令行更改字符集,securecrt更改;
sed -i 's#gbk#utf8#g' /opt/bak_db_rich3_mysql.sql
mysql -uroot -p'123456' -S /data/3306/mysql.sock </opt/bak_db_rich3_mysql.sql

18.mysql密碼丟失找回
停止數(shù)據(jù)庫,跳過授權(quán)表啟動,登陸數(shù)據(jù)庫并修改密碼,最后在配置件里修改成新的密碼重啟數(shù)據(jù)庫
mysqld_safe --default-file=/data/3306/my.cnf --skip-grant-table &
mysql -S /data/3306/mysql.sock
update mysql.user set password=password('123456') where user='root' and host='localhost';
flush privileges;
vim /data/3306/my.cnf ---->mysql_pwd='123456'
/data/3306/mysql restart

19.中文數(shù)據(jù)亂碼原理及如何防止亂碼
mysql數(shù)據(jù)庫配置文件設(shè)置支持中文的字符集,終端設(shè)置支持中文的配置

20.設(shè)置id為主鍵,在name字段上創(chuàng)建普通索引
use rich3;
alter table test add primary key(id);//改表增加主鍵id
alter table test add index name(name);//改表增加索引name
desc test;//查看表結(jié)構(gòu)

21.在字段name后插入手機(jī)號字段(shouji),類型char(11)
alter table test add shouji char(11) after name;

22.在所有字段上插入2條記錄,數(shù)據(jù)任意
insert into test values(4,18,'rich',11111111111),(5,28,'tomn',22222222222);
update test set shouji='33333333333' where id<4;
update test set shouji='44444444444' where id<3;
update test set shouji='55555555555' where id<2;

23.在shouji字段上對前8個字符創(chuàng)建普通索引
alter table test add index shouji(shouji(8));

24.查看創(chuàng)建的索引及索引類型等信息
show index from test;
show index from test\G

25.刪除name,shouji列的索引
alter table test drop index name;
alter table test drop index shouji;

26.對name列的前6個字符以及shouji前8個字符組建聯(lián)合索引
alter table test add index lianhe(name(6),shouji(8));

27.查詢shouji以135開關(guān),名字為rich的記錄,提前插入記錄
insert into test values(6,38,'rich',13567894561);
select  from test where name='rich';
select  from test where name='rich' and shouji like '135%';

28.查詢上述語句的執(zhí)行計(jì)劃(是否使用聯(lián)合索引等)

  • explain select * from test where name='rich' and shouji like '135%';
  • 對于以上MySQL中簡單語句如何應(yīng)用相關(guān)內(nèi)容,大家還有什么不明白的地方嗎?或者想要了解更多相關(guān),可以繼續(xù)關(guān)注我們的行業(yè)資訊板塊。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI