Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Python Articles
Page 508 of 852
How to show all the tables present in the database and server in MySQL using Python?
We may sometimes require to get the list of all the tables present in our database. This can be done by using the SHOW TABLES command.The SHOW TABLES command is used to display the table names in a database as well as the server.SyntaxTo show the tables present in a database −SHOW TABLESThe above statement when executed using the cursor object returns the names of the tables present in our database.To show the tables present in a serverSELECT table_name FROM information_schema.tablesSteps to show all tables present in a database and server using MySQL in pythonimport MySQL connectorestablish connection with the ...
Read MoreHow to count SQL table columns using Python?
It may be required to count the number of columns present in a SQL table.This is done using count(*) function with information_schema.columns and the WHERE clause. The WHERE clause is used to specify the name of the table whose columns are to be counted.SyntaxSELECT COUNT(*) FROM information_schema.columns WHERE table_name= ‘your_table_name’Steps to count columns in a table using MySQL in pythonimport MySQL connectorestablish connection with the connector using connect()create the cursor object using cursor() methodcreate a query using the appropriate mysql statementsexecute the SQL query using execute() methodclose the connectionSuppose we have a table named “Students” as below −+----------+---------+-----------+------------+ | ...
Read MoreHow to add comment to column in MySQL using Python?
A comment is a readable explanation provided about something. The comments in the code are provided to explain the purpose of the code staement used. This enables the outside person to understand the need and use of that particular code.The comments are ignored by the compilers and are not executed. These are just for the explanatory purpose and are of great importance.We can add comments to columns in a sql table. These comments will explain the function or tell something about the column. If some other person is viewing and working on that table, he can get idea about the ...
Read MoreHow to get the id after INSERT into MySQL database using Python?
The data is inserted into the table in Mysql using the INSERT statement. While inserting the data into the table, either the data must be provided in the same order as the column definition in the database or the column names must be provided along with the data while using INSERT statement.To get and print the ID of the last inserted row, lastrowid is used. This is a special keyword which is used to get the ID of the last inserted row. There are certain prerequisites to be taken care of before using this method.The ID column must be the ...
Read MoreExplain the use of COUNT() AND SUM() in MySQL using Python?
These are the functions used to perform the arithmetic operations on the column values in a table.The COUNT() function is used to return the number of rows which satisfy a certain condition.The SUM() function is used to return the sum of numerical values in a column in the table.The NULL values are ignored.SyntaxCOUNT()SELECT COUNT(column_name) FROM table_name WHERE conditionSUM()SELECT SUM(column_name) FROM table_nameSteps invloved to use count() and sum() functions on a table using MySQL in pythonimport MySQL connectorestablish connection with the connector using connect()create the cursor object using cursor() methodcreate a query using the appropriate mysql statementsexecute the SQL query using ...
Read MoreHow to copy a table in MySQL using Python?
We can create copy of an existing table in mysql using Python. The entire table will be copied including the columns, the column definitions and all the rows of the table.SyntaxCREATE TABLE table_name SELECT * FROM existing_tabletable_name is the name of the new table to be created. existing_table is the name of the table which is to be copied.Steps to copy a table using MySQL in pythonimport MySQL connectorestablish connection with the connector using connect()create the cursor object using cursor() methodcreate a query using the appropriate mysql statementsexecute the SQL query using execute() methodclose the connectionSuppose, we have a table ...
Read MoreWhat is the rollback() method in Python MySQL?
The rollback() method is one among the various methods in Python which is used to make the database transactions.Here, we will discuss about the rollback() method.The rollback() method is used to revert the last change or commit made to the database. If a condition arises such that the user is not satisfied with the changes made to the database, or if the transaction fails, then the rollback() method is operated to bring the database to its original state which was before committing the changes. This is a very important method as it helps in maintaing the integrity of the database ...
Read MoreWhat is Python commit() method in MySQL?
The commit() method is one among the various methods in Python which is used to make the database transactions.Here, we will discuss about the commit() method. The commit() method is used to confirm the changes made by the user to the database. Whenever any change is made to the database using update or any other statements, it is necessary to commit the changes. If we donot use the commit() method after making any changes to the database, the database will not not be updated and changes will not be reflected.Syntaxdb.commit()db refers to the database connection object.Given below is an example ...
Read MoreExplain the use of MIN() and MAX() using MySQL in Python?
The MIN() and MAX() functions are used to perform arithmetic operations on the columns of the table.As the name suggests, the MIN() function is used to select and return the samllest value from the selected column.The MAX() function, on the other hand, selects and returns the highest value from the selected column.SyntaxMIN()SELECT MIN(column_name) FROM table_nameMAX()SELECT MAX(column_name) FROM table_nameSteps invloved in finding minimum and maximum value from a column in table using MySQL in pythonimport MySQL connectorestablish connection with the connector using connect()create the cursor object using cursor() methodcreate a query using the appropriate mysql statementsexecute the SQL query using execute() ...
Read MoreHow can you perform inner join on two tables using MySQL in Python?
We can join two tables in SQL based on a common column between them or based on some specified condition. There are different types of JOIN available to join two SQL tables.Here, we will discuss about the inner join on two tables.JOIN and INNER JOIN both work the same way. The INNER JOIN matches each row in one table with every row in other table and allows to combine the rows from both the tables which either have somecommon column or which satisfy some condition which is specified.When applying join among two tables, we need to specify the condition based ...
Read More