新聞中心
本文主要給大家介紹管理MySQL數(shù)據(jù)庫的幾個(gè)常用簡(jiǎn)單命令,文章內(nèi)容都是筆者用心摘選和編輯的,具有一定的針對(duì)性,對(duì)大家的參考意義還是比較大的,下面跟筆者一起了解下管理Mysql數(shù)據(jù)庫的幾個(gè)常用簡(jiǎn)單命令吧。
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比大峪網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式大峪網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋大峪地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
一、數(shù)據(jù)管理的幾個(gè)階段
1、人工管理階段
特點(diǎn): 數(shù)據(jù)不能長(zhǎng)期保存 數(shù)據(jù)不能共享 數(shù)據(jù)不具有獨(dú)立性 程序本身管理數(shù)據(jù)
2、文件系統(tǒng)階段
特點(diǎn): 數(shù)據(jù)可長(zhǎng)期保存 數(shù)據(jù)由文件系統(tǒng)管理 數(shù)據(jù)冗余大、共享性差 數(shù)據(jù)獨(dú)立性差
3、數(shù)據(jù)庫系統(tǒng)階段
特點(diǎn): 數(shù)據(jù)實(shí)現(xiàn)結(jié)構(gòu)化 數(shù)據(jù)實(shí)現(xiàn)了共享性 數(shù)據(jù)獨(dú)立性強(qiáng) 數(shù)據(jù)粒度變小
二、數(shù)據(jù)庫
1、數(shù)據(jù)庫系統(tǒng)涉及的幾個(gè)概念
DB:Database(數(shù)據(jù)庫) DBMS:Database Management System數(shù)據(jù)庫管理系統(tǒng) DBS:數(shù)據(jù)庫系統(tǒng) 計(jì)算機(jī)硬件、軟件 DB DBMS DBA(數(shù)據(jù)庫管理員)
2、常見的數(shù)據(jù)庫管理系統(tǒng)
* Oracle 甲骨文 * MySQL 甲骨文 * Sql Server 微軟 (主要用在windows環(huán)境) * DB2 IBM公司 * SQLlite 用在移動(dòng)端開發(fā)
一般使用MySQL
3、MySQL
為什么選擇MYSQL?
(1)開源 (2)免費(fèi) (3)跨平臺(tái) (4)性能較好 (5)用戶多,在很多軟件公司中使用
4、MySQL數(shù)據(jù)的安裝
(1)獲取MYSQL安裝程序
http://www.mysql.com/downloads/
MySQL Community Edition
(2)兩種安裝包
Msi包(安裝版) 解壓縮版
(3)解壓縮版安裝與卸載(初學(xué)者最好使用解壓縮版)
解壓后 bin/mysql.exe 客戶端程序 bin/mysqld.exe 云服務(wù)器端程序 bin/mysqldump.exe 數(shù)據(jù)備份程序 初始化(只需要執(zhí)行一次) bin/mysqld --initialize --console 初始化結(jié)束會(huì)生成data的文件夾,其中保存系統(tǒng)和用戶的數(shù)據(jù) 注意記錄臨時(shí)密碼 w8ef!u*y5nZg 啟動(dòng)數(shù)據(jù)庫服務(wù) bin/mysqld --console 端口用的是3306 可以使用Ctrl+C 停止服務(wù)程序,或者直接關(guān)閉窗口
(4)使用客戶端連接(首先開啟服務(wù)端)
使用客戶端連接 bin/mysql -u 用戶名 -p
初始化時(shí)mysql有一個(gè) root (數(shù)據(jù)庫管理員用戶)
bin\mysql -uroot -p
正確登錄后會(huì)出現(xiàn) mysql> 提示符,要退出用quit命令
修改密碼(只需要執(zhí)行一次)
alter user 'root'@'localhost' identified by 'root';
(5)配置mysql的bin目錄至環(huán)境變量(只需要執(zhí)行一次),在path中配置
(6)將mysql安裝為系統(tǒng)服務(wù)(只需要執(zhí)行一次) bin\mysqld install 服務(wù)名
如果服務(wù)名沒有提供,默認(rèn)為MySQL
如果要卸載服務(wù):sc delete 服務(wù)名
如果安裝服務(wù)或刪除時(shí)出現(xiàn)權(quán)限不足,用管理員身份運(yùn)行cmd(黑窗口)
5、MYSQL的目錄結(jié)構(gòu)
Bin:mysql應(yīng)用程序目錄 Data:一般為數(shù)據(jù)目錄 Docs:文檔 Include:頭文件 Lib:庫文件 Mysql-test:測(cè)試目錄 Share:存放字符集及不同語言信息 Sql-bench:壓力測(cè)試工具 Mydefault.ini :mysql配置文件
6、數(shù)據(jù)庫基本概念
數(shù)據(jù)庫:一個(gè)數(shù)據(jù)庫管理系統(tǒng)(DBMS)可以管理多個(gè)數(shù)據(jù)庫 數(shù)據(jù)表:一個(gè)數(shù)據(jù)庫中有多個(gè)數(shù)據(jù)表 數(shù)據(jù)行:一個(gè)數(shù)據(jù)表中有多個(gè)數(shù)據(jù)行 數(shù)據(jù)列(字段):一行數(shù)據(jù)有多個(gè)列 視圖:虛擬表 索引:用于加快對(duì)表數(shù)據(jù)的訪問 主鍵:主鍵字段不允許為空并且唯一標(biāo)識(shí)表中的一行 外鍵:用于指定兩個(gè)表之間的關(guān)系 事務(wù):更新數(shù)據(jù)的一個(gè)程序單元
7、建庫
設(shè)置默認(rèn)的字符編碼
一種是在建庫語句上帶上字符編碼
另一種是在啟動(dòng)服務(wù)時(shí)添加配置文件
(1)建庫SQL語句(在mysql提示符下運(yùn)行)
create database 數(shù)據(jù)庫名 character set utf8mb4;
mysql中的utf8字符不夠完整,utf8mb4是完整版本的
刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名;
查看建庫信息
show create database 數(shù)據(jù)庫名;
(2)要修改全局的設(shè)置,在mysql解壓目錄添加 my.ini 配置文件,在其中輸入(推薦)
[mysqld] character-set-server=utf8mb4
停止舊服務(wù),刪除舊服務(wù),安裝新服務(wù),啟動(dòng)新服務(wù)
net stop mysql sc delete mysql mysqld install net start mysql
三、MySQL數(shù)據(jù)庫操作命令
1、建表(table)
一個(gè)庫中有多張表,每張表里有多條數(shù)據(jù)
表分成行(row 橫向)與列(column 縱向)
學(xué)號(hào) 姓名 性別
1 張三 男
2 李四 女
...
- 選庫
use 庫名; - 建表
create table 表名( 列1名 類型, 列2名 類型, ... );
create table student( id int, name varchar(10), sex char(1) );
整數(shù)類型: tinyint(1個(gè)字節(jié)), smallint(2個(gè)字節(jié)), int(4個(gè)字節(jié)), bigint(8個(gè)字節(jié))
無符號(hào)數(shù)字 tinyint unsigned (0~255)
浮點(diǎn)類型: float, double
定點(diǎn)小數(shù): decimal(總位數(shù), 小數(shù)位數(shù))
decimal(10, 2) 小數(shù)部分兩位,整數(shù)部分最大8位
字符類型
char(長(zhǎng)度) char(10) 表示最多存10個(gè)字符, 定長(zhǎng),效率高
varchar(長(zhǎng)度) varchar(10) 表示最多存儲(chǔ)10個(gè)字符,變長(zhǎng)
"abc" "abc " 存儲(chǔ)時(shí),長(zhǎng)度不足,用空格補(bǔ)齊
"abc" "abc" 存儲(chǔ)時(shí),根據(jù)實(shí)際長(zhǎng)度存儲(chǔ),可以節(jié)省空間
日期類型
datetime
timestamp
- 插入數(shù)據(jù)
insert into 表名(列1, 列2, ... 列n) values (值1, 值2, ... 值n);
注意:值個(gè)數(shù)要與列個(gè)數(shù)一致
insert into student(id,name,sex) values(1, '張三', '男');
- 查詢數(shù)據(jù)
語法:select 列1, 列2, ... from 表名;
select id, name, sex from student;
查看所有庫
show databases;
- 查看所有表
show tables;
- 唯一主鍵
每張表只能有一個(gè)主鍵
主鍵的值必須是唯一,且非空的create table 表名( 列1名 類型 primary key, 列2名 類型, ... );
刪表語法:drop table 表名;
create table student( id int primary key, name varchar(10), sex char(1) );
insert into student(id,name,sex) values(null, '李四', '男');
自增列,用來解決主鍵沖突問題
在主鍵列后加入:auto_increment
1 2 3 4 ....
因?yàn)閕d列由數(shù)據(jù)庫維護(hù),所以有了自增列后就不需要給id列賦值了
create table student( id int primary key auto_increment, name varchar(10), sex char(1) );
一次插入多條記錄(mysql獨(dú)有)
insert into student(name,sex) values('李四', '男'),('王五', '男'),('趙柳', '男');
查詢所有列
select from student;
mysql會(huì)把翻譯成:id,name,sex- 刪除記錄(只刪除數(shù)據(jù),不刪表)
delete from 表 where 條件;
刪除表(連表和數(shù)據(jù)一塊刪除)
drop table 表;
只刪除id=1004的記錄
delete from student where id=6;
create table a ( birthday datetime );
insert into a (birthday) values ('1988-5-4');
insert into a (birthday) values ('1988-5-4 16:39:00');
設(shè)置自增列從1001開始的兩種方法
1.創(chuàng)建表的時(shí)候
create table b ( id int primary key auto_increment, name varchar(20) ) auto_increment=1001;
2.修改表
create table b ( id int primary key auto_increment, name varchar(20) ); alter table b auto_increment=1001;
2、數(shù)據(jù)庫語法
SQL語句
1. DDL 數(shù)據(jù)定義語言
create database 數(shù)據(jù)庫名; create table 表名(列定義); (重點(diǎn)) drop database 數(shù)據(jù)庫名; drop table 表名; alter table 表 ... (添加列, 修改列, 刪除列, 重命名列8.0才有) alter user 用戶 create -- 創(chuàng)建xx定義, drop -- 刪除xx定義, alter -- 修改xx定義
添加列
語法:alter table 表名 add 列名 數(shù)據(jù)類型;
例如:給student新增一個(gè)age列
alter table student add age tinyint unsigned;
修改列
語法:alter table 表名 modify 列名 新類型;
例如要修改列的長(zhǎng)度定義原來varchar(10)
alter table student modify name varchar(20);
刪除列
語法:alter table 表名 drop 列名;
重命名列
語法:alter table 表名 rename column 舊列名 to 新列名;(mysql8.0才有)
2. DML (數(shù)據(jù)操控語言) (重點(diǎn))
insert
語法1:
insert into 表名(列...) values(值...); 插入一行
語法2:
insert into 表名(列...) values(值...), (值...), (值...), (值...); 插入多行
create table student2( id int primary key, name varchar(20), sex char(1) );
語法3:從表1查詢,把查詢結(jié)果插入表2
insert into 表2 select * from 表1;
如果兩張表結(jié)構(gòu)不一樣,可以在select后加具體的列名,以便和新表的列相匹配
例如:
create table student3( id int primary key, name varchar(20) ); insert into student3 select id,name from student;
load data
可以把外部文本文件的內(nèi)容導(dǎo)入到數(shù)據(jù)庫表中
語法:load data infile '文件路徑\文件名字' into table 表名;
create table hero( id int primary key, name varchar(10), loc varchar(10), sex char(1), birth int, death int, power int );
要讓load data命令生效,必須修改設(shè)置:
[mysqld] character-set-server=utf8mb4 secure-file-priv=
其中secure-file-priv默認(rèn)是null值,表示不允許加載文件
可以改為具體目錄名,表示只能從這個(gè)目錄加載文件
如果改為"",表示可以從任意目錄加載文件
例如:加載之前heroes.txt,把數(shù)據(jù)存入hero:
load data infile 'e:\\heroes.txt' into table hero;
如果文件中的列分隔符是, 不是默認(rèn)\t 鍵,需要用 fields TERMINATED BY來指定分隔符
load data infile 'e:\\person.txt' into table person fields TERMINATED BY ',';
source
source 文件路徑/文件名
- 其文件內(nèi)容必須是合法的sql語句
- 與load的區(qū)別:不用引號(hào),建議使用/分隔路徑,文件編碼與操作系統(tǒng)編碼一致(gbk)
update 更新
語法:
update 表名 set 列名=新值 where 條件;
update person set sex='男'; // 把所有記錄性別都改為男 update person set sex='男' where id=1; // 只修改id=1的性別為男
delete 刪除
語法:
delete from 表名; // 刪除表中所有記錄(危險(xiǎn)操作) delete from 表名 where 條件; // 刪除滿足條件的記錄
select 查詢
語法:
select 列名... from 表 where 條件 group by 分組條件 having 分組篩選條件 order by 排序條件 limit;
條件 where
= 等值匹配 != 不等值匹配 > 大于 < 小于 >= 大于等于 <= 小于等于
邏輯運(yùn)算符組合多個(gè)條件
邏輯與(兩個(gè)條件同時(shí)成立) and 例如:
select * from hero where sex='女' and loc='建業(yè)';
邏輯或(兩個(gè)條件有一個(gè)成立,結(jié)果就是真) or
select * from hero where name='小喬' or id=200; 邏輯非 (條件取反) not
列 between 值1 and 值2 等價(jià)于 列 >= 值1 and 列 <= 值2, 注意小值要在前面,包含邊界的值
列 in (值1,值2,... 值n) 等價(jià)于 列=值1 or 列=值2 ... or 列=值n 注意值列表的長(zhǎng)度
like 模糊查詢 其中匹配通配符 % 表示匹配0~多個(gè)任意字符
通配符 _ 表示匹配1個(gè)任意字符
例如:
select * from hero where power between 85 and 90; select * from hero where power >= 85 and power <=90;
not in not like not between ... and
排序條件
排序條件:列名 升降序 如果升降序關(guān)鍵字省略,默認(rèn)是asc
升序-> 由小到大 asc
降序-> 由大到小 desc
select * from hero order by power desc limit 10;
多列排序: 排序條件1, 排序條件2 ...
先按照條件1排序,條件1中取值相同的,再按照條件2排序
限制返回結(jié)果個(gè)數(shù)
limit m; // 最多返回m個(gè)結(jié)果 limit n,m; // 最多返回m個(gè)結(jié)果,n代表起始下標(biāo),下標(biāo)從0開始
經(jīng)常用來實(shí)現(xiàn)分頁應(yīng)用,假設(shè)每頁10條
第一頁 limit 0,10; 第二頁 limit 10,10; 第三頁 limit 20,10;
分組條件
select count(*),max(sal),min(sal),sum(sal),avg(sal),deptno from emp group by deptno;
count(*) 表示求每組的個(gè)數(shù) max(列) 求最大值 min(列) 求最小值 sum(列) 求和 avg(列) 求平均值
分組之后,
- select子句中只能出現(xiàn)分組條件列和組函數(shù),其他列不能出現(xiàn)在select中,
- order by 子句中只能出現(xiàn)分組條件列和組函數(shù),其他列不能出現(xiàn)在order by中,
例如:
select deptno,max(sal),ename from emp group by deptno; // ename不符合剛才的規(guī)定
select deptno,max(sal) from emp order by ename; // 錯(cuò)誤的
having 也是過濾
where > group by > having > select > order by > limit // sql語句的執(zhí)行順序
select count(*), deptno from emp where count(*) >=5 group by deptno; // 因?yàn)閣here先執(zhí)行,這時(shí)候還沒有分組,不知道個(gè)數(shù),錯(cuò)誤 select count(*), deptno from emp group by deptno having count(*)>=5;
有時(shí)候篩選條件既可以寫在where 上,也可以寫在having (優(yōu)先采用where)
select count(*), deptno from emp where deptno=10 or deptno=30 group by deptno; select count(*), deptno from emp group by deptno having deptno=10 or deptno=30;
多列分組 (了解)
多個(gè)列取值都相同的分為一組
group by 列1,列2 ...
select count(*),deptno,job from emp group by job,deptno;
多列分組時(shí),列的順序不影響結(jié)果
多表結(jié)構(gòu)和連接查詢
select ... from 表1 inner join 表2 on 連接條件 where group by having order by limit;
select empno,ename,sal,emp.deptno,dept.deptno,dname,loc from emp inner join dept on emp.deptno = dept.deptno;
表1 表1別名
select empno,ename,sal,e.deptno,d.deptno,dname,loc from emp e inner join dept d on e.deptno = d.deptno;
+-------+--------+---------+--------+ +--------+------------+----------+
| empno | ename | sal | deptno | | deptno | dname | loc |
+-------+--------+---------+--------+ +--------+------------+----------+
| 7369 | SMITH | 800.00 | 20 | | 20 | RESEARCH | DALLAS |
| 7499 | ALLEN | 1600.00 | 30 | | 30 | SALES | CHICAGO |
| 7521 | WARD | 1250.00 | 30 | | 30 | SALES | CHICAGO |
| 7566 | JONES | 2975.00 | 20 | | 20 | RESEARCH | DALLAS |
| 7654 | MARTIN | 1250.00 | 30 | | 30 | SALES | CHICAGO |
| 7698 | BLAKE | 2850.00 | 30 | | 30 | SALES | CHICAGO |
| 7782 | CLARK | 2450.00 | 10 | | 10 | ACCOUNTING | NEW YORK |
| 7788 | SCOTT | 3000.00 | 20 | | 20 | RESEARCH | DALLAS |
| 7839 | KING | 5000.00 | 10 | | 10 | ACCOUNTING | NEW YORK |
| 7844 | TURNER | 1500.00 | 30 | | 30 | SALES | CHICAGO |
| 7876 | ADAMS | 1100.00 | 20 | | 20 | RESEARCH | DALLAS |
| 7900 | JAMES | 950.00 | 30 | | 30 | SALES | CHICAGO |
| 7902 | FORD | 3000.00 | 20 | | 20 | RESEARCH | DALLAS |
| 7934 | MILLER | 1300.00 | 10 | | 10 | ACCOUNTING | NEW YORK |
+-------+--------+---------+--------+
+--------+------------+----------+
| deptno | dname | loc |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
幾種連接查詢
表1 inner join 表2 on 連接條件 (內(nèi)連接:兩張表的記錄必須完全滿足連接條件,才會(huì)出現(xiàn)在最后結(jié)果中) 表1 left outer join 表2 on 連接條件 (左外連接) 表1 right outer join 表2 on 連接條件 (右外連接)
select empno, ename, e.deptno, d.deptno, d.dname, d.loc from emp e left outer join dept d on d.deptno=e.deptno;
left outer join 位于連接左側(cè)的表,不管是否連接到了記錄,都會(huì)出現(xiàn)在結(jié)果中
符合連接條件的記錄,和內(nèi)連接效果一樣
不符合連接條件的記錄,對(duì)應(yīng)另一張表的列都是null值
right outer join 位于連接右側(cè)的表,不管是否連接到了記錄,都會(huì)出現(xiàn)在結(jié)果中
outer可以省略
連接查詢的等價(jià)寫法
- 內(nèi)連接的等價(jià)寫法 (重要)
select ... from 表1,表2 where 連接條件;
select e.empno,e.ename,e.deptno,d.deptno,d.dname from emp e, dept d where e.deptno=d.deptno;
- mysql 獨(dú)有的 (了解)
select ... from 表1 inner|left join 表2 using(deptno); // 兩張表的連接列名要相同
select e.empno,e.ename,e.deptno,d.deptno,d.dname from emp e inner join dept d using(deptno);
常用函數(shù)
select count(*) from emp; // 求整張表的行數(shù)
select max(sal) from emp; // 求整張表的工資最大值
Bit Functions 位運(yùn)算函數(shù)
Comparison operators 比較運(yùn)算符
Control flow functions 流程控制
Date and Time Functions 日期函數(shù)
year() 截取年份 month() date() date_add(日期 時(shí)間間隔); 其中時(shí)間間隔的語法:interval n 單位 select empno,ename,date_add(hiredate, interval 1 month ),hiredate from emp; 加一個(gè)月 select empno,ename,date_add(hiredate, interval 3 day ),hiredate from emp; 加3天 SELECT EXTRACT(DAY_MINUTE FROM '2009-07-02 13:02:03'); 提取日期中的從天到分鐘的部分 select now() 獲取當(dāng)前時(shí)間
Encryption Functions 加密
Information Functions
Logical operators 邏輯運(yùn)算符
Miscellaneous Functions 剩余的函數(shù)
Numeric Functions 數(shù)學(xué)函數(shù)
rand() 生成一個(gè)從[0.0 ~ 1.0) 之間的隨機(jī)小數(shù), 小于1.0 floor() 舍去小數(shù) round() 四舍五入
String Functions 字符串函數(shù)
left(字符串, n) n代表從左邊要截取的字符 lower() upper() substr(字符串,下標(biāo), 長(zhǎng)度) 下標(biāo)從1開始 求字符串長(zhǎng)度的例子:select * from hero where char_length(name)=4;
注意:length只能用來求字符串的長(zhǎng)度 ,不能用來求漢字的長(zhǎng)度.
導(dǎo)出數(shù)據(jù)
- cmd > mysqldump -uroot -p 庫名 >> 要保存的文件.sql (source的逆操作, 內(nèi)部是sql語句)
- 把表中的數(shù)據(jù)保存到文本文件中 (load data infile的逆操作)
select * from 表 into outfile '文件路徑\文件名' 看完以上關(guān)于管理Mysql數(shù)據(jù)庫的幾個(gè)常用簡(jiǎn)單命令,很多讀者朋友肯定多少有一定的了解,如需獲取更多的行業(yè)知識(shí)信息 ,可以持續(xù)關(guān)注我們的行業(yè)資訊欄目的。
網(wǎng)頁標(biāo)題:管理Mysql數(shù)據(jù)庫的幾個(gè)常用簡(jiǎn)單命令
本文網(wǎng)址:http://www.dlmjj.cn/article/ieeepd.html