The Wayback Machine - https://web.archive.org/web/20241127030601/https://www.geeksforgeeks.org/adding-new-enum-column-to-an-existing-mysql-table-using-python/
Open In App

Adding new enum column to an existing MySQL table using Python

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

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 for communicating with a MySQL database. 

Database table in use:

We are going to use geeks(Database name) database and table describing the salary.

Approach:

  • Import module.
  • Make a connection request with the database.
  • Create an object for the database cursor.
  • Execute the following MySQL query:
ALTER TABLE table_name ADD colunm_name  ENUM('field1','field2',...)
  • And print the result.

Below is the implementation with python:

Python3




# Establish connection to MySQL database
import mysql.connector
  
db = mysql.connector.connect(
  host = "localhost",
  user = "root",
  password = "rot23",
  database = "geeks"
  )
  
# getting the cursor by cursor() method
mycursor = db.cursor()
  
query = "ALTER TABLE store ADD Department  ENUM('Sweet','Snaks');"
  
mycursor.execute(query)
  
mycursor.execute(" desc store;") 
myresult = mycursor.fetchall() 
for row in myresult: 
    print(row)
      
db.commit() 
  
# close the Connection
db.close()


Output:

Let’s check on SQL shell:



Similar Reads

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
Adding a new NOT NULL column in MySQL using Python
Prerequisite: Python: MySQL Create Table In this article, we are going to see how to add a new NOT NULL column 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 communicat
2 min read
Create a new column in Pandas DataFrame based on the existing columns
While working with data in Pandas, we perform a vast array of operations on the data to get the data in the desired form. One of these operations could be that we want to create new columns in the DataFrame based on the result of some operations on the existing columns in the DataFrame. Let's discuss several ways in which we can do that. Given a Da
4 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
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 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 append new data to existing XML using Python ElementTree
Extensible Markup Language (XML) is a widely used format for storing and transporting data. In Python, the ElementTree module provides a convenient way to work with XML data. When dealing with XML files, it is common to need to append new data to an existing XML document. This can be achieved efficiently using Python's ElementTree module. What is P
4 min read
Adding two columns to existing PySpark DataFrame using withColumn
In this article, we are going to see how to add two columns to the existing Pyspark Dataframe using WithColumns. WithColumns is used to change the value, convert the datatype of an existing column, create a new column, and many more. Syntax: df.withColumn(colName, col) Returns: A new :class:`DataFrame` by adding a column or replacing the existing c
2 min read
Adding Data into Existing XML File in Python
We are given an XML existing file and our task is to append new data into that existing XML using Python. In this article, we will see how we can append new data to the existing XML using Python. Creating an XML fileIn this example, an XML tree is constructed using the xml.etree.ElementTree module in Python, where child elements with attributes and
3 min read
How to add column sum as new column in PySpark dataframe ?
In this article, we are going to see how to perform the addition of New columns in Pyspark dataframe by various methods. It means that we want to create a new column that will contain the sum of all values present in the given row. Now let's discuss the various methods how we add sum as new columns But first, let's create Dataframe for Demonstratio
4 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
Add a Column to Existing CSV File in Python
Working with CSV files is a common task in data manipulation and analysis, and Python provides versatile tools to streamline this process. Here, we have an existing CSV file and our task is to add a new column to the existing CSV file in Python. In this article, we will see how we can add a column to a CSV file in Python. Add a New Column to Existi
3 min read
How to append a new row to an existing csv file?
For writing a CSV file, the CSV module provides two different classes writer and Dictwriter. Here we will discuss 2 ways to perform this task effectively. The First will be 'append a list as a new row to the existing CSV file' and second way is 'Append a dictionary as a new row to the existing CSV file.' First, let's have a look at our existing CSV
3 min read
How to update existing table rows in SQLAlchemy in Python?
In this article, we are going to see how to use the UPDATE statement in SQLAlchemy against a PostgreSQL database in Python. Creating table for demonstration:Import necessary functions from the SQLAlchemy package. Establish connection with the PostgreSQL database using create_engine() function as shown below, create a table called books with columns
2 min read
Create MapType Column from Existing Columns in PySpark
An RDD transformation that applies the transformation function to every element of the data frame is known as a map in Pyspark. There occurs various situations when you have numerous columns and you need to convert them to map-type columns. It can be done easily by using the create_map function with the map key column name and column name as argume
3 min read
How to insert a pandas DataFrame to an existing PostgreSQL table?
In this article, we are going to see how to insert a pandas DataFrame to an existing PostgreSQL table. Modules neededpandas: Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fas
3 min read
PySpark - Adding a Column from a list of values using a UDF
A data frame that is similar to a relational table in Spark SQL, and can be created using various functions in SparkSession is known as a Pyspark data frame. There occur various circumstances in which you get data in the list format but you need it in the form of a column in the data frame. If a similar situation has occurred with you, then you can
4 min read
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
enum.IntEnum in Python
With the help of enum.IntEnum() method, we can get the enumeration based on integer value, if we compare with normal enum based class it will fail by using enum.IntEnum() method. Syntax : enum.IntEnum Return : IntEnum doesn't have a written type. Example #1 : In this example we can see that by using enum.IntEnum() method, we are able to get the enu
1 min read
enum in Python
Enumerations in Python are implemented by using the module named "enum". Enumerations are created using classes. Enums have names and values associated with them. Let's cover the different concepts of Python Enum in this article.What are Enums and Why are they useful?Enumerations or Enums is a set of symbolic names bound to unique values. It can be
6 min read
enum.auto() in Python
With the help of enum.auto() method, we can get the assigned integer value automatically by just using enum.auto() method. Syntax : enum.auto()Automatically assign the integer value to the values of enum class attributes.Example #1 : In this example we can see that by using enum.auto() method, we are able to assign the numerical values automaticall
1 min read
Add comment to column in MySQL using 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
3 min read
Rename column name in MySQL using Python
In this article, we are going to see how we can rename or alter the name of a column 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 for communicating with a MySQL database. Ap
1 min read
Adding New Variable to Pandas DataFrame
In this article let's learn how to add a new variable to pandas DataFrame using the assign() function and square brackets. Pandas is a Python package that offers various data structures and operations for manipulating numerical data and time series. It is mainly popular for importing and analyzing data much easier. Whereas Pandas DataFrame is a pot
3 min read
Add a new column in Pandas Data Frame Using a Dictionary
Pandas is basically the library in Python used for Data Analysis and Manipulation. To add a new Column in the data frame we have a variety of methods. But here in this post, we are discussing adding a new column by using the dictionary. Let's take Example! # Python program to illustrate # Add a new column in Pandas # Importing the pandas Library im
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 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 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
Article Tags :
Practice Tags :
three90RightbarBannerImg