快速掌握MySQL基礎(chǔ)教程方法

本文主要給大家介紹快速掌握MySQL基礎(chǔ)教程方法,文章內(nèi)容都是筆者用心摘選和編輯的,具有一定的針對性,對大家的參考意義還是比較大的,下面跟筆者一起了解下快速掌握MySQL基礎(chǔ)教程方法吧。

創(chuàng)新互聯(lián)公司服務項目包括臨汾網(wǎng)站建設(shè)、臨汾網(wǎng)站制作、臨汾網(wǎng)頁制作以及臨汾網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,臨汾網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到臨汾省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!

1分鐘安裝

Part1:寫在最前

MySQL安裝的方式有三種:

①rpm包安裝

②二進制包安裝

③源碼安裝

這里我們推薦二進制包安裝,無論從安裝速度還是用于生產(chǎn)庫安裝環(huán)境來說,都是沒問題的?,F(xiàn)在生產(chǎn)庫一般采用MySQL5.6,測試庫采用MySQL5.7。

MySQL5.6安裝看這里

http://suifu.blog.51cto.com/9167728/1846671

MySQL5.7安裝看這里

http://suifu.blog.51cto.com/9167728/1855415

8分鐘數(shù)據(jù)庫操作

Part1:登錄

MySQL的登錄方式為:

-u為用戶名,-p為密碼,如果您用了上述本文的安裝腳本,默認密碼為MANAGER

[root@HE3 ~]# mysql -uroot -pMANAGER

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 11

Server version: 5.7.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Part2:表基礎(chǔ)操作

①查看數(shù)據(jù)庫中有哪些庫

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| he1                |

| he3                |

| maxscale           |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

7 rows in set (0.00 sec)


②增刪改查

同Excel一樣,數(shù)據(jù)庫中也是要增刪改查的主要涉及的語法如下:

查:

首先選擇相應的庫

mysql> use maxscale

Database changed

select * from 表名;是查詢這張表所有內(nèi)容的意思;

select 列名,列名 from 表名;是查詢這張表想看的列內(nèi)容;

mysql> select a,b from helei;

+--------+------+

| a      | b    |

+--------+------+

| HE3    | a    |

| 寫入   | b    |

| 測試   | c    |

| 于浩   | d    |

| 賀磊   | e    |

+--------+------+

6 rows in set (0.00 sec)

增:

insert into 表名 values('想插入的內(nèi)容'); 往表中插入一條記錄的意思;

mysql> insert into helei values('插入','f');

Query OK, 1 row affected (0.01 sec)

mysql> select a,b from helei;

+--------+------+

| a      | b    |

+--------+------+

| HE3    | a    |

| 寫入   | b    |

| 測試   | c    |

| 于浩   | d    |

| 賀磊   | e    |

| 插入   | f    |

+--------+------+

6 rows in set (0.00 sec)

我這里表名叫helei;

刪:

delete from helei where b='f';刪除helei表中b列是f的所有記錄;

mysql> delete from helei where b='f';

Query OK, 1 row affected (0.01 sec)

mysql> select * from helei;

+--------+------+

| a      | b    |

+--------+------+

| HE3    | a    |

| 寫入   | b    |

| 測試   | c    |

| 于浩   | d    |

| 賀磊   | e    |

+--------+------+

5 rows in set (0.00 sec)

可以看到這里b列為f的整個一行就被刪除掉了。

改:

update 表名 set 列名='改成所需內(nèi)容' where 限定條件。

mysql> update helei set b='改' where a='賀磊';

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from helei;

+--------+------+

| a      | b    |

+--------+------+

| HE3    | a    |

| 寫入   | b    |

| 測試   | c    |

| 于浩   | d    |

| 賀磊   | 改   |

+--------+------+

5 rows in set (0.00 sec)

③表級操作

創(chuàng)建表

創(chuàng)建表t,這里用生產(chǎn)庫的來做例子,id列自增主鍵,log為varchar類型,可以存30個字符;

mysql> CREATE TABLE `t` (

    -> `id`  int UNSIGNED NOT NULL AUTO_INCREMENT ,

    -> `log`  varchar(30) NOT NULL DEFAULT '' ,

    -> PRIMARY KEY (`id`)

    -> )

    -> ;

Query OK, 0 rows affected (0.01 sec)

刪除表

刪除表t,整表刪除;

mysql> drop table t;

Query OK, 0 rows affected (0.02 sec)

Part3:庫基礎(chǔ)操作

創(chuàng)建庫

mysql> CREATE DATABASE helei DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;

Query OK, 1 row affected (0.00 sec)

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| he1                |

| he3                |

| helei              |

| maxscale           |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

8 rows in set (0.00 sec)

刪除庫

刪除名為helei的庫,注意,這一操作會刪除掉helei庫中所有的表;

mysql> drop database helei;

Query OK, 0 rows affected (0.00 sec)

1分鐘系統(tǒng)級操作

Part1:啟停數(shù)據(jù)庫

[root@HE3 ~]# /etc/init.d/mysqld status

 SUCCESS! MySQL running (3173)

[root@HE3 ~]# /etc/init.d/mysqld stop

Shutting down MySQL.... SUCCESS! 

[root@HE3 ~]# /etc/init.d/mysqld start

Starting MySQL.. SUCCESS!

附錄

Part1:常用SQL

創(chuàng)建和授權(quán)用戶

CREATE USER 'helei'@'%' IDENTIFIED BY 'MANAGER';

GRANT SELECT,insert,update,delete ON *.* TO 'helei'@'%';

創(chuàng)建數(shù)據(jù)庫:

CREATE DATABASE www CHARACTER SET utf8 COLLATE utf8_bin;

密碼變更:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MANAGER');

 

統(tǒng)計哪些ip連接

mysql> select substring_index(host,':', 1) from information_schema.processlist;

 

統(tǒng)計每個IP連接數(shù):

mysql> select substring_index(host,":", 1) ip, count(*)  from information_schema.processlist  group by  ip;

 

到庫級別的ip連接數(shù)查看:

mysql> select db, substring_index(host,":", 1) ip, count(*)  from information_schema.processlist  group by db, ip;

 

查看當前連接數(shù)

mysql> show status like 'Threads%';

 

粗略統(tǒng)計每張表的大小

mysql> select table_schema,table_name,table_rows from tables order by table_rows desc;

 

要想知道每個數(shù)據(jù)庫的大小的話,步驟如下:

1、進入information_schema 數(shù)據(jù)庫(存放了其他的數(shù)據(jù)庫的信息)

use information_schema;

 

2、查詢所有數(shù)據(jù)的大?。?/p>

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;

 

3、查看指定數(shù)據(jù)庫的大?。?/p>

比如查看數(shù)據(jù)庫home的大小

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home';

 

4、查看指定數(shù)據(jù)庫的某個表的大小

比如查看數(shù)據(jù)庫home中 members 表的大小

select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home' and table_name='members'; 

 

無法更新或刪除數(shù)據(jù)??梢酝ㄟ^設(shè)置FOREIGN_KEY_CHECKS變量來避免這種情況。 

  

SET FOREIGN_KEY_CHECKS = 0; 

刪除完成后設(shè)置 

SET FOREIGN_KEY_CHECKS = 1; 

 

其他: 

關(guān)閉唯一性校驗 

set unique_checks=0; 

set unique_checks=1;

 

變更字符集

ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8;

 

添加主鍵

alter table `helei` add column `id` int(10) not null auto_increment primary key comment '主鍵' first;   但會鎖表,先在測試庫中測試時間,如果時間長,嘗試利用pt工具

 

重命名表

alter table helei rename to helei_old;

 

鎖表(用戶退出則失效)

flush tables with read lock;unlock table;

 

鎖某張表

lock tables helei read;

 

找出id是奇數(shù)和偶數(shù)

select * from t where id &1

select * from t where id=(id>>1)<<1

  

查看數(shù)據(jù)庫已運行時間

show global status like 'uptime';

看完以上關(guān)于快速掌握MySQL基礎(chǔ)教程方法,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業(yè)知識信息 ,可以持續(xù)關(guān)注我們的行業(yè)資訊欄目的。

標題名稱:快速掌握MySQL基礎(chǔ)教程方法
瀏覽地址:http://www.muchs.cn/article8/ippiip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)頁設(shè)計公司品牌網(wǎng)站建設(shè)、服務器托管、網(wǎng)站建設(shè)、面包屑導航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站