This article describes how to create and delete databases in MySQL and how to operate related PHP scripts. here, we will focus on command operations under mysqladmin in Linux. For more information, see
Use mysqladmin to create a database
For normal users, you may need specific permissions to create or delete MySQL databases.
Therefore, we use the root user to log on. the root user has the highest permissions and can use the mysql mysqladmin command to create a database.
Instance
The following Command demonstrates the process of creating a database. The data name is TUTORIALS:
[root@host]# mysqladmin -u root -p create TUTORIALS
Enter password:******
After the preceding command is successfully executed, the MySQL database TUTORIALS is created.
Use a PHP script to create a database
PHP uses the mysql_query function to create or delete a MySQL database.
This function has two parameters. TRUE is returned when execution is successful. otherwise, FALSE is returned.
Syntax
bool mysql_query( sql, connection );
Instance
The following example demonstrates how to create a database using PHP:
Creating MySQL Database
';$sql = 'CREATE DATABASE TUTORIALS';$retval = mysql_query( $sql, $conn );if(! $retval ){ die('Could not create database: ' . mysql_error());}echo "Database TUTORIALS created successfully\n";mysql_close($conn);?>
Use mysqladmin to delete a database
When you log on to the mysql server as a common user, you may need specific permissions to create or delete a MySQL database.
Therefore, we use the root user to log on. the root user has the highest permissions and can use the mysql mysqladmin command to create a database.
Exercise caution when deleting a database because all data will disappear after the Delete command is executed.
The following instance deletes the database TUTORIALS (the database has been created on it ):
[root@host]# mysqladmin -u root -p drop TUTORIALS
Enter password:******
After executing the delete database command, a prompt box appears to confirm whether the database is deleted:
Dropping the database is potentially a very bad thing to do.Any data stored in the database will be destroyed.Do you really want to drop the 'TUTORIALS' database [y/N] yDatabase "TUTORIALS" dropped
Use a PHP script to delete a database
PHP uses the mysql_query function to create or delete a MySQL database.
This function has two parameters. TRUE is returned when execution is successful. otherwise, FALSE is returned.
Syntax
bool mysql_query( sql, connection );
Instance
The following example demonstrates how to use the PHP mysql_query function to delete a database:
Deleting MySQL Database
'; $ SQL = 'drop database tutorials'; $ retval = mysql_query ($ SQL, $ conn); if (! $ Retval) {die ('database deletion failed: '. mysql_error ();} echo "database TUTORIALS deleted \ n"; mysql_close ($ conn);?>
Note: When you use a PHP script to delete a database, you do not need to confirm whether to delete the information or delete the specified database directly. Therefore, you must be careful when deleting the database.