如何解决如何在phpMyAdmin中创建此关系?
| 我正在跟着一本书,要我在数据库中创建以下关系:ALTER TABLE \'tbl_issue\' ADD CONSTRAINT \'FK_issue_project\' FOREIGN KEY (\'project_id\') REFERENCES \'tbl_project\' (\'id\') ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE \'tbl_issue\' ADD CONSTRAINT \'FK_issue_owner\' FOREIGN KEY (\'owner_id\') REFERENCES \'tbl_user\' (\'id\') ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE \'tbl_issue\' ADD CONSTRAINT \'FK_issue_requester\' FOREIGN KEY (\'requester_id\') REFERENCES \'tbl_user\' (\'id\') ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE \'tbl_project_user\' ADD CONSTRAINT \'FK_project_ user\' FOREIGN KEY (\'project_id\') REFERENCES \'tbl_project\' (\'id\') ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE \'tbl_project_user\' ADD CONSTRAINT \'FK_user_ project\' FOREIGN KEY (\'user_id\') REFERENCES \'tbl_user\' (\'id\') ON DELETE CASCADE ON UPDATE RESTRICT;
当我尝试运行sql时,出现如下错误:
#1064 - You have an error in your sql Syntax; check the manual that corresponds to your MysqL server version for the right Syntax to use near \'\'tbl_issue\' ADD CONSTRAINT \'FK_issue_project\' FOREIGN KEY (\'project_id\') REFEREN\' at line 1
因此,我想尝试(在视觉上)在PHPMyAdmin中进行操作。你是怎样做的?
DB sql:
-- PHPMyAdmin sql Dump
-- version 3.3.9.2
-- http://www.PHPmyadmin.net
--
-- Host: localhost
-- Generation Time: May 24,2011 at 01:27 PM
-- Server version: 5.5.9
-- PHP Version: 5.3.5
SET sql_mode=\"NO_AUTO_VALUE_ON_ZERO\";
--
-- Database: `trackstar`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_issue`
--
CREATE TABLE `tbl_issue` (
`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(256) NOT NULL,`description` varchar(2000) DEFAULT NULL,`project_id` int(11) DEFAULT NULL,`type_id` int(11) DEFAULT NULL,`status_id` int(11) DEFAULT NULL,`owner_id` int(11) DEFAULT NULL,`requester_id` int(11) DEFAULT NULL,`create_time` datetime DEFAULT NULL,`create_user_id` int(11) DEFAULT NULL,`update_time` datetime DEFAULT NULL,`update_user_id` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Dumping data for table `tbl_issue`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_project`
--
CREATE TABLE `tbl_project` (
`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(128) DEFAULT NULL,`description` text,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Dumping data for table `tbl_project`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_project_user`
--
CREATE TABLE `tbl_project_user` (
`project_id` int(11) NOT NULL,`user_id` int(11) NOT NULL,PRIMARY KEY (`project_id`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `tbl_project_user`
--
-- --------------------------------------------------------
--
-- Table structure for table `tbl_user`
--
CREATE TABLE `tbl_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,`email` varchar(256) NOT NULL,`username` varchar(256) DEFAULT NULL,`password` varchar(256) DEFAULT NULL,`last_login_time` datetime DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Dumping data for table `tbl_user`
--
解决方法
您应该使用反引号,如下所示:
ALTER TABLE `tbl_issue`
ADD CONSTRAINT `FK_issue_project` FOREIGN KEY (`project_id`)
REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
,从表名周围删除引号-引号使MySQL认为您正在更改字符串而不是表:
ALTER TABLE tbl_issue ADD CONSTRAINT \'FK_issue_project\' FOREIGN KEY (\'project_id\') REFERENCES tbl_project (\'id\') ON DELETE CASCADE ON UPDATE RESTRICT;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。