这个插入语句有什么问题?
What is wrong with this insert statement?
给定以下两个表:
CREATE TABLE Members(
email VARCHAR(20) PRIMARY KEY,
password VARCHAR(20),
pref_game_genre VARCHAR(20)
)
CREATE TABLE Normal_Users(
email VARCHAR(20) PRIMARY KEY FOREIGN KEY REFERENCES Members,
first_name VARCHAR(20),
last_name VARCHAR(20),
date_of_birth date,
age as (YEAR(CURRENT_TIMESTAMP) - YEAR(date_of_birth))
)
我正在尝试执行以下插入语句:
INSERT INTO Normal_Users VALUES('testemail@gmail.com', 'Adam', 'Robert', '05-04-1990')
但我收到以下错误:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Normal_Us__email__74994623". The conflict occurred in database "DatabasesProject", table "dbo.Members", column 'email'.
The statement has been terminated.
我做错了什么?
您必须先填充 Members
table。
INSERT INTO Members(email) VALUES('testemail@gmail.com')
INSERT INTO Normal_Users VALUES('testemail@gmail.com', 'Adam', 'Robert', '05-04-1990')
在 Normal_Users 上,您将电子邮件设置为主键和外键。
更好的解决方案是只将电子邮件设置为外键并选择另一个作为主键(可能是生成的 ID)。
给定以下两个表:
CREATE TABLE Members(
email VARCHAR(20) PRIMARY KEY,
password VARCHAR(20),
pref_game_genre VARCHAR(20)
)
CREATE TABLE Normal_Users(
email VARCHAR(20) PRIMARY KEY FOREIGN KEY REFERENCES Members,
first_name VARCHAR(20),
last_name VARCHAR(20),
date_of_birth date,
age as (YEAR(CURRENT_TIMESTAMP) - YEAR(date_of_birth))
)
我正在尝试执行以下插入语句:
INSERT INTO Normal_Users VALUES('testemail@gmail.com', 'Adam', 'Robert', '05-04-1990')
但我收到以下错误:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Normal_Us__email__74994623". The conflict occurred in database "DatabasesProject", table "dbo.Members", column 'email'. The statement has been terminated.
我做错了什么?
您必须先填充 Members
table。
INSERT INTO Members(email) VALUES('testemail@gmail.com')
INSERT INTO Normal_Users VALUES('testemail@gmail.com', 'Adam', 'Robert', '05-04-1990')
在 Normal_Users 上,您将电子邮件设置为主键和外键。
更好的解决方案是只将电子邮件设置为外键并选择另一个作为主键(可能是生成的 ID)。