SQL relationships

Python में Databases का परिचय

Jason Myers

Co-Author of Essential SQLAlchemy and Software Engineer

Relationships

  • डुप्लिकेट डेटा से बचने दें
  • एक ही जगह बदलाव करना आसान बनाएँ
  • ऐसी जानकारी अलग टेबल में रखें जो अक्सर नहीं चाहिए
Python में Databases का परिचय

Relationships

Python में Databases का परिचय

Automatic joins

stmt = select([census.columns.pop2008, 
        state_fact.columns.abbreviation])

results = connection.execute(stmt).fetchall() print(results)
[(95012, u'IL'),
 (95012, u'NJ'),
 (95012, u'ND'),
 (95012, u'OR'),
 (95012, u'DC'),
 (95012, u'WI'),
 ...
Python में Databases का परिचय

Join

  • एक Table और वैकल्पिक expression लेता है जो बताता है कि दोनों टेबल कैसे related हैं
  • अगर relationship पहले से परिभाषित है और reflection से मिलती है, तो expression की ज़रूरत नहीं
  • select() के तुरंत बाद आता है, और where(), order_by() या group_by() से पहले
Python में Databases का परिचय

select_from()

  • डिफ़ॉल्ट, derived FROM clause को एक join से बदलने के लिए उपयोग होता है
  • join() clause को wrap करता है
Python में Databases का परिचय

select_from() उदाहरण

stmt = select([func.sum(census.columns.pop2000)])

stmt = stmt.select_from(census.join(state_fact))
stmt = stmt.where(state_fact.columns.circuit_court == '10')
result = connection.execute(stmt).scalar() print(result)
14945252
Python में Databases का परिचय

बिना predefined relationship के tables join करना

  • Join एक Table और वैकल्पिक expression लेता है जो बताता है कि दोनों टेबल कैसे related हैं
  • केवल उन्हीं rows पर join होगा जहाँ दोनों कॉलम के डेटा match करते हैं
  • अलग-अलग type वाले कॉलम पर join करने से बचें
Python में Databases का परिचय

select_from() उदाहरण

stmt = select([func.sum(census.columns.pop2000)])

stmt = stmt.select_from( census.join(state_fact, census.columns.state == state_fact.columns.name))
stmt = stmt.where( state_fact.columns.census_division_name == 'East South Central')
result = connection.execute(stmt).scalar() print(result)
16982311
Python में Databases का परिचय

अभ्यास करते हैं!

Python में Databases का परिचय

Preparing Video For Download...