introd u ction to relational databases
play

Introd u ction to relational databases IN TR OD U C TION TO IMP OR - PowerPoint PPT Presentation

Introd u ction to relational databases IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON H u go Bo w ne - Anderson Data Scientist at DataCamp What is a relational database ? Based on relational model of data First described b y Edgar Ted


  1. Introd u ction to relational databases IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON H u go Bo w ne - Anderson Data Scientist at DataCamp

  2. What is a relational database ? Based on relational model of data First described b y Edgar “ Ted ” Codd INTRODUCTION TO IMPORTING DATA IN PYTHON

  3. E x ample : North w ind database INTRODUCTION TO IMPORTING DATA IN PYTHON

  4. E x ample : North w ind database INTRODUCTION TO IMPORTING DATA IN PYTHON

  5. E x ample : North w ind database INTRODUCTION TO IMPORTING DATA IN PYTHON

  6. E x ample : North w ind database INTRODUCTION TO IMPORTING DATA IN PYTHON

  7. The Orders table INTRODUCTION TO IMPORTING DATA IN PYTHON

  8. The Orders table INTRODUCTION TO IMPORTING DATA IN PYTHON

  9. The Orders table INTRODUCTION TO IMPORTING DATA IN PYTHON

  10. The Orders table INTRODUCTION TO IMPORTING DATA IN PYTHON

  11. Tables are linked INTRODUCTION TO IMPORTING DATA IN PYTHON

  12. Tables are linked INTRODUCTION TO IMPORTING DATA IN PYTHON

  13. Tables are linked INTRODUCTION TO IMPORTING DATA IN PYTHON

  14. Relational model Widel y adopted Codd ’ s 12 R u les / Commandments Consists of 13 r u les (z ero - inde x ed !) Describes w hat a Relational Database Management S y stem sho u ld adhere to to be considered relational INTRODUCTION TO IMPORTING DATA IN PYTHON

  15. Relational Database Management S y stems INTRODUCTION TO IMPORTING DATA IN PYTHON

  16. Relational Database Management S y stems INTRODUCTION TO IMPORTING DATA IN PYTHON

  17. Relational Database Management S y stems INTRODUCTION TO IMPORTING DATA IN PYTHON

  18. Relational Database Management S y stems INTRODUCTION TO IMPORTING DATA IN PYTHON

  19. Let ' s practice ! IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON

  20. Creating a database engine in P y thon IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON H u go Bo w ne - Anderson Data Scientist at DataCamp

  21. Creating a database engine SQLite database Fast and simple SQLAlchem y Works w ith man y Relational Database Management S y stems from sqlalchemy import create_engine engine = create_engine('sqlite:///Northwind.sqlite') INTRODUCTION TO IMPORTING DATA IN PYTHON

  22. Getting table names from sqlalchemy import create_engine engine = create_engine('sqlite:///Northwind.sqlite') table_names = engine.table_names() print(table_names) ['Categories', 'Customers', 'EmployeeTerritories', 'Employees', 'Order Details', 'Orders', 'Products', 'Region', 'Shippers', 'Suppliers', 'Territories'] INTRODUCTION TO IMPORTING DATA IN PYTHON

  23. Let ' s practice ! IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON

  24. Q u er y ing relational databases in P y thon IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON H u go Bo w ne - Anderson Data Scientist at DataCamp

  25. Basic SQL q u er y SELECT * FROM Table_Name Ret u rns all col u mns of all ro w s of the table E x ample : SELECT * FROM Orders We ’ ll u se SQLAlchem y and pandas INTRODUCTION TO IMPORTING DATA IN PYTHON

  26. Workflo w of SQL q u er y ing Import packages and f u nctions Create the database engine Connect to the engine Q u er y the database Sa v e q u er y res u lts to a DataFrame Close the connection INTRODUCTION TO IMPORTING DATA IN PYTHON

  27. Yo u r first SQL q u er y from sqlalchemy import create_engine import pandas as pd engine = create_engine('sqlite:///Northwind.sqlite') con = engine.connect() rs = con.execute("SELECT * FROM Orders") df = pd.DataFrame(rs.fetchall()) con.close() INTRODUCTION TO IMPORTING DATA IN PYTHON

  28. Printing y o u r q u er y res u lts print(df.head()) 0 1 2 3 4 0 10248 VINET 5 7/4/1996 12:00:00 AM 8/1/1996 12:00:00 AM 1 10251 VICTE 3 7/8/1996 12:00:00 AM 8/5/1996 12:00:00 AM 2 10254 CHOPS 5 7/11/1996 12:00:00 AM 8/8/1996 12:00:00 AM 3 10256 WELLI 3 7/15/1996 12:00:00 AM 8/12/1996 12:00:00 AM 4 10258 ERNSH 1 7/17/1996 12:00:00 AM 8/14/1996 12:00:00 AM INTRODUCTION TO IMPORTING DATA IN PYTHON

  29. Set the DataFrame col u mn names from sqlalchemy import create_engine import pandas as pd engine = create_engine('sqlite:///Northwind.sqlite') con = engine.connect() rs = con.execute("SELECT * FROM Orders") df = pd.DataFrame(rs.fetchall()) df.columns = rs.keys() con.close() INTRODUCTION TO IMPORTING DATA IN PYTHON

  30. Set the data frame col u mn names print(df.head()) OrderID CustomerID EmployeeID OrderDate 0 10248 VINET 5 7/4/1996 12:00:00 AM 1 10251 VICTE 3 7/8/1996 12:00:00 AM 2 10254 CHOPS 5 7/11/1996 12:00:00 AM 3 10256 WELLI 3 7/15/1996 12:00:00 AM 4 10258 ERNSH 1 7/17/1996 12:00:00 AM INTRODUCTION TO IMPORTING DATA IN PYTHON

  31. Using the conte x t manager from sqlalchemy import create_engine import pandas as pd engine = create_engine('sqlite:///Northwind.sqlite') with engine.connect() as con: rs = con.execute("SELECT OrderID, OrderDate, ShipName FROM Orders") df = pd.DataFrame(rs.fetchmany(size=5)) df.columns = rs.keys() INTRODUCTION TO IMPORTING DATA IN PYTHON

  32. Let ' s practice ! IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON

  33. Q u er y ing relational databases directl y w ith pandas IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON H u go Bo w ne - Anderson Data Scientist at DataCamp

  34. The pandas w a y to q u er y from sqlalchemy import create_engine import pandas as pd engine = create_engine('sqlite:///Northwind.sqlite') with engine.connect() as con: rs = con.execute("SELECT * FROM Orders") df = pd.DataFrame(rs.fetchall()) df.columns = rs.keys() df = pd.read_sql_query("SELECT * FROM Orders", engine) INTRODUCTION TO IMPORTING DATA IN PYTHON

  35. Let ' s practice ! IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON

  36. Ad v anced q u er y ing : e x ploiting table relationships IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON H u go Bo w ne - Anderson Data Scientist at DataCamp

  37. Tables are linked INTRODUCTION TO IMPORTING DATA IN PYTHON

  38. Tables are linked INTRODUCTION TO IMPORTING DATA IN PYTHON

  39. Tables are linked INTRODUCTION TO IMPORTING DATA IN PYTHON

  40. JOINing tables INTRODUCTION TO IMPORTING DATA IN PYTHON

  41. JOINing tables INTRODUCTION TO IMPORTING DATA IN PYTHON

  42. JOINing tables INTRODUCTION TO IMPORTING DATA IN PYTHON

  43. JOINing tables INTRODUCTION TO IMPORTING DATA IN PYTHON

  44. JOINing tables INTRODUCTION TO IMPORTING DATA IN PYTHON

  45. INNER JOIN in P y thon ( pandas ) from sqlalchemy import create_engine import pandas as pd engine = create_engine('sqlite:///Northwind.sqlite') df = pd.read_sql_query("SELECT OrderID, CompanyName FROM Orders INNER JOIN Customers on Orders.CustomerID = Customers.CustomerID", engine) print(df.head()) OrderID CompanyName 0 10248 Vins et alcools Chevalier 1 10251 Victuailles en stock 2 10254 Chop-suey Chinese 3 10256 Wellington Importadora 4 10258 Ernst Handel INTRODUCTION TO IMPORTING DATA IN PYTHON

  46. Let ' s practice ! IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON

  47. Final Tho u ghts IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON H u go Bo w ne - Anderson Data Scientist at DataCamp

  48. What y o u’v e learned : Relational databases Q u eries SELECT WHERE JOIN INTRODUCTION TO IMPORTING DATA IN PYTHON

  49. Ne x t co u rse : Scrape data from the w eb Interact w ith APIs INTRODUCTION TO IMPORTING DATA IN PYTHON

  50. Let ' s practice ! IN TR OD U C TION TO IMP OR TIN G DATA IN P YTH ON

Recommend


More recommend