MYSQLi Create DB


Data inventory has one or more tables.

Create a database

The CREATE   DATABASE statement is used to create a database in MySQL.

Grammar

CREATE   DATABASE database_name

Category       Detailed explanation
Basic grammarCreate database database name;
Example Create database liwenkai;
 Example descriptionCreate a number library, the name of the database is liwenkai
   
In order for PHP to execute the above statement, we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.

Rules for creating a database:

1. Cannot duplicate the name of other databases, otherwise an error will occur.

2. The name can consist of any letter, Arabic number, underscore (_), and "$". It can start with any of the above characters, but you cannot use the number alone, otherwise it will be confused with the value.

3. You cannot use the MYSQL keyword as the database name and table name of the database.

4. By default, the database name under Windows, the case of the table name is not sensitive. Linux is the opposite, so in order to facilitate database migration between platforms, it is recommended to use lowercase to define the database name and table name.

Instance

In the following example, a database named "myDB" is created:

 Header("Content-type:text/html;charset=utf-8"); //Set the encoding
 $servername = "localhost";
 $username = "root";
 $password = "root";
 
 
 // Create a connection
 $conn = mysqli_connect($servername, $username, $password);
 / / Check the connection
 If (!$conn) {
     Die("Connection failed: " . mysqli_connect_error());
 }
 / / Create a database
 $sql = "CREATE DATABASE my_db";
 If (mysqli_query($conn, $sql)) {
     Echo "database created successfully";
 } else {
     Echo "Database creation failed: " . mysqli_error($conn);
 }
 Mysqli_close($conn);
 

Program running result:

The database was created successfully

Delete database


Category   Detailed explanation
Basic grammarDrop database library name;
ExampleDrop database my_db;
Example descriptionDelete a number library, the name of the database is my_db
 
Instance

In the following example, delete a database named " myDB ":

 Header("Content-type:text/html;charset=utf-8"); //Set the encoding
 $servername = "localhost";
 $username = "root";
 $password = "root";
 
 
 // Create a connection
 $conn = mysqli_connect($servername, $username, $password);
 / / Check the connection
 If (!$conn) {
     Die("Connection failed: " . mysqli_connect_error());
 }
 / / Create a database
 $sql = "drop database myDB";
 If (mysqli_query($conn, $sql)) {
     Echo "database deleted successfully";
 } else {
     Echo "Database deletion failed: " . mysqli_error($conn);
 }
 Mysqli_close($conn);
Program running result:

Database deleted successfully

[Remember] Note : After the database is deleted, all the following data will be deleted, so be sure to carefully and delete the corresponding backup before deleting.

Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :