MySQL[一]
一丶概述
1·什么是MySQL丶Oracle丶SQLite丶Access丶MS SQL Server等?
答:放數(shù)據(jù)的倉庫,如:在ATM的實列中我們創(chuàng)建了一個db目錄,稱其為數(shù)據(jù)庫·
2·什么事MySQL,Oracle,SQLite,Access,MS SQL Server等?
答:他們均是一個軟件,都有兩個主要的功能,
·1.強數(shù)據(jù)保存到文件或內(nèi)存
·2.接收特定的命令,然后對文件進行相對應(yīng)的操作·
3丶什么是SQL?
答:上述提到MySQL等軟件可以接收命令,并做出相對應(yīng)的操作 ,由于命令中可以包含刪除文件,獲取文件內(nèi)容等眾多操作,對于編寫的命令就是SQL語句·
二丶下載安裝
MySQL是一個關(guān)系型數(shù)據(jù)庫管理系統(tǒng),由瑞典MySQL AB公司開發(fā),目前屬于Oracle旗下公司.MySQL最流行的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),在WEB應(yīng)用方面MySQL是最好的RDBMS(Relational Database Manaqement System,關(guān)系數(shù)據(jù)庫管理系統(tǒng))應(yīng)用軟件之一·
想要MySQL來儲存并操作數(shù)據(jù),則需要做幾件事情:
1·安裝MySQL服務(wù)端
2·安裝MySQL客戶端
3·[客戶端]連接[服務(wù)端]
4·[客戶端]發(fā)送命令給[服務(wù)端MySQL]服務(wù)的接收命令并執(zhí)行相應(yīng)操作(增刪改查等)
下載:
http://dev.mysql.com/downloads/mysql/
服務(wù)端啟動:
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 或者 Conntrol+D三丶數(shù)據(jù)庫操作
1·顯示數(shù)據(jù)庫
SHOW DATABASES; #注意一定要加分號
默認數(shù)據(jù)庫
mysql - 用戶權(quán)限相關(guān)數(shù)據(jù)test - 用于用戶測試數(shù)據(jù)information_schema - MySQL本身架構(gòu)相關(guān)數(shù)據(jù)2·創(chuàng)建數(shù)據(jù)庫
#utf-8 CREATE DATABASE 數(shù)據(jù)庫名稱 DEFAULT CHARSET utf-8 COLLATE utf8_general_ci;#gbk CREATE DATABASE 數(shù)據(jù)庫名稱 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;3·使用數(shù)據(jù)庫
USE db_name;顯示當前使用的數(shù)據(jù)庫中所有表:SHOW TABLES;
?4·用戶管理
創(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表中,所以也可以直接對其進行操作(不建議)5·授權(quán)管理
show grants for '用戶名'@'IP地址' --查看權(quán)限grant 權(quán)限 on數(shù)據(jù)庫.表 to '用戶名'@'IP地址' -- 權(quán)限r(nóng)evoke 權(quán)限 on數(shù)據(jù)庫.表 from '用戶名'@'IP地址' --取消權(quán)限all.privileges 除grant外的所有權(quán)限select 僅查權(quán)限select,insert 查和插入權(quán)限 .....usage 無訪問權(quán)限alter 使用alter tablealter routine 使用alter procedure 和 drop procedurecreate 使用create tablecreate routine 使用create procedurecreate temporary tables 使用create temporary tablescreate user 使用create user,drop user,reanme user和revoke all privilegescreate view 使用create viewdelete 使用deletedrop 使用drop tableexecute 使用call和存儲過程file 使用select info onto outfile 和 load data infilegrant option 使用grant 和 revokeindex 使用indexinsert 使用insertlock tables 使用lock tableprocess 使用show full processlistselect 使用selectshow databases 使用show databasesshow vies 使用show viewupdate 使用updatereload 使用flushshutdown 使用mysqladmin shutdown(關(guān)閉MySQL)replication 服務(wù)器位置的訪問replication slave 由復(fù)制從屬使用
#對于數(shù)據(jù)庫對于目標數(shù)據(jù)庫以及內(nèi)部其他: 數(shù)據(jù)庫名.* 數(shù)據(jù)庫中的所有數(shù)據(jù)庫名.表 指定數(shù)據(jù)庫中的某張表數(shù)據(jù)庫名.存儲過程 指定數(shù)據(jù)庫中的存儲過程*.* 所有數(shù)據(jù)庫
#對于用戶和IP用戶名@IP地址 用戶只能在改IP下才能訪問用戶名@192.168.1.% 用戶名只能在改IP段下才能訪問(統(tǒng)配符%表示任意)用戶名@% 用戶可以再任意IP下訪問(默認IP地址為%)
#示列g(shù)rant 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'
四丶數(shù)據(jù)表基本
1丶創(chuàng)建表
create table 表名(列名 類型 是否可以為空列名 類型 是否可以為空 )ENGINE=InnoDB DEFAULT CHARSET=utf8
是否可以為空,null表示空,非字符串not null - 不可空null - 可空
默認值,創(chuàng)建列表示可以指定默認值,當插入數(shù)據(jù)時如果未主動設(shè)置,則自動添加默認值 create table tbl(nid int not null defalut 2,num int not null)
自增,如果為某列設(shè)置自增列,插入數(shù)據(jù)時無需設(shè)置此列,默認將自增(表中只能有一個自增列)create table tbl(nid int not null auto_increment primary key, num int null)或create table tbl(nid int not null auto_increment,num int null,index(nid))注意:1,對于自增列,必須是索引(含主鍵)2,對于自增可以設(shè)置步長和起始值show session variables like 'auto_inc%';set session auto_increment_increment=2;set session auto_increment_offset=10;show global variables like 'quto_inc%';set global auto_increment_increment=2;set global auto_increment_offset=10;
主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一·create table tbl(nid int not null auto_increment primary key,num int null )或 create table tbl(nid int not null,num int not null,primary key(nid,num)) 外鍵,一個特殊的索引,只能是指定內(nèi)容creat 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) refences color(nid))
2丶刪除表
drop ?table ? 表名
3丶清空表
? ? ? ? ? ?delete from 表名
? ? ? ? ? ?truncate table ?表名
4丶修改表
添加列:alter table 表名 modify column 列名 類型;
? ? ?刪除列: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 constaint 外鍵名稱(形如:FK_從表_主表) foreign key (外鍵字段) references 主表(主鍵字段);
刪除外鍵:alter table 表名 drop foreign key 外鍵名稱
修改默認值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 10000;
刪除默認值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
5丶基本數(shù)據(jù)類型
MySQL的數(shù)據(jù)類型大致分為:數(shù)值,時間和字符串
bit[(M)]二進制位(101001),m表示二進制位的長度(1-64),默認m=1tinyint[(m)] [unsigned] [zerofill]小整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:有符號:-128 ~ 127.無符號:0 ~ 255特別的: MySQL中無布爾值,使用tinyint(1)構(gòu)造。int[(m)][unsigned][zerofill]整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:有符號:-2147483648 ~ 2147483647無符號:0 ~ 4294967295特別的:整數(shù)類型中的m僅用于顯示,對存儲范圍無限制。例如: int(5),當插入數(shù)據(jù)2時,select 時數(shù)據(jù)顯示為: 00002bigint[(m)][unsigned][zerofill]大整數(shù),數(shù)據(jù)類型用于保存一些范圍的整數(shù)數(shù)值范圍:有符號:-9223372036854775808 ~ 9223372036854775807無符號:0 ~ 18446744073709551615decimal[(m[,d])] [unsigned] [zerofill]準確的小數(shù)值,m是數(shù)字總個數(shù)(負號不算),d是小數(shù)點后個數(shù)。 m最大值為65,d最大值為30。特別的:對于精確數(shù)值計算時需要用此類型decaimal能夠存儲精確值的原因在于其內(nèi)部按照字符串存儲。FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]單精度浮點數(shù)(非準確小數(shù)值),m是數(shù)字總個數(shù),d是小數(shù)點后個數(shù)。無符號:-3.402823466E+38 to -1.175494351E-38,01.175494351E-38 to 3.402823466E+38有符號:01.175494351E-38 to 3.402823466E+38**** 數(shù)值越大,越不準確 ****DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]雙精度浮點數(shù)(非準確小數(shù)值),m是數(shù)字總個數(shù),d是小數(shù)點后個數(shù)。無符號:-1.7976931348623157E+308 to -2.2250738585072014E-30802.2250738585072014E-308 to 1.7976931348623157E+308有符號:02.2250738585072014E-308 to 1.7976931348623157E+308**** 數(shù)值越大,越不準確 ****char (m)char數(shù)據(jù)類型用于表示固定長度的字符串,可以包含最多達255個字符。其中m代表字符串的長度。PS: 即使數(shù)據(jù)小于m長度,也會占用m長度varchar(m)varchars數(shù)據(jù)類型用于變長的字符串,可以包含最多達255個字符。其中m代表該數(shù)據(jù)類型所允許保存的字符串的最大長度,只要長度小于該最大值的字符串都可以被保存在該數(shù)據(jù)類型中。注:雖然varchar使用起來較為靈活,但是從整個系統(tǒng)的性能角度來說,char數(shù)據(jù)類型的處理速度更快,有時甚至可以超出varchar處理速度的50%。因此,用戶在設(shè)計數(shù)據(jù)庫時應(yīng)當綜合考慮各方面的因素,以求達到最佳的平衡texttext數(shù)據(jù)類型用于保存變長的大字符串,可以組多到65535 (2**16 ? 1)個字符。mediumtextA TEXT column with a maximum length of 16,777,215 (2**24 ? 1) characters.longtextA 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');DATEYYYY-MM-DD(1000-01-01/9999-12-31)TIMEHH:MM:SS('-838:59:59'/'838:59:59')YEARYYYY(1901/2155)DATETIMEYYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 Y)TIMESTAMPYYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某時)·更多參考
? ? http://dev.mysql.com/doc/refman/5.7/en/data-type-overview.html
http://www.runoob.com/mysql/mysql-data-types.html
五丶表內(nèi)容操作
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>14丶查
select * from 表 select * from 表 where id > 1 select nid,name,gender as gg from 表 where id > 15丶其他
a、條件select * from 表 where id > 1 and name != 'alex' 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、通配符select * from 表 where name like 'ale%' - ale開頭的所有(多個字符串)select * from 表 where name like 'ale_' - ale開頭的所有(一個字符)c、限制select * from 表 limit 5; - 前5行select * from 表 limit 4,5; - 從第4行開始的5行select * from 表 limit 5 offset 4 - 從第4行開始的5行d、排序select * from 表 order by 列 asc - 根據(jù) “列” 從小到大排列select * from 表 order by 列 desc - 根據(jù) “列” 從大到小排列select * from 表 order by 列1 desc,列2 asc - 根據(jù) “列1” 從大到小排列,如果相同則按列2從小到大排序e、分組select num from 表 group by numselect num,nid from 表 group by num,nidselect num,nid from 表 where nid > 10 group by num,nid order nid descselect num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nidselect num from 表 group by num having max(id) > 10特別的:group by 必須在where之后,order by之前f、連表無對應(yīng)關(guān)系則不顯示select A.num, A.name, B.namefrom A,BWhere A.nid = B.nid無對應(yīng)關(guān)系則不顯示select A.num, A.name, B.namefrom A inner join Bon A.nid = B.nidA表所有顯示,如果B中無對應(yīng)關(guān)系,則值為nullselect A.num, A.name, B.namefrom A left join Bon A.nid = B.nidB表所有顯示,如果B中無對應(yīng)關(guān)系,則值為nullselect A.num, A.name, B.namefrom A right join Bon A.nid = B.nidg、組合組合,自動處理重合select nicknamefrom Aunionselect namefrom B組合,不處理重合select nicknamefrom Aunion allselect namefrom B View Code?
?
總結(jié)
- 上一篇: APACHE如何里一个站点绑定多个域名?
- 下一篇: C++中对Mysql的操作函数可以参考以