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

PDF
Python my sql database connection
PPTX
Database connectivity in python
PDF
MySQL User Conference 2009: Python and MySQL
PDF
Python in the database
PDF
MySQL for beginners
PPTX
MySql:Introduction
PDF
Lab1-DB-Cassandra
PPTX
TDD in the wild
Python my sql database connection
Database connectivity in python
MySQL User Conference 2009: Python and MySQL
Python in the database
MySQL for beginners
MySql:Introduction
Lab1-DB-Cassandra
TDD in the wild

What's hot (20)

PPTX
Python database interfaces
PPTX
MySql:Basics
PDF
Store and Process Big Data with Hadoop and Cassandra
DOCX
Php mysql connectivity
PDF
C* Summit EU 2013: Cassandra Made Simple with CQL Drivers and DevCenter
PDF
Lab2-DB-Mongodb
PPTX
Html web sql database
PDF
Lodash js
PPTX
Psycopg2 - Connect to PostgreSQL using Python Script
PPTX
Sequelize
PDF
Struts database access
PDF
Underscore and Backbone Models
KEY
PPTX
Create & Execute First Hadoop MapReduce Project in.pptx
PPTX
Introduction databases and MYSQL
PPTX
Cookies in php
PDF
Euruko 2009 - DataObjects
PDF
Apache Cassandra Lesson: Data Modelling and CQL3
PDF
Cucumber on the JVM with Groovy
Python database interfaces
MySql:Basics
Store and Process Big Data with Hadoop and Cassandra
Php mysql connectivity
C* Summit EU 2013: Cassandra Made Simple with CQL Drivers and DevCenter
Lab2-DB-Mongodb
Html web sql database
Lodash js
Psycopg2 - Connect to PostgreSQL using Python Script
Sequelize
Struts database access
Underscore and Backbone Models
Create & Execute First Hadoop MapReduce Project in.pptx
Introduction databases and MYSQL
Cookies in php
Euruko 2009 - DataObjects
Apache Cassandra Lesson: Data Modelling and CQL3
Cucumber on the JVM with Groovy
Ad

Similar to Python my SQL - create table (20)

PPTX
Pyhton with Mysql to perform CRUD operations.pptx
PPTX
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
PPTX
python db connection samples and program
PPTX
PYTHON_DATABASE_CONNECTIVITY.pptxPYTHON_DATABASE
PPTX
Mysql python
PDF
Mysql python
PPTX
PythonDatabaseAPI -Presentation for Database
PPTX
Session 2 - "MySQL Basics & Schema Design"
PPTX
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
PPTX
015. Interface Python with sql interface ppt class 12
PPTX
Interface Python with MySQLwedgvwewefwefwe.pptx
PDF
Python my SQL - create database
PDF
Interface Python with MySQL.pdf
PPTX
interface with mysql.pptx
PPTX
Using Mysql.pptx
PPTX
SQL-Connectivity python for beginners easy explanation with concepts and outp...
PPTX
Interface Python with MySQL connectivity.pptx
PDF
sql_data.pdf
Pyhton with Mysql to perform CRUD operations.pptx
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
python db connection samples and program
PYTHON_DATABASE_CONNECTIVITY.pptxPYTHON_DATABASE
Mysql python
Mysql python
PythonDatabaseAPI -Presentation for Database
Session 2 - "MySQL Basics & Schema Design"
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
015. Interface Python with sql interface ppt class 12
Interface Python with MySQLwedgvwewefwefwe.pptx
Python my SQL - create database
Interface Python with MySQL.pdf
interface with mysql.pptx
Using Mysql.pptx
SQL-Connectivity python for beginners easy explanation with concepts and outp...
Interface Python with MySQL connectivity.pptx
sql_data.pdf
Ad

More from Learnbay Datascience (20)

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

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PPTX
Pharma ospi slides which help in ospi learning
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
From loneliness to social connection charting
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Business Ethics Teaching Materials for college
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
STATICS OF THE RIGID BODIES Hibbelers.pdf
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Pharma ospi slides which help in ospi learning
Anesthesia in Laparoscopic Surgery in India
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Renaissance Architecture: A Journey from Faith to Humanism
human mycosis Human fungal infections are called human mycosis..pptx
From loneliness to social connection charting
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Business Ethics Teaching Materials for college
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
How to Manage Starshipit in Odoo 18 - Odoo Slides
Week 4 Term 3 Study Techniques revisited.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Revamp in MTO Odoo 18 Inventory - Odoo Slides
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx

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