获取 SQL 外键时遇到问题,因为它显示错误 1364“字段没有默认值”?
刚刚接触 SQL 的学生。我知道类似的问题已经在这里被问过多次,但我似乎无法弄清楚问题出在哪里。我正在做一个记录血压的基本项目。我的想法是,以下代码将初始化用户表(以跟踪登录者)和读数表(血压读数)的架构:
CREATE DATABASE IF NOT EXISTS bloodpressure;
USE bloodpressure;
CREATE TABLE IF NOT EXISTS appusers (
userid INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
username VARCHAR(25) UNIQUE NOT NULL,
psword VARCHAR(25) NOT NULL
);
CREATE TABLE IF NOT EXISTS bpreadings (
reading INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
user_id INT NOT NULL,
sys DECIMAL(3 , 0 ) NOT NULL,
dia DECIMAL(3 , 0 ) NOT NULL,
pulse DECIMAL(3 , 0 ) NOT NULL,
FOREIGN KEY (user_id)
REFERENCES appusers (userid)
);
我遇到的问题是错误代码:
Error Code: 1364. Field 'user_id' doesnt have a default value
这是在我之后run:
INSERT INTO appusers (username, psword) VALUES ("user1", "pwd1");
运行正确,然后尝试 run:
INSERT INTO bpreadings (sys, dia, pulse) VALUES (128, 72, 88);
我最初的想法是使用 FK 从 appusers 中的 userid 填充它,但它不会保留。我认为我不需要 Readings.user_id 的默认值。我还检查了插入带有数据的 appusers,并且主键 userid 填充良好并且自动增量正确。我只是在努力让它延续下去。这是我遗漏的简单内容还是设计中的整体缺陷?
先感谢您!
Student new to SQL. I understand similar question has been asked multiple times on here but i can not seem to figure out where the issue is. I was making a basic project that logs blood pressure. My thinking was that the following code would initialize the schema of a user table (to track who is logged in) and a readings table (blood pressure readings):
CREATE DATABASE IF NOT EXISTS bloodpressure;
USE bloodpressure;
CREATE TABLE IF NOT EXISTS appusers (
userid INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
username VARCHAR(25) UNIQUE NOT NULL,
psword VARCHAR(25) NOT NULL
);
CREATE TABLE IF NOT EXISTS bpreadings (
reading INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
user_id INT NOT NULL,
sys DECIMAL(3 , 0 ) NOT NULL,
dia DECIMAL(3 , 0 ) NOT NULL,
pulse DECIMAL(3 , 0 ) NOT NULL,
FOREIGN KEY (user_id)
REFERENCES appusers (userid)
);
The issue i'm running into to is the error code:
Error Code: 1364. Field 'user_id' doesnt have a default value
This is after I run:
INSERT INTO appusers (username, psword) VALUES ("user1", "pwd1");
which runs correctly and then try to run:
INSERT INTO bpreadings (sys, dia, pulse) VALUES (128, 72, 88);
my original thinking was to use a FK to fill it from the userid in appusers but it does not carry over. I wouldnt think I would need a default value on readings.user_id. I also checked inserting appusers with data and the primary key userid fills fine and autoincrements correctly. I'm just struggling trying to get it to carry over. Is it something simple i am missing or is it an overall flaw in the design?
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您将user_id声明为not null属性,这意味着它不能为空,您需要将查询更改为类似的内容:
替代方法是删除表格创建的“非零”条件,看起来像这样:
It is because you declared user_id as NOT NULL attribute which means it cannot be empty, you need to change your query into something like this:
The alternative is to remove your "NOT NULL" condition on table creation which gonna be looks like this: