I am new to this SQL; I have seen similar question with much bigger programs, which I can't understand at the moment. I am making a database for games of cards to use in my homepage.
I am using MySQL Workbench on Windows. The error I get is:
Error Code: 1364. Field 'id' doesn't have a default value
CREATE TABLE card_games ( nafnleiks varchar(50), leiklysing varchar(3000), prentadi varchar(1500), notkunarheimildir varchar(1000), upplysingar varchar(1000), ymislegt varchar(500), id int(11) PK ); insert into card_games (nafnleiks, leiklysing, prentadi, notkunarheimildir, upplysingar, ymislegt) values('Svartipétur', 'Leiklýsingu vantar', 'Er prentað í: Þórarinn Guðmundsson (2010). Spilabókin - Allir helstu spilaleikir og spil.', 'Heimildir um notkun: Árni Sigurðsson (1951). Hátíðir og skemmtanir fyrir hundrað árum', 'Aðrar upplýsingar', 'ekkert hér sem stendur' ); values('Handkurra', 'Leiklýsingu vantar', 'Er prentað í: Þórarinn Guðmundsson (2010). Spilabókin - Allir helstu spilaleikir og spil.', 'Heimildir um notkun', 'Aðrar upplýsingar', 'ekkert her sem stendur' ); values('Veiðimaður', 'Leiklýsingu vantar', 'Þórarinn Guðmundsson (2010). Spilabókin - Allir helstu spilaleikir og spil. Reykjavík: Bókafélagið. Bls. 19-20.', 'vantar', 'vantar', 'vantar' );
This is caused by MySQL having a strict mode set which won't allow INSERT or UPDATE commands with empty fields where the schema doesn't have a default value set. There are a couple of fixes for this. First 'fix' is to assign a default value to your schema.
When you add an AUTO_INCREMENT column, column values are filled in with sequence numbers automatically. For MyISAM tables, you can set the first sequence number by executing SET INSERT_ID= value before ALTER TABLE or by using the AUTO_INCREMENT= value table option.
Basically this is a bug in MySQL that causes the problem but a work around is simple. The problem happens when the Auto-Increment value of a table grows beyond the limit. Just run this SQL query in MySQL to fix the bug. Table_name is the name of the table where you found the error while inserting data into.
As id
is the primary key, you cannot have different rows with the same value. Try to change your table so that the id
is auto incremented:
id int NOT NULL AUTO_INCREMENT
and then set the primary key as follows:
PRIMARY KEY (id)
All together:
CREATE TABLE card_games ( id int(11) NOT NULL AUTO_INCREMENT, nafnleiks varchar(50), leiklysing varchar(3000), prentadi varchar(1500), notkunarheimildir varchar(1000), upplysingar varchar(1000), ymislegt varchar(500), PRIMARY KEY (id));
Otherwise, you can indicate the id
in every insertion, taking care to set a different value every time:
insert into card_games (id, nafnleiks, leiklysing, prentadi, notkunarheimildir, upplysingar, ymislegt) values(1, 'Svartipétur', 'Leiklýsingu vantar', 'Er prentað í: Þórarinn Guðmundsson (2010). Spilabókin - Allir helstu spilaleikir og spil.', 'Heimildir um notkun: Árni Sigurðsson (1951). Hátíðir og skemmtanir fyrir hundrað árum', 'Aðrar upplýsingar', 'ekkert hér sem stendur' );
There are 2 solutions mentioned below:
MySQL is most likely in STRICT SQL mode. Try to execute SQL query SET GLOBAL sql_mode=''
or edit your my.cnf / my.ini to make sure you aren't setting STRICT_ALL_TABLES
and/or STRICT_TRANS_TABLES
.
If Solution-1 is not working then try Solution-2 as given in below steps:
STRICT_ALL_TABLES
and/or STRICT_TRANS_TABLES
and then Click on Apply Changes.Note: I have tested these solutions in MySQL Server 5.7
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With