1拴事、多表聯(lián)合查詢
查詢沒有被購買過的商品
select goods.name from goods left join user on goods.gid=user.gid where user.id is NULL;
查詢哪類商品是銷量冠軍
select category, count(*) as c from user join goods on user.gid=goods.gid group by category order by c desc limit 1;
查詢哪個(gè)商品是銷量冠軍
select goods.name, goods.price, count(*) as c from user join goods on user.gid=goods.gid group by goods.name order by c desc limit 1;
分組只能寫分組字段和統(tǒng)計(jì)字段,寫其它字段報(bào)錯(cuò)
進(jìn)入mysql執(zhí)行如下指令
SET @@GLOBAL.sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";
清空表
truncate table 表名 清空表圣蝎,id會(huì)從1開始
delete from table 清空表挤聘,id從上次記錄的值開始
創(chuàng)建用戶
mysql -h ip地址 -u用戶名 -p
create user 'test'@'ip' identified by '密碼'
2、索引
索引是什么捅彻?索引就是個(gè)東西组去,類似是目錄的東西
數(shù)據(jù)庫在查詢的時(shí)候,是一條一條挨著查呢步淹。查詢效率太低了从隆。
如何提高查詢效率?索引
如果在查詢的時(shí)候缭裆,經(jīng)常通過某個(gè)字段查詢键闺,那就要考慮給這個(gè)字段添加索引。
索引的缺點(diǎn):在插入的時(shí)候效率就比較低
索引有
普通索引
唯一索引 要確保唯一性
主鍵索引 是特殊的唯一索引澈驼,但是不能為空
全文索引
3辛燥、數(shù)據(jù)庫導(dǎo)入導(dǎo)出
數(shù)據(jù)庫里面的表可以導(dǎo)出來,導(dǎo)出來一般都是sql腳本的東西缝其, data.sql
【注】版本挎塌,通過指令導(dǎo)出和通過不同的可視化工具導(dǎo)出,格式也不一樣内边。
mysql自帶的指令:
正常終端模式下
導(dǎo)出:mysqldump -uroot -p 數(shù)據(jù)庫名>c:\data.sql
導(dǎo)入:mysql -uroot -p 數(shù)據(jù)庫名<c:\data.sql
注意榴都,導(dǎo)入之前首先創(chuàng)建一個(gè)新的數(shù)據(jù)庫
Navicat導(dǎo)出和導(dǎo)入:
右鍵==》轉(zhuǎn)儲(chǔ)sql文件
右鍵==》運(yùn)行sql文件
4、python操作數(shù)據(jù)庫
需要使用到第三方庫:pymysql漠其,在python交互界面輸入 import pymysql 看有沒有報(bào)錯(cuò)嘴高,報(bào)錯(cuò)了就是沒有它,沒報(bào)錯(cuò)就是有它
安裝之:pip install pymysql
代碼操作之
port只能是整型
用pymysql獲取MySQL中的信息
查
import pymysql
# 第一步和屎,連接數(shù)據(jù)庫介汹,類似于指令里面的 mysql -uroot -p
# mysql -h地址 -uroot -p
'''
連接數(shù)據(jù)庫需要用到的參數(shù)
主機(jī):host
端口:port
用戶名:user
密碼:password
指定數(shù)據(jù)庫:db
指定字符集:charset
'''
db = pymysql.connect(host='localhost', port=3306, user='root', password='123456', db='dudu', charset='utf8')
# 連接成功之后土全,得到一個(gè)對(duì)象
# print(db)
# 首先根據(jù)db得到游標(biāo),游標(biāo)就是執(zhí)行sql語句用到的東西
# cursor = db.cursor()
# 給cursor添加一個(gè)參數(shù),讓其的到數(shù)據(jù)是一個(gè)字典
cursor = db.cursor(cursor=pymysql.cursors.DictCursor)
# 準(zhǔn)備sql語句帝牡,執(zhí)行sql語句
sql = 'select * from star'
# 返回結(jié)果rows是受影響的行數(shù)
rows = cursor.execute(sql)
# print(rows)
# 通過cursor的方法得到數(shù)據(jù)
# 返回一個(gè)元組败砂,元組里面每個(gè)元素的值對(duì)應(yīng)的就是數(shù)據(jù)表中每個(gè)字段對(duì)應(yīng)的值
# 獲取內(nèi)容的時(shí)候,里面有個(gè)迭代器在記錄你的位置
# print(cursor.fetchone())
# print(cursor.fetchone())
# print(cursor.fetchone())
# print(cursor.fetchone())
# print(cursor.fetchmany(5))
# print(cursor.fetchmany(5))
# 元組里面套元組
# print(cursor.fetchall())
# 打印得到所有的用戶名
ret = cursor.fetchall()
for obj in ret:
print('我叫%s,我來自%s,我有%s¥' % (obj['name'], obj['province'], obj['money']))
# print(ret)
# 遍歷每一個(gè)元組
# for tp in ret:
# print(tp[1])
# 最后呀,要記得關(guān)閉
cursor.close()
db.close()
如果為windows版的mysql抹竹,host為localhost;如果是虛擬機(jī)止潮,host為虛擬機(jī)的IP
增窃判、刪、改
import pymysql
db = pymysql.connect(host='10.7.181.122', port=3306, user='root', passwd='123456', db='dudu', charset='utf8')
cursor = db.cursor()
'''
# 增
# 準(zhǔn)備sql語句
obj = {'name': '李云龍', 'money': '20', 'province': '河南', 'age': 36, 'sex': '男'}
# 注意喇闸,這里的引號(hào)需要加
sql = 'insert into star(name, money, province, age, sex) values("%s", "%s", "%s", "%s", "%s")' % (obj['name'], obj['money'], obj['province'], obj['age'], obj['sex'])
'''
# 刪
id = 8
sql = 'delete from star where id=%s' % id
# 改
# sql = 'update star set name="馬德華" where id=7'
# 注意袄琳,沒有添加進(jìn)去,是因?yàn)闆]有提交燃乍,需要提交才能成功
try:
cursor.execute(sql)
# 提交唆樊,寫入磁盤
db.commit()
except Exception as e:
print(e)
# 回滾到最初始的狀態(tài)
db.rollback()
finally:
cursor.close()
db.close()
面向?qū)ο?/p>
import pymysql
class MyMysql(object):
def __init__(self, host, port, user, password, db, charset):
self.host = host
self.port = port
self.user = user
self.password = password
self.db = db
self.charset = charset
# 鏈接數(shù)據(jù)庫
self.connect()
def connect(self):
# 鏈接數(shù)據(jù)庫和獲取游標(biāo)
self.db = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password, db=self.db, charset=self.charset)
self.cursor = self.db.cursor()
def run(self, sql):
ret = None
try:
ret = self.cursor.execute(sql)
self.db.commit()
except Exception as e:
self.db.rollback()
finally:
self.close()
return ret
def close(self):
self.cursor.close()
self.db.close()
def insert(self, sql):
return self.run(sql)
def update(self, sql):
return self.run(sql)
def delete(self, sql):
return self.run(sql)
def read_one(self, sql):
ret = None
try:
self.cursor.execute(sql)
# 獲取得到數(shù)據(jù)
ret = self.cursor.fetchone()
except Exception as e:
print('查詢失敗')
finally:
self.close()
return ret
def read_many(self, sql):
ret = None
try:
self.cursor.execute(sql)
# 獲取得到數(shù)據(jù)
ret = self.cursor.fetchall()
except Exception as e:
print('查詢失敗')
finally:
self.close()
return ret
5、redis安裝和學(xué)習(xí)
什么是redis刻蟹?是一個(gè)基于內(nèi)存的數(shù)據(jù)庫逗旁,計(jì)算機(jī)里面,有內(nèi)存舆瘪,有硬盤
文件都在硬盤中存放片效,代碼在運(yùn)行的時(shí)候,有一個(gè)變量a
內(nèi)存:讀寫快英古,但是斷電消失淀衣,不大
硬盤:讀寫慢,但是可以持久化保存召调,大膨桥,隨便存
什么是NoSQL? not only sql 非關(guān)系型數(shù)據(jù)庫
鍵值對(duì)唠叛,根據(jù)鍵立馬就可以得到值
redis只嚣、MongoDB
redis官網(wǎng):redis.io redis.cn
5種數(shù)據(jù)類型:字符串(string)、列表(list)玻墅、集合(set)介牙、哈希(hash)、有序集合(zset)
redis官網(wǎng)只有l(wèi)inux版本的澳厢,不支持windows
學(xué)習(xí)方式:指令交互,可視化囚似,代碼操作