MYSQL 入門全套


點(diǎn)擊「閱讀原文」查看良許原創(chuàng)精品視頻。
點(diǎn)擊「閱讀原文」查看良許原創(chuàng)精品視頻。
MySQL簡介
1、什么是數(shù)據(jù)庫 ?
數(shù)據(jù)庫(Database)是按照數(shù)據(jù)結(jié)構(gòu)來組織、存儲(chǔ)和管理數(shù)據(jù)的倉庫,它產(chǎn)生于距今六十多年前,隨著信息技術(shù)和市場的發(fā)展,特別是二十世紀(jì)九十年代以后,數(shù)據(jù)管理不再僅僅是存儲(chǔ)和管理數(shù)據(jù),而轉(zhuǎn)變成用戶所需要的各種數(shù)據(jù)管理的方式。數(shù)據(jù)庫有很多種類型,從最簡單的存儲(chǔ)有各種數(shù)據(jù)的表格到能夠進(jìn)行海量數(shù)據(jù)存儲(chǔ)的大型數(shù)據(jù)庫系統(tǒng)都在各個(gè)方面得到了廣泛的應(yīng)用。
主流的數(shù)據(jù)庫有:sqlserver,mysql,Oracle、SQLite、Access、MS SQL Server等,本文主要講述的是mysql
2、數(shù)據(jù)庫管理是干什么用的?
a. 將數(shù)據(jù)保存到文件或內(nèi)存
b. 接收特定的命令,然后對(duì)文件進(jìn)行相應(yīng)的操作
PS:如果有了以上管理系統(tǒng),無須自己再去創(chuàng)建文件和文件夾,而是直接傳遞 命令 給上述軟件,讓其來進(jìn)行文件操作,他們統(tǒng)稱為數(shù)據(jù)庫管理系統(tǒng)(DBMS,Database Management System)
MySQL安裝
MySQL是一種開放源代碼的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)(RDBMS),MySQL數(shù)據(jù)庫系統(tǒng)使用最常用的數(shù)據(jù)庫管理語言–結(jié)構(gòu)化查詢語言(SQL)進(jìn)行數(shù)據(jù)庫管理。在 WEB 應(yīng)用方面MySQL是最好的 RDBMS (Relational Database Management System,關(guān)系數(shù)據(jù)庫管理系統(tǒng)) 應(yīng)用軟件之一。
使用mysql必須具備一下條件
a. 安裝MySQL服務(wù)端
b. 安裝MySQL客戶端
c. 【客戶端】連接【服務(wù)端】
d. 【客戶端】發(fā)送命令給【服務(wù)端MySQL】服務(wù)的接受命令并執(zhí)行相應(yīng)操作(增刪改查等)
1、下載地址:http://dev.mysql.com/downloads/mysql/
2、安裝
windows安裝請(qǐng)參考:
http://www.cnblogs.com/lonelywolfmoutain/p/4547115.html
linux下安裝:http://www.cnblogs.com/chenjunbiao/archive/2011/01/24/1940256.html
注:以上兩個(gè)鏈接有完整的安裝方式,擼主也是參考他的安裝的,安裝完以后mysql.server start啟動(dòng)mysql服務(wù)
MySQL操作
一、連接數(shù)據(jù)庫
mysql ?-u user -p ? ? ? ? ? ? ? ? ?
例:mysql -u root -p常見錯(cuò)誤如下:
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 或者 Ctrl+D二、查看數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫,使用數(shù)據(jù)庫查看數(shù)據(jù)庫:?
show databases;默認(rèn)數(shù)據(jù)庫:
mysql - 用戶權(quán)限相關(guān)數(shù)據(jù)
test - 用于用戶測試數(shù)據(jù)
information_schema - MySQL本身架構(gòu)相關(guān)數(shù)據(jù)創(chuàng)建數(shù)據(jù)庫: ? ?
create database db1 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; # utf8編碼
create database db1 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; # gbk編碼使用數(shù)據(jù)庫:
use db1;顯示當(dāng)前使用的數(shù)據(jù)庫中所有表:
SHOW TABLES;三、用戶管理
創(chuàng)建用戶
create user '用戶名'@'IP地址' identified by '密碼';刪除用戶
drop user '用戶名'@'IP地址';修改用戶
rename user '用戶名'@'IP地址'; to '新用戶名'@'IP地址';修改密碼
set password for '用戶名'@'IP地址' = Password('新密碼');注:用戶權(quán)限相關(guān)數(shù)據(jù)保存在mysql數(shù)據(jù)庫的user表中,所以也可以直接對(duì)其進(jìn)行操作(不建議)
四、權(quán)限管理
mysql對(duì)于權(quán)限這塊有以下限制:
all privileges:除grant外的所有權(quán)限
select:僅查權(quán)限
select,insert:查和插入權(quán)限
...
usage:無訪問權(quán)限
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和存儲(chǔ)過程
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(關(guān)閉MySQL)
super:使用change master、kill、logs、purge、master和set global。還允許mysqladmin調(diào)試登陸
replication client:服務(wù)器位置的訪問
replication slave:由復(fù)制從屬使用對(duì)于數(shù)據(jù)庫及內(nèi)部其他權(quán)限如下:
數(shù)據(jù)庫名.* ? ? ? ? ? ?數(shù)據(jù)庫中的所有
數(shù)據(jù)庫名.表 ? ? ? ? ? 指定數(shù)據(jù)庫中的某張表
數(shù)據(jù)庫名.存儲(chǔ)過程 ? ? ?指定數(shù)據(jù)庫中的存儲(chǔ)過程
*.* ? ? ? ? ? ? ? ? ? 所有數(shù)據(jù)庫對(duì)于用戶和IP的權(quán)限如下:
用戶名@IP地址 ? ? ? ?用戶只能在改IP下才能訪問
用戶名@192.168.1.% ? 用戶只能在改IP段下才能訪問(通配符%表示任意)
用戶名@% ? ? ? ? ? ? 用戶可以再任意IP下訪問(默認(rèn)IP地址為%)1、查看權(quán)限:
show grants for '用戶'@'IP地址'2、授權(quán)
grant ?權(quán)限 on 數(shù)據(jù)庫.表 to ? '用戶'@'IP地址'3、取消授權(quán)
revoke 權(quán)限 on 數(shù)據(jù)庫.表 from '用戶名'@'IP地址'授權(quán)實(shí)例如下:
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'MySQL表操作
1、查看表
show tables; # 查看數(shù)據(jù)庫全部表
select * from 表名; # 查看表所有內(nèi)容2、創(chuàng)建表
create table 表名(
? ?列名 ?類型 ?是否可以為空,
? ?列名 ?類型 ?是否可以為空
)ENGINE=InnoDB DEFAULT CHARSET=utf8來一個(gè)實(shí)例好詳解
CREATE TABLE `tab1` (
?`nid` int(11) NOT NULL auto_increment,
?`name` varchar(255) DEFAULT zhangyanlin,
?`email` varchar(255),
?PRIMARY KEY (`nid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;注:
默認(rèn)值,創(chuàng)建列時(shí)可以指定默認(rèn)值,當(dāng)插入數(shù)據(jù)時(shí)如果未主動(dòng)設(shè)置,則自動(dòng)添加默認(rèn)值
自增,如果為某列設(shè)置自增列,插入數(shù)據(jù)時(shí)無需設(shè)置此列,默認(rèn)將自增(表中只能有一個(gè)自增列)注意:1、對(duì)于自增列,必須是索引(含主鍵)2、對(duì)于自增可以設(shè)置步長和起始值
主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個(gè)列,則它的值必須唯一,如果是多列,則其組合必須唯一。
3、刪除表
drop table 表名4、清空表內(nèi)容
delete from 表名
truncate table 表名5、修改表
添加列: ?
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 外鍵名稱
修改默認(rèn)值:
ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
刪除默認(rèn)值:
ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;對(duì)于上述這些操作是不是看起來很麻煩,很浪費(fèi)時(shí)間,別慌!有專門的軟件能提供這些功能,操作起來非常簡單,這個(gè)軟件名字叫Navicat Premium ,大家自行在網(wǎng)上下載,練練手,但是下面的即將講到表內(nèi)容操作還是建議自己寫命令來進(jìn)行
6、基本數(shù)據(jù)類型
MySQL的數(shù)據(jù)類型大致分為:數(shù)值、時(shí)間和字符串
bit[(M)]
? ? ? ? ? ?二進(jìn)制位(101001),m表示二進(jìn)制位的長度(1-64),默認(rèn)m=1
tinyint[(m)] [unsigned] [zerofill]
? ? ? ? ? ?小整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:
? ? ? ? ? ?有符號(hào):
? ? ? ? ? ? ? ?-128 ~ 127.
? ? ? ? ? ?無符號(hào):
? ? ? ? ? ? ? ?0 ~ 255
? ? ? ? ? ?特別的: MySQL中無布爾值,使用tinyint(1)構(gòu)造。
int[(m)][unsigned][zerofill]
? ? ? ? ? ?整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:
? ? ? ? ? ? ? ?有符號(hào):
? ? ? ? ? ? ? ? ? ?-2147483648 ~ 2147483647
? ? ? ? ? ? ? ?無符號(hào):
? ? ? ? ? ? ? ? ? ?0 ~ 4294967295
? ? ? ? ? ?特別的:整數(shù)類型中的m僅用于顯示,對(duì)存儲(chǔ)范圍無限制。例如: int(5),當(dāng)插入數(shù)據(jù)2時(shí),select 時(shí)數(shù)據(jù)顯示為:00002
bigint[(m)][unsigned][zerofill]
? ? ? ? ? ?大整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:
? ? ? ? ? ? ? ?有符號(hào):
? ? ? ? ? ? ? ? ? ?-9223372036854775808 ~ 9223372036854775807
? ? ? ? ? ? ? ?無符號(hào):
? ? ? ? ? ? ? ? ? ?0 ?~ ?18446744073709551615
decimal[(m[,d])] [unsigned] [zerofill]
? ? ? ? ? ?準(zhǔn)確的小數(shù)值,m是數(shù)字總個(gè)數(shù)(負(fù)號(hào)不算),d是小數(shù)點(diǎn)后個(gè)數(shù)。 m最大值為65,d最大值為30。
? ? ? ? ? ?特別的:對(duì)于精確數(shù)值計(jì)算時(shí)需要用此類型
? ? ? ? ? ? ? ? ? decaimal能夠存儲(chǔ)精確值的原因在于其內(nèi)部按照字符串存儲(chǔ)。
FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
? ? ? ? ? ?
? ? ? ? ? ?單精度浮點(diǎn)數(shù)(非準(zhǔn)確小數(shù)值),m是數(shù)字總個(gè)數(shù),d是小數(shù)點(diǎn)后個(gè)數(shù)。
? ? ? ? ? ? ? ?無符號(hào):
? ? ? ? ? ? ? ? ? ?-3.402823466E+38 to -1.175494351E-38,
? ? ? ? ? ? ? ? ? ?0
? ? ? ? ? ? ? ? ? ?1.175494351E-38 to 3.402823466E+38
? ? ? ? ? ? ? ?有符號(hào):
? ? ? ? ? ? ? ? ? ?0
? ? ? ? ? ? ? ? ? ?1.175494351E-38 to 3.402823466E+38
? ? ? ? ? ?**** 數(shù)值越大,越不準(zhǔn)確 ****
DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]
? ? ? ? ? ?雙精度浮點(diǎn)數(shù)(非準(zhǔn)確小數(shù)值),m是數(shù)字總個(gè)數(shù),d是小數(shù)點(diǎn)后個(gè)數(shù)。
? ? ? ? ? ? ? ?無符號(hào):
? ? ? ? ? ? ? ? ? ?-1.7976931348623157E+308 to -2.2250738585072014E-308
? ? ? ? ? ? ? ? ? ?0
? ? ? ? ? ? ? ? ? ?2.2250738585072014E-308 to 1.7976931348623157E+308
? ? ? ? ? ? ? ?有符號(hào):
? ? ? ? ? ? ? ? ? ?0
? ? ? ? ? ? ? ? ? ?2.2250738585072014E-308 to 1.7976931348623157E+308
? ? ? ? ? ?**** 數(shù)值越大,越不準(zhǔn)確 ****
char (m)
? ? ? ? ? ?char數(shù)據(jù)類型用于表示固定長度的字符串,可以包含最多達(dá)255個(gè)字符。其中m代表字符串的長度。
? ? ? ? ? ?PS: 即使數(shù)據(jù)小于m長度,也會(huì)占用m長度
? ? ? ? ? ?
varchar(m)
? ? ? ? ? ?varchars數(shù)據(jù)類型用于變長的字符串,可以包含最多達(dá)255個(gè)字符。其中m代表該數(shù)據(jù)類型所允許保存的字符串的最大長度,只要長度小于該最大值的字符串都可以被保存在該數(shù)據(jù)類型中。
? ? ? ? ? ?注:雖然varchar使用起來較為靈活,但是從整個(gè)系統(tǒng)的性能角度來說,char數(shù)據(jù)類型的處理速度更快,有時(shí)甚至可以超出varchar處理速度的50%。因此,用戶在設(shè)計(jì)數(shù)據(jù)庫時(shí)應(yīng)當(dāng)綜合考慮各方面的因素,以求達(dá)到最佳的平衡
text
? ? ? ? ? ?text數(shù)據(jù)類型用于保存變長的大字符串,可以組多到65535 (2**16 ? 1)個(gè)字符。
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');
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 年某時(shí))MySQL表內(nèi)容操作
表內(nèi)容操作無非就是增刪改查,當(dāng)然用的最多的還是查,而且查這一塊東西最多,用起來最難,當(dāng)然對(duì)于大神來說那就是so easy了,對(duì)于我這種小白還是非常難以靈活運(yùn)用的,下面咱來一一操作一下
1、增
insert into 表 (列名,列名...) values (值,值,...)
insert into 表 (列名,列名...) values (值,值,...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from 表
例:
? ?insert into tab1(name,email) values('zhangyanlin','[email protected]')2、刪
delete from 表 ? # 刪除表里全部數(shù)據(jù)
delete from 表 where id=1 and name='zhangyanlin' # 刪除ID =1 和name='zhangyanlin' 那一行數(shù)據(jù)3、改
update 表 set name = 'zhangyanlin' where id>14、查
select * from 表
select * from 表 where id > 1
select nid,name,gender as gg from 表 where id > 1
查這塊的條件太多太多我給列舉出來至于組合還得看大家的理解程度哈
a、條件判斷where
select * from 表 where id > 1 and name != 'aylin' and num = 12;
select * from 表 where id between 5 and 16;
select * from 表 where id in (11,22,33)
select * from 表 where id not in (11,22,33)
select * from 表 where id in (select nid from 表)b、通配符like
select * from 表 where name like 'zhang%' ?# zhang開頭的所有(多個(gè)字符串)
select * from 表 where name like 'zhang_' ?# zhang開頭的所有(一個(gè)字符)c、限制limit
select * from 表 limit 5; ? ? ? ? ? ?- 前5行
select * from 表 limit 4,5; ? ? ? ? ?- 從第4行開始的5行
select * from 表 limit 5 offset 4 ? ?- 從第4行開始的5行d、排序asc,desc
select * from 表 order by 列 asc ? ? ? ? ? ? ?- 根據(jù) “列” 從小到大排列
select * from 表 order by 列 desc ? ? ? ? ? ? - 根據(jù) “列” 從大到小排列
select * from 表 order by 列1 desc,列2 asc ? ?- 根據(jù) “列1” 從大到小排列,如果相同則按列2從小到大排序e、分組group by
select num from 表 group by num
select num,nid from 表 group by num,nid
select num,nid from 表 ?where nid > 10 group by num,nid order nid desc
select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
select num from 表 group by num having max(id) > 10
特別的:group by 必須在where之后,order by之前
本公眾號(hào)全部博文已整理成一個(gè)目錄,請(qǐng)?jiān)诠娞?hào)里回復(fù)「m」獲??! 推薦閱讀:
5T技術(shù)資源大放送!包括但不限于:C/C++,Linux,Python,Java,PHP,人工智能,單片機(jī),樹莓派,等等。在公眾號(hào)內(nèi)回復(fù)「1024」,即可免費(fèi)獲取??!
