您好,登錄后才能下訂單哦!
本文主要給大家介紹快速掌握MySQL基礎(chǔ)教程方法,文章內(nèi)容都是筆者用心摘選和編輯的,具有一定的針對(duì)性,對(duì)大家的參考意義還是比較大的,下面跟筆者一起了解下快速掌握MySQL基礎(chǔ)教程方法吧。
Part1:寫在最前
MySQL安裝的方式有三種:
①rpm包安裝
②二進(jìn)制包安裝
③源碼安裝
這里我們推薦二進(jìn)制包安裝,無論從安裝速度還是用于生產(chǎn)庫(kù)安裝環(huán)境來說,都是沒問題的?,F(xiàn)在生產(chǎn)庫(kù)一般采用MySQL5.6,測(cè)試庫(kù)采用MySQL5.7。
MySQL5.6安裝看這里
http://suifu.blog.51cto.com/9167728/1846671
MySQL5.7安裝看這里
http://suifu.blog.51cto.com/9167728/1855415
Part1:登錄
MySQL的登錄方式為:
-u為用戶名,-p為密碼,如果您用了上述本文的安裝腳本,默認(rèn)密碼為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ù)庫(kù)中有哪些庫(kù)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| he1 |
| he3 |
| maxscale |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.00 sec)
②增刪改查
同Excel一樣,數(shù)據(jù)庫(kù)中也是要增刪改查的主要涉及的語法如下:
查:
首先選擇相應(yīng)的庫(kù)
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è)試 | 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è)試 | 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è)試 | c |
| 于浩 | d |
| 賀磊 | e |
+--------+------+
5 rows in set (0.00 sec)
可以看到這里b列為f的整個(gè)一行就被刪除掉了。
改:
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è)試 | c |
| 于浩 | d |
| 賀磊 | 改 |
+--------+------+
5 rows in set (0.00 sec)
③表級(jí)操作
創(chuàng)建表
創(chuàng)建表t,這里用生產(chǎn)庫(kù)的來做例子,id列自增主鍵,log為varchar類型,可以存30個(gè)字符;
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:庫(kù)基礎(chǔ)操作
創(chuàng)建庫(kù)
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)
刪除庫(kù)
刪除名為helei的庫(kù),注意,這一操作會(huì)刪除掉helei庫(kù)中所有的表;
mysql> drop database helei;
Query OK, 0 rows affected (0.00 sec)
Part1:啟停數(shù)據(jù)庫(kù)
[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ù)庫(kù):
CREATE DATABASE www CHARACTER SET utf8 COLLATE utf8_bin;
密碼變更:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MANAGER');
統(tǒng)計(jì)哪些ip連接
mysql> select substring_index(host,':', 1) from information_schema.processlist;
統(tǒng)計(jì)每個(gè)IP連接數(shù):
mysql> select substring_index(host,":", 1) ip, count(*) from information_schema.processlist group by ip;
到庫(kù)級(jí)別的ip連接數(shù)查看:
mysql> select db, substring_index(host,":", 1) ip, count(*) from information_schema.processlist group by db, ip;
查看當(dāng)前連接數(shù)
mysql> show status like 'Threads%';
粗略統(tǒng)計(jì)每張表的大小
mysql> select table_schema,table_name,table_rows from tables order by table_rows desc;
要想知道每個(gè)數(shù)據(jù)庫(kù)的大小的話,步驟如下:
1、進(jìn)入information_schema 數(shù)據(jù)庫(kù)(存放了其他的數(shù)據(jù)庫(kù)的信息)
use information_schema;
2、查詢所有數(shù)據(jù)的大?。?/p>
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;
3、查看指定數(shù)據(jù)庫(kù)的大?。?/p>
比如查看數(shù)據(jù)庫(kù)home的大小
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home';
4、查看指定數(shù)據(jù)庫(kù)的某個(gè)表的大小
比如查看數(shù)據(jù)庫(kù)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)閉唯一性校驗(yà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; 但會(huì)鎖表,先在測(cè)試庫(kù)中測(cè)試時(shí)間,如果時(shí)間長(zhǎng),嘗試?yán)胮t工具
重命名表
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ù)庫(kù)已運(yùn)行時(shí)間
show global status like 'uptime';
看完以上關(guān)于快速掌握MySQL基礎(chǔ)教程方法,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業(yè)知識(shí)信息 ,可以持續(xù)關(guān)注我們的行業(yè)資訊欄目的。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。