The Wayback Machine - https://web.archive.org/web/20241001064118/https://www.geeksforgeeks.org/python-mysql-create-table/
Open In App

Python: MySQL Create Table

Last Updated : 10 Jan, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify the same command.
Installation 
Follow the below-mentioned process for installing the dependencies for python MySQL

  1. Navigate to the python script directory using the command prompt.
  2. Execute the command 
     
pip install mysql-connector

Python Mysql Connector Module Methods
 

1. connect(): This function is used for establishing a connection with the MySQL server. The following are the arguments that are used to initiate a connection:

  1. user: User name associated with the MySQL server used to authenticate the connection
  2. password: Password associated with the user name for authentication
  3. database: Data base in the MySQL for creating the Table

2. cursor(): Cursor is the workspace created in the system memory when the SQL command is executed. This memory is temporary and the cursor connection is bounded for the entire session/lifetime and the commands are executed 
 

3. execute(): The execute function takes a SQL query as an argument and executes. A query is an SQL command which is used to create, insert, retrieve, update, delete etc. 
 

 

Data Base

The database is an organization of information structured into multiple tables. Databases are organized in such a way so that manipulating the data is easy i.e Creating, inserting, updating, and deleting etc.
SQL command for Creating Database : 
 

CREATE DATABASE ;

Example: Consider the below example for creating a database in MySQL(Ex: college)
 

python




# Python code for creating Database
# Host: It is the server name. It will be
# "localhost" if you are using localhost database
 
import mysql.connector as SQLC
# Establishing connection with the SQL
 
DataBase = SQLC.connect(
  host ="server name",
  user ="user name",
  password ="password"
)
# Cursor to the database
Cursor = DataBase.cursor()
 
Cursor.execute("CREATE DATABASE College")
print("College Data base is created")


Output :
 

College Data base is created

 

Image

 

Table

 

  1. The table is a collection of data organized in the form of rows and columns. Table is present within a database.
  2. Rows are also called tuples
  3. Columns are called the attributes of the table

SQL command for Creating Table : 
 

CREATE TABLE
(
     column_name_1 column_Data_type, 
     column_name_2 column_Data_type, 
     :
     :
     column_name_n column_Data_type
);

 

SQL Data types

Data types are used for defining the type of data that will be stored in the cell of the table. 
Different Types of the Datatypes 
 

  1. Numeric
  2. Character/String
  3. Date/time.
  4. Unicode Character/String
  5. Binary

Apart from the above-mentioned datatypes, there are other miscellaneous data types in MySQL that include datatypes of CLOB, BLOB, JSON, XML.
Consider the below-mentioned python code for creating a table of the “student” which contains two Columns Name, Roll number in the database “college” previously created.
 

python




# Python code for creating Table in the Database
# Host: It is the server name. It will be "localhost"
# if you are using localhost database
 
import mysql.connectors as SQLC
def CreateTable():
      
     # Connecting To the Database in Localhost
     DataBase = SQLC.connect(
                 host ="server name",
                 user ="user name",
                 password ="password",
                 database ="College"
               )
 
     # Cursor to the database
     Cursor = DataBase.cursor()
 
     # Query for Creating the table
     # The student table contains two columns Name and
     # Name of data type varchar i.e to store string
     # and Roll number of the integer data type.
     TableName ="CREATE TABLE Student
                (
                    Name VARCHAR(255),
                    Roll_no int
                );"
 
     Cursor.execute(TableName)
     print("Student Table is Created in the Database")
     return
 
# Calling CreateTable function
CreateTable()


Output :
 

Student Table is Created in the Database

 



Previous Article
Next Article

Similar Reads

Connect MySQL database using MySQL-Connector Python
While working with Python we need to work with databases, they may be of different types like MySQL, SQLite, NoSQL, etc. In this article, we will be looking forward to how to connect MySQL databases using MySQL Connector/Python.MySQL Connector module of Python is used to connect MySQL databases with the Python programs, it does that using the Pytho
2 min read
How to Get the Minimum and maximum Value of a Column of a MySQL Table Using Python?
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to get the Minimum and Maximum Value of a Column of a MySQL Table Using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an
2 min read
Python MySQL - Insert into Table
MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify the same command. Note: Before we insert data into
3 min read
Python MySQL - Drop Table
A connector is employed when we have to use MySQL with other programming languages. The work of MySQL-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server. Drop Table Command Drop command affects the structure of the table and not data. It is u
2 min read
How to Count the Number of Rows in a MySQL Table in Python?
MySQL server is an open-source relational database management system which is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a web server we use various modules in Python such a
2 min read
How to Print Out All Rows of a MySQL Table in Python?
MySQL server is an open-source relational database management system which is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a web server(here XAMPP) we use various modules in P
2 min read
How to Copy a Table in MySQL Using Python?
In this article, we will create a table in MySQL and will create a copy of that table using Python. We will copy the entire table, including all the columns and the definition of the columns, as well as all rows of data in the table. To connect to MySQL database using python, we need PyMySql module. The cursor class allows python to execute SQL com
3 min read
How to Compute the Average of a Column of a MySQL Table Using Python?
A MySQL connector is needed to generate a connection between Python and the MySQL server. Here we will import mysql.connector library to get the average of the specified column in the given database. If you need to know how to install MySQL, see How to Install MySQL in Python 3. Average Function of SQL SQL AVG() function returns the average of valu
2 min read
Grant MySQL table and column permissions using Python
MySQL server is an open-source relational database management system that is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a web server, we use various modules in Python such a
2 min read
How to Compute the Sum of All Rows of a Column of a MySQL Table Using Python?
MySQL server is an open-source relational database management system that is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a web server, we use various modules in Python such a
2 min read
How to Perform Arithmetic Across Columns of a MySQL Table Using Python?
Python is a dynamic language, and Python applications can be integrated with database servers. The module used to access a MySQL database from Python is MySQL Connector Python. PyMySQL, MySQLDB and mysqlclient are other Python modules to communicate with a MySQL database server in Python. However, we will use MySQL Connector Python in this article
5 min read
How to Add a Column to a MySQL Table in Python?
Prerequisite: Python: MySQL Create Table Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicating with a MySQL database. ALTER statement of SQL With the ALTER statement, one can add, drop
4 min read
How to Rename a MySQL Table in Python?
MySQL Connector-Python module is an API in python used to communicate with a MySQL database server. It only requires the standard Python libraries and has no additional dependencies. There are various other modules in Python like PyMySQL and mysqlclient which can be used to access a database server. In this article, we will use MySQL Connector-Pyth
3 min read
How to Copy a Table Definition in MySQL Using Python?
Python requires an interface to access a database server. Python supports a wide range of interfaces to interact with various databases. To communicate with a MySQL database, MySQL Connector Python module, an API written purely in Python, is used. This module is self-sufficient meaning that it does not have dependencies and only requires the standa
6 min read
How to Concatenate Column Values of a MySQL Table Using Python?
Prerequisite: Python: MySQL Create Table In this article, we show how to concatenate column values of a MySQL table using Python. We use various data types in SQL Server to define data in a particular column appropriately. We might have requirements to concatenate data from multiple columns into a string Concatenating Columns means that one is merg
2 min read
How to Get the Size of a Table in MySQL using Python?
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to get the size of a table in MySQL using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicatin
2 min read
Deleting Element from Table in MySql using Python
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to get the size of a table in MySQL using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicatin
2 min read
How to insert values into MySQL server table using Python?
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to get the size of a table in MySQL using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python for communicatin
2 min read
Inserting data into a new column of an already existing table in MySQL using Python
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to Inserting data into a new column of an already existing table in MySQL using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector Python modul
2 min read
Retrieve Image and File stored as a BLOB from MySQL Table using Python
Prerequisites: MySQL server should be installed In this post, we will be talking about how we can store files like images, text files, and other file formats into a MySQL table from a python script. Sometimes, just like other information, we need to store images and files into our database and provide it the security equivalent to other data. In My
3 min read
Adding new enum column to an existing MySQL table using Python
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to add a new enum column to an existing MySQL table using Python. Python allows the integration of a wide range of database servers with applications. A database interface is required to access a database from Python. MySQL Connector-Python module is an API in python
1 min read
Python MySQL - Insert record if not exists in table
In this article, we will try to insert records and check if they EXISTS or not. The EXISTS condition in SQL is used to check if the result of a correlated nested query is empty (contains no tuples) or not. It can be used to INSERT, SELECT, UPDATE, or DELETE statements. Pre-requisite Connect MySQL Database to PythonPythonInsert Record If Not Exists
4 min read
Insert Multiple Rows in MySQL Table in Python
MySQL is an open-source, Relational Database Management System (RDBMS) that stores data in a structured format using rows and columns. It is software that enables users to create, manage, and manipulate databases. So this is used in various applications across a wide range of industries and domains. To run the MySQL server in our systems we need to
5 min read
Display the Pandas DataFrame in table style and border around the table and not around the rows
Let us see how to style a Pandas DataFrame such that it has a border around the table. We will be using the set_table_styles() method of the Styler class in the Pandas module. set_table_styles() Syntax : set_table_styles(self, table_styles) Parameters : table_styles : List, each individual table_style should be a dictionary with selector and props
1 min read
Python MySQL - Create Database
Python Database API ( Application Program Interface ) is the Database interface for the standard Python. This standard is adhered to by most Python Database interfaces. There are various Database servers supported by Python Database such as MySQL, GadFly, mSQL, PostgreSQL, Microsoft SQL Server 2000, Informix, Interbase, Oracle, Sybase etc. To conne
2 min read
Create MySQL Database Login Page in Python using Tkinter
Prerequisites: Python GUI – tkinter, Python MySQL – Select QueryTkinter is one of the Python libraries which contains many functions for the development of graphic user interface pages and windows. Login pages are important for the development of any kind of mobile or web application. This page is most essential for user authentication purposes.We
2 min read
How to Create a Pivot table with multiple indexes from an excel sheet using Pandas in Python?
The term Pivot Table can be defined as the Pandas function used to create a spreadsheet-style pivot table as a DataFrame. It can be created using the pivot_table() method. Syntax: pandas.pivot_table(data, index=None) Parameters: data : DataFrame index: column, Grouper, array, or list of the previous index: It is the feature that allows you to group
2 min read
Python SQLite - Create Table
In this article, we will discuss how can we create tables in the SQLite database from the Python program using the sqlite3 module. In SQLite database we use the following syntax to create a table: CREATE TABLE database_name.table_name( column1 datatype PRIMARY KEY(one or more columns), column2 datatype, column3 datatype, ..... columnN datatype ); N
1 min read
How to Create a Pivot Table in Python using Pandas?
A pivot table is a statistical table that summarizes a substantial table like a big dataset. It is part of data processing. This summary in pivot tables may include mean, median, sum, or other statistical terms. Pivot tables are originally associated with MS Excel but we can create a pivot table in Pandas using Python using the Pandas Dataframe piv
3 min read
PostgreSQL - Create table using Python
Creating tables in a PostgreSQL database using Python is a common task for developers working with databases. This process involves defining the structure of your data and ensuring that your database is optimized for efficient storage and retrieval. In this article, we will walk through the steps of creating tables in PostgreSQL using Python. Prere
3 min read
Article Tags :
Practice Tags :