數(shù)據(jù)庫
關系性數(shù)據(jù)庫
? oracle
mysql
Postgresql (加州伯克利分校的)
-
SQL server
?
非關系數(shù)據(jù)庫
?
- redis s
- memcache
mysql 的最大的特點
- ?支持插件式擴展
- 網(wǎng)易公司的inno sql
- 結構化查詢語句
1 .Mac 下安裝mysql
去官網(wǎng)下 Mysql
? ![](/Users/eternal/Desktop/屏幕快照 2017-06-07 14.40.59.png)
選擇5.6 版本
5.7 版本默認開啟
only_full_group_by 模塊,此模塊會導致 sql語句執(zhí)行嚴格的group by 的模式宴猾,下面的例子會報錯
例
select id ,name from user group by province ;//這樣的語句報錯;
解決方法是在字段前面加 any_value();
第二種方法是
用命令行登錄mySQL 執(zhí)行以下命令
select @@sql_mode;
//查到的結果
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION;
然后找到 my.conf 文件
mysqld 這行要開啟
粘貼上去
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION;
進行重啟服務;
選擇 5.6版本
![](/Users/eternal/Desktop/屏幕快照 2017-06-07 14.41.12.png)
執(zhí)行安裝
點擊運行就行
執(zhí)行更改密碼命令
mysqladmin -uroot password '新密碼'
圖形化界面去下載phpmyadmin 放到目錄下面 就可以使用
2.mysql的sql 語句
Mysql -u 用戶名 -p | 進入 | |
---|---|---|
quit | 退出 | |
\g | 寫完了執(zhí)行 | |
\h | 獲得幫助 | |
\c | 清除已輸入命令 | |
? | ||
Create database | 創(chuàng)建數(shù)據(jù)庫 | |
Use database | 使用數(shù)據(jù)庫 | |
Show databases | 查看所有的庫 | |
Drop database | 刪掉數(shù)據(jù)庫 | |
desc 表名 | 查看表結構 | |
show create table 表名 | 查看創(chuàng)建表的語句 | |
alter table user add column sex int(3) after age; | 給表增加字段 | |
alter table user drop column sex int(3) after age; | 刪除表字段 | |
alter table user change column sex int(3) after age; | ||
alter table yinhangka rename bankcard; |
-
增
insert insto 表名(username,字段2 明未,字段n)value('值1','值2','值3');
?
-
刪
delete from 表名 where 條件
delete from 表名 where id in(''); //in里面寫值
?```
-
改
update 表名 set 字段1 =值1,字段2 =值2 ,字段n值n where [限制條件]
?
-
查
select * from table; where 字段 between and 區(qū)間選擇
?
笛卡爾乘積(對兩個表進行 的操作都叫做笛卡爾成績)
(將表1 的所有字段1 對1 與表2 的所有字段都進行匹配)
select name from shop where price > 600 limit 2;
Order by 字段 asc ()
?
-
desc(能獲取最新數(shù)據(jù)的id)
Order by id asc 升序 從小到大 order by id desc,sid asc 降序從大到小 > < = != >= <= 表達式 Or and Where 字段 in值( select name price from shop where sid in (2,3); in值里面?zhèn)鬟f什么就去執(zhí)行什么
置頂?shù)膶崿F(xiàn)本質(zhì)上是 先按置頂順序排序,在按時間順序排列
update tieze set zhidng =1 where
-
Limit ();這個是分頁的重點
一頁顯示多少條數(shù)據(jù)
-
distinct
select distinct price from shopping // 去掉重復值
?
?聚合函數(shù)
在MySQL 里面基本不用 concat 與md5 函數(shù)
前面是負載均衡服務器酸纲,下面有很多服務器嘁扼,數(shù)據(jù)庫是瓶頸,每用一次函數(shù)
就會消耗很多資源
Max 最大值 select * from shop where price = min(price) Count 統(tǒng)計 Avg s平均值 Min 最小 Sum 總和 Select sum(price) from shop Concat mysql 里面的拼接字符串 MD5 mysql 里面的函數(shù) -
GROUP 分組
hvaing 與where 的區(qū)別,having 是在結果集中繼續(xù)取數(shù)據(jù)
Group by 字段 With roll up 會進行統(tǒng)計 HAVING having 是在已找到的數(shù)據(jù)里面進行操作 用法示例子 select count(shengfen) as total ,shengfen from user group by shengfen having total >3; ?
多表聯(lián)合查詢
內(nèi)連接 | Select 表字段 as 別名 from 表1,表2 where 條件 | select user.uid,user.name,shopping.name,shopping.price from user,shopping where shopping.sid= user.sid; | |
---|---|---|---|
內(nèi)連接 | Select 表.字段as 別from 表inner join 表2 on條件 | select u.uid as uid,u.name as uname ,s.name as sname,s.price as price from user as u inner join shopping as s on u.sid =s.sid; | |
左關聯(lián) | select 字段 from left join表2 on 條件 | select u.uid as uid,u.name as uname ,s.name as sname,s.price as price from user as u inner join shopping as s on u.sid =s.sid; | 以左邊為主沒匹配到的顯示為空 |
右關聯(lián) | select 字段 from left join表2 on 條件 | select * from user right join shopping on user.sid =shopping.sid; | 以右邊為主憔杨,左表沒匹配到的顯示為空 |
Union | 將結構相同的兩次查詢數(shù)據(jù)組合到一起 | 兩個語句用union 連接起來 | |
Union all | 不會干掉重復數(shù)據(jù) |
? 內(nèi)連接
select user.uid,user.name,shopping.name,shopping.price from user,shopping where shopping.sid= user.sid;
//內(nèi)連接
select u.uid as uid,u.name as uname ,s.name as sname,s.price as price from user as u inner join shopping as s on u.sid =s.sid;
`
select u.uid as uid ,u.name as uname,s.name sname,s.price from user as u, shopping as s where s.sid =u.uid;
+-----+---------+-----------+---------+
| uid | uname | sname | price |
+-----+---------+-----------+---------+
| 1 | xiaobao | iphone7 | 1222222 |
| 2 | xiaohua | 汽車 | 131313 |
| 3 | xiaobao | 化妝品 | 31313 |
| 4 | xiaohua | 汽車 | 131313 |
+-----+---------+-----------+---------+
4 rows in set (0.00 sec)
了解的命令
命令 | 作用 |
---|---|
FLush tables | 刷新表 |
Grant all on 庫.表 Grant . to'root'@ '%' identified by 'liwenkai'; | %代表用戶可以對任意電腦的ip進行登錄,我們不這樣寫是不安全的 |
Show variables | 查看用戶服務器的狀態(tài) |
這些我們都可以通過phpMyAdmin 的創(chuàng)建用戶賬戶界面來查看 | 只是了解 |
分庫分表分機器
一張表拆成不同的表
User 1 處理效率提高
user 2
User 3
User 4
? 可以分成不同的機器
? 北京放到北京服務器
? 河南放到河南服務器
-
數(shù)據(jù)庫中間件
自動化的處理數(shù)據(jù)庫相關的多服務器與單服務器的一種特殊的服務器