目錄 一、MySQL概述 二、下載安裝 三、資料庫操作 四、數據表操作 五、表內容操作 一、MySQL概述 MySQL是一個關係型資料庫管理系統,由瑞典MySQL AB 公司開發,目前屬於 Oracle 旗下產品。MySQL 是最流行的關係型資料庫管理系統之一,在 WEB 應用方面,MySQL是最好 ...
目錄
一、MySQL概述
二、下載安裝
三、資料庫操作
四、數據表操作
五、表內容操作
一、MySQL概述
MySQL是一個關係型資料庫管理系統,由瑞典MySQL AB 公司開發,目前屬於 Oracle 旗下產品。MySQL 是最流行的關係型資料庫管理系統之一,在 WEB 應用方面,MySQL是最好的 RDBMS (Relational Database Management System,關係資料庫管理系統) 應用軟體。
MySQL是一種關係資料庫管理系統,關係資料庫將數據保存在不同的表中,而不是將所有數據放在一個大倉庫內,這樣就增加了速度並提高了靈活性。
MySQL所使用的 SQL 語言是用於訪問資料庫的最常用標準化語言。MySQL 軟體採用了雙授權政策,分為社區版和商業版,由於其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,一般中小型網站的開發都選擇 MySQL 作為網站資料庫。
二、下載安裝
1. Windows
下載地址:http://dev.mysql.com/downloads/mysql/
解壓並初始化:
如果想要讓MySQL安裝在指定目錄,那麼就將解壓後的文件夾移動到指定目錄,如:C:\mysql-5.7.18-winx64
MySQL解壓後的 bin 目錄下有一大堆的可執行文件,若C:\mysql-5.7.18-winx64目錄下無data文件夾,新建一個空data文件夾後執行如下命令初始化數據:
cd c:\mysql-5.7.18-winx64\bin
mysqld --initialize-insecure
啟動服務:
# 進入可執行文件目錄
cd c:\mysql-5.7.18-winx64\bin
# 啟動MySQL服務
mysqld
啟動MySQL客戶端並連接MySQL服務:
由於初始化時使用的【mysqld --initialize-insecure】命令,其預設未給root賬戶設置密碼
# 進入可執行文件目錄
cd c:\mysql-5.7.18-winx64\bin
# 連接MySQL伺服器
mysql -u root -p
# 提示請輸入密碼,直接回車
到此為止,MySQL服務端已經安裝成功並且客戶端已經可以連接上,以後再操作MySQL時,只需要重覆上述步驟即可。但是,上述步驟中重覆的進入可執行文件目錄比較繁瑣,如想日後操作簡便,可以做如下操作。
添加環境變數,將MySQL可執行文件添加到環境變數中,如此一來,以後再啟動服務並連接時,僅需:
# 啟動MySQL服務,在終端輸入
mysqld
# 連接MySQL服務,在終端輸入:
mysql -u root -p
上一步解決了一些問題,但不夠徹底,因為在執行 mysqld 啟動MySQL伺服器時,當前終端會被hang住,那麼做一下設置即可解決此問題:
將MySQL服務製作成windows服務,
# 製作MySQL的Windows服務,在終端執行此命令:
"c:\mysql-5.7.18-winx64\bin\mysqld" --install
# 移除MySQL的Windows服務,在終端執行此命令:
"c:\mysql-5.7.18-winx64\bin\mysqld" --remove
註冊成服務之後,以後再啟動和關閉MySQL服務時,僅需執行如下命令:
# 啟動MySQL服務
net start mysql
# 關閉MySQL服務
net stop mysql
2. Linux
安裝:
yum install mysql-server
服務端啟動:
mysql.server start
客戶端連接:
連接:
mysql -h host -u user -p
常見錯誤:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2), it means that the MySQL server daemon (Unix) or service (Windows) is not running.
退出:
QUIT 或者 Control+D
三、資料庫操作
1. 顯示資料庫
show databases;
預設資料庫:
mysql - 用戶許可權相關數據
test - 用於用戶測試數據
information_schema - MySQL本身架構相關數據
2. 創建資料庫
# utf-8 create database db_name default charset utf8 collate utf8_general_ci; # gbk create database db_name default charset gbk collate gbk_chinese_ci;
3. 使用資料庫
use db_name; #顯示當前使用的資料庫中的所有表: show tables;
4. 用戶管理
創建用戶 create user '用戶名'@'IP地址' identified by '密碼'; 刪除用戶 drop user '用戶名'@'IP地址'; 修改用戶 rename user '用戶名'@'IP地址' to '新用戶名'@'IP地址';; 修改密碼 set password for '用戶名'@'IP地址' = Password('新密碼') PS:用戶許可權相關數據保存在mysql資料庫的user表中,所以也可以直接對其進行操作(不建議)
5. 授權管理
show grants for '用戶'@'IP地址' -- 查看許可權 grant 許可權 on 資料庫.表 to '用戶'@'IP地址' -- 授權 revoke 許可權 on 資料庫.表 from '用戶'@'IP地址' -- 取消許可權
許可權:
all privileges 除grant外的所有許可權 select 僅查許可權 select,insert 查和插入許可權 ... usage 無訪問許可權 alter 使用alter table alter routine 使用alter procedure和drop procedure create 使用create table create routine 使用create procedure create temporary tables 使用create temporary tables create user 使用create user、drop user、rename user和revoke all privileges create view 使用create view delete 使用delete drop 使用drop table execute 使用call和存儲過程 file 使用select into outfile 和 load data infile grant option 使用grant 和 revoke index 使用index insert 使用insert lock tables 使用lock table process 使用show full processlist select 使用select show databases 使用show databases show view 使用show view update 使用update reload 使用flush shutdown 使用mysqladmin shutdown(關閉MySQL) super 使用change master、kill、logs、purge、master和set global。還允許mysqladmin調試登陸 replication client 伺服器位置的訪問 replication slave 由複製從屬使用
資料庫:
對於目標資料庫以及內部其他: 資料庫名.* 資料庫中的所有 資料庫名.表 指定資料庫中的某張表 資料庫名.存儲過程 指定資料庫中的存儲過程 *.* 所有資料庫
用戶和IP:
用戶名@IP地址 用戶只能在該IP下才能訪問 用戶名@192.168.1.% 用戶只能在該IP段下才能訪問(通配符%表示任意) 用戶名@% 用戶可以在任意IP下訪問(預設IP地址為%)
示例:
grant all privileges on db1.tb1 TO '用戶名'@'IP' grant select on db1.* TO '用戶名'@'IP' grant select,insert on *.* TO '用戶名'@'IP' revoke select on db1.tb1 from '用戶名'@'IP' 特殊的: flush privileges,將數據讀取到記憶體中,從而立即生效。
四、數據表操作
1. 創建表
create table 表名( 列名 類型 是否可以為空, 列名 類型 是否可以為空 )engine=innodb default charset=utf8
是否可空:
null 表示空,非字元串 not null - 不可空
預設值:
預設值,創建列時可以指定預設值,當插入數據時如果未主動設置,則自動添加預設值 create table tb1( nid int not null defalut 2, num int not null )
自增:
自增,如果為某列設置自增列,插入數據時無需設置此列,預設將自增(表中只能有一個自增列) create table tb1( nid int not null auto_increment primary key, num int null ) 或
create table tb1( nid int not null auto_increment, num int null, index(nid) ) 註意:1、對於自增列,必須是索引(含主鍵)。 2、對於自增可以設置步長和起始值
show session variables like 'auto_inc%'; set session auto_increment_increment=2; set session auto_increment_offset=10; shwo global variables like 'auto_inc%'; set global auto_increment_increment=2; set global auto_increment_offset=10;
主鍵:
主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。
create table tb1( nid int not null auto_increment primary key, num int null ) 或 create table tb1( nid int not null, int not null, primary key(nid,num) )
外鍵:
外鍵,一個特殊的索引,只能是指定內容 create table color( nid int not null primary key, name char(16) not null ) create table fruit( nid int not null primary key, smt char(32) null , color_id int not null, constraint fk_cc foreign key (color_id) references color(nid) )
2. 刪除表
drop table tb_name;
3. 清空表
delete from tb_name; 只清空內容,占位還在,若再次插入,則會從清空前的位置繼續 truncate table tb_name; 完全清空
4. 修改表
添加列:alter table 表名 add 列名 類型 刪除列:alter table 表名 drop column 列名 修改列: alter table 表名 modify column 列名 類型; -- 類型 alter table 表名 change 原列名 新列名 類型; -- 列名,類型 添加主鍵: alter table 表名 add primary key(列名); 刪除主鍵: alter table 表名 drop primary key; alter table 表名 modify 列名 int, drop primary key; 添加外鍵:alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵欄位) references 主表(主鍵欄位); 刪除外鍵:alter table 表名 drop foreign key 外鍵名稱 修改預設值:alter table testalter_tbl alter i set default 1000; 刪除預設值:alter table testalter_tbl alter i drop default;
5. 基本數據類型
MySQL的數據類型大致分為:數值、時間和字元串
數值:
bit[(M)] 二進位位(101001),m表示二進位位的長度(1-64),預設m=1 tinyint[(m)] [unsigned] [zerofill] 小整數,數據類型用於保存一些範圍的整數數值範圍: 有符號: -128 ~ 127. 無符號: 0 ~ 255 特別的: MySQL中無布爾值,使用tinyint(1)構造。 int[(m)][unsigned][zerofill] 整數,數據類型用於保存一些範圍的整數數值範圍: 有符號: -2147483648 ~ 2147483647 無符號: 0 ~ 4294967295 特別的:整數類型中的m僅用於顯示,對存儲範圍無限制。例如: int(5),當插入數據2時,select 時數據顯示為: 00002 bigint[(m)][unsigned][zerofill] 大整數,數據類型用於保存一些範圍的整數數值範圍: 有符號: -9223372036854775808 ~ 9223372036854775807 無符號: 0 ~ 18446744073709551615 decimal[(m[,d])] [unsigned] [zerofill] 準確的小數值,m是數字總個數(負號不算),d是小數點後個數。 m最大值為65,d最大值為30。 特別的:對於精確數值計算時需要用此類型 decaimal能夠存儲精確值的原因在於其內部按照字元串存儲。 FLOAT[(M,D)] [UNSIGNED] [ZEROFILL] 單精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。 無符號: -3.402823466E+38 to -1.175494351E-38, 0 1.175494351E-38 to 3.402823466E+38 有符號: 0 1.175494351E-38 to 3.402823466E+38 **** 數值越大,越不准確 **** DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] 雙精度浮點數(非準確小數值),m是數字總個數,d是小數點後個數。 無符號: -1.7976931348623157E+308 to -2.2250738585072014E-308 0 2.2250738585072014E-308 to 1.7976931348623157E+308 有符號: 0 2.2250738585072014E-308 to 1.7976931348623157E+308 **** 數值越大,越不准確 ****
時間:
DATE YYYY-MM-DD(1000-01-01/9999-12-31) TIME HH:MM:SS('-838:59:59'/'838:59:59') YEAR YYYY(1901/2155) DATETIME YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 Y) TIMESTAMP YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)
字元串:
char (m) char數據類型用於表示固定長度的字元串,可以包含最多達255個字元。其中m代表字元串的長度。 PS: 即使數據小於m長度,也會占用m長度 varchar(m) varchars數據類型用於變長的字元串,可以包含最多達255個字元。其中m代表該數據類型所允許保存的字元串的最大長度,只要長度小於該最大值的字元串都可以被保存在該數據類型中。 註:雖然varchar使用起來較為靈活,但是從整個系統的性能角度來說,char數據類型的處理速度更快,有時甚至可以超出varchar處理速度的50%。因此,用戶在設計資料庫時應當綜合考慮各方面的因素,以求達到最佳的平衡 text text數據類型用於保存變長的大字元串,可以組多到65535 (2**16 − 1)個字元。 mediumtext A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters. longtext A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1) characters.
其它:
enum 枚舉類型, An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.) 示例: CREATE TABLE shirts ( name VARCHAR(40), size ENUM('x-small', 'small', 'medium', 'large', 'x-large') ); INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small'); set 集合類型 A SET column can have a maximum of 64 distinct members. 示例: CREATE TABLE myset (col SET('a', 'b', 'c', 'd')); INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
更多參考:
- http://www.runoob.com/mysql/mysql-data-types.html
- http://dev.mysql.com/doc/refman/5.7/en/data-type-overview.html
五、表內容操作
1. 增
insert into 表 (列名,列名...) values (值,值,值...) insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...) insert into 表 (列名,列名...) select (列名,列名...) from 表
2. 刪
delete from 表 delete from 表 where id=1 and name='alex'
3. 改
update 表 set name = 'alex' where id>1
4. 查
select * from 表 select * from 表 where id > 1 select nid,name,gender as gg from 表 where id > 1
5. 其它
1 a、條件 2 select * from 表 where id > 1 and name != 'alex' and num = 12; 3 4 select * from 表 where id between 5 and 16; 5 6 select * from 表 where id in (11,22,33) 7 select * from 表 where id not in (11,22,33) 8 select * from 表 where id in (select nid from 表) 9 10 b、通配符 11 select * from 表 where name like 'ale%' - ale開頭的所有(多個字元串) 12 select * from 表 where name like 'ale_' - ale開頭的所有(一個字元) 13 14 c、限制 15 select * from 表 limit 5; - 前5行 16 select * from 表 limit 4,5; - 從第4行開始的5行 17 select * from 表 limit 5 offset 4 - 從第4行開始的5行 18 19 d、排序 20 select * from 表 order by 列 asc - 根據 “列” 從小到大排列 21 select * from 表 order by 列 desc - 根據 “列” 從大到小排列 22 select * from 表 order by 列1 desc,列2 asc - 根據 “列1” 從大到小排列,如果相同則按列2從小到大排序 23 24 e、分組 25 select num from 表 group by num 26 select num,nid from 表 group by num,nid 27 select num,nid from 表 where nid > 10 group by num,nid order nid desc 28 select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid 29 30 select num from 表 group by num having max(id) > 10 31 32 特別的:group by 必須在where之後,order by之前 33 34 f、連表 35 無對應關係則不顯示 36 select A.num, A.name, B.name 37 from A,B 38 Where A.nid = B.nid 39 40 無對應關係則不顯示 41 select A.num, A.name, B.name 42 from A inner join B 43 on A.nid = B.nid 44 45 A表所有顯示,如果B中無對應關係,則值為null 46 select A.num, A.name, B.name 47 from A left join B 48 on A.nid = B.nid 49 50 B表所有顯示,如果B中無對應關係,則值為null 51 select A.num, A.name, B.name 52 from A right join B 53 on A.nid = B.nid 54 55 g、組合 56 組合,自動處理重合 57 select nickname 58 from A 59 union 60 select name 61 from B 62 63 組合,不處理重合 64 select nickname 65 from A