flat files vs db files
play

Flat Files vs. DB Files So far, our PHP examples have - PowerPoint PPT Presentation

Flat Files vs. DB Files So far, our PHP examples have used regular text files O>en called FLAT FILES These have a certain advantage,


  1. Flat ¡Files ¡vs. ¡DB ¡Files ¡ – So ¡far, ¡our ¡PHP ¡examples ¡have ¡used ¡regular ¡text ¡ files ¡ • O>en ¡called ¡FLAT ¡FILES ¡ – These ¡have ¡a ¡certain ¡advantage, ¡since ¡we ¡can ¡edit ¡ the ¡files ¡easily ¡and ¡can ¡read ¡them ¡without ¡any ¡ special ¡so>ware ¡ – However, ¡they ¡have ¡many ¡disadvantages ¡as ¡well ¡ • It ¡is ¡difficult ¡to ¡"update" ¡the ¡data ¡in ¡a ¡file ¡without ¡ rewriJng ¡the ¡enJre ¡file ¡ – How ¡to ¡change ¡data ¡in ¡the ¡middle ¡of ¡the ¡file? ¡ 1 ¡

  2. Flat ¡Files ¡vs. ¡DB ¡Files ¡ • Concurrent ¡access ¡of ¡the ¡file ¡is ¡tricky ¡ – We ¡use ¡FLOCK ¡to ¡lock ¡out ¡the ¡file, ¡but ¡even ¡that ¡only ¡ works ¡when ¡used ¡consistently ¡ – We ¡also ¡o>en ¡FLOCK ¡a ¡file ¡for ¡a ¡long ¡period ¡of ¡Jme ¡to ¡ prevent ¡corrupJon ¡– ¡limiJng ¡access ¡to ¡the ¡file ¡for ¡that ¡ Jme ¡ • Even ¡if ¡we ¡really ¡need ¡to ¡lock ¡only ¡part ¡of ¡the ¡file ¡ • Access ¡can ¡be ¡slow, ¡especially ¡if ¡the ¡data ¡is ¡large ¡ • Access ¡privileges ¡must ¡be ¡implemented ¡by ¡the ¡ programmer ¡ 2 ¡

  3. Flat ¡Files ¡vs. ¡DB ¡Files ¡ • An ¡alternaJve ¡is ¡to ¡use ¡a ¡DATABASE ¡to ¡store ¡our ¡ data ¡ – Most ¡common ¡databases ¡now ¡are ¡relaJonal ¡ databases ¡ • We ¡have ¡data ¡stored ¡in ¡tables ¡and ¡relate ¡the ¡data ¡from ¡one ¡ table ¡to ¡that ¡of ¡another ¡ – Access ¡is ¡faster ¡than ¡flat ¡files ¡ – Queries ¡to ¡obtain ¡specific ¡sets ¡of ¡data ¡can ¡be ¡done ¡ using ¡a ¡well-­‑defined ¡query ¡language ¡ – User ¡has ¡random ¡access ¡to ¡data ¡ – Concurrent ¡access ¡handling ¡is ¡built ¡in ¡ – Access ¡privileges ¡are ¡built-­‑in ¡ 3 ¡

  4. Database ¡DefiniJons ¡ • Some ¡definiJons ¡/ ¡noJons ¡we ¡will ¡be ¡using ¡ – Database ¡ • The ¡overall ¡collecJon ¡of ¡data ¡– ¡may ¡consist ¡of ¡many ¡ tables ¡ – Table ¡ • An ¡individual ¡"relaJon" ¡in ¡the ¡relaJonal ¡database ¡ – Relates ¡keys ¡to ¡values ¡ • Table ¡Column ¡ – An ¡a]ribute ¡in ¡the ¡table ¡ • Table ¡Row ¡ – An ¡enJty ¡in ¡the ¡table ¡ – Typically ¡has ¡a ¡value ¡for ¡each ¡column ¡ ¡ 4 ¡

  5. Database ¡DefiniJons ¡ • Key ¡ – An ¡a]ribute ¡that ¡uniquely ¡idenJfies ¡an ¡enJty ¡ » Ex: ¡SSN ¡for ¡a ¡student ¡at ¡UVA ¡ • Foreign ¡Key ¡ – Key ¡used ¡to ¡relate ¡data ¡in ¡one ¡table ¡with ¡data ¡in ¡another ¡table ¡ » Ex: ¡PSID ¡may ¡be ¡key ¡to ¡a ¡student ¡table ¡ » May ¡also ¡be ¡a ¡foreign ¡key ¡in ¡a ¡table ¡for ¡a ¡given ¡course ¡ • Schema ¡ – A ¡set ¡of ¡table ¡designs ¡that ¡determine ¡a ¡database ¡ – Does ¡not ¡yet ¡include ¡the ¡data ¡– ¡simply ¡shows ¡how ¡it ¡will ¡be ¡ structured ¡in ¡the ¡database ¡ 5 ¡

  6. Database ¡DefiniJons ¡ • RelaJonships ¡-­‑-­‑ ¡how ¡do ¡data ¡in ¡different ¡tables ¡ relate? ¡ – One ¡to ¡one ¡ • An ¡enJty ¡in ¡a ¡table ¡corresponds ¡to ¡a ¡single ¡enJty ¡in ¡ another ¡table ¡ • The ¡relaJonship ¡is ¡typically ¡established ¡using ¡a ¡foreign ¡ key ¡for ¡one ¡or ¡both ¡enJJes ¡ – Ex: ¡If ¡we ¡have ¡a ¡table ¡for ¡Student_Info ¡and ¡a ¡table ¡for ¡ Academic_History, ¡there ¡is ¡a ¡one-­‑to-­‑one ¡relaJonship ¡between ¡ them ¡ ¡ – One ¡to ¡many ¡ • An ¡enJty ¡in ¡a ¡table ¡corresponds ¡to ¡1 ¡or ¡more ¡enJJes ¡in ¡ another ¡table ¡ 6 ¡

  7. Database ¡DefiniJons ¡ • Ex: ¡If ¡the ¡table ¡for ¡Academic_History ¡has ¡an ¡entry ¡for ¡each ¡term, ¡ the ¡relaJonship ¡now ¡becomes ¡one ¡student ¡to ¡many ¡terms ¡ • Many ¡to ¡many ¡ – MulJple ¡enJJes ¡in ¡one ¡table ¡correspond ¡to ¡mulJple ¡ enJJes ¡in ¡another ¡table ¡ – This ¡relaJonship ¡is ¡o>en ¡defined ¡by ¡a ¡separate ¡table, ¡ which ¡in ¡fact ¡changes ¡it ¡into ¡2 ¡one ¡to ¡many ¡relaJonships ¡ • Ex: ¡Tables ¡Student_Info ¡and ¡Courses_Taken ¡have ¡a ¡many ¡to ¡many ¡ relaJonship, ¡since ¡a ¡student ¡can ¡take ¡many ¡courses ¡and ¡each ¡ course ¡can ¡be ¡taken ¡by ¡many ¡students ¡ • However, ¡if ¡we ¡create ¡a ¡new ¡table ¡Student_Courses, ¡we ¡can ¡have ¡ each ¡enJty ¡be ¡a ¡pair: ¡Student_Id, ¡Course_id ¡ • Now ¡Student_Info ¡has ¡a ¡one ¡to ¡many ¡relaJonship ¡with ¡ Student_Courses, ¡and ¡so ¡does ¡Courses_Taken ¡ 7 ¡

  8. Database ¡Theory? ¡ – There ¡is ¡a ¡lot ¡of ¡database ¡theory ¡about ¡how ¡to ¡ best ¡create ¡a ¡schema ¡ • Best ¡modeling ¡the ¡data ¡you ¡are ¡storing ¡ • Storing ¡it ¡most ¡efficiently ¡ • Designing ¡most ¡efficient ¡queries ¡ – We ¡will ¡leave ¡most ¡of ¡this ¡to ¡the ¡Database ¡course ¡ • Take ¡CS ¡4750 ¡ • Here ¡we ¡are ¡more ¡concerned ¡with ¡the ¡fundamentals, ¡ and ¡for ¡using ¡MySQL ¡through ¡PHP ¡and ¡a ¡Web ¡interface ¡ 8 ¡

  9. Geing ¡Started ¡with ¡MySQL ¡ • First ¡we ¡need ¡access ¡ • Typically ¡this ¡is ¡done ¡by ¡the ¡site ¡administrator ¡ • Since ¡you ¡are ¡using ¡your ¡own ¡servers, ¡you ¡are ¡the ¡ administrators! ¡ • This ¡can ¡be ¡done ¡in ¡several ¡ways: ¡ – XAMPP ¡comes ¡with ¡phpMyAdmin ¡ • This ¡is ¡accessed ¡via ¡the ¡Web ¡Server ¡and ¡allows ¡you ¡to ¡ access ¡/ ¡modify ¡the ¡DB ¡through ¡a ¡Web ¡interface ¡ – XAMPP ¡also ¡comes ¡with ¡a ¡mysql ¡program ¡ • This ¡allows ¡command ¡line ¡access ¡to ¡the ¡MySQL ¡server ¡ – You ¡can ¡use ¡either ¡of ¡these ¡to ¡iniJalize ¡your ¡DB ¡ – Let’s ¡try ¡a ¡simple ¡example ¡with ¡both ¡ – See ¡handouts ¡and ¡demo ¡ 9 ¡

  10. Geing ¡Started ¡with ¡MySQL ¡ ¡ • The ¡mysql ¡program ¡also ¡allows ¡you ¡to ¡run ¡batch ¡commands ¡using ¡ the ¡“source” ¡command ¡ – Be ¡careful ¡if ¡you ¡do ¡this! ¡ • Note: ¡The ¡root ¡MySQL ¡account ¡in ¡XAMPP ¡iniJally ¡has ¡ no ¡password ¡ – If ¡others ¡will ¡have ¡access ¡to ¡your ¡site, ¡you ¡need ¡to ¡add ¡ one! ¡ – There ¡are ¡other ¡security ¡holes ¡in ¡XAMPP ¡that ¡you ¡should ¡ address ¡if ¡it ¡will ¡be ¡shared ¡ – To ¡address ¡/ ¡fix ¡these, ¡go ¡to ¡the ¡XAMPP ¡home ¡page ¡on ¡ your ¡system: ¡ h]p://localhost/ ¡ • Click ¡on ¡the ¡Security ¡link ¡for ¡more ¡informaJon ¡ 10 ¡

  11. Geing ¡Started ¡with ¡MySQL ¡ • There ¡is ¡a ¡lot ¡of ¡info ¡available ¡for ¡MySQL ¡ – For ¡MySQL ¡home ¡page, ¡see: ¡ h]p://www.mysql.com ¡ ¡ – For ¡docs, ¡see: ¡ h]p://dev.mysql.com/doc/refman/5.0/en/ ¡ ¡ • There ¡are ¡many ¡versions ¡in ¡the ¡doc ¡site ¡– ¡depends ¡on ¡your ¡version ¡ of ¡MySQL ¡ – Googling ¡various ¡issues ¡you ¡may ¡have ¡is ¡also ¡ helpful ¡ 11 ¡

  12. Seing ¡up ¡a ¡DB ¡ • As ¡menJoned ¡before, ¡first ¡we ¡should ¡develop ¡ a ¡schema ¡for ¡our ¡database ¡ • Once ¡that ¡is ¡done ¡saJsfactorily, ¡we ¡need ¡to ¡ set ¡up ¡our ¡tables ¡and ¡iniJalize ¡the ¡DB ¡ – Can ¡be ¡done ¡fairly ¡simply ¡using ¡a ¡script ¡in ¡ conjuncJon ¡with ¡one ¡or ¡more ¡flat ¡files ¡ • The ¡nice ¡thing ¡here ¡is ¡that ¡the ¡flat ¡files ¡are ¡needed ¡only ¡ for ¡iniJalizaJon ¡ • Once ¡the ¡database ¡is ¡set ¡up, ¡we ¡use ¡it ¡exclusively ¡ 12 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend