YouTip LogoYouTip

Mysql Create Database

* * * We can create a database using the `create` command after logging into the MySQL service. The syntax is as follows: ```sql CREATE DATABASE database_name; The following command simply demonstrates the process of creating a database named : ```bash [root@host]# mysql -u root -p Enter password:****** # After logging in, enter the terminal mysql> create DATABASE ; The basic syntax for creating a database is as follows: ```sql CREATE DATABASE database_name ; If you wish to specify some options when creating a database, you can use other parameters of the `CREATE DATABASE` statement. For example, you can specify the character set and collation: ## Example ```sql CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; If the database already exists, executing `CREATE DATABASE` will cause an error. To avoid this, you can add the `IF NOT EXISTS` clause to the `CREATE DATABASE` statement: ## Example ```sql CREATE DATABASE IF NOT EXISTS mydatabase; * * * ## Creating a Database Using mysqladmin `mysqladmin` is a command-line utility provided by MySQL for performing administrative tasks. Through `mysqladmin`, you can perform various database management operations, including creating databases. Here is the basic syntax for creating a database using `mysqladmin`: ```bash mysqladmin -u your_username -p create your_database * The `-u` parameter is used to specify the MySQL username. * The `-p` parameter indicates that a password needs to be entered. * `create` is the operation to be performed, indicating the creation of a database. * `your_database` is the name of the database to be created. Using a regular user, you may need specific privileges to create or delete MySQL databases. Here, we log in using the root user. The root user has the highest privileges and can use the `mysqladmin` command to create databases. After executing the above command, the system will prompt you to enter a password. After entering the password, press the Enter key. The following command simply demonstrates the process of creating a database named : ```bash [root@host]# mysqladmin -u root -p create Enter password:****** After the above command is executed successfully, it will create the MySQL database . If you wish to specify the character set and collation when creating a database, you can use the `-default-character-set` and `-default-collation` parameters: ```bash mysqladmin -u your_username -p create your_database --default-character-set=utf8mb4 --default-collation=utf8mb4_general_ci The above code creates a database using the `utf8mb4` character set and `utf8mb4_general_ci` collation. Please note that when executing these commands, ensure that the MySQL server is running and that you have sufficient privileges to perform these operations. If you wish to use `mysqladmin` to connect to the MySQL server to perform other administrative tasks, such as checking server status, restarting the server, etc., you can use the following command format: ```bash mysqladmin -u your_username -p your_command Here, `your_command` is the specific administrative command you wish to execute. For example, to check the status of the MySQL server, you can use: ```bash mysqladmin -u your_username -p status This will prompt you to enter a password and then display information about the server status. * * * ## Creating a Database Using a PHP Script PHP uses the `mysqli_query` function to create or delete MySQL databases. This function takes two parameters and returns `TRUE` on success, or `FALSE` on failure. ### Syntax ```php mysqli_query(connection, query, resultmode); | Parameter | Description | | --- | --- | | _connection_ | Required. Specifies the MySQL connection to use. | | _query_ | Required. Specifies the query string. | | _resultmode_ | Optional. A constant. Can be any of the following values: * `MYSQLI_USE_RESULT` (Use this if you need to retrieve a large amount of data) * `MYSQLI_STORE_RESULT` (Default) | ### Example The following example demonstrates using PHP to create a database: ## Create Database ```php <?php $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = '123456'; $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn) { die('Connection failed: ' . mysqli_error($conn)); } echo 'Connected successfully
'; $sql = 'CREATE DATABASE '; $retval = mysqli_query($conn, $sql); if(! $retval) { die('Failed to create database: ' . mysqli_error($conn)); } echo "Database created successfullyn"; mysqli_close($conn); ?> After successful execution, the following result is returned: !(#) If the database already exists, after execution, the following result is returned: !(#)
← Mysql Drop DatabaseMysql Connection β†’