MySQL无法识别外键

时间:2015-12-07 02:44:12

标签: mysql database foreign-keys

我正在创建一个用户可以创建登录的数据库。如果他们已登录,他们可以通过表单提交数据,如果有意义,他们的数据应该与他们的主键/登录信息相匹配。

有两个表,Login_InfoNumbers。第一个表的主键是UserID,我也将UserId放在Numbers表中:

CREATE TABLE `Login_Info` (

`UserID` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`Username` varchar(25),
`Password` varchar (25)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

CREATE TABLE `Numbers` (

`UserID` int(11),
`Savings` varchar(25),
`Goal1` varchar(25),
`Goal2` varchar(25),
`Goal3` varchar(25),
`Cost1` varchar(25),
`Cost2` varchar(25),
`Cost3` varchar(25),
`Weight1` varchar(25),
`Weight2` varchar(25),
`Weight3` varchar(25),
`TotalCost` varchar(25),
`PercentRound` varchar(25)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

要使UserID成为外键,我在第二个表中创建了一个名为EntryNumber的新属性作为主键,然后将UserId作为外键:

CREATE TABLE `Numbers` (

`EntryNumber` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`UserID` FOREIGN KEY REFERENCES Login_Info(UserID),
`Savings` varchar(25),
`Goal1` varchar(25),
`Goal2` varchar(25),
`Goal3` varchar(25),
`Cost1` varchar(25),
`Cost2` varchar(25),
`Cost3` varchar(25),
`Weight1` varchar(25),
`Weight2` varchar(25),
`Weight3` varchar(25),
`TotalCost` varchar(25),
`PercentRound` varchar(25)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

现在,当我尝试添加数据库时,我收到一条消息,指出FOREIGN KEY REFERENCES的语法不正确。我已经尝试了很多方法来添加FK,但我总是得到一条FK语法不正确的消息。我不确定我的错误可能在这里。我很感激,如果有人可以看看,让我知道我在通过另一个表中使用的PK创建FK时缺少的元素,以创建两个表之间的链接。

3 个答案:

答案 0 :(得分:1)

MySQL foreign key documentation中,它清楚地表明语法与你​​所写的完全不同:

2015-12-08 19:19:30 [scrapy] INFO: Scrapy 1.0.3.post6+g2d688cd started (bot: jpg)
2015-12-08 19:19:30 [scrapy] INFO: Optional features available: ssl, http11
2015-12-08 19:19:30 [scrapy] INFO: Overridden settings: {'NEWSPIDER_MODULE': 'jpg.spiders', 'SPIDER_MODULES': ['jpg.spiders'], 'COOKIES_ENABLED': False, 'DOWNLOAD_DELAY': 3, 'BOT_NAME': 'jpg'}
2015-12-08 19:19:30 [scrapy] INFO: Enabled extensions: CloseSpider, TelnetConsole, LogStats, CoreStats, SpiderState
2015-12-08 19:19:30 [scrapy] INFO: Enabled downloader middlewares: HttpAuthMiddleware, DownloadTimeoutMiddleware, UserAgentMiddleware, RetryMiddleware, DefaultHeadersMiddleware, MetaRefreshMiddleware, HttpCompressionMiddleware, RedirectMiddleware, ChunkedTransferMiddleware, DownloaderStats
2015-12-08 19:19:30 [scrapy] INFO: Enabled spider middlewares: HttpErrorMiddleware, OffsiteMiddleware, RefererMiddleware, UrlLengthMiddleware, DepthMiddleware
2015-12-08 19:19:30 [scrapy] INFO: Enabled item pipelines: ImagesPipeline
2015-12-08 19:19:30 [scrapy] INFO: Spider opened
2015-12-08 19:19:30 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
2015-12-08 19:19:30 [scrapy] DEBUG: Telnet console listening on 127.0.0.1:6023
2015-12-08 19:19:31 [scrapy] DEBUG: Crawled (200) <GET http://www.kevinsmedia.com/km/mp3z/Fluke/Risotto/> (referer: None)
2015-12-08 19:19:31 [scrapy] DEBUG: Scraped from <200 http://www.kevinsmedia.com/km/mp3z/Fluke/Risotto/>
{'images': []}
2015-12-08 19:19:31 [scrapy] INFO: Closing spider (finished)
2015-12-08 19:19:31 [scrapy] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 254,
 'downloader/request_count': 1,
 'downloader/request_method_count/GET': 1,
 'downloader/response_bytes': 2975,
 'downloader/response_count': 1,
 'downloader/response_status_count/200': 1,
 'finish_reason': 'finished',
 'finish_time': datetime.datetime(2015, 12, 8, 22, 19, 31, 294139),
 'item_scraped_count': 1,
 'log_count/DEBUG': 3,
 'log_count/INFO': 7,
 'response_received_count': 1,
 'scheduler/dequeued': 1,
 'scheduler/dequeued/memory': 1,
 'scheduler/enqueued': 1,
 'scheduler/enqueued/memory': 1,
 'start_time': datetime.datetime(2015, 12, 8, 22, 19, 30, 619918)}
2015-12-08 19:19:31 [scrapy] INFO: Spider closed (finished)

所以,你可能需要这样的东西:

CREATE TABLE parent (
    id INT NOT NULL,
    PRIMARY KEY (id)
) ENGINE=INNODB;

CREATE TABLE child (
    id INT, 
    parent_id INT,
    INDEX par_ind (parent_id),
    FOREIGN KEY (parent_id) 
        REFERENCES parent(id)
        ON DELETE CASCADE
) ENGINE=INNODB;

答案 1 :(得分:0)

您需要将表定义为:

CREATE TABLE `Numbers` (

`EntryNumber` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`UserID` int(11) not null,
`Savings` varchar(25),
`Goal1` varchar(25),
`Goal2` varchar(25),
`Goal3` varchar(25),
`Cost1` varchar(25),
`Cost2` varchar(25),
`Cost3` varchar(25),
`Weight1` varchar(25),
`Weight2` varchar(25),
`Weight3` varchar(25),
`TotalCost` varchar(25),
`PercentRound` varchar(25),

FOREIGN KEY (UserID) REFERENCES Login_Info(UserID) 

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

答案 2 :(得分:0)

可能您需要将存储引擎更改为兼容的存储引擎,例如 InnoDB。

在 phpmyadmin 中,您可以在“操作”选项卡中找到它。