Python: MySQL Create 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.

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

  • connect() :
    This Function is used for extablishing 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
  • cursor()
    Cursor is the work space 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

  • execute()
    The execute function takes a SQL query as an argument and executes.A query is any 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)

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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")

chevron_right


Output :

College Data base is created

Table

  1. Table is a collection of data organized in the form of rows and columns. Table is present withint 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
     # Roll number of data types 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()

chevron_right


Output :

Student Table is Created in the Database



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.