概念:
按照一定的數(shù)據(jù)結(jié)構(gòu)來存儲和管理數(shù)據(jù)的倉庫
Mysql的客戶端工具:
字符界面
mysql
圖形界面
MySQL Workbench
第三方工具(Windows)
SQLyog
navicat
各文件的安裝位置:
數(shù)據(jù)庫相關(guān)文件(datadir目錄)
/var/lib/mysql
可執(zhí)行文件
/usr/bin
服務(wù)器-mysqld
/usr/sbin
啟動/關(guān)閉Mysql服務(wù)器:
使用操作系統(tǒng)工具systemctl管理mysql服務(wù)
systemctl status mysqld
systemctl start mysqld
systemctl stop mysqld
systemctl restart mysqld
連接至Mysql服務(wù)器:
mysql -uroot -p
Enter password:(密碼)
忘記root密碼后的重置
設(shè)置mysql啟動參數(shù)
[root@jayss bin]# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
重啟mysqld
[root@jayss etc]# systemctl restart mysqld
登錄mysql赎线,不輸入用戶和密碼直接回車
[root@jayss etc]# mysql
修改密碼
mysql> flush privileges
mysql> alter user root@localhost identified by 'Root@1995';
還原啟動參數(shù)
[root@jayss ~]# systemctl unset-environment MYSQLD_OPTS="--skip-grant-tables"
簡單信息查詢
查看MySQL版本
mysql> select version();
查看當前用戶
mysql> select user();
查看當前數(shù)據(jù)庫
mysql> select database();
查看當前連接的狀態(tài)
mysql> status
查看當前時間
mysql> select now();
查看表結(jié)構(gòu)
mysql> desc t;
執(zhí)行SQL腳本文件:
[root@jayss ~]# mysql -u root -p jayss < t.sql
庫級操作語句:
顯示所有的庫:show databases;
創(chuàng)建庫:create databases db_name廷没;
刪除庫:drop database db_name;
進入數(shù)據(jù)庫:use db_name;
表級操作語句:
顯示所有表:show tables;
創(chuàng)建表:create tble table_name;
顯示創(chuàng)建表的信息:show create table table_name;
刪除表:drop table table_name;
字段類型
數(shù)值類型:
數(shù)值類型 | 特性 |
---|---|
tinyint | 用一個字節(jié)存放整數(shù)(0,255) |
smallint | 兩個字節(jié)(0,65535) |
mediumint | 三個字節(jié)(0,16777215) |
int | 四個字節(jié)(0,4294967295) |
bigint | 八個字節(jié) |
float(m,n) | 單精度浮點型(四個字節(jié)) |
double(m,n) | 雙精度浮點型,m總個數(shù)垂寥,d小數(shù)位(八字節(jié)) |
字符類型:
字符類型 | 特性 |
---|---|
char(size) | 定長字符串(0-255字節(jié)) |
varchar(size) | 變長字符串(0-65535字節(jié)) |
tinyblolb | 不超過255個字符串的二進制字符串(0-255字節(jié)) |
tinytext | 短文本字符串(0-255字節(jié)) |
bolob | 二進制形式的長文本數(shù)據(jù)( 0-65 535字節(jié)) |
text | 長文本數(shù)據(jù)(0-65 535字節(jié)) |
mediumblob | 二進制形式的中等長度文本數(shù)據(jù)(0-16 777 215字節(jié)) |
mediumtext | 中等長度文本數(shù)據(jù)( 0-16 777 215字節(jié)) |
longbolob | 二進制形式的極大文本數(shù)據(jù)(0-4 294 967 295字節(jié)) |
longext | 極大文本數(shù)據(jù)( 0-4 294 967 295字節(jié)) |
時間日期類型:
時間日期類型 | 特性 |
---|---|
date | 日期颠黎,格式:2019-01-27 |
time | 時間,格式:12::12:30 |
datetime | 時間日期滞项,格式:2019-01-27 12:12:30 |
timestamp | 自動存儲記錄修改的時間 |
year | 存放年 |
約束條件:
約束是一種限制
通過對表中的數(shù)據(jù)做出限制來確保表中數(shù)據(jù)的完整性和唯一性
約束類型 | 關(guān)鍵字 | 特點 |
---|---|---|
默認 | default | 插入數(shù)據(jù)時狭归,若無明確為字段賦值則自動賦予默認值 |
非空 | not null | 限制一個字段的值不能為空,insert的時候必須為該字段賦值 |
唯一 | unique key | 限制一個字段的值不重復(fù)文判,該字段的數(shù)據(jù)不能出現(xiàn)重復(fù)的 |
自增長 | auto_increment | 自動編號过椎,和主鍵組合使用,一個表里面只能有一個自增長 |
主鍵 | primary key | 通常每張表都需要一個主鍵來體現(xiàn)唯一性戏仓,每張表里面只能有一個主鍵 |
外鍵 | foreign key | 保持數(shù)據(jù)的一致性疚宇,我有的你一定有, 你沒有的赏殃, 我絕對沒有 |
查詢
select語基本結(jié)構(gòu):
select … 指定要查詢的列名
from … 指定要查詢的表名
where … 指定查詢條件
group by … 指定要分組列
having … 指定過濾分組的條件
order by … 指定排序列
模糊查詢:
1.任意多個字符:%
2.任意一個字符:_
常用聚合函數(shù):
統(tǒng)計個數(shù):count(column)
列出字段全部值:group_conact(column)
求和:sum(column)
平均值:avg(column)
最大值:max(column)
最小值:min(column)
簡單查詢:
只指定表名:select * from table_name;
指定列名:select ename,sal from table_name;
使用列別名:select ename as emp_name from table_name;
附加條件:select ename from table_name where sal>2000;
排序:select ename from table_name order by sal;
連接查詢:
內(nèi)連接:
1.無條件內(nèi)連接:
Mysql> select * from tb1 [inner] join tb2
2.有條件內(nèi)連接
Mysql> select * from tb1 inner join tb2
-> on tb1_id =tb2_id;
外連接:
1.左外連接:(以左表為基準)
mysql> select * from tb1 left join tb2
-> on tb1_id= tb2_id;
2.右外連接:(以右表為基準)
mysql> select * from tb1 right join tb2
-> on tb1_id= tb2_id;
操作:
插入數(shù)據(jù):
指定字段插入:
insert into tb_name(字段) values(值)敷待;
全字段插入:
insert into tb_name values(值);
多行插入:
insert into tb_name(字段) values(值1),(值2)...嗓奢;
修改數(shù)據(jù):
修改所有數(shù)據(jù):
update tb_name set value_1=value_2;
修改多個:
update tb_name set value1_=value_2,value_3=value_4;
修改滿足條件的數(shù)據(jù):
update tb_name set value_1=value_2 where id=1
刪除數(shù)據(jù):
刪除表中所有的數(shù)據(jù):
delete from tb_name;
刪除表中滿足條件的數(shù)據(jù):
delete from tb_name where id1;
修改表結(jié)構(gòu):
修改表名:
alter table tb_name rename to new_name;
修改字段名:
alter table tb_name change name new_name 字段類型;
修改字段類型:
alter table tb_name modify 字段名 字段類型;
添加字段:
alter table tb_name add 字段名 類型;
刪除字段:
alter table tb_name drop 字段名;
表關(guān)系:
一對一
一對多
多對多
用戶管理
密碼:
1.密碼策略相關(guān)參數(shù):
mysql> show variables like 'validate%';
2.查看密碼策略制定參數(shù):
mysql> show variables like 'validate_password_policy';
3.修改密碼策略參數(shù):(修改密碼策略級別為0)
mysql> set global validate_password.policy=0;
mysql> select @@validate_password.policy;
用戶:
1.內(nèi)置用戶:
'root'@'localhost'
'mysql.sys'@'localhost'
'mysql.session'@'localhost'
'mysql.infoschema'@'localhost'
2.創(chuàng)建用戶
mysql> create user law identified by 'jayss'; #默認為jayss@'%'
mysql> create user tian@'%' identified by 'tian';
mysql> create user law@192.168.244.255 identified by 'jayss';
3.刪除用戶
mysql> drop user jayss;
4.鎖住/解鎖用戶
mysql> alter user jayss account lock;
mysql> alter user jayss account unlock;
5.修改口令:
mysql> alter user jayss@'%' identified by 'jayssjayss';
權(quán)限層次:
關(guān)鍵字 | 權(quán)限層次 |
---|---|
global | 服務(wù)器全局生效的權(quán)限 |
database | 數(shù)據(jù)庫生效的權(quán)限 |
table | 指定表生效的權(quán)限 |
column | 指定列生效的權(quán)限 |
routine | 存儲過程讼撒、函數(shù)生效的權(quán)限 |
查詢權(quán)限信息:
查詢自己的權(quán)限:
mysql> show grants;
root帳號查詢其他帳號:
mysql> show grants for user1@localhost;
mysql> show grants for jayss;
優(yōu)化
index種類:B-Tree、R-Tree股耽、hash
創(chuàng)建索引:
指定主鍵或附帶not null的唯一約束
自動創(chuàng)建clustered index
創(chuàng)建普通索引
1.mysql> create table t(a int, b char(10), c char(10), index idx_b(b));
2.mysql> alter table t add index idx_a(a);
3.mysql> create index idx_c on t(c);
多列復(fù)合索引
mysql> create index idx_com on emp(empno, ename, sal);
刪除索引:
1.mysql> drop index idx_a on t;
2.mysql> alter table t drop index idx_abc;
查看索引信息:
mysql> show index from t\G
歡迎技術(shù)交流
WeChat......