CS - Full SQL
CS - Full SQL
YES WE CS
DATABASE
Advantages of Database
● Reduces redundancy.
● Controls data inconsistency.
● Facilitates data sharing.
● Ensures data security
● Integrity can be maintained.
S U R E S H B A B U P , 9787640719 YES WE CS
DBMS (Data Base Management System)
• DBMS(DataBase Management System) is a software which helps us
to establish and implement a database.
• It consists of a group of programs which helps in storing, retrieving
and manipulating data while considering appropriate safety
measures.
S U R E S H B A B U P , 9787640719 YES WE CS
DATABASE MODEL
S U R E S H B A B U P , 9787640719 YES WE CS
YES WE CS
COMPARISON OF DATABASE MODEL
S U R E S H B A B U P , 9787640719 YES WE CS
RELATIONAL DATA MODEL
RELATIONAL DATABASE
• A relational database is a collective set of multiple data sets organized by tables,
records and columns.
• Relational database establish a well-defined relationship between database
tables.
• Tables communicate and share information, which facilitates data searcheability,
organization and reporting.
• A Relational database use Structured Query Language (SQL), which is a standard
user application that provides an easy programming interface for database
interaction.
S U R E S H B A B U P , 9787640719 YES WE CS
RELATIONAL DATA MODEL
S U R E S H B A B U P , 9787640719 WE CS
RELATIONAL DATA MODEL TERMINOLOGIES
1. Relation : A table storing logically related data.
2. Domain : Pool of values from which the actual values appearing in a given column
are drawn.
3. Tuple : row or record
4. Attribute : Column or field
5. Degree : no. of columns/attributes.
6. Cardinality : no. of rows/tuples.
7. View : A virtual table that doesn’t really exist, but it’s used to view the base table or
tables derived from one or more base tables.
8. Primary key : One or more attributes that can uniquely identify tuples within the
table.
9. Candidate key : All attribute combinations inside a table that are candidates to
become primary key.
10. Alternate key : A candidate key which is not primary key.
11.Foreign key : A non-key attribute whose values are derived from the primary key of
some other table, is called foreign key in its current table.
S U R E S H B A B U P , 9787640719 YES WE CS
KEYS IN A DATABASE
K e y plays an important role in relational database. It is used for
identifying unique rows from table & establishes relationship among
tables on need.
S U R E S H B A B U P , 9787640719 YES WE CS
SQL
• SQL is an acronym of Structured Query Language.
• It is a standard language developed and used for accessing and
modifying relational databases.
• The SQL language was originally developed at the IBM research
laboratory in SanJosé, in connection with a project developing a
prototype for a relational database management system called
System R in the early 70s.
• SQL is being used by many database management systems.
• Some of them are:
MySQL, PostgreSQL, Oracle, SQLite, Microsoft SQLServer
S U R E S H B A B U P , 9787640719 YES WE CS
ADVANTAGES OF
• Interactive Language- This language can be used for communicating with the
databases and receive answers to the complex questions in seconds.
• Multiple data views- The users can make different views of database structure
and databases for the different users.
• Portability- SQL can be used in the program in PCs, servers, laptops, and even
some of the mobile phones and even on different dbms softwares.
• No coding needed- It is very easy to manage the database systems without any
need to write the substantial amount of code by using the standard SQL.
• Well defined standards- Long established are used by the SQL databases that
is being used by ISO and ANSI. There are no standards adhered by the non-SQL
databases.
S U R E S H B A B U P , 9787640719 YES WE CS
MySQL
• MySQL is currently the most popular open source database software.
• It is a multi-user, multi-threaded database management system.
• MySQL is especially popular on the web. It is one of the parts of the
very popular LAMP platform( Linux, Apache, MySQL and PHP) or
WIMP platform (Windows, Apache, MySQL and PHP).
• MySQL AB was founded by Michael Widenius(Monty), David Axmark
and Allan Larsson in Sweden in year 1995.
Name of Dolphin -Logo
Name of Michael “sakila”
Widenius's daughter
(My)
Structured Query Language
(SQL)
S U R E S H B A B U P , 9787640719 YES WE CS
TYPES OF SQL COMMANDS
1. Data Definition Language (DDL): is the part of SQL that allows a database user to create
and restructure database object.
– CREATE : Creates a database, new table, a view of a table, index, cursor and trigger
or other object in database.
– ALTER : Modifies an existing database object, such as a table, view and index etc.
– DROP : Deletes an entire table, a view of a table or other object in the database.
– RENAME: Used to renaming table, view etc
– TRUNCATE: Used to delete record permanently from a table.
2. Data Manipulation Language (DML): is the part of SQL used to manipulate data within
objects of a relational database.
– INSERT: used to insert a record on the table.
– UPDATE: modification of records in the table.
– DELETE: used to remove records in the table.
3. Data Query Language (DQL): A query language is a language in which a user requests
information from a table.
– SELECT: used to retrieve certain records from one or more table.
S U R E S H B A B U P , 9787640719 YES WE CS
TYPES OF SQL COMMANDS
4. Data Control Language (DCL): SQL allow you to control access to data within the
database. DCL normally used to create objects related to user access and also control
the distribution of privileges among users.
– GRANT: is used to grant both system-level and object-level privileges to an
existing database user accounts.
– REVOKE: To removes privileges that have been granted to database users.
5. Transaction Control Language (TCL): commands are used to manage transactions in
database. These are used to manage the changes made by DML Statement.
‾ COMMIT: Used to permanently save any database transactions.
‾ ROLLBACK: Used restores the database to last committed state.
‾ SAVEPOINT: Used to temporarily save a transaction.
S U R E S H B A B U P , 9787640719 YES WE CS
DATA TYPES OF MySQL
S U R E S H B A B U P , 9787640719 YES WE CS
DATABASE COMMANDS
• Show databases;
– It will display list of databases available in the system
S U R E S H B A B U P , 9787640719 YES WE CS
DATABASE COMMANDS
• USE databasename;
– It is used to use the particular database
– Ex. USE student;
S U R E S H B A B U P , 9787640719 YES WE CS
COMMANDS & OPERATORS
S U R E S H B A B U P , 9787640719 YES WE CS
TABLE COMMANDS
CREATE COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
INTEGRITY CONSTRAINTS
Integrity Constraints are the protocols that a table's data columns must
follow. These are used to restrict the types of information that can be
entered into a table. This means that the data in the database is accurate
and reliable.
Types of constraints:
1. The NOT NULL constraint prevents a column from having a NULL value.
2. When no value is defined for a column, the DEFAULT Constraint provides
a default value.
3. A UNIQUE Constraint ensures that any value in a column is unique.
4. The CHECK Constraint - All the rows must satisfy this rule.
5. Each row/record in a database table is uniquely identified by the
PRIMARY KEY.
6. A FOREIGN KEY recognizes a row/record in any database table uniquely.
S U R E S H B A B U P , 9787640719 YES WE CS
INTEGRITY CONSTRAINTS
S U R E S H B A B U P , 9787640719 YES WE CS
INTEGRITY CONSTRAINTS
mysql> create table student(name char(20),rollno int PRIMARY KEY, marks int);
S U R E S H B A B U P , 9787640719 YES WE CS
INSERT COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
INSERT COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
SELECT COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
SELECT COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
SELECT COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
SELECT COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
SELECT COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
SELECT COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
SELECT COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
SELECT COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
LIKE (Pattern matching)
It allows for a comparison of one string value with another string value which is
not identical. We use two wildcards (% and _ ) for this purpose:
• %: The percent sign matches any string.
• _: The underscore matches any single character.
SELECT * FROM emp WHERE ename LIKE ‘A%’;
It gives the details of those employees whose name starts from character A.
Example: Anu
S U R E S H B A B U P , 9787640719 YES WE CS
UPDATE COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
FOREIGN KEY
mysql> create table customer (sid int primary key,
score int, address varchar(25));
mysql> create table order1 (oid int primary key,sid int, foreign key (sid)
references customer(sid) );
S U R E S H B A B U P , 9787640719 YES WE CS
SQL JOINS
S U R E S H B A B U P , 9787640719 YES WE CS
CARTESIAN PRODUCT
S U R E S H B A B U P , 9787640719 YES WE CS
CARTESIAN PRODUCT
S U R E S H B A B U P , 9787640719 YES WE CS
EQUI JOIN
S U R E S H B A B U P , 9787640719 YES WE CS
NATURAL JOIN
S U R E S H B A B U P , 9787640719 YES WE CS
DROP COMMAND
S U R E S H B A B U P , 9787640719 YES WE CS
ALTER COMMAND
• ADDING A COLUMN
S U R E S H B A B U P , 9787640719 YES WE CS
ALTER COMMAND
• MODIFY A COLUMN
S U R E S H B A B U P , 9787640719 YES WE CS
ALTER COMMAND
• DELETE A COLUMN
S U R E S H B A B U P , 9787640719 YES WE CS
BASICS OF DATABASE CONNECTIVITY
1. Python MySQL
Python can be used in database applications.
One of the most popular databases is MySQL.
2. MySQL Database
To be able to experiment with the code examples , you should have MySQL installed on
your computer.
You can download a free MySQL database at https://www.mysql.com/downloads/.
S U R E S H B A B U P , 9787640719 YES WE CS
BASICS OF DATABASE CONNECTIVITY
3.Installation of MySQL Driver
• Python needs a MySQL driver to access the MySQL database.
• In this topic we will use the driver "MySQL Connector".
• It is recommend that to use PIP to install "MySQL Connector".
• PIP is most likely already installed in your Python environment.
• Navigate your command line to the location of PIP, and type
the following:
Download and install "MySQL Connector":
S U R E S H B A B U P , 9787640719 YES WE CS
PYTHON – MySQL CONNECTIVITY
STEPS TO BE FOLLOWED TO CONNECT MYSQL WITH FUNCTIONS
1. Start Python
2. Import module in the program mysql.connector
3. Connection establishment using connect function
mysql.connector.connect()
4. Create a cursor object for the program cursor()
5. Execute the query cursor.execute()
6. Fetch the result from MySQL using functions
fetchone() fetchall() fetchmany() rowcount()
7. Close the environment close()
S U R E S H B A B U P , 9787640719 YES WE CS
PYTHON – MySQL CONNECTIVITY
S U R E S H B A B U P , 9787640719 YES WE CS
PYTHON – MySQL CONNECTIVITY
Program for testing MySQL Connector
To test if the installation was successful, or if you already have
"MySQL Connector" installed, create a Python page with the
following content:
import mysql.connector
S U R E S H B A B U P , 9787640719 YES WE CS
PYTHON – MySQL CONNECTIVITY
Program for Creating Connection
Start by creating a connection to the database.
Use the username and password from your MySQL database:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password ="1234"
)
print(mydb)
mydb.close()
Now you can start querying the database using SQL statements.
S U R E S H B A B U P , 9787640719 YES WE CS
PYTHON – MySQL CONNECTIVITY
Creating a database named "SCHOOL":
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="1234"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE SCHOOL")
mydb.close()
If the above code was executed with no errors, you have successfully created a
database.
S U R E S H B A B U P , 9787640719 YES WE CS
PYTHON – MySQL CONNECTIVITY
Check if Database Exists
You can check if a database exist by listing all databases in your
system by using the "SHOW DATABASES" statement:
1. Start Python
import mysql.connector 2. mysql.connector
mydb = mysql.connector.connect( 3. mysql.connector.connect()
host="localhost",
user="root",
password="1234"
)
mycursor = mydb.cursor() 4. cursor()
mycursor.execute("SHOW DATABASES") 5. cursor.execute()
for x in mycursor: 6. fetchone() / fetchall() /
print(x) fetchmany() / rowcount()
mydb.close() 7. close()
S U R E S H B A B U P , 9787640719 YES WE CS
PYTHON – MySQL CONNECTIVITY
to create table in Mysql database.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",pa
sswd="1234",database="school")
mycursor=mydb.cursor()
mycursor.execute("create table student(rollno int(3) primary
key,name varchar(20),age int(2))")
mydb.close()
S U R E S H B A B U P , 9787640719 YES WE CS
PYTHON – MySQL CONNECTIVITY
to insert data in Mysql database.
import mysql.connector
mydb = mysql.connector.connect( host="localhost",
user="root",password="sql123", database="CLASS12")
mycursor = mydb.cursor()
mycursor.execute("INSERT INTO student VALUES(101,’BABU’, 18)")
mydb.commit()
print(mycursor.rowcount, "record inserted.")
mydb.close()
S U R E S H B A B U P , 9787640719 YES WE CS
PYTHON – MySQL CONNECTIVITY
to display data from Mysql database
import mysql.connector
mydb = mysql.connector.connect(host="localhost",
user="root", password="sql123",
database="CLASS12")
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM Information")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
mydb.close()
S U R E S H B A B U P , 9787640719 YES WE CS
PYTHON – MySQL CONNECTIVITY
to update data in Mysql database.
import mysql.connector
mydb = mysql.connector.connect(host="localhost",
user="root", password="1234", database="school")
mycursor = mydb.cursor()
mycursor.execute("UPDATE student SET age = '17' WHERE
name = 'Ravi' ")
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
mydb.close()
S U R E S H B A B U P , 9787640719 YES WE CS
PYTHON – MySQL CONNECTIVITY
to delete data in Mysql database.
import mysql.connector
mydb = mysql.connector.connect(host="localhost",
user="root", password="1234", database="school")
mycursor = mydb.cursor()
mycursor.execute(" DELETE FROM STUDENT WHERE
NAME=‘RAVI’ ")
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
mydb.close()
S U R E S H B A B U P , 9787640719 YES WE CS