gpt4 book ai didi

php - 用php获取mysql表结构

转载 作者:行者123 更新时间:2023-11-29 06:12:00 25 4
gpt4 key购买 nike

我有一个脚本,它采用数据库并为数据库生成类似导出的查询作为备份。我需要这个脚本是有目的的。我的 php 代码是:

mysql_query("SHOW CREATE TABLE `myTable`");

数据库中有17张表。我正在为所有 17 个表运行 for 循环。我得到了我不想要的带有外键的创建表查询。它返回像-

CREATE TABLE `customers` (
`person_id` int(10) NOT NULL,
`account_number` varchar(255) DEFAULT NULL,
`taxable` int(1) NOT NULL DEFAULT '1',
`deleted` int(1) NOT NULL DEFAULT '0',
UNIQUE KEY `account_number` (`account_number`),
KEY `person_id` (`person_id`),
CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`person_id`) REFERENCES `people` (`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

由于外键的原因,在运行创建表的查询时会出现错误。有没有什么方法可以获取没有外键的sql?就像-

CREATE TABLE IF NOT EXISTS `customers` (
`person_id` int(10) NOT NULL,
`account_number` varchar(255) DEFAULT NULL,
`taxable` int(1) NOT NULL DEFAULT '1',
`deleted` int(1) NOT NULL DEFAULT '0',
UNIQUE KEY `account_number` (`account_number`),
KEY `person_id` (`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

最佳答案

在创建表期间禁用外键。

SET FOREIGN_KEY_CHECKS=0

CREATE TABLE a
CREATE TABLE b
...

再次启用 key

SET FOREIGN_KEY_CHECKS=1

关于php - 用php获取mysql表结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8667945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com