In the previous MySQL Tutorial on Selecting Database and Table, you learnt how particular databases and tables within the them are selected. In this tutorial, you will learn how the table is created within a particular database.
Select the database in which you want to create a table and then create table as follows:
Create table table_name(column1_name data type, column2_name data type);
[(Optional) You can mention the size of data types in brackets]
For example,
Use mysql; Create table employee (employee_id int(2), salary int(5));
To do this using PHP, write the create table query in mysql_query function as follows:
<?php $link=mysql_connect('localhost','root','your password'); mysql_select_db('mysql'); echo "connected to mysql"; $r=mysql_query(‘Create table employee (employee_id int(2), salary int(5))’); If($r) { echo "table created"; } Else { Echo “not created”; } ?>
The output is as follows:
In the above example, mysql_connect connects to the database, mysql_select_db selects the particular database and mysql_query executes the query from the database.
Now that you know how to create a table within a particular database, continue with the next MySQL tutorial to learn how you can insert your data into the tables. Also, if you have any queries, please leave a comment below.