gpt4 book ai didi

php - 使用 PHP PDO 创建数据库时出现问题

转载 作者:行者123 更新时间:2023-11-29 02:07:49 26 4
gpt4 key购买 nike

我的 PHP 应用程序中的 SQL 查询有问题。当用户第一次访问它时,应用程序执行此查询以创建所有数据库:

CREATE TABLE `databases` (
`id` bigint(20) NOT NULL auto_increment,
`driver` varchar(45) NOT NULL,
`server` text NOT NULL,
`user` text NOT NULL,
`password` text NOT NULL,
`database` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

-- --------------------------------------------------------

--
-- Table structure for table `modules`
--

CREATE TABLE `modules` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`title` varchar(100) NOT NULL,
`type` varchar(150) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=29 ;

-- --------------------------------------------------------

--
-- Table structure for table `modules_data`
--

CREATE TABLE `modules_data` (
`id` bigint(20) NOT NULL auto_increment,
`module_id` bigint(20) unsigned NOT NULL,
`key` varchar(150) NOT NULL,
`value` tinytext,
PRIMARY KEY (`id`),
KEY `fk_modules_data_modules` (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=184 ;

-- --------------------------------------------------------

--
-- Table structure for table `modules_position`
--

CREATE TABLE `modules_position` (
`user_id` bigint(20) unsigned NOT NULL,
`tab_id` bigint(20) unsigned NOT NULL,
`module_id` bigint(20) unsigned NOT NULL,
`column` smallint(1) default NULL,
`line` smallint(1) default NULL,
PRIMARY KEY (`user_id`,`tab_id`,`module_id`),
KEY `fk_modules_order_users` (`user_id`),
KEY `fk_modules_order_tabs` (`tab_id`),
KEY `fk_modules_order_modules` (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `tabs`
--

CREATE TABLE `tabs` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`title` varchar(60) NOT NULL,
`columns` smallint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

-- --------------------------------------------------------

--
-- Table structure for table `tabs_has_modules`
--

CREATE TABLE `tabs_has_modules` (
`tab_id` bigint(20) unsigned NOT NULL,
`module_id` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`tab_id`,`module_id`),
KEY `fk_tabs_has_modules_tabs` (`tab_id`),
KEY `fk_tabs_has_modules_modules` (`module_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`login` varchar(60) NOT NULL,
`password` varchar(64) NOT NULL,
`email` varchar(100) NOT NULL,
`name` varchar(250) default NULL,
`user_level` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_users_user_levels` (`user_level`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

-- --------------------------------------------------------

--
-- Table structure for table `users_has_tabs`
--

CREATE TABLE `users_has_tabs` (
`user_id` bigint(20) unsigned NOT NULL,
`tab_id` bigint(20) unsigned NOT NULL,
`order` smallint(2) NOT NULL,
`columns_width` varchar(255) default NULL,
PRIMARY KEY (`user_id`,`tab_id`),
KEY `fk_users_has_tabs_users` (`user_id`),
KEY `fk_users_has_tabs_tabs` (`tab_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `user_levels`
--

CREATE TABLE `user_levels` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`level` smallint(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

-- --------------------------------------------------------

--
-- Table structure for table `user_meta`
--

CREATE TABLE `user_meta` (
`id` bigint(20) unsigned NOT NULL auto_increment,
`user_id` bigint(20) unsigned default NULL,
`key` varchar(150) NOT NULL,
`value` longtext NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_user_meta_users` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `modules_data`
--
ALTER TABLE `modules_data`
ADD CONSTRAINT `fk_modules_data_modules` FOREIGN KEY (`module_id`) REFERENCES `modules` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

--
-- Constraints for table `modules_position`
--
ALTER TABLE `modules_position`
ADD CONSTRAINT `fk_modules_order_modules` FOREIGN KEY (`module_id`) REFERENCES `modules` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_modules_order_tabs` FOREIGN KEY (`tab_id`) REFERENCES `tabs` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_modules_order_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

--
-- Constraints for table `users`
--
ALTER TABLE `users`
ADD CONSTRAINT `fk_users_user_levels` FOREIGN KEY (`user_level`) REFERENCES `user_levels` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Constraints for table `user_meta`
--
ALTER TABLE `user_meta`
ADD CONSTRAINT `fk_user_meta_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION;

INSERT INTO `user_levels` VALUES(1, 10);
INSERT INTO `user_levels` VALUES(2, 1);


INSERT INTO `users` VALUES(1, 'admin', 'password', 'changethis@testing.com', NULL, 1);
INSERT INTO `user_meta` VALUES (NULL, 1, 'last_tab', 1);

在某些环境中我得到这个错误:

SQLSTATE[HY000]: General error: 1005 Can't create table 'dms.databases' (errno: 150)

我尝试了所有可以在 Google 上找到的方法,但没有任何效果。

奇怪的是,如果我在 PhpMyAdmin 中运行这个查询,他会创建我的数据库,没有任何错误。

最佳答案

问题很可能出在您查询的约束部分。

InnoDB 文档中提到错误 150:

If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.

If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. Similarly, if an ALTER TABLE fails and it refers to error 150, that means a foreign key definition would be incorrectly formed for the altered table. You can use SHOW ENGINE INNODB STATUS to display a detailed explanation of the most recent InnoDB foreign key error in the server.

来源:http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

如果引用的定义不相同(即一个是有符号的,一个是无符号的 bigint)或将多个列的唯一索引与它们混合,也会发生这种情况。

关于php - 使用 PHP PDO 创建数据库时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2884822/

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