CREATE DATABASE

语法

CREATE [OR REPLACE] {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [create_specification] ...

create_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name

描述

CREATE DATABASE创建一个给定名称的数据库。该语句要求对database具有CREATE权限。CREATE SCHEMA是CREATE DATABASE的同义词。如果使用了IF NOT EXISTS子句,当数据库已存在时将返回一条warning信息而不是返回错误。

OR REPLACE

MariaDB starting with 10.1.3

MariaDB 10.1.3中引入了OR REPLACE子句。

如果使用了可选的OR REPLACE子句,它是下面语句的简写形式:

DROP DATABASE IF EXISTS db_name;
CREATE DATABASE db_name ...;

IF NOT EXISTS

当使用了IF NOT EXISTS子句,当指定的数据库已存在时,MariaDB将返回一个warning而不是错误信息。

示例

CREATE DATABASE db1;
Query OK, 1 row affected (0.18 sec)

CREATE DATABASE db1;
ERROR 1007 (HY000): Can't create database 'db1'; database exists

CREATE OR REPLACE DATABASE db1;
Query OK, 2 rows affected (0.00 sec)

CREATE DATABASE IF NOT EXISTS db1;
Query OK, 1 row affected, 1 warning (0.01 sec)

SHOW WARNINGS;
+-------+------+----------------------------------------------+
| Level | Code | Message                                      |
+-------+------+----------------------------------------------+
| Note  | 1007 | Can't create database 'db1'; database exists |
+-------+------+----------------------------------------------+

设置字符集和排序规则character sets and collation。详细信息见 Setting Character Sets and Collations

CREATE DATABASE czech_slovak_names 
  CHARACTER SET = 'keybcs2'
  COLLATE = 'keybcs2_bin';

See also

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.