
我嘗試過了重新創造一個資料庫使用者和一個資料庫實例(在現有的基礎上運行),因此我在 Mysql CLI 中運行了以下兩個命令:
create user '${test}'@'localhost' identified by '${psw}';
create database ${test};
我得到這個輸出:
第 1 行發生錯誤 1396 (HY000):「test」@「localhost」的動作 CREATE USER 失敗
錯誤的原因當然是因為使用者已經存在)。
我的問題
test
如果我已經有一個資料庫使用者和一個具有相同值 ( )的資料庫實例,如何確保上述命令將在現有資料上運行?
回答後更新
mysql -V
帶來:
mysql Ver 14.14 Distrib 5.7.20,適用於 Linux (x86_64),使用 EditLine 包裝器
答案1
if not exists
只需在您的查詢中使用即可。例如:
create if not exists user '${test}'@'localhost' identified by '${psw}';
或者
create database if not exists ${test};
您的mysql版本可能不同,請參考正確的版本。但更多資訊可以在以下位置找到: