博客
关于我
数据工程师面试必备——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处理Excel数据
查看>>
Python基础:集合与文件操作
查看>>
Python基础:搭建开发环境(1)
查看>>
PYTHON基础:如何阅读N个整数,直到#39;
查看>>
Python基础:11变量作用域和闭包
查看>>
Python基础: with模式和__enter__ 和 __exit__
查看>>
Python基础(7)--函数
查看>>
Python基础部分-while循环,格式化输出
查看>>
Python基础语法:顺序语句结构
查看>>
Python基础语法:理解代码与写代码
查看>>
Python基础语法:条件和分支
查看>>
Python基础语法:基本数据类型(数字类型和布尔类型)
查看>>
Python基础语法:基本数据类型(列表)
查看>>
Python基础语法:内置函数
查看>>
Python基础语法:你不得不知的几种变量类型
查看>>
Python基础语法教程,零基础入门到精通,看完这一篇就够了
查看>>
Python基础语法与执行脚本的3种方式
查看>>
python基础语法
查看>>
python基础语法
查看>>
python系列【仅供参考】:python测试开发基础---multiprocessing.Pool
查看>>