Home >>Python Tutorial >Python MySQL Create Table

Python MySQL Create Table


To Create a Table

This is the pymysql create table module of our Python MySQL tutorial. And to create any particular kind of table in MySQL you will be required to use the statement of ‘CREATE TABLE’. And while using this statement it is also important for you to remember that you will be required to define the exact name of the database while you are in the process of creating a connection. For example, if you wish to create a database named ‘mydatabase’ then  
import mysql.connector



mydb = mysql.connector.connect (

  host=“localhost”

  user=“yourusername”

 passwd=“yourpassword”

 database=“mydatabase”

)



mycursor = mydb.cursor ( )



mycursor.execute (“CREATE TABLE customers (name VARCHAR(255), address VARCHAR  (255)” )
  While executing this code that is mentioned above, if you see no error popping up while completing the task of python create table mysql then that means that you have successfully crated a table.  

To Check if Table Exist

If you wish to check whether some particular kind of python create table mysql exists or not then you can also do that in this programming language. To check if a table exists or not you need to use the statement “SHOW TABLES”. Once you use this statement then all the tables that you have will be shown on the screen and then you can find the table that you needed to check on from that list.

For example

import mysql.connector



mydb = mysql.connector.connect (

  host=“localhost”

  user=“yourusername”

 passwd=“yourpassword”

 database=“mydatabase”

)



mycursor = mydb.cursor ( )



mycursor.execute (“SHOW TABLES”)



for x in mycursor :

  print ( x )
 

The Primary Key

It is suggested that whenever you try and perform the task of python create table then you should always create a particular column that has a unique key for every single record that you make in that table. This particular task can be accomplished by going ahead and defining the PRIMARY KEY for a python create table. The statement of “INT AUTO_INCREMENT PRIMARY KET” is used. This statement helps in inserting a unique number for every single record that you make in the table. This unique number starts from 1 and is increased by 1 for every successive record.

For example

import mysql.connector



mydb = mysql.connector.connect (

  host=“localhost”

  user=“yourusername”

 passwd=“yourpassword”

 database=“mydatabase”

)



mycursor = mydb.cursor ( )



mycursor.execute (“CREATE TABLE customers ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR (255), address VARCHAR (255)” )

If the table that you were planning to use already exists then you need to use the ALTER TABLE keyword to perform the same task.

import mysql.connector



mydb = mysql.connector.connect (

  host=“localhost”

  user=“yourusername”

 passwd=“yourpassword”

 database=“mydatabase”

)



mycursor = mydb.cursor ( )



mycursor.execute (“ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY” )
  With this, we finish the pymysql create table part of our Python MySQL tutorial.