为什么我不能为该列分配外键?
Why I cannot assign foreign key to this column?
我有一个主要 table 用户
create table `users` (
`id` int (10),
`first_name` varchar (192),
`last_name` varchar (192),
`password` varchar (96),
`phone` int (10),
`email` varchar (768),
`date_created` timestamp
);
然后我有一个 table 帐户,外键 user_id 指的是主要 table 用户
create table `accounts` (
`id` int (10),
`user_id` int (11),
`account_number` bigint (12),
`account_type` varchar (192),
`balance` double ,
`currency` varchar (9),
`date_created` timestamp
);
最后,我有一个 table 的交易,但是我也无法将外键分配给用户 ID
create table `transactions` (
`id` int (10),
`user_id` int (11),
`from_account_id` int (12),
`to_account_id` int (11),
`amount` double ,
`currency` varchar (9),
`timestamp` timestamp
);
每当我尝试从交易 table 向 user_id 添加外键以引用帐户中的 user_id 或用户 table 中的 ID 时,我都会遇到同样的错误两次。
Cannot add or update a child row: a foreign key constraint fails
(bank
.#sql-34ec_2
, CONSTRAINT #sql-34ec_2_ibfk_3
FOREIGN KEY
(user_id
) REFERENCES accounts
(user_id
))
我怎样才能避免这种情况,我还能做些什么来正确引用特定的单元格?
截断所有表并为此值设置外键。
您的数据类型可能不同。 10 与 11。显示您正在执行的代码
我有一个主要 table 用户
create table `users` (
`id` int (10),
`first_name` varchar (192),
`last_name` varchar (192),
`password` varchar (96),
`phone` int (10),
`email` varchar (768),
`date_created` timestamp
);
然后我有一个 table 帐户,外键 user_id 指的是主要 table 用户
create table `accounts` (
`id` int (10),
`user_id` int (11),
`account_number` bigint (12),
`account_type` varchar (192),
`balance` double ,
`currency` varchar (9),
`date_created` timestamp
);
最后,我有一个 table 的交易,但是我也无法将外键分配给用户 ID
create table `transactions` (
`id` int (10),
`user_id` int (11),
`from_account_id` int (12),
`to_account_id` int (11),
`amount` double ,
`currency` varchar (9),
`timestamp` timestamp
);
每当我尝试从交易 table 向 user_id 添加外键以引用帐户中的 user_id 或用户 table 中的 ID 时,我都会遇到同样的错误两次。
Cannot add or update a child row: a foreign key constraint fails (
bank
.#sql-34ec_2
, CONSTRAINT#sql-34ec_2_ibfk_3
FOREIGN KEY (user_id
) REFERENCESaccounts
(user_id
))
我怎样才能避免这种情况,我还能做些什么来正确引用特定的单元格?
截断所有表并为此值设置外键。
您的数据类型可能不同。 10 与 11。显示您正在执行的代码