[My]SQL 是否有类似预处理器的功能?
我正在为我的第一个数据库驱动的应用程序编写一个小型部署 SQL 脚本。
在这个过程中,我发现我很多重复自己,例如:
GRANT USAGE ON *.* TO 'foo'@'localhost';
DROP USER 'foo'@'localhost';
CREATE USER 'foo'@'localhost' IDENTIFIED BY 'password';
如果我可以使用变量或宏来替换常见的数据,那就太棒了。 是否可以实现类似以下代码片段的内容?
#define USER 'foo' #or "Type USER = 'foo'"
#define HOST 'localhost' #or "Type HOST = 'localhost'"
GRANT USAGE ON *.* TO USER@HOST
DROP USER USER@HOST
CREATE USER USER@HOST IDENTIFIED BY 'password'
I'm writing a small deployment SQL script for my first database-driven app.
In the process, I find that I repeat myself a lot, for instance:
GRANT USAGE ON *.* TO 'foo'@'localhost';
DROP USER 'foo'@'localhost';
CREATE USER 'foo'@'localhost' IDENTIFIED BY 'password';
It would be fantastic if I could use a variable or a macro to replace commonly occurring data. Is it possible to implement something like the the following snippet?
#define USER 'foo' #or "Type USER = 'foo'"
#define HOST 'localhost' #or "Type HOST = 'localhost'"
GRANT USAGE ON *.* TO USER@HOST
DROP USER USER@HOST
CREATE USER USER@HOST IDENTIFIED BY 'password'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大多数 SQL 数据库都有某种类型的绑定变量可供您使用。
例如,在 PostgreSQL 中,您在 plsql 中使用 \set 命令:
但是,我不确定 MySQL 是否有类似的东西。 它确实有变量,并且由于主机和用户是一个字符串,您也许可以执行以下操作:
如果变量不适用于 drop 和 create user 语句,您始终可以直接修改 mysql.user 表,只是不要忘记之后执行
flush特权
。Most SQL databases have some kind of bind variables that you can use for that.
For instance, in PostgreSQL you use the \set command in plsql:
However, I am not sure if MySQL have something like that. It do have variables, and since the host and user is a string, you might be able to do something like this:
If variables doesn't work with the drop and create user statements, you can always modify the mysql.user table directly, just don't forget to execute
flush privileges
after.你当然可以这样做:
You can certainly do something like: