In this article we are going to show you how to create a MySQL database using PHP? Well, creating a database is necessary for those applications where the amount of data is large. It helps a web developer to fetch the required information very easily whenever he wants to manipulate the data.
A database helps to store a set of information in an organised way. The information is generally stored in the database in a tabular form which makes it easy for a web developer to fetch the relevant information according to his need.
For creating a MySQL database using php, the first thing is to open a connection to a MySQL server by using mysql_connect() function. Then the further coding is done to create a new database using mysql_create_db() in php. Once a database has been created, you can add different values or information by applying MySQL queries.
In order to add value to the database, first we create a table by using CREATE TABLE statement. Then, the values are added to the table by using INSERT INTO statement.
Now we are going to put forward the steps involved in creating a MySQL database using shell script.
CREATING MYSQL DATABASE USING SHELL SCRIPT
1st Step : Open XAMPP Control Panel and Click Shell Tab.
A MySQL database can be created by using shell script. To start the proceedings, you have to open XAMPP Control Panel and click on the Shell tab which is present in the extreme right corner as shown below in the image.
2nd Step : Establish a Connection With MySQL Server.
A new window will get opened which will provide you an environment to write SQL commands for creating a database. But, first you have to establish a connection to the MySQL server. For this purpose, type “mysql -hlocalhost -uroot -p” and hit enter. It will ask you for the password. Enter the password, if any, and then hit enter. It will display you the text “Welcome to the MySQL monitor “, “Your MySQL connection id is xyz” and “server version” along with some other details if you are successfully connected to the server. It will also ask you to type’\h’ for help or ‘\c’ to clear the current input statement.
3rd Step : Create MySQL Database.
Now you are all set to create a MySQL database. Just type “create database dbname” followed by semicolon and hit enter as shown in the image below. Here, we have created a database w3trainingschool by typing “create database w3trainingschool”. On the successful creation of database, it will show a message “Query OK, 1 row affected (time in seconds).
4th Step : Show Databases.
By typing show databases, it will provide you the whole list of databases you have previously made along with the currently made database as shown in the image below.
5th Step : Choose Database.
You are now required to choose the currently made database as your default database to put in the desired information using the SQL commands. In order to perform this function, simply type “use dbname” and hit enter. Here, we have chosen w3trainingschool as our default database for further implementations. A message “Database changed” will appear on your screen.
6th Step : Create a Table.
If you type the command ‘show tables’, it will show you a message ‘Empty set’ as there is no table present in the database. So, you need to make a table in order to add information into the database. For this purpose, CREATE TABLE statement is used whose basic syntax is shown below.
Syntax : CREATE TABLE tablename ( column1 datatype (size), column2 datatype (size), column3 datatype (size) );
Column1, column2, column3 are the names of the column. Datatype signifies the type of data in the column like varchar, integer etc. followed by the size or the maximum length it can include.
In order to select or copy data into the table, you can use SELECT INTO statement whose syntax is : SELECT * FROM Tablename.
7th Step : Add Information Into the Table.
After you have created a table, the next thing is to add data or information into the table. INSERT INTO statement is used for this purpose whose basic syntax is shown below.
Syntax : INSERT INTO tablename ( column1, column2, column3 ) VALUES ( int, ‘varchar’, int )
It should be noted that the datatype value having varchar is always written between two apostrophe as shown in the syntax and in the example below as well.
Along with adding data or information into the table, there are some other statements in SQL like UPDATE which is used to change the information of the column, ALTER which is used to add, delete or modify the existing column, DROP which is used to delete tables or databases.
In this article, we have provided you the best possible information about how to create MySQL database using Shell Script. There are some other ways too to create MySQL database which we will be publishing in our next article. So, stay tuned to our blogs for further details and information.