SlideShare a Scribd company logo
2
Most read
3
Most read
Swipe
Python MySQL - Create Table
The CREATE TABLE statement is used to create
tables in MYSQL database.
Here, you need to specify the name of the table
and, definition (name and datatype) of each
column.
Python MySQL - Create Table
Syntax
Following is the syntax to create a table in
MySQL:-
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,.....
columnN datatype,);
Following query creates a table named EMPLOYEE
in MySQL with five columns namely, FIRST_NAME,
LAST_NAME, AGE, SEX and, INCOME.
Example
mysql> CREATE TABLE EMPLOYEE(
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT
);Query OK, 0 rows affected (0.42 sec)
The DESC statement gives you the description of
the specified table. Using this you can verify if the
table has been created or not as shown below
Example
Creating a table in MySQL using python
The method named execute() (invoked on the
cursor object) accepts two variables:-
A String value representing the query to be
executed.
An optional args parameter which can be a
tuple, list or, dictionary, representing the
parameters of the query (values of the place
holders).
It returns an integer value representing the
number of rows effected by the query.
Once a database connection is established, you
can create tables by passing the CREATE TABLE
query to the execute() method.
In short, to create a table using python 7minus;
Import mysql.connector package.
Create a connection object using the
mysql.connector.connect() method, by passing
the user name, password, host (optional
default: localhost) and, database (optional) as
parameters to it.
Create a cursor object by invoking the cursor()
method on the connection object created
above.
Then, execute the CREATE TABLE statement by
passing it as a parameter to the execute()
method.
Example
Following example creates a table named
Employee in the database mydb.
import mysql.connector
#establishing the connection
conn = mysql.connector.connect(
user='root', password='password', host='127.0.0.1',
database='mydb')#Creating a cursor object using the cursor()
method
cursor = conn.cursor()#Dropping EMPLOYEE table if already
exists.
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
#Creating table as per requirement
sql ='''CREATE TABLE EMPLOYEE(
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT
)'''
cursor.execute(sql)#Closing the connection
conn.close()
Python MySQL - Insert Data
Python MySQL - Insert Data
Python MySQL - Select Data
Topics for next Post
Stay Tuned with

More Related Content

What's hot (20)

PPTX
Python database interfaces
Mohammad Javad Beheshtian
 
PPTX
MySql:Basics
DataminingTools Inc
 
PDF
Store and Process Big Data with Hadoop and Cassandra
Deependra Ariyadewa
 
DOCX
Php mysql connectivity
abhikwb
 
PDF
C* Summit EU 2013: Cassandra Made Simple with CQL Drivers and DevCenter
DataStax Academy
 
PDF
Lab2-DB-Mongodb
Lilia Sfaxi
 
PPTX
Html web sql database
AbhishekMondal42
 
PDF
Lodash js
LearningTech
 
PPTX
Psycopg2 - Connect to PostgreSQL using Python Script
Survey Department
 
PPTX
Sequelize
Tarek Raihan
 
PDF
Struts database access
Abass Ndiaye
 
PDF
Underscore and Backbone Models
Kentucky JavaScript Users Group
 
KEY
Sequel
Stoyan Zhekov
 
PPTX
Create & Execute First Hadoop MapReduce Project in.pptx
vishal choudhary
 
PPTX
Introduction databases and MYSQL
Naeem Junejo
 
PPTX
Cookies in php
ArunaDevi63
 
PDF
Euruko 2009 - DataObjects
Dirkjan Bussink
 
PDF
Apache Cassandra Lesson: Data Modelling and CQL3
Markus Klems
 
PDF
Cucumber on the JVM with Groovy
Richard Paul
 
Python database interfaces
Mohammad Javad Beheshtian
 
MySql:Basics
DataminingTools Inc
 
Store and Process Big Data with Hadoop and Cassandra
Deependra Ariyadewa
 
Php mysql connectivity
abhikwb
 
C* Summit EU 2013: Cassandra Made Simple with CQL Drivers and DevCenter
DataStax Academy
 
Lab2-DB-Mongodb
Lilia Sfaxi
 
Html web sql database
AbhishekMondal42
 
Lodash js
LearningTech
 
Psycopg2 - Connect to PostgreSQL using Python Script
Survey Department
 
Sequelize
Tarek Raihan
 
Struts database access
Abass Ndiaye
 
Underscore and Backbone Models
Kentucky JavaScript Users Group
 
Create & Execute First Hadoop MapReduce Project in.pptx
vishal choudhary
 
Introduction databases and MYSQL
Naeem Junejo
 
Cookies in php
ArunaDevi63
 
Euruko 2009 - DataObjects
Dirkjan Bussink
 
Apache Cassandra Lesson: Data Modelling and CQL3
Markus Klems
 
Cucumber on the JVM with Groovy
Richard Paul
 

Similar to Python my SQL - create table (20)

PPTX
Python with MySql.pptx
Ramakrishna Reddy Bijjam
 
PPTX
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
HistoryScienceWorld
 
PPTX
python db connection samples and program
usha raj
 
PPTX
PYTHON_DATABASE_CONNECTIVITY.pptxPYTHON_DATABASE
usha raj
 
PPTX
Mysql python
Janu Jahnavi
 
PDF
Mysql python
Janu Jahnavi
 
PPTX
PythonDatabaseAPI -Presentation for Database
dharawagh9999
 
PPTX
Session 2 - "MySQL Basics & Schema Design"
LogaRajeshwaranKarth
 
PPTX
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
teachersduniya.com
 
PPTX
015. Interface Python with sql interface ppt class 12
Shuvanth
 
PPTX
Interface Python with MySQLwedgvwewefwefwe.pptx
AyushKumarXIthclass
 
PDF
Python my SQL - create database
Learnbay Datascience
 
PDF
Interface Python with MySQL.pdf
DhirajKumarBiswal
 
PPTX
interface with mysql.pptx
KRITIKAOJHA11
 
PPTX
Using Mysql.pptx
StephenEfange3
 
PPTX
SQL-Connectivity python for beginners easy explanation with concepts and outp...
harshitagrawal2608
 
PPTX
Interface Python with MySQL connectivity.pptx
BEENAHASSINA1
 
PDF
sql_data.pdf
VandanaGoyal21
 
Python with MySql.pptx
Ramakrishna Reddy Bijjam
 
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
HistoryScienceWorld
 
python db connection samples and program
usha raj
 
PYTHON_DATABASE_CONNECTIVITY.pptxPYTHON_DATABASE
usha raj
 
Mysql python
Janu Jahnavi
 
Mysql python
Janu Jahnavi
 
PythonDatabaseAPI -Presentation for Database
dharawagh9999
 
Session 2 - "MySQL Basics & Schema Design"
LogaRajeshwaranKarth
 
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
teachersduniya.com
 
015. Interface Python with sql interface ppt class 12
Shuvanth
 
Interface Python with MySQLwedgvwewefwefwe.pptx
AyushKumarXIthclass
 
Python my SQL - create database
Learnbay Datascience
 
Interface Python with MySQL.pdf
DhirajKumarBiswal
 
interface with mysql.pptx
KRITIKAOJHA11
 
Using Mysql.pptx
StephenEfange3
 
SQL-Connectivity python for beginners easy explanation with concepts and outp...
harshitagrawal2608
 
Interface Python with MySQL connectivity.pptx
BEENAHASSINA1
 
sql_data.pdf
VandanaGoyal21
 
Ad

More from Learnbay Datascience (20)

PDF
Top data science projects
Learnbay Datascience
 
PDF
Python - mySOL
Learnbay Datascience
 
PDF
AI - Issues and Terminology
Learnbay Datascience
 
PDF
AI - Fuzzy Logic Systems
Learnbay Datascience
 
PDF
AI - working of an ns
Learnbay Datascience
 
PDF
Artificial Intelligence- Neural Networks
Learnbay Datascience
 
PDF
AI - Robotics
Learnbay Datascience
 
PDF
Applications of expert system
Learnbay Datascience
 
PDF
Components of expert systems
Learnbay Datascience
 
PDF
Artificial intelligence - expert systems
Learnbay Datascience
 
PDF
AI - natural language processing
Learnbay Datascience
 
PDF
Ai popular search algorithms
Learnbay Datascience
 
PDF
AI - Agents & Environments
Learnbay Datascience
 
PDF
Artificial intelligence - research areas
Learnbay Datascience
 
PDF
Artificial intelligence composed
Learnbay Datascience
 
PDF
Artificial intelligence intelligent systems
Learnbay Datascience
 
PDF
Applications of ai
Learnbay Datascience
 
PDF
Tableau - waterfall charts
Learnbay Datascience
 
PDF
Tableau motion charts
Learnbay Datascience
 
PDF
Tableau histogram
Learnbay Datascience
 
Top data science projects
Learnbay Datascience
 
Python - mySOL
Learnbay Datascience
 
AI - Issues and Terminology
Learnbay Datascience
 
AI - Fuzzy Logic Systems
Learnbay Datascience
 
AI - working of an ns
Learnbay Datascience
 
Artificial Intelligence- Neural Networks
Learnbay Datascience
 
AI - Robotics
Learnbay Datascience
 
Applications of expert system
Learnbay Datascience
 
Components of expert systems
Learnbay Datascience
 
Artificial intelligence - expert systems
Learnbay Datascience
 
AI - natural language processing
Learnbay Datascience
 
Ai popular search algorithms
Learnbay Datascience
 
AI - Agents & Environments
Learnbay Datascience
 
Artificial intelligence - research areas
Learnbay Datascience
 
Artificial intelligence composed
Learnbay Datascience
 
Artificial intelligence intelligent systems
Learnbay Datascience
 
Applications of ai
Learnbay Datascience
 
Tableau - waterfall charts
Learnbay Datascience
 
Tableau motion charts
Learnbay Datascience
 
Tableau histogram
Learnbay Datascience
 
Ad

Recently uploaded (20)

PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
epi editorial commitee meeting presentation
MIPLM
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
epi editorial commitee meeting presentation
MIPLM
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Introduction to Indian Writing in English
Trushali Dodiya
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 

Python my SQL - create table

  • 1. Swipe Python MySQL - Create Table
  • 2. The CREATE TABLE statement is used to create tables in MYSQL database. Here, you need to specify the name of the table and, definition (name and datatype) of each column. Python MySQL - Create Table Syntax Following is the syntax to create a table in MySQL:- CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype,..... columnN datatype,);
  • 3. Following query creates a table named EMPLOYEE in MySQL with five columns namely, FIRST_NAME, LAST_NAME, AGE, SEX and, INCOME. Example mysql> CREATE TABLE EMPLOYEE( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT );Query OK, 0 rows affected (0.42 sec)
  • 4. The DESC statement gives you the description of the specified table. Using this you can verify if the table has been created or not as shown below Example
  • 5. Creating a table in MySQL using python The method named execute() (invoked on the cursor object) accepts two variables:- A String value representing the query to be executed. An optional args parameter which can be a tuple, list or, dictionary, representing the parameters of the query (values of the place holders). It returns an integer value representing the number of rows effected by the query.
  • 6. Once a database connection is established, you can create tables by passing the CREATE TABLE query to the execute() method. In short, to create a table using python 7minus; Import mysql.connector package. Create a connection object using the mysql.connector.connect() method, by passing the user name, password, host (optional default: localhost) and, database (optional) as parameters to it. Create a cursor object by invoking the cursor() method on the connection object created above. Then, execute the CREATE TABLE statement by passing it as a parameter to the execute() method.
  • 7. Example Following example creates a table named Employee in the database mydb. import mysql.connector #establishing the connection conn = mysql.connector.connect( user='root', password='password', host='127.0.0.1', database='mydb')#Creating a cursor object using the cursor() method cursor = conn.cursor()#Dropping EMPLOYEE table if already exists. cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
  • 8. #Creating table as per requirement sql ='''CREATE TABLE EMPLOYEE( FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), INCOME FLOAT )''' cursor.execute(sql)#Closing the connection conn.close()
  • 9. Python MySQL - Insert Data Python MySQL - Insert Data Python MySQL - Select Data Topics for next Post Stay Tuned with