Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
673 views
in Technique[技术] by (71.8m points)

mysql - 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',
'Leiklysingu vantar',
'Er prentae í: Tórarinn Guemundsson (2010). Spilabókin - Allir helstu spilaleikir og spil.',
'Heimildir um notkun: árni Siguresson (1951). Hátíeir og skemmtanir fyrir hundrae árum',
'Aerar upplysingar',
'ekkert hér sem stendur'
);

values('Handkurra',
'Leiklysingu vantar',
'Er prentae í: Tórarinn Guemundsson (2010). Spilabókin - Allir helstu spilaleikir og spil.',
'Heimildir um notkun', 
'Aerar upplysingar',
'ekkert her sem stendur'
);

values('Veieimaeur',
'Leiklysingu vantar',
'Tórarinn Guemundsson (2010). Spilabókin - Allir helstu spilaleikir og spil. Reykjavík: Bókafélagie. Bls. 19-20.',
'vantar',
'vantar',
'vantar'
);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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', 'Leiklysingu vantar', 'Er prentae í: Tórarinn Guemundsson (2010). Spilabókin - Allir helstu spilaleikir og spil.', 'Heimildir um notkun: árni Siguresson (1951). Hátíeir og skemmtanir fyrir hundrae árum', 'Aerar upplysingar', 'ekkert hér sem stendur' );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...