Thursday, May 18, 2023
HomeSoftware EngineeringThe best way to Create a Major Key for a MySQL Database...

The best way to Create a Major Key for a MySQL Database in Python


You may create a Major Key to your MySQL database in Python as follows.

First, it’s essential to know if the Major Key already exists.

Possibility 1 – The Major Key doesn’t Exist

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd",
  database = "your_database"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE TABLE prospects (id INT AUTO_INCREMENT PRIMARY KEY, identify VARCHAR(255), tackle VARCHAR(255))")

Possibility 2 – The Major Key already Exists

import mysql.connector

mydb = mysql.connector.join(
  host = "localhost",
  person = "username",
  password = "YoUrPaSsWoRd",
  database = "your_database"
)

mycursor = mydb.cursor()

mycursor.execute("ALTER TABLE prospects ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments