* * *
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:
!(#)
π Categories
- β‘ JavaScript (1589)
- π PHP (872)
- π Python3 (810)
- π HTML (691)
- βοΈ C# (650)
- π Python (594)
- β Java (552)
- βοΈ PyTorch (534)
- π§ Linux (472)
- βοΈ C (432)
- π¦ jQuery (406)
- π¨ CSS (377)
- π XML (259)
- π¦ jQuery UI (231)
- π― Bootstrap (220)
- βοΈ C++ (215)
- π °οΈ Angular (205)
- π HTML DOM (201)
- π΄ Redis (188)
- π Web Building (142)
- π Vue.js (141)
- π R (131)
- πΌ Pandas (124)
- ποΈ SQL (105)
- βοΈ Docker (86)
- βοΈ TypeScript (73)
- βοΈ Highcharts (70)
- π AI Agent (70)
- βοΈ React (68)
- π Node.js (65)
- βοΈ Machine Learning (60)
- π Git (59)
- π΅ Go (58)
- π Markdown (58)
- π’ NumPy (55)
- π§ͺ Flask (54)
- βοΈ Scala (53)
- ποΈ SQLite (52)
- π JSTL (52)
- βοΈ VS Code (51)
- π MongoDB (49)
- π Perl (48)
- π Ruby (47)
- π Matplotlib (47)
- βοΈ Uncategorized (46)
- π Swift (46)
- ποΈ PostgreSQL (46)
- βοΈ Data Structures (46)
- π Playwright (46)
- π iOS (45)
- ποΈ MySQL (44)
- βοΈ LangChain (43)
- π FastAPI (40)
- βοΈ Ionic (38)
- π Design Patterns (37)
- βοΈ Eclipse (37)
- π¨ CSS3 (34)
- π Lua (34)
- βοΈ Codex (34)
- πΈ Django (32)
- βοΈ OpenCV (32)
- π Rust (31)
- π JSP (31)
- βοΈ Claude Code (31)
- π Pillow (30)
- βοΈ OpenCode (28)
- π AI Skills (27)
- π Flutter (26)
- π Maven (26)
- π¨ Tailwind CSS (25)
- π§ TensorFlow (25)
- π Servlet (24)
- π Dart (23)
- π Assembly (23)
- βοΈ Memcached (22)
- βοΈ SVG (22)
- βοΈ Electron (22)
- π NLP (22)
- π Regex (21)
- π Android (20)
- π£ Kotlin (19)
- π Julia (19)
- π SOAP (17)
- π Selenium (17)
- π PowerShell (17)
- π Sass (16)
- π HTTP (16)
- π Zig (15)
- π AI (15)
- π AJAX (14)
- π Swagger (14)
- βοΈ Scikit-learn (13)
- βοΈ ECharts (13)
- βοΈ Chart.js (13)
- βοΈ Cursor (13)
- βοΈ SciPy (12)
- π RDF (12)
- π Ollama (12)
- π Next.js (12)
- π Plotly Dash (12)
- π JSON (11)
- π RESTful API (11)
- π WSDL (9)
- βοΈ CMake (8)
- π Firebug (7)
- π Nginx (6)
- βΈοΈ Kubernetes (6)
- π Jupyter (6)
- π LaTeX (4)
- π UniApp (4)
- ποΈ SQL Server (1)
YouTip