MySQL 如何为phpmyadmin中的现有表生成创建表脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11739014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate a create table script for an existing table in phpmyadmin?
提问by Marko Cakic
How can I generate a create table script for an existing table in phpmyadmin?
如何在 phpmyadmin 中为现有表生成创建表脚本?
回答by Karan Punamiya
Use the following query in sql tab:
在 sql 选项卡中使用以下查询:
SHOW CREATE TABLE tablename
To view full query There is this Hyperlink named +Options left above, There select Full Texts
查看完整查询 上面有这个名为 +Options 的超链接,有选择 Full Texts
回答by Devart
Run SHOW CREATE TABLE <table name>
query.
运行SHOW CREATE TABLE <table name>
查询。
回答by Eric Leschinski
Mysqladmin can do the job of saving out the create table script.
Mysqladmin 可以完成保存创建表脚本的工作。
Step 1, create a table, insert some rows:
第1步,创建一个表,插入一些行:
create table penguins (id int primary key, myval varchar(50))
insert into penguins values(2, 'werrhhrrhrh')
insert into penguins values(25, 'weeehehehehe')
select * from penguins
Step 2, use mysql dump command:
第二步,使用mysql dump命令:
mysqldump --no-data --skip-comments --host=your_database_hostname_or_ip.com -u your_username --password=your_password your_database_name penguins > penguins.sql
Step 3, observe the output in penguins.sql:
第三步,观察penguins.sql中的输出:
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
DROP TABLE IF EXISTS `penguins`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `penguins` (
`id` int(11) NOT NULL,
`myval` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
The output is cluttered by a number of executional-condition tokens above and below. You can filter them out if you don't want them in the next step.
输出被上方和下方的许多执行条件标记弄得杂乱无章。如果您在下一步中不想要它们,您可以将它们过滤掉。
Step 4 (Optional), filter out those extra executional-condition tokens this way:
第 4 步(可选),通过这种方式过滤掉那些额外的执行条件标记:
mysqldump --no-data --skip-comments --compact --host=your_database_hostname_or_ip.com -u your_username --password=your_password your_database_name penguins > penguins.sql
Which produces final output:
产生最终输出:
eric@dev /home/el $ cat penguins.sql
DROP TABLE IF EXISTS `penguins`;
CREATE TABLE `penguins` (
`id` int(11) NOT NULL,
`myval` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
回答by Samir Mangroliya
Run query is sql tab
运行查询是 sql 选项卡
SHOW CREATE TABLE tableName
显示创建表表名
Click on
点击
+Options -> Choose Full texts -> Click on Go
+Options -> 选择全文 -> 点击 Go
Copy Create Table query and paste where you want to create new table.
复制创建表查询并粘贴到要创建新表的位置。
回答by Eric Leschinski
Query information_schema.columns directly:
直接查询information_schema.columns:
select * from information_schema.columns
where table_name = 'your_table' and table_schema = 'your_database'
回答by Narasimman V
This may be a late reply. But it may help others. It is very simple in MY SQL Workbench ( I am using Workbench version 6.3 and My SQL Version 5.1 Community edition): Right click on the table for which you want the create script, select 'Copy to Clipboard --> Create Statement' option. Simply paste in any text editor you want to get the create script.
这可能是一个迟到的答复。但它可能会帮助其他人。在 MY SQL Workbench 中非常简单(我使用的是 Workbench 6.3 版和 My SQL 5.1 社区版):右键单击要为其创建脚本的表,选择“复制到剪贴板 --> 创建语句”选项。只需粘贴您想要获取创建脚本的任何文本编辑器。
回答by falcon_01
Using PHP Function.
使用 PHP 函数。
Of course query function ($this->model) you have to change to your own.
当然查询函数($this->model)你得改成你自己的。
/**
* Creating a copy table based on the current one
*
* @param type $table_to_copy
* @param type $new_table_name
* @return type
* @throws Exception
*/
public function create($table_to_copy, $new_table_name)
{
$sql = "SHOW CREATE TABLE ".$table_to_copy;
$res = $this->model->queryRow($sql, PDO::FETCH_ASSOC);
if(!filled($res['Create Table']))
throw new Exception('Could not get the create code for '.$table_to_copy);
$newCreateSql = preg_replace(array(
'@CREATE TABLE `'.$table_to_copy.'`@',
'@KEY `'.$table_to_copy.'(.*?)`@',
'@CONSTRAINT `'.$table_to_copy.'(.*?)`@',
'@AUTO_INCREMENT=(.*?) @',
), array(
'CREATE TABLE `'.$new_table_name.'`',
'KEY `'.$new_table_name.'`',
'CONSTRAINT `'.$new_table_name.'`',
'AUTO_INCREMENT=1 ',
), $res['Create Table']);
return $this->model->exec($newCreateSql);
}
回答by Sumit kumar
I found another way to export table in sql file.
我找到了另一种在 sql 文件中导出表的方法。
Suppose my table is abs_item_variations
假设我的桌子是 abs_item_variations
abs_item_variations ->structure -> propose table structure -> export -> Go
回答by Dhaval Mistry
Export whole database select format as SQL. Now, open that SQL file which you have downloaded using notepad, notepad++ or any editor. You will see all the tables and insert queries of your database. All scripts will be available there.
将整个数据库选择格式导出为 SQL。现在,使用记事本、记事本++或任何编辑器打开您下载的 SQL 文件。您将看到数据库的所有表和插入查询。所有脚本都将在那里可用。
回答by BJ_
One more way. Select the target table in the left panel in phpMyAdmin, click on Export tab, unselect Data block and click on Go button.
还有一种方式。在 phpMyAdmin 的左侧面板中选择目标表,单击“导出”选项卡,取消选择“数据块”并单击“执行”按钮。