Mariadb 简明教程
MariaDB - Create Database
在 MariaDB 中创建或删除数据库通常需要特权,这些特权通常只授予 root 用户或管理员。在这些帐户下,您有两种创建数据库的选项 − mysqladmin 二进制文件和 PHP 脚本。
Creation or deletion of databases in MariaDB requires privileges typically only given to root users or admins. Under these accounts, you have two options for creating a database − the mysqladmin binary and a PHP script.
mysqladmin binary
以下示例演示了使用 mysqladmin 二进制文件创建名为 Products 的数据库 −
The following example demonstrates the use of the mysqladmin binary in creating a database with the name Products −
[root@host]# mysqladmin -u root -p create PRODUCTS
Enter password:******
PHP Create Database Script
PHP 在创建 MariaDB 数据库时采用 mysql_query 函数。该函数使用两个参数,一个可选,成功时返回“true”值,失败时返回“false”值。
PHP employs the mysql_query function in creating a MariaDB database. The function uses two parameters, one optional, and returns either a value of “true” when successful, or “false” when not.
Syntax
审阅以下 create database script 语法 −
Review the following create database script syntax −
bool mysql_query( sql, connection );
参数说明如下 −
The description of the parameters is given below −
S.No |
Parameter & Description |
1 |
sql This required parameter consists of the SQL query needed to perform the operation. |
2 |
connection When not specified, this optional parameter uses the most recent connection used. |
尝试以下示例代码以创建数据库 −
Try the following example code for creating a database −
<html>
<head>
<title>Create a MariaDB Database</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<br />';
$sql = 'CREATE DATABASE PRODUCTS';
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not create database: ' . mysql_error());
}
echo "Database PRODUCTS created successfully\n";
mysql_close($conn);
?>
</body>
</html>
成功删除后,您将看到以下输出 −
On successful deletion, you will see the following output −
mysql> Database PRODUCTS created successfully
mysql> SHOW DATABASES;
+-----------------------+
| Database |
+-----------------------+
| PRODUCTS |
+-----------------------+