[MDEV-5359] CREATE OR REPLACE, CREATE IF NOT EXISTS, DROP IF EXISTS Created: 2013-11-28  Updated: 2023-02-24  Resolved: 2015-04-03

Status: Closed
Project: MariaDB Server
Component/s: OTHER
Fix Version/s: 10.1.4

Type: Task Priority: Major
Reporter: VAROQUI Stephane Assignee: Alexander Barkov
Resolution: Fixed Votes: 4
Labels: gsoc14

Issue Links:
Blocks
is blocked by MDEV-7112 Split HA_CREATE_INFO Closed
is blocked by MDEV-7280 DATABASE: CREATE OR REPLACE Closed
is blocked by MDEV-7281 EVENT: CREATE OR REPLACE Closed
is blocked by MDEV-7282 SP: CREATE OR REPLACE, CREATE IF NOT ... Closed
is blocked by MDEV-7283 UDF: CREATE OR REPLACE, CREATE IF NOT... Closed
is blocked by MDEV-7284 INDEX: CREATE OR REPLACE Closed
is blocked by MDEV-7285 SERVER: CREATE OR REPLACE and CREATE ... Closed
is blocked by MDEV-7286 TRIGGER: CREATE OR REPLACE, CREATE IF... Closed
is blocked by MDEV-7287 VIEW: CREATE IF NOT EXISTS Closed
is blocked by MDEV-7288 USER/ROLE: CREATE OR REPLACE, CREATE ... Closed
PartOf
includes MDEV-5491 CREATE OR REPLACE TABLE Closed
Relates
relates to MDEV-7886 CREATE VIEW IF NOT EXISTS produces a ... Closed
relates to MDEV-30537 Feature request : REVOKE IF EXISTS / ... Open

 Description   

This task is to add support for OR REPLACE and IF EXISTS / IF NOT EXISTS to all CREATE and DROP variants for all objects (where it makes sense). For (but not limited to) functions, procedures, triggers, events, views, users, roles, and databases.

see also the original task description in the history

We have these object affected:

Object      CREATE OR REPLACE  CREATE IF NOT EXISTS  DROP IF EXISTS
--------    -----------------  --------------------  --------------
DATABASE      -                 +                     +
EVENT         -                 +                     +
FUNCTION      -                 -                     +
FUNCTION(UDF) -                 -                     -
INDEX         -                 +                     +
PROCEDURE     -                 -                     +
ROLE          -                 -                     -
SERVER        -                 -                     +
TABLE         +                 +                     +
TRIGGER       -                 -                     +
USER          -                 -                     -
VIEW          +                 -                     +

There are also engine-specific (NDB) statements
"CREATE LOGFILE GROUP" and "CREATE TABLESPACE".
But these statements will be done in a separate task if(when) needed.

General guidance "CREATE OR REPLACE" should work

Atomicity

"CREATE OR REPLACE OBJECT name ..." is a short for

DROP OBJECT IF EXISTS name;
CREATE OBJECT name ...;

where OBJECT is DATABASE, EVENT, FUNCTION, INDEX, PROCEDURE,
ROLE, SERVER, TABLE, TRIGGER, USER, VIEW.

However, unlike a DROP followed by a CREATE,
"CREATE OR REPLACE" should work atomically ("all or nothing"),
i.e. either both DROP and CREATE should complete, or nothing should happen.

It should never happen that DROP succeeds, but then CREATE fails
because of insufficient privileges or some other logical errors
(CREATE can only fail if some fatal errors happen).

Therefore, all privileges required to perform both DROP and CREATE
should be checked before the action. If there is a DROP privilege,
but there is not enough CREATE privilege, the command should do nothing
and return with an error, reporting insufficient privileges.

CREATE OR REPLACE and metadata locking

Both "DROP" and "CREATE" parts of "CREATE OR REPLACE"
should be done under a single metadata lock, to make sure
the concurrent clients do not interfere in between.

CREATE OR REPLACE and replication

For all objects, "CREATE OR REPLACE" should be put into binary log as a single statement.
It should not be replaced to "DROP" followed by "CREATE"

All statements "CREATE IF NOT EXISTS" and "DROP IF EXISTS" should
be binlogged, even if nothing happened in the master side.

Object specific comments for CREATE OR REPLACE

CREATE OR REPLACE DATABASE

Requires the DROP and the CREATE privileges for the database.

"CREATE OR REPLACE db1" should never change the current database,
including the case if "db1" was the current database before the operation.
I.e. the operation should neither "unuse" the current database, nor use a new one.

CREATE OR REPLACE EVENT

Requires the EVENT privilege.
If the old event exists and has the "DEFINER=another_user" part,
requires the SUPER privilege.
If the new event definition has the "DEFINER=another_user" part,
requires the SUPER privilege.

CREATE OR REPLACE FUNCTION

Requires the ROUTINE privilege.
If the old function exists and has the "DEFINER=another_user" part,
requires the SUPER privilege.
If the new function definition has the "DEFINER=another_user" part,
requires the SUPER privilege.

CREATE OR REPLACE PROCEDURE

See "CREATE OR REPLACE FUNCTION".

CREATE OR REPLACE TRIGGER

Requires the TRIGGER privilege.
If the old trigger exists and has the "DEFINER=another_user" part,
requires the SUPER privilege.
If the new trigger definition has the "DEFINER=another_user" part,
requires the SUPER privilege.

CREATE OR REPLACE INDEX

Requires the INDEX privilege.

CREATE OR REPLACE SERVER

Requires the SUPER privilege.

CREATE OR REPLACE USER

DROP requires the global CREATE USER privilege,
or the DELETE privilege for the mysql database.
CREATE requires global CREATE USER privilege,
or the INSERT privilege for the mysql database.

CREATE OR REPLACE ROLE

See "CREATE OR REPLACE USER".



 Comments   
Comment by Michael Widenius [ 2013-12-27 ]

We already have this for VIEWS.
I am now starting to implement this for TABLE.
The rest will follow...

Comment by Michael Widenius [ 2014-04-22 ]

CREATE OR REPLACE for tables has already been added to MariaDB 10.0

Comment by Jean Weisbuch [ 2015-01-27 ]

Adding the possibility for mysqldump to use CREATE IF NOT EXIST would also be interresting if it gets implemented.

Generated at Thu Feb 08 07:03:40 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.