Вы находитесь на странице: 1из 26

NIELIT Gorakhpur Centre

MMMUT Campus, Gorakhpur (UP), PIN-273010

Introduction to Database
Programming in Python

1
© NIELIT Gorakhpur Centre.
Introduction to Database Programming in Python

 The Python programming language has powerful features for database
programming.
 Python supports various databases like MySQL, Oracle, Microsoft SQL Server,Sybase,
PostgreSQL, MangoDB etc.
 Python also supports Data Definition Language (DDL), Data Manipulation Language
(DML) and Data Query Statements.
 For database programming, the Python DB API is a widely used module that
provides a database application programming interface.

© NIELIT Gorakhpur Centre. 2


Introduction to Database Programming in Python
 Benefits of Python for database programming

There are many good reasons to use Python for programming database applications:
 Programming in Python is arguably more efficient and faster compared to other
languages.
 Python is famous for its portability.
 It is platform independent.
 Python supports SQL cursors.
 In many programming languages, the application developer needs to take care of
the open and closed connections of the database, to avoid further exceptions and
errors. In Python, these connections are taken care of.
 Python supports relational database systems.
 Python database APIs are compatible with various databases, so it is very easy to
migrate and port database application interfaces.

© NIELIT Gorakhpur Centre. 3


Introduction to Database Programming in Python
 DB-API (SQL-API) for Python

 Python DB-API is independent of any database engine, which enables you to


write Python scripts to access any database engine.
 The Python DB API implementation for MySQL is mysql-connector .
 For PostgreSQL, it supports psycopg2, PyGresQL modules.
 DB-API implementations for Oracle is cx_oracle.
 Most databases ship with ODBC drivers, so chances are high that you can use
one of these drivers together with a Python ODBC interface to connect your
Python application with any database on the market.
 Python’s DB-API consists of connection objects, cursor objects, standard
exceptions and some other module contents, all of which we will discuss.

© NIELIT Gorakhpur Centre. 4


Introduction to Database Programming in Python
Installation of database connection Module

For MySQL

_> python -m pip install mysql-connector

For ODBC

_> python -m pip install pyodbc

© NIELIT Gorakhpur Centre. 5


Introduction to Database Programming in Python
 Test connection to database

© NIELIT Gorakhpur Centre. 6


Introduction to Database Programming in Python
 Test connection to database

© NIELIT Gorakhpur Centre. 7


Introduction to Database Programming in Python
 Test connection to database

© NIELIT Gorakhpur Centre. 8


Introduction to Database Programming in Python
 Test connection to database

© NIELIT Gorakhpur Centre. 9


Introduction to Database Programming in Python
 Test connection to database

© NIELIT Gorakhpur Centre. 10


Introduction to Database Programming in Python
 Test connection to database

© NIELIT Gorakhpur Centre. 11


Introduction to RDBMS

Databases are useful for storing information categorically. A company may have a database
with the following tables:
•Employees
•Products
•Customers
•Orders
 RDBMS stands for Relational Database Management System.

 RDBMS is the basis for SQL, and for all modern database systems such as MS SQL
Server, IBM DB2, Oracle, MySQL, and Microsoft Access.

 The data in RDBMS is stored in database objects called tables. A table is a collection of
related data entries and it consists of columns and rows.

© NIELIT Gorakhpur Centre. 12


RDBMS SQL
 SQL Commands

DDL (Data Definition Language)


DML (Data Manipulation Language)
•CREATE DATABASE - creates a new
•SELECT - extracts data from a database
database
•INSERT INTO - inserts new data into a
•DROP DATABASE - deletes a database
database
•CREATE TABLE - creates a new table
•UPDATE - updates data in a database
•ALTER TABLE - modifies a table
•DELETE - deletes data from a database
•DROP TABLE - deletes a table

© NIELIT Gorakhpur Centre. 13


RDBMS SQL
 Syntax (Create, Drop)

CREATE DATABASE databasename;

DROP DATABASE databasename;

CREATE TABLE table_name (


column1 datatype not null, column2 datatype, column3 datatype,
.... );

DROP TABLE table_name

© NIELIT Gorakhpur Centre. 14


RDBMS SQL
 Alter table

To add column:
ALTER TABLE table_name
ADD column_name datatype;

To delete column:

ALTER TABLE table_name


DROP COLUMN column_name;

To change data type of column:

ALTER TABLE table_name


MODIFY COLUMN column_name datatype;

© NIELIT Gorakhpur Centre. 15


RDBMS SQL
 Insert, Update, Delete

INSERT INTO table_name (column1, column2, column3,


...)
VALUES (value1, value2, value3, ...);

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

DELETE FROM table_name WHERE condition;

© NIELIT Gorakhpur Centre. 16


RDBMS SQL
 Select

SELECT column1, column2, ...


FROM table_name;

SELECT * FROM table_name;

SELECT column1, column2, ...


FROM table_name
WHERE condition;

© NIELIT Gorakhpur Centre. 17


RDBMS SQL
 AND, OR, NOT, ORDER BY

SELECT column1, column2, ...


FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

SELECT column1, column2, ...


FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

SELECT column1, column2, ...


FROM table_name
WHERE NOT condition;

SELECT column1, column2, ...


FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

© NIELIT Gorakhpur Centre. 18


RDBMS SQL
 MIN() MAX() COUNT() AVG() SUM()

SELECT MIN(column_name)
FROM table_name
WHERE condition; SELECT AVG(column_name)
FROM table_name
SELECT MAX(column_name) WHERE condition;
FROM table_name
WHERE condition; SELECT SUM(column_name)
FROM table_name
SELECT COUNT(column_name) WHERE condition;
FROM table_name
WHERE condition;

© NIELIT Gorakhpur Centre. 19


RDBMS SQL
 LIKE

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;

For pattern, Use:


% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character

© NIELIT Gorakhpur Centre. 20


RDBMS SQL
 GROUP BY

The GROUP BY statement is often used with aggregate functions (COUNT,


MAX, MIN, SUM, AVG) to group the result-set by one or more columns.

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)

© NIELIT Gorakhpur Centre. 21


RDBMS SQL
 SQL JOIN

A JOIN clause is used to combine rows from two or more tables, based
on a related column between them.
• Inner Join
• Left Join
• Right Join
• Full Outer Join

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

© NIELIT Gorakhpur Centre. 22


RDBMS SQL
 SQL JOIN

© NIELIT Gorakhpur Centre. 23


RDBMS SQL
 Foreign Key
• A FOREIGN KEY is a key used to link two tables together.
• A FOREIGN KEY is a field (or collection of fields) in one table that
refers to the PRIMARY KEY in another table.
• The table containing the foreign key is called the child table, and the
table containing the candidate key is called the referenced or parent
table
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
Alter table tablename
Add foreign key (fieldname) Alter table employee
References Add foreign key (dept_id)
tablename2(fieldname); References department(dept_id);

© NIELIT Gorakhpur Centre. 24


RDBMS SQL

EmployeeDB
Create following table:
• employee (emp_id, emp_name, emp_fname, emp_dob,
emp_city, emp_mobile, emp_gender, emp_salary,
dept_id)

• salary (emp_id, salary_month, salary_year, absent,


leave, total_salary)

• department (dept_id, dept_name )


© NIELIT Gorakhpur Centre. 25
© NIELIT Gorakhpur Centre. 26

Вам также может понравиться