Skip to content
Advertisement

id value needs a default value according to error 1364?

I want to create two tables. practice has a AUTO_INCREMENT attachment and is a PRIMARY KEY. continued has the id entity continued_id which exists as a FOREIGN KEY that references practice(user_id). Mysql executes the code below fine until line 19, where I receive the 1364 error, stating that continued_id has no default value.

I am confused. I thought that user_id, which auto_increments, and it being the PK, would have a defining value of 1,2,3… I thought that continued_id is equivalent to user_id, and therefore its default value is 1? Perhaps I am misunderstanding how PK’s and FK’s actually work in sql?

Error:

Advertisement

Answer

Your inserts into continued need to be linked to an entry in practice. You can either do that by immediately following the insert into practice with an insert into continued using LAST_INSERT_ID() for continued_id:

or by referring to the appropriate entry in practice using an INSERT ... SELECT query:

or

Demo on dbfiddle

Note that you do not need to declare continued_id as AUTO_INCREMENT.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement