从数组创建 mysql table
Create mysql table from array
使用 SHOW TABLE 然后 SHOW FULL COLUMNS 我得到了这个数组
Array
(
[tabel_de_test] => Array
(
[0] => Array
(
[Field] => id
[Type] => int(11)
[Collation] =>
[Null] => NO
[Key] => PRI
[Default] =>
[Extra] => auto_increment
[Privileges] => select,insert,update,references
[Comment] => comentariu
)
[1] => Array
(
[Field] => numee
[Type] => varchar(100)
[Collation] => utf8_unicode_ci
[Null] => NO
[Key] =>
[Default] =>
[Extra] =>
[Privileges] => select,insert,update,references
[Comment] =>
)
)
)
我试图用一个函数
把它放回mysql
function tosql($tbl_name,$fields)
{
foreach($fields as $f_id => $f_arr)
{
$in.=''.$f_arr['Field'].' '.$f_arr['Type'].' '.$f_arr['Extra'].',';
}
return 'CREATE TABLE '.$tbl_name.' ('.trim($in).')';
}
但是我需要所有参数,而不仅仅是字段名称、类型和其他参数。
如何正确操作?
因为您的基本要求是
how to make the SQL to make an existing table ?
这个问题我直接回答
SQL 提供一个非常有用的查询 (doc) :
SHOW CREATE TABLE my_target_table
那会打印出类似的东西:
CREATE TABLE `engine_cost` (
`engine_name` varchar(64) NOT NULL,
`device_type` int(11) NOT NULL,
`cost_name` varchar(64) NOT NULL,
`cost_value` float DEFAULT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`comment` varchar(1024) DEFAULT NULL,
PRIMARY KEY (`cost_name`,`engine_name`,`device_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 STATS_PERSISTENT=0
/!\ 注意,这不会导出可能绑定在 table
上的触发器
使用
SELECT trigger_name
FROM information_schema.triggers
WHERE EVENT_OBJECT_TABLE='my_target_table';
和
SHOW CREATE TRIGGER my_target_trigger
使用 SHOW TABLE 然后 SHOW FULL COLUMNS 我得到了这个数组
Array
(
[tabel_de_test] => Array
(
[0] => Array
(
[Field] => id
[Type] => int(11)
[Collation] =>
[Null] => NO
[Key] => PRI
[Default] =>
[Extra] => auto_increment
[Privileges] => select,insert,update,references
[Comment] => comentariu
)
[1] => Array
(
[Field] => numee
[Type] => varchar(100)
[Collation] => utf8_unicode_ci
[Null] => NO
[Key] =>
[Default] =>
[Extra] =>
[Privileges] => select,insert,update,references
[Comment] =>
)
)
)
我试图用一个函数
把它放回mysqlfunction tosql($tbl_name,$fields)
{
foreach($fields as $f_id => $f_arr)
{
$in.=''.$f_arr['Field'].' '.$f_arr['Type'].' '.$f_arr['Extra'].',';
}
return 'CREATE TABLE '.$tbl_name.' ('.trim($in).')';
}
但是我需要所有参数,而不仅仅是字段名称、类型和其他参数。 如何正确操作?
因为您的基本要求是
how to make the SQL to make an existing table ?
这个问题我直接回答
SQL 提供一个非常有用的查询 (doc) :
SHOW CREATE TABLE my_target_table
那会打印出类似的东西:
CREATE TABLE `engine_cost` (
`engine_name` varchar(64) NOT NULL,
`device_type` int(11) NOT NULL,
`cost_name` varchar(64) NOT NULL,
`cost_value` float DEFAULT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`comment` varchar(1024) DEFAULT NULL,
PRIMARY KEY (`cost_name`,`engine_name`,`device_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 STATS_PERSISTENT=0
/!\ 注意,这不会导出可能绑定在 table
上的触发器使用
SELECT trigger_name
FROM information_schema.triggers
WHERE EVENT_OBJECT_TABLE='my_target_table';
和
SHOW CREATE TRIGGER my_target_trigger