开发者

Using triggers / routines with table prefixes in mysql

I'm used to use mysql table prefixes in开发者_如何转开发 my php scripts. Yet triggers and routines sometimes are very useful too. Ok. Let's say i have a table: 'pre_customers'. And a procedure sth like

CREATE FUNCTION `get_all_clients`() RETURNS int(11)
    BEGIN
    DECLARE sum INT ;
    SELECT COUNT(id) INTO sum FROM pre_customers ;
    RETURN sum;
END

No big deal, just for example. And there is also a constant

<?php
define( 'DB_PREFIX', 'pre_' ) ;

It's being used for changing table prefixes. If i need to make an sql-request in the script i make it like this

$query = "SELECT * FROM " . DB_PREFIX . "customers" ;
$result = mysql_query( $query ) ;
...

Alright, but if i want to change this prefix in the php-script along with the table names it's gonna ruin all stored routines and triggers, they still will apply to 'pre_customers' table. So the question is is there a common practice how normally programmers solve this problem.


So the question is is there a common practice how normally programmers solve this problem.

  • Create a dump
  • Create some kind of template using that dump, with pre_ replaced with %db_prefix%
  • When you need to change prefix - replace prefix in the template and import it to mysql
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜