Interface Python With SQL Database: Apurv Gupta
Interface Python With SQL Database: Apurv Gupta
Database
By –
Apurv Gupta
Bethany Convent School
Database - Introduction
A database is nothing but an organized collection of data.
Data is organized into rows, columns and tables and it is indexed to make it easier to find
relevant information.
All companies whether large or small use databases. So it become necessary to develop
project/software using any programming language like python in such a manner which can
interface with such databases which support SQL.
Generalised form of Interface of python with SQL Database can be understood with the
help of this diagram-
Advantages of DBMS
Database reduces Redundancy- It removes duplication of data because data are kept at
one place and all the application refers to the centrally maintained database.
Database controls Inconsistency- When two copies of the same data do not agree to each
other, then it is called Inconsistency. By controlling redundancy, the inconsistency is also
controlled.
Database facilitate Sharing of Data- Data stored in the database can be shared among
several users.
Database ensures Security- Data are protected against accidental or intentional disclosure
to unauthorized person or unauthorized modification.
Database maintains Integrity- It enforces certain integrity rules to insure the validity or
correctness of data. For ex. A date can’t be like 31/31/2000.
RDBMS
RDBMS Terms
Relation (Table)- A Relation or Table is Matrix like structure arranged in Rows and
Columns.
Domain- It is collection of values from which the value is derived for a column.
Tuple / Entity / Record- Rows of a table is called Tuple or Record.
Attribute/ Field- Column of a table is called Attribute or Field.
Degree- Number of columns (attributes) in a table.
Cardinality- Number of rows (Records) in a table.
Keys in RDBMS
Primary Key- A primary is a column or set of columns in a table that uniquely identifies
tuples (rows) in that table.
Candidate Key- It is an attribute or a set of attributes or keys participating for Primary
Key, to uniquely identify each record in that table.
Alternate Key- Out of all candidate keys, only one gets selected as primary key,
remaining keys are known as alternate or secondary keys.
Foreign Key- Foreign keys are the columns of a table that points to the primary key of
another table. They act as a cross-reference between tables.
Keys in RDBMS
SQL
DDL (Data Definition Language)- To create database and table structure-commands like
CREATE , ALTER , DROP etc.
DCL (Data Control Language)- Used to control the transactions. commands like
COMMIT, ROLLBACK, SAVEPOINT etc.
To insert new rows into an existing table use the INSERT command:- mysql>INSERT
INTO student values(‘dwivedi’,’freya’,’Udaipur’,’4’);
Similarly we can insert multiple records.With the SELECT command we can retrieve
previously inserted rows- mysql> SELECT * FROM student;
Selecting rows by using the WHERE clause in the SELECT command- mysql>
SELECT * FROM student WHERE class=“4";
Selecting specific columns(Projection) by listing their names- mysql> SELECT
first_name, class FROM student;
To modify or update entries in the table use the UPDATE command- mysql> UPDATE
student SET class=“V" WHERE firstname= “freya";
Database commands in MySQL
Deleting selected rows from a table using the DELETE command- mysql> DELETE
FROM student WHERE firstname=“amar";
A general form of SELECT is-
SELECT what to select(field name) FROM table(s)
WHERE condition that the data must satisfy;
Comparison operators are: < ; <= ; = ; != or <> ; >= ; >
Logical operators are: AND ; OR ; NOT
Comparison operator for special value NULL: IS
mysql> SELECT * FROM Student WHERE City IS NULL ;
BETWEEN- to access data in specified range-
mysql> SELECT * FROM Student WHERE class between 4 and 6;
IN- operator allows us to easily test if the expression in the list of values- mysql>
SELECT * FROM Student WHERE class in (4,5,6);
Pattern Matching – LIKE Operator - A string pattern can be used in SQL using the following
wild card:
% Represents a substring in any length
_ Represents a single character
Example:
‘A%’ represents any string starting with ‘A’ character.
‘_ _A’ represents any 3 character string ending with ‘A’.
‘_B%’ represents any string having second character ‘B’
‘_ _ _’ represents any 3 letter string.
A pattern is case sensitive and can be used with LIKE operator.
mysql> SELECT * FROM Student WHERE Name LIKE ‘A%’;
mysql> SELECT * FROM Student WHERE Name LIKE%Singh%’;
mysql> SELECT Name, City FROM Student WHERE Class>=8 AND Name LIKE ‘%Kumar%’ ;
Ordering Query Result – ORDER BY Clause - A query result can be orders in ascending
(A-Z) or descending (Z-A) order as per any column. Default is Ascending order. - mysql>
SELECT * FROM Student ORDER BY class;
To get descending order use DESC key word. - mysql> SELECT * FROM Student
ORDER BY class DESC;
Creating Table with Constraints
ANT
O RT
P mysql> CREATE TABLE Persons
IM (ID int NOT NULL PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName
varchar(255), Age int, City varchar(255) DEFAULT ‘Jaipur’, CONSTRAINT CHK_Person
CHECK (Age>=18));
mysql> CREATE TABLE Orders (OrderID int NOT NULL, OrderNumber int NOT
NULL, PersonID int, PRIMARY KEY (OrderID), FOREIGN KEY (PersonID)
REFERENCES Persons(ID));
Altering Table
The SQL ALTER TABLE command is used to add, delete or modify columns in an existing
table. You should also use the ALTER TABLE command to add and drop various constraints on
an existing table.
The basic syntax of an ALTER TABLE command to add a New Column in an existing
table is as follows:
ALTER TABLE table_name ADD column_name datatype;
The basic syntax of an ALTER TABLE command to DROP COLUMN in an existing table
is as follows:
ALTER TABLE table_name DROP COLUMN column_name;
The basic syntax of an ALTER TABLE command to change the DATA TYPE of a column
in a table is as follows:
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
The basic syntax of an ALTER TABLE command to add a NOT NULL constraint to a
column in a table is as follows.
ALTER TABLE table_name MODIFY column_name datatype NOT NULL;