Introduction to Database Programming with Python Gary Stewart

Slides:



Advertisements
Similar presentations
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Advertisements

CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Designing a Database Unleashing the Power of Relational Database Design.
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
MySQL and PHP By Trevor Adams.
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
CSC 2720 Building Web Applications Database and SQL.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
IST Databases and DBMSs Todd S. Bacastow January 2005.
Computing for Bioinformatics Introduction to databases What is a database? Database system components Data types DBMS architectures DBMS systems available.
Database Lecture # 1 By Ubaid Ullah.
The Relational Model These slides are based on the slides of your text book.
1 DATABASE TECHNOLOGIES BUS Abdou Illia, Fall 2012 (September 5, 2012)
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
ASP.NET Programming with C# and SQL Server First Edition
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
RDB/1 An introduction to RDBMS Objectives –To learn about the history and future direction of the SQL standard –To get an overall appreciation of a modern.
Introduction to SQL Steve Perry
Introduction to Database Systems
Databases. Database A database is an organized collection of related data.
IE 423 – Design of Decision Support Systems Data modeling and database development.
Web Services Week 8 Aims: –Using web services as front ends to databases Objectives: –Review of relational databases –Connecting to and querying databases.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
MET280: Computing for Bioinformatics Introduction to databases What is a database? Not a spreadsheet. Data types and uses DBMS (DataBase Management System)
HSCI 709 SQL Data Definition Language. SQL Standard SQL-92 was developed by the INCITS Technical Committee H2 on Databases. SQL-92 was designed to be.
CSC 2720 Building Web Applications Database and SQL.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
Database Fred Durao What is a database? A database is any organized collection of data. Some examples of databases you may encounter in.
1 Chapter 1 Introduction. 2 Introduction n Definition A database management system (DBMS) is a general-purpose software system that facilitates the process.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Chapter 10: The Data Tier We discuss back-end data storage for Web applications, relational data, and using the MySQL database server for back-end storage.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
CS 1308 Computer Literacy and the Internet
Course FAQ’s I do not have any knowledge on SQL concepts or Database Testing. Will this course helps me to get through all the concepts? What kind of.
Visual Programing SQL Overview Section 1.
Using SQL Connecting, Retrieving Data, Executing SQL Commands, … Svetlin Nakov Technical Trainer Software University
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational.
Mining real world data RDBMS and SQL. Index RDBMS introduction SQL (Structured Query language)
Database Connectivity with ASP.NET. 2 Introduction Web pages commonly used to: –Gather information stored on a Web server database Most server-side scripting.
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Introduction to Core Database Concepts Getting started with Databases and Structure Query Language (SQL)
Databases and SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Programming for the Web MySQL Command Line Using PHP with MySQL Dónal Mulligan BSc MA
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
CS320 Web and Internet Programming SQL and MySQL
Chapter 8 Working with Databases and MySQL
Database.
CS3220 Web and Internet Programming SQL and MySQL
DATABASE TECHNOLOGIES
CS3220 Web and Internet Programming SQL and MySQL
SQL – Application Persistence Design Patterns
INTRODUCTION A Database system is basically a computer based record keeping system. The collection of data, usually referred to as the database, contains.
Presentation transcript:

Introduction to Database Programming with Python Gary Stewart

Persistent Data Many software solutions require persistent data, i.e. data stored permanently There are many ways to have data persist –text files, e.g. comma delimited files –object binary files, e.g. using pickle –databases, e.g. sqlite3 2

Databases A database, or Database Management System (DBMS) manages persistent data The DBMS allows data storage, retrieval, privacy, security and integrity There are many commercial and open source examples –Commercial - SQL Server, Oracle, MS Access –Open Source - MySQL, Postgresql, sqlite 3

Types of Databases There are various kinds of DBMSs, e.g. –object oriented DBMSs –no-SQL DBMSs –relational DBMSs –object-relational DBMSs Most commonly used systems are Relational Database Management Systems (RDBMS) 4

Relational DBMSs Relational DBMSs store sets of data in a tabular format, which are typically related to each other The data is manipulated with a language called SQL (Structured Query Language) These RDBMSs work very similarly Most RDBMSs are client-server based Examples –SQL Server, Oracle, MySQL, PostgreSQL 5

Tables, Records, Fields In a RDBMS data is typically stored in a tabular format in tables, or relations Each table has fields, or attributes, which indicate what is stored in each column The table also has many records, or tuples, which indicate what is stored in each row 6

Example The following example is of a Student table It has fields for StudentNumber, Name, Surname, Address It contains 3 records for particular students field record 7

Field Data Types When creating a new table the data type for each field needs to be specified RDBMSs have many data types relating to integers, real or floating point numbers, strings or text, images, etc. Field data types can also be further constrained with size, precision, etc. 8

Example The following example is of a Stock table with fields and corresponding datatypes 9

Primary Keys When defining a table, it is useful to specify a field (or set of fields) which uniquely identifies a record This unique identifier is referred to as the primary key The primary key is typically an id number or unique code which identifies a record 10

Example The following example is of a Course table primary key 11

Relationships RDBMSs allow the definition of relationships between data, as they occur in the real world, e.g. –Students and Courses –Employees and Branches These relationships can have different cardinalities, depending how one record in a table relates to another record in the related table, e.g. –one to one –one to many –many to many 12

Foreign Keys These relationships are indicated by using the primary key of the related table When the primary key of a table is used in another table to specify the relationship, it is referred to as a foreign key 13

Example Consider Student and ResidenceRoom tables A one-to-one relationship exits between Student and ResidenceRoom foreign key 14

Example Consider Employee and Branch tables A one to many relationship exits between Branch and Employee foreign key 15

Example Consider Student and Course tables A many to many relationship exits between Student and Course An extra StudentCourse needs to be created foreign keys 16

Relationships Cardinalities are expressed using foreign keys –one to one, the “subset” table contains a foreign key –one to many, the many table contains a foreign key –many to many, a new table is created to contain both foreign keys Table relationships defined in this way avoids duplication of data 17

Without Relationships A database with tables, but no keys and no relationships would duplicate information 18

sqlite sqlite is a lightweight, server-less, open source RDBMS engine It is the “most widely deployed SQL database engine in the world”, e.g. –Apple computers and iphones –Mozilla Firefox Sqlite is unlike most RDBMSs, since the database is stored in a single file, and it is not client-server based 19

sqlite It conforms to the basic principles of RDBMSs Accessible by many programming languages and management tools –Languages - PHP, Python, Java, C++ –Management Tools - SQLite Expert, SQLite Spy The current version of sqlite is sqlite3 20

sqlite data types sqlite has many data types, but the primary ones are the following –text, for storing text or strings –real, for storing floating point number –integer, for storing integers 21

SQLite Expert SQLite Expert Personal is a free GUI-based management tool for sqlite databases 22

SQL Data in RDBMSs is manipulated using SQL (Structured Query Language) Basic SQL commands: –create, creates a table –drop, drops/deletes a table –insert, inserts a record into a table –update, updates fields in a table record –delete, deletes records from a table –select, selects data from a table 23

SQL: create and drop The create SQL command creates a new table create table (,…) When creating a table the field names and their data types need to be specified create table Person (Name text, Age integer) 24

SQL: drop The drop SQL command deletes a table drop table When dropping a table only the table name is specified drop table Person 25

SQL: insert The insert SQL command insert records into a table insert into values (,…) When inserting the values need to be specified in order insert into Person values (“John Smith”, 20) 26

SQL: delete The delete SQL command deletes records from a table delete from where = The delete command has a where clause to indicate which records to delete delete from Person where Name = “Kim Jones” 27

SQL: update The update SQL command updates fields in a table record update set = where = The update command has a set clause to indicate which fields to update The update command has a where clause to indicate which record to update update Person set Age=21 where Name=“John Smith” 28

SQL: select The SQL select command selects data from a table select,… from where = The field names to select can be specified explicitly, or * for all fields select Name, Age from Person select * from Person 29

SQL: select The select command can have a where clause specifying which records to select select Age from Person where Name = “John Smith” 30

Multiple SQL Commands When using multiple SQL statements each statement needs to end with a semi colon ; create table Person (Name text, Age integer); insert into Person values (“John Smith”, 20); update Person set Age=21 where Name=“John Smith”; select * from Person; 31

Python and sqlite3 Python has a module which enables it to manipulate sqlite3 databases import sqlite3 A sqlite3 database is created using the connect() function and specifying a filename db = sqlite3.connect(‘my_database.db’) An SQL command can be executed by calling the execute method db.execute(‘create table Person (name text, age int)’) 32

Python and sqlite3 When executing an insert statement it is possible to use the ? notation as placeholders for the values and pass the values in a tuple db.execute('insert into Person values (?,?)',('bob',20)) 33

Python and sqlite3 If the SQL command(s) modifies the database the commit() method needs to be called for the command to take effect db.commit() Committing can be done after a set of commands Committing is necessary for the following SQL commands to take effect –create,insert,update,delete,drop 34

Python and sqlite3 When executing a SQL select command the execute() method returns a cursor which can be iterated over with a for loop to access the records returned cursor = db.execute(‘select * from Person’) for row in cursor: print(row) Each row/record is returned as a tuple 35

Databases in Development Databases are commonly used in the typical software development process on many platforms – desktop, web, etc. Usually the database is first designed with tables, relationships, etc. with a management tool Then a software application is developed which interacts with the database 36

Resources sqlite – SQL – Python and sqlite3 –Python reference documentation 37