![]() ![]() Let's see how to insert a single row into the table. Use INSERT INTO table_name (column_names) VALUES (data) statement to insert into the table. We have added the column id to the users table. # 'FIRST' keyword in the statement will add a column in the starting of the tableĬursor.execute("ALTER TABLE users ADD COLUMN id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST") # adding 'id' column to the 'users' table We use ALTER TABLE table_name ADD PRIMARY KEY(column_name) statement to add a Primary Key to a table. Adding Primary KeyĪdding Primary Key to the existing table. Let's see how we can add a column with Primary Key to the existing table. Now, we have dropped the Primary Key column. import nnector as mysqlĬursor.execute("ALTER TABLE users DROP id") We use ALTER TABLE table_name DROP column_name statement to drop the column with Primary Key. # it will print all the columns as 'tuples' in a list # 'DESC table_name' is used to get all columns information # creating the 'users' table again with the 'PRIMARY KEY'Ĭursor.execute("CREATE TABLE users (id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), user_name VARCHAR(255))") # 'DROP TABLE table_name' statement will drop the table from a database # first we have to 'drop' the table which has already created to create it again with the 'PRIMARY KEY' Let's see how to create Primary Key for a table. The statement INT AUTO_INCREMENT PRIMARY KEY is used to identify each row uniquely with a number starting from 1. To create a Primary Key, we use the PRIMARY KEY statement while creating the table. It helps to find each row uniquely in the table. Primary Key:- It is a unique value in the table. Tables = cursor.fetchall() # it returns list of tables present in the database ![]() # getting all the tables which are present in 'datacamp' database See all the tables present in the database using the SHOW TABLES statement. You have successfully created the table users in the datacamp database. # creating a table called 'users' in the 'datacamp' databaseĬursor.execute("CREATE TABLE users (name VARCHAR(255), user_name VARCHAR(255))") Use the CREATE TABLE table_name to create a table in the selected database. Now, you have connected to the database called datacamp. The above code will execute with no errors if the database exists. Run the following code, to select datacamp database which we have created a minute before. Before creating tables, we have to select a database first. # 'fetchall()' method fetches all the rows from the last executed statementĭatabases = cursor.fetchall() # it returns a list of all databases presentĬreating tables in the database to store the information. # executing the statement using 'execute()' method To see all the databases we use SHOW DATABASES statement. See all the databases present in MySQL using the following code. Make sure that the database does not exist. If the database already exists you will get an error. # below statement is used to create tha 'datacamp' databaseĬursor.execute("CREATE DATABASE datacamp") # 'execute()' method is used to compile a 'SQL' statement # creating an instance of 'cursor' class which is used to execute the 'SQL' statements in 'Python' To create a database in MySQL, we use CREATE DATABASE database_name statement. Now, we will create a database with the name datacamp. That's it now you have connected to the MySQL database. Print(db) # it will print a connection object if everything is fine # it takes 3 required parameters 'host', 'user', 'passwd' # connecting to the database using 'connect()' method # importing 'nnector' as mysql for convenient Now, connect to the database using your username and password. To create a new user refer to MySQL official documentation. If you don't remember your username or password, create a new user with a password. Now, we will connect to the database using username and password of MySQL. If the above code runs without any errors, then you have successfully installed nnector, and it is ready to use. Now, check whether you have installed the nnector correctly or not using the following code. Download the nnector from here and install it on your computer. We need nnector to connect Python Script to the MySQL database. Next, you have to install nnector for Python. You need to install the MySQL server to follow this tutorial. MySQL is one of the most popular databases.ĭownload and install MySQL from the MySQL's official website. Informing the Database if you make any changes in the table.Procedure To Follow In Python To Work With MySQL This tutorial will teach you how you can work with MySQL in Python.This tutorial is not meant to teach you complete SQL syntax or language.If you don't know SQL, take the Datacamp's free SQL course. Python Data Types, Control Structures, Loops, etc., If you're new to Python learn Datacamp's free Intro to Python for Data Science course.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |