博客
关于我
数据工程师面试必备——Python与数据库的那些事
阅读量:142 次
发布时间:2019-02-27

本文共 4139 字,大约阅读时间需要 13 分钟。

Python ??????????????????

??????Python?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????


????????????????

???????????????SQL??????????NoSQL??????????????????????????????????????

1.1 SQL???

SQL?????????????????????????????????????

  • SQL Server
  • Oracle
  • MySQL
  • PostgreSQL

SQL???????

  • ???????????????????
  • ?????????????????
  • ???????????????????

1.2 NoSQL???

NoSQL???????????????????????????????????????

  • MongoDB
  • Redis
  • CouchDB

NoSQL???????

  • ???????????????
  • ????????????????
  • ?????????????????????

1.2 SQLite??

?????????????SQLite??????SQLite?????????????????????

import sqlite3# ???????db = sqlite3.connect(':memory:')cur = db.cursor()# ?????cur.execute('''CREATE TABLE IF NOT EXISTS Customer(id INTEGER PRIMARY KEY, firstname VARCHAR(255), lastname VARCHAR(255))''')cur.execute('''CREATE TABLE IF NOT EXISTS Item(id INTEGER PRIMARY KEY, title VARCHAR(255), price DECIMAL)''')cur.execute('''CREATE TABLE IF NOT EXISTS BoughtItem(ordernumber INTEGER PRIMARY KEY, customerid INTEGER, itemid INTEGER, price DECIMAL,  FOREIGN KEY (customerid) REFERENCES Customer(id), FOREIGN KEY (itemid) REFERENCES Item(id))''')# ????cur.execute('''INSERT INTO Customer(firstname, lastname)VALUES ('Bob', 'Adams'), ('Amy', 'Smith'), ('Rob', 'Bennet')''')cur.execute('''INSERT INTO Item(title, price)VALUES ('USB', 10.2), ('Mouse', 12.23), ('Monitor', 199.99)''')cur.execute('''INSERT INTO BoughtItem(customerid, itemid, price)VALUES (1, 1, 10.2),       (1, 2, 12.23),       (1, 3, 199.99),       (2, 3, 180.00),       (3, 2, 11.23)''')

1.3 SQL????

?????????????????????????? AVG?COUNT?MAX?MIN ? SUM??????????????

# ???????????cur.execute('''SELECT itemid, AVG(price) FROM BoughtItem GROUP BY itemid''')print(cur.fetchall())  # ??: [(1, 10.2), (2, 11.73), (3, 189.995)]# ???????????cur.execute('''SELECT item.title, AVG(boughtitem.price) FROM BoughtItem AS boughtitemINNER JOIN Item AS item ON (item.id = boughtitem.itemid)GROUP BY boughtitem.itemid''')print(cur.fetchall())  # ??: [('USB', 10.2), ('Mouse', 11.73), ('Monitor', 189.995)]# ??????????cur.execute('''SELECT customer.firstname, SUM(boughtitem.price) FROM BoughtItem AS boughtitemINNER JOIN Customer AS customer ON (customer.id = boughtitem.customerid)GROUP BY customer.firstname''')print(cur.fetchall())  # ??: [('Amy', 180), ('Bob', 222.42), ('Rob', 11.23)]

1.4 ??SQL??

???SQL?????????????????

  • ????????????????
  • ???????????????????
  • ???????????????????

1.5 ??SQL??

??????????????????????? EXPLAIN QUERY PLAN ????

cur.execute('''EXPLAIN QUERY PLANSELECT customer.firstname, item.title, item.price, boughtitem.priceFROM BoughtItem AS boughtitemINNER JOIN Customer AS customer ON (customer.id = boughtitem.customerid)INNER JOIN Item AS item ON (item.id = boughtitem.itemid)''')print(cur.fetchall())

????????????MongoDB

MongoDB????????NoSQL????????????????????MongoDB??????

import pymongo# ??MongoDB???$ pip install pymongo# ??MongoDB???client = pymongo.MongoClient("mongodb://localhost:27017/")# ????????db = client.example_databasecustomers = db.customersitems = db.items# ????customers_data = [    {"firstname": "Bob", "lastname": "Adams"},    {"firstname": "Amy", "lastname": "Smith"},    {"firstname": "Rob", "lastname": "Bennet"}]items_data = [    {"title": "USB", "price": 10.2},    {"title": "Mouse", "price": 12.23},    {"title": "Monitor", "price": 199.99}]# ????????customers.insert_many(customers_data)items.insert_many(items_data)

2.2 MongoDB????

??????????

# ???????????for i in db.customers.find({"firstname": "Bob"}):    print(i["boughtitems"])

2.3 MongoDB????

?????????????????????

# ??????????db.customers.create_index([("name", pymongo.DESCENDING)])

????????Redis

Redis???????????????????????????????Redis??????

import redisfrom datetime import timedelta# ??Redis???r = redis.Redis()# ??????????def get_name(request, *args, **kwargs):    id = request.get('id')    if id in r:        return r.get(id)    else:        name = 'Bob'        r.setex(id, timedelta(minutes=60), name)        return name

??

?????????????????????????????????????????SQL?NoSQL???????????????????????????????????????????????ETL???????????????????????

转载地址:http://jzpd.baihongyu.com/

你可能感兴趣的文章
python 多进程-进阶-进程间通信之Pipe
查看>>
python 多进程-进阶-进程间通信之Queue
查看>>
python 如何“否定“value : 如果为真则返回假,如果为假则返回真
查看>>
Python 如何在 Web 环境中使用 Matplotlib 进行数据可视化
查看>>
python 如何把字符串转换成浮点数
查看>>
python 如果文件夹不存在就创建文件夹
查看>>
Python 如果有很多或以收缩形式
查看>>
Python 子进程 Popen 与 Pyinstaller
查看>>
Python 子进程 Popen.communicate() 等价于 Popen.stdout.read()?
查看>>
Python 子进程 Popen:为什么会出现“ls *.txt“?不行?
查看>>
Python 子进程参数
查看>>
python 字典 key 和value 互换
查看>>
python 字典sorted自定义排序,按照key or value排序
查看>>
python 字典和列表区别_元组列表和字典的主要区别是什么?
查看>>
python 字符串中特定字符替换,截取
查看>>
Python 字符串总结
查看>>
python 字符串显示中文_Python字符串开头的b"、u"、r"与中文乱码
查看>>
Python 字符串的几种拼装方式
查看>>
python 存入数据库bigint_python基础_MySQL的bigint类型
查看>>
python 学习 面向对象编程
查看>>