基本語法(命令)
- 【連接】數(shù)據(jù)庫
mysql -h localhost -u root -p
-h 服務(wù)器地址
-u 用戶名
-p 密碼(回車后輸入)
-P(大寫) 端口號
-b 關(guān)閉蜂鳴器
mysql -u root -p
【庫】的操作:
【查看庫】show databases;
【創(chuàng)建庫】create database (庫名) default charset=utf8mb4;
【刪除庫】drop database (庫名);
【打開庫】use (庫名);
【表】的操作:
【查看表】show tables;
【創(chuàng)建表】:
mysql> create table (表名) (
-> uid int,
-> name char(4)
-> ) engine=innodb default charset=utf8mb4;
【查看建表語句】show create table (表名)\G;
【查看語句分析語句是否運用索引】:
desc select * from (表名) where uid=1;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t1
type: const
possible_keys: PRIMARY // 是否存在索引
key: PRIMARY // 是否運用索引
key_len: 4
ref: const
rows: 1 // 受影響行數(shù)(主要看這個)
Extra:
1 row in set (0.00 sec)
【刪除表】drop table (表名);
【設(shè)置表嚴格模式】truncate (表名);
【查看表結(jié)構(gòu)】desc (表名);
【查看表注釋】show create table (表名);
【查看表里字段詳細信息】show full columns from (表名);
【修改表注釋】alter table (表名) comment "(修改后的字段注釋)";
【修改字段的注釋】
alter table (表名) modify column (要修改的字段名) varchar(200) comment "(修改后的字段注釋)";
【修改字段的注釋和字符集】
alter table (表名) modify column (字段名) varchar(200) character set utf8;
【把字段 shipping_status 的類型從 ENUM('未收貨') 改為 ENUM('未收貨','未發(fā)貨')】
ALTER TABLE yed_orders MODIFY shipping_status ENUM('未收貨','未發(fā)貨');
對數(shù)據(jù)的【增刪改查】:
【增加數(shù)據(jù)】insert into (表名)(id,name,nickname,sex) value(1,"zhangsan","張三","女");
【增加多條】insert into (表名)(id,name,nickname,sex) value(1,"zhangsan","張三","女"),(2,"lisi","李四","人妖");
【暴力添加數(shù)據(jù)】insert into (表名)(name,email) select name,email from (同前面的表名) where id = 5;
【刪除數(shù)據(jù)】delete from (表名) where id=250;
【修改數(shù)據(jù)】update (表名) set (列名)=(新值) where (列名)=(字段名);
查詢數(shù)據(jù) 可以多樣化戳玫,下面我列出各種奇葩查詢方式
1. 條件部分查詢
【查詢數(shù)據(jù)】select * from (表名); (*代表所有字段,一般數(shù)據(jù)量大的不要這么查詢會卡死)
【查詢部分字段】select name,sex from (表名);
【列合并】select id,name,count(age,sex)xxoo from (表名); (xxoo是指給age和sex合并字段后起的別名)
【查詢10年后年齡的人】select id,name,age+10 xxoo from (表名);
【去除重復(fù)字段】select distinct * from (表名);
【某個班級的學(xué)生】select * from (表名) where class="一年級三班";
【一年級三班的女生】select * from (表名) where class="一年級三班" and sex="女"; (and用法''并且'')
【年齡>20的學(xué)生】select * from (表名) where age > 20;
【id號除了10-40的都查詢 方法1:】select * from (表名) where id<10 or id>40; (or用法''或者'')
【id號除了10-40的都查詢 方法2:】select * from (表名) where id not between 10 and 40;
【A班男生和B班女生】select * from (表名) where (class="A" and sex="男") or (class="B" and sex="女");
【查找多個id】select * from (表名) where id in(2,4,8,23,58);
【找名字為兩個字的人】select * from (表名) where name like "__"; (一個‘_’代表一個字符)
【查找?guī)?貓"的詞】select * from (表名) where name like "%貓%";
【查找有三也有貓字】select * from (表名) where name like "%三%" and name like "%貓%";
【時間從遠到近排序】select * from (表名) order by time;
【時間從近到遠排序】select * from (表名) order by time desc;
【先時間再年齡排序】select * from (表名) order by time,age desc; (處理相同情況下一般用不到)
3. 分組查詢
group by統(tǒng)計函數(shù) count(*)統(tǒng)計記錄條數(shù) sum(字段名)對該字段求和
avg()平均數(shù) max()某字段最大值 min()最小值
【總共多少人】select count(*) from (表名);
【總共多少人起別名查詢】select count(*) countName from (表名);
【每個班人數(shù)】select class,count(*) from (表名) group by class;
【每個班最大年齡】select class,max(age) from (表名) group by class;
【在平均年齡之上的人】select * from (表名) where age >(select avg(age) from (表名));
【每個班的男生女生各多少】select class,sex,count(*) from (表名) group by class,sex;
4. 分頁查詢
【顯示前5條數(shù)據(jù)】select * from (表名) limit 5;
【跳過前兩條,顯示后面3條】select * from (表名) limit 2,3;
顯示第X頁的公式:select * from (表名) limit(x-1)*跳過條數(shù),顯示條數(shù);
通俗一點就是:(當(dāng)前頁-1)*每頁多少條,每頁多少條;
5. Join聯(lián)表查詢
內(nèi)聯(lián) 方式1:select * from (表A),(表B) where (表A).id = (表B).id;
內(nèi)聯(lián) 方式2:select * from (表A) inner join (表B) on (表A).id = (表B).id;
(inner join用法)
左聯(lián):select * from (表A) left join (表B) on (表A).id = (表B).id;
(left join用法)
右聯(lián):select * from (表A) right join (表B) on (表A).id = (表B).id;
(right join用法)
6. 修改數(shù)據(jù)庫root密碼
- mysqladmin修改
mysqladmin -u root -p password
- Sql修改
>use mysql
>set password for root@localhost = password('123'); // 熱修改不需要重啟
>flush privileges;
7. 授權(quán)添加用戶
授權(quán) 具體權(quán)限 ON 庫.表 TO '誰'@'在哪兒登錄' IDENTIFIED BY 密碼
【添加用戶】grant select,insert on discuz.stu to 'xxoo'@'%' identified by '密碼'
【刪除用戶】drop user ''@'localhost';
操作數(shù)據(jù)表結(jié)構(gòu)(命令)
對表字段的【增刪改查】
【添加字段】alter table (表名) add (字段名) (字段屬性);
// 示例:
// alter table user add column is_disable tinyint(1) not null default '0' comment '是否啟用0啟動1禁用' after email;
// comment是字段注釋 after是指定放在字段后面
【刪除字段】alter table (表名) drop (字段名);
【修改字段】alter table (表名) modify (字段名) (新的字段屬性); (修改方法1)
【修改字段】alter table (表名) change (原字段名) (新字段名) (新字段屬性); (修改方法2)
【查詢字段】desc (表名);
【修改表名】alter table (原表名A) rename as (新表名B);
【字段后面加字段】alter table (表名) add (新字段名) (新字段屬性) after (參照物字段名);
數(shù)據(jù)庫的導(dǎo)入導(dǎo)出
導(dǎo)出數(shù)據(jù)【庫】:mysqldump -u root -p (庫名)>(文件名).sql;
導(dǎo)出數(shù)據(jù)【庫】只要表結(jié)構(gòu):mysqldump -u root -p -d (庫名)>(文件名).sql;
導(dǎo)入數(shù)據(jù)【庫】:mysql -u root -p (庫名)<(文件名).sql;
導(dǎo)出數(shù)據(jù)【表】:mysqldump -u root -p (庫名) (表名)>(文件名).sql;
導(dǎo)入數(shù)據(jù)【表】:
方法1:mysql -u root -p (庫名)</data/home/(單個表名).sql
方法2:Mysql>source /data/home/(單個表名).sql
數(shù)據(jù)庫事務(wù)模式
begin 開啟事務(wù)(只記錄有效金句,刪除表蛾方、庫鸟款、字段無效)
rollback 回滾(撤銷)
commit 確認(提交)
操作數(shù)據(jù)表添加索引
方式1 (alter)
【添加普通索引】alter table (表名) add index 索引名(字段名);
【添加唯一索引】alter table (表名) add uniqe 索引名(字段名);
【添加主鍵索引】alter table (表名) add primary key 索引名(字段名);
方式2 (create)
【創(chuàng)建普通索引】create index (索引名) on 表名(字段名);
【創(chuàng)建唯一索引】create uniqe index (索引名) on 表名(字段名);
字段約束的詞語:
unsigned 無符號 zerofill 零填充,位數(shù)不夠時0填充 not null 不能為空 unique 唯一
default 給默認值 auto_increment 自增 primary key 主鍵,不能為空且唯一
索引 : 可以提高查詢速率(但是在insert和update時會產(chǎn)生額外的開銷)
- mysql索引類型normal,unique哩治,full text的區(qū)別是什么猿规?
normal
:普通索引唯一的任務(wù)是加快對數(shù)據(jù)的訪問速度
unique
:唯一索引表示唯一的,不允許重復(fù)的索引尤揣,可以為null
full text
:全文索引表示全文搜索的索引搔啊。 FULLTEXT用于搜索很長一篇文章的時候,效果最好北戏。用在比較短的文本负芋,如果就一兩行字的,普通的INDEX 也可以
spatial
:空間索引空間索引只能在存儲引擎為MYISAM的表中創(chuàng)建
key
:普通索引普通非唯一索引
primary
:唯一索引嗜愈,不允許為null
- MySQL索引方法
B-Tree:
B-Tree是最常見的索引類型旧蛾,所有值(被索引的列)都是排過序的,每個葉節(jié)點到跟節(jié)點距離相等蠕嫁。
所以`B-Tree`適合用來查找某一范圍內(nèi)的數(shù)據(jù)锨天,而且可以直接支持數(shù)據(jù)排序`(ORDER BY)`
`B-Tree`在`MyISAM`里的形式和`Innodb`稍有不同:
`MyISAM`表數(shù)據(jù)文件和索引文件是分離的,索引文件僅保存數(shù)據(jù)記錄的磁盤地址
`InnoDB`表數(shù)據(jù)文件本身就是主索引拌阴,葉節(jié)點`data`域保存了完整的數(shù)據(jù)記錄
Hash索引:
`1.`僅支持"=","IN"和"<=>"精確查詢绍绘,不能使用范圍查詢:
由于Hash索引比較的是進行Hash運算之后的Hash值,不能用于范圍的過濾
`2.`不支持排序:由于Hash索引中存放的是經(jīng)過Hash計算之后的Hash值
`3.`在任何時候都不能避免表掃描:無法從Hash索引中直接完成查詢,
還是要通過訪問表中的實際數(shù)據(jù)進行相應(yīng)的比較陪拘,并得到相應(yīng)的結(jié)果
`4.`檢索效率高厂镇,索引的檢索可以一次定位,不像B-Tree索引需要從根節(jié)點到枝節(jié)點左刽,
最后才能訪問到頁節(jié)點這樣多次的IO訪問捺信,所以Hash索引的查詢效率要遠高于B-Tree索引
`5.`只有Memory引擎支持顯式的Hash索引
Mac 用包管理 brew 安裝 Mysql
【查看所有版本】brew search mysql
【指定安裝版本】brew install mysql@5.7
【啟動命令】brew services start mysql@5.7
或 /usr/local/opt/mysql@5.7/bin/mysql.server start
【查看進程】ps -ef | grap mysql
【查看包路徑】brew --prefix mysql@5.7
【設(shè)置bash命令】alias mysql=/usr/local/opt/mysql@5.7/bin/mysql
或 sudo ln -s /usr/local/opt/mysql@5.7/bin/mysql /usr/bin
- 配置外部訪問
1.【打開配置文件】vim /usr/local/etc/my.cnf
注釋bind-address = 127.0.0.1 改為 # bind-address = 127.0.0.1
2.【修改mysql連接限制】后退看我 Navicat 文章。
如果你也是 少年健忘 || 老年健忘欠痴,希望能給你帶來方便使用迄靠。