Search code examples
sql-serverdatabasesql-insertcreate-table

What is wrong with this insert statement?


Given the following two tables:

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))
)

I am trying to execute the following insert statement:

 INSERT INTO Normal_Users VALUES('testemail@gmail.com', 'Adam', 'Robert', '05-04-1990')

But I am getting the following error:

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.

What am I doing wrong?


Solution

  • You have to populate Members table first.

    INSERT INTO Members(email) VALUES('testemail@gmail.com')
    INSERT INTO Normal_Users VALUES('testemail@gmail.com', 'Adam', 'Robert', '05-04-1990')