Unit III - Database Management 20-21-1
Unit III - Database Management 20-21-1
Unit III - Database Management 20-21-1
DATABASE CONCEPTS
A Database is a collection of DATA/INFORMATION that is organized
so that it can be easily accessed, managed and updated.
KEYS IN A DATABASE
Key plays an important role in relational database; it is used for identifying
unique rows from table & establishes relationship among tables on need.
Types of keys in DBMS
Primary Key – A primary is a column or set of columns in a table that
uniquely identifies tuples (rows) in that table.
What is a unique key?
A Unique key constraint uniquely identified each record in the database.
This provides uniqueness for the column or set of columns.
A Primary key constraint has automatic unique constraint defined on it.
But not, in the case of Unique Key.
There can be many unique constraint defined per table, but only one
Primary key constraint defined per 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.
MySQL Features
Open Source & Free of Cost:
It is Open Source and available at free of cost.
Portability:
Small enough in size to install and run it on any types of Hardware and OS like
Linux,MS Windows or Mac etc.
Security :
Its Databases are secured & protected with password.
Connectivity
Various APIs are developed to connect it with many programming languages.
Query Language
It supports SQL (Structured Query Language) for handling database.
Types of SQL Commands
DDL (Data Definition Language)
To create database and table structure-commands like
CREATE , ALTER , DROP etc.
DML (Data Manipulation Language)
Record/rows related operations. Commands like SELECT....,
INSERT..., DELETE..., UPDATE.... etc.
DCL (Data Control Language) used to manipulate
permissions or access rights to the tables. commands like
GRANT, REVOKE etc.
TCL(Transactional control Language)
Used to control the transactions. Commands like COMMIT,
ROLLBACK, SAVEPOINT etc.
Creating a Database
Deleting a database
mysql> DROP database <databasename>;
to remove table
mysql> drop table <tablename>;
To show list of tables in opened database
mysql> SHOW TABLES;
Creating a table in the database is achieved with
CREATE table statement.
IN- operator allows us to easily test if the expression in the list of values.
mysql> CREATE TABLE Persons (ID int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,FirstName varchar(255),Age int,
City varchar(255) DEFAULT ‘Jaipur');
Table Name : ip1
____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_______________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
Table Name: ip3
_______________________________
______________________________________________________
________________________________________________________________________________
___________________________________________________________________
________________________
Functions
MYSQL ORDER BY
The ORDER BY clause is used in a SELECT statement to sort results either in
ascending or descending order.
In practical applications many times there arises a need to get reports based on
some groups of data. SQL provides GROUP BY Clause to handle all such
requirements.
The SQL GROUP BY Clause is used along with the group functions to retrieve
data grouped according to one or more columns.
Eg:
Eg:
MYSQL GROUP BY WITH HAVING
Eg:
Aggregate Functions & NULL- Perform operation over set of values
Consider a table emp1 having following records
Aggregate function will not take NULL into consideration.
NULL is simply ignored by all the aggregate functions.
1. COUNT() - This function is used to count the number of values in a column.
Eg:
2. MAX() - This function is used to get the maximum value from a column.
Eg:
3. MIN() - This function is used to get the minimum value from a column.
Eg:
4. AVG() - This function is used to get the average value of a numeric column.
Eg:
Types of JOIN
Following are the types of JOIN that we can use in SQL:
Equi Join or Inner Join
Natural Join
Left Join
Right Join
Consider the table(s) Suppliers and Orders.
Syntax:
[or]
Eg:
Query:
[or]
Syntax:
Query:
Syntax:
Query:
Right Join :
The RIGHT JOIN is a particular type of join that selects rows from both left and
right tables that are matched, plus all rows from the right table (Table B) even with
no matching rows found in the left table (Table A).
Syntax:
• Porting of data from one dbms to other is easily possible as it support large
range of APIs for various databases.
Here we are using mysql as back end database because of it is open source,free
and portable and widely used. Any one of mysql-connector or MySQLdb can be
used for database programming.
mysql-connector
MySQL-Connector enables Python programs to access MySQL databases, using
an API that is compliant with the Python Database API Specification v2.0 (PEP
249). It is written in pure Python and does not have any dependencies except for
the Python Standard Library.
Steps to use mysql-connector
1. Download Mysql API ,exe file and install it
2. Install Mysql-Python Connector (Open command prompt and execute
command) >pip install mysql-connector
3. Now connect Mysql server using python
4. Write python statement in python shell import mysql.connector
If no error message is shown means mysql connector is properly installed
Cursor object :
The MySQLCursor class instantiates objects that can execute operations such as
SQL statements. Cursor objects interact with the MySQL server using a
MySQLConnection object.
On successful execution of above program a table named student with four fields
rollno,name,age will be created in school database.
We can check student table in mysql shell also,if required.
MySQLCursor.fetchall() Method
The method fetches all (or all remaining) rows of a query result set and
returns a list of tuples. If no more rows are available, it returns an empty list.
How to fetch one record of a table at run time
MySQLCursor.fetchone() Method
This method retrieves the next row of a query result set and returns a single
sequence, or None if no more rows are available. By default, the returned tuple
consists of data returned by the MySQL server, converted to Python objects.
buffered=True
We have used my_cursor as buffered cursor. my_cursor =
my_connect.cursor(buffered=True)
This type cursor fetches rows and buffers them after getting output from MySQL
database. We can use such cursor as iterator. There is no point in using buffered
cursor for single fetching of rows.If we don’t use buffered cursor then we will
get -1 as output from rowcount.