Creating tables by queryWe can create new table by using Query. You can seen how to execute a query and get the records or modify the records in MySQL. Same way we can run one sql command to create tables.
The above query will create a table sample_tb and add one column empno to it. But note that we have to execute the above code. After execution we can find out whether the query has successfully executed or not by using one if condition. If the query is not executed successfully then we will print the error message. If table create process is successful then we will display a success message. Here is the code
$connection stores mysqli connection details, declared at config file
As you can see we have created the table, with one column to store the data. Now what happens if the table sample_tb already exists and we will try to create again? The system will generate an error message.
So before trying to create the table we will delete the table and then create again. ( Note: do this if your requirement is there ). We also can't delete the table without checking the table is there or not. So we will use one if exists command like this.
We can easily execute the create table query to generate the table. Note that this combination is used inside many scripts where temporary tables are create and deleted at the end.
How to check if table is created or not ?We can use SHOW TABLE query to list the tables in database.(Our database name is sql_tutorial )
This article is written by plus2net.com team.
![]() ![]() | ||||||||
| ||||||||