CREATE DATABASE

You are viewing an old version of this article. View the current version here.

Sintaxis

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

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

Descripción

CREATE DATABASE crea una base de datos con el nombre dado. Para utilizar esta declaración, necesita los privilegios para CREATE la base de datos. CREATE SCHEMA es sinónimo de CREATE DATABASE.

Para usar identificadores validos como nombres de bases de datos, ver Identifier Names

OR REPLACE

MariaDB starting with 10.1.3

La cláusula OR REPLACE se agregó en MariaDB 10.1.3

Si se utiliza la cláusula opcional OR REPLACE, actúa como un atajo para:

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

IF NOT EXISTS

Cuando se usa la cláusula IF NOT EXISTS, MariaDB devolverá una advertencia en lugar de un error si la base de datos especificada ya existe.

COMMENT

MariaDB starting with 10.5.0

A partir de MariaDB 10.5.0, es posible agregar un comentario de un máximo de 1024 bytes. Si la longitud del comentario excede esta longitud, se genera un código 4144 de error/advertencia. El comentario de la base de datos también se agrega al archivo db.opt, así como a la tabla information_schema.schemata.

Ejemplos

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 |
+-------+------+----------------------------------------------+

Establecer los conjuntos de caracteres y colecciones. Ver Setting Character Sets and Collations para mas detalles.

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

Comentarios, de MariaDB 10.5.0:

CREATE DATABASE presentations COMMENT 'Presentations for conferences';

Ver también

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.