Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Field 'id' doesn't have a default value?

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' ); 
like image 289
Tomas Albertsson Avatar asked Sep 16 '14 09:09

Tomas Albertsson


People also ask

Does not have a default value MySQL?

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.

How do I create an existing column automatically increment in MySQL?

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.

Why Autoincrement is not working?

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.


2 Answers

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' ); 
like image 191
fedorqui 'SO stop harming' Avatar answered Sep 28 '22 11:09

fedorqui 'SO stop harming'


There are 2 solutions mentioned below:

Solution 1

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.

Solution 2

If Solution-1 is not working then try Solution-2 as given in below steps:

  1. Run MySQL Administrator tool as Administrator.
  2. Then go to Startup Variable.
  3. Then go to the Advance tab.
  4. find SQL Mode and remove the STRICT_ALL_TABLES and/or STRICT_TRANS_TABLES and then Click on Apply Changes.
  5. Restart MySQL Server.
  6. Done.

Note: I have tested these solutions in MySQL Server 5.7

like image 23
Renish Aghera Avatar answered Sep 28 '22 09:09

Renish Aghera