MariaDB 10.6にIF NOT EXISTS文でユーザーを作成する

2022年1月10日

環境
Windows 10 64bit
MariaDB 10.6.4

書式
CREATE USER IF NOT EXISTS ユーザー名 IDENTIFIED BY 'パスワード’
作成ユーザーが存在しない場合新しいユーザーを作成します。同じユーザが既に存在する場合何もしません。

使用例
1.ユーザー「test10」が存在しない場合

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MariaDB [(none)]> create user if not exists test10@localhost identified by 'test10';
Query OK, 0 rows affected (0.130 sec)
MariaDB [(none)]> create user if not exists test10@localhost identified by 'test10'; Query OK, 0 rows affected (0.130 sec)
MariaDB [(none)]> create user if not exists test10@localhost identified by 'test10';
Query OK, 0 rows affected (0.130 sec)

2.ユーザー「test10」が存在する場合

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MariaDB [(none)]> create user if not exists test10@localhost identified by 'test10';
Query OK, 0 rows affected, 1 warning (0.039 sec)
警告の内容を確認します
MariaDB [(none)]> show warnings;
+-------+------+-----------------------------------------------------------+
| Level | Code | Message |
+-------+------+-----------------------------------------------------------+
| Note | 1973 | Can't create user 'test10'@'localhost'; it already exists |
+-------+------+-----------------------------------------------------------+
1 row in set (0.040 sec)
MariaDB [(none)]> create user if not exists test10@localhost identified by 'test10'; Query OK, 0 rows affected, 1 warning (0.039 sec) 警告の内容を確認します MariaDB [(none)]> show warnings; +-------+------+-----------------------------------------------------------+ | Level | Code | Message | +-------+------+-----------------------------------------------------------+ | Note | 1973 | Can't create user 'test10'@'localhost'; it already exists | +-------+------+-----------------------------------------------------------+ 1 row in set (0.040 sec)
MariaDB [(none)]> create user if not exists test10@localhost identified by 'test10';
Query OK, 0 rows affected, 1 warning (0.039 sec)
警告の内容を確認します
MariaDB [(none)]> show warnings;
+-------+------+-----------------------------------------------------------+
| Level | Code | Message                                                   |
+-------+------+-----------------------------------------------------------+
| Note  | 1973 | Can't create user 'test10'@'localhost'; it already exists |
+-------+------+-----------------------------------------------------------+
1 row in set (0.040 sec)

 

MariaDB

Posted by arkgame