By Alan Beaulieu
Publisher: O'Reilly Media
Released: April 2009
ISBN: 9780596520830
上面的书中有代码来创建一个MySQL数据库,他们在这里发布了: http://examples.oreilly.com/9780596520847/LearningSQLExample.sql
当我运行代码时,一切“看起来”都没问题,但只有在我删除这些代码行之后:
/* recreate employee self-referencing foreign key */
alter table employee add constraint fk_e_emp_id
foreign key (superior_emp_id) references employee (emp_id);
我相信我很清楚这段代码是做什么的,并且它是在create table语句中执行的 - 并且似乎从未被撤消过(因此我不清楚为什么需要导致错误的代码);可能是错的。
这是我获得的那些代码行的错误消息,无论它是否在上面链接的代码中执行,或者在下面的情况下,在从提供的代码中删除它后运行,运行提供的代码(它创建数据库),然后在创建的数据库上独立运行它:
Enter password: **********************************************************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.1.53-community MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use bank
Database changed
mysql> alter table employee add constraint fk_e_emp_id
-> foreign key (superior_emp_id) references employee (emp_id);
ERROR 1005 (HY000): Can't create table 'bank.#sql-80c_11' (errno: 121)
mysql>exit
更新
万一你想知道,因为我发现代码有点奇怪,本书使用链接代码的说明基本上是:
create database bank;
use bank
source C:\LearningSQLExample.sql;
答案 0 :(得分:2)
我认为这句话没有理由存在。
他们正在使用临时表来确保只有有效的FK被分配给superior_emp_id列,所以我无法想象需要删除约束然后重新添加它。我猜他们最初有代码可以做到这一点,因为填充employees表的方式,然后将代码重新安排到现在的位置。呃,即使这个解释也没有意义。
无论如何,在发布之前,他们至少不会简单地浏览示例代码。
答案 1 :(得分:0)
删除employee表中unsigned_emp_id和emp_id的无符号减速,然后重试。
在员工的表格创建中:
create table employee
(emp_id smallint unsigned not null auto_increment,
fname varchar(20) not null,
lname varchar(20) not null,
start_date date not null,
end_date date,
superior_emp_id smallint unsigned,
dept_id smallint unsigned,
title varchar(20),
assigned_branch_id smallint unsigned,
constraint fk_e_emp_id
foreign key (superior_emp_id) references employee (emp_id),
constraint fk_dept_id
foreign key (dept_id) references department (dept_id),
constraint fk_e_branch_id
foreign key (assigned_branch_id) references branch (branch_id),
constraint pk_employee primary key (emp_id)
);
empid标记为unsigned
:emp_id smallint unsigned not null auto_increment
请尝试删除此unsigned
。
答案 2 :(得分:-1)
2016年6月,我在Win7 PC上使用MySQL Community Server 5.7.13
首先,
添加var fs = require('fs-extra')
fs.copy('/tmp/myfile', '/tmp/mynewfile', function (err) {
if (err) return console.error(err)
console.log("success!")
}) // copies file
fs.copy('/tmp/mydir', '/tmp/mynewdir', function (err) {
if (err) return console.error(err)
console.log('success!')
}) // copies directory, even if it has subdirectories or file
,如下所示
SET SQL_SAFE_UPDATES = 0;
创建临时表
/* create data for self-referencing foreign key 'superior_emp_id' */
SET SQL_SAFE_UPDATES = 0;
等
其次, 从代码中显示的任何位置删除未签名。
代码就像这些mod的魅力一样。