MySQL INSERT INTO statement giving ERROR CODE 1062












0















I have a F1 database and I'm trying to insert the team principal's name (from Principals table) into the Teams table, but for some reason it won't work. I'm not sure if my insert into statement is wrong, but I can't see why it would be. The code is below





CREATE TABLE Teams (
Team_Name VARCHAR(30) NOT NULL,
Driver_1 INT NULL,
Driver_2 INT NULL,
Nation VARCHAR(30) NOT NULL,
Seasons INT NOT NULL,
No_Titles INT NOT NULL DEFAULT 0,
Principal VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (Team_Name));

CREATE TABLE Principals (
Principal_No INT NOT NULL,
Principal_Name VARCHAR(30) UNIQUE NOT NULL,
Team VARCHAR(30) NULL,
Age INT NOT NULL,
Nationality VARCHAR(30) NOT NULL,
Seasons INT NOT NULL DEFAULT 0,
PRIMARY KEY (Principal_No),
FOREIGN KEY (Team) REFERENCES Teams(Team_Name));


Here's the insert statement. The Principals table has already been populated with the principal names and their corresponding teams



INSERT INTO Teams (Principal) 
SELECT Principal_Name
FROM Principals
WHERE Team IN(
SELECT Team_Name
FROM Teams);









share|improve this question

























  • Is auto increment of primary key working?

    – CS_noob
    Nov 23 '18 at 13:38











  • Sorry, I should have mentioned I'm new to SQL. So I'm not sure what you mean?

    – Clank
    Nov 23 '18 at 13:44






  • 1





    Hi there, welcome to SO. "but for some reason it won't work" this should be more specific. What was (is) the sample data you used and what was the expected output? Was there any error output or messages?

    – TrebuchetMS
    Nov 23 '18 at 13:44


















0















I have a F1 database and I'm trying to insert the team principal's name (from Principals table) into the Teams table, but for some reason it won't work. I'm not sure if my insert into statement is wrong, but I can't see why it would be. The code is below





CREATE TABLE Teams (
Team_Name VARCHAR(30) NOT NULL,
Driver_1 INT NULL,
Driver_2 INT NULL,
Nation VARCHAR(30) NOT NULL,
Seasons INT NOT NULL,
No_Titles INT NOT NULL DEFAULT 0,
Principal VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (Team_Name));

CREATE TABLE Principals (
Principal_No INT NOT NULL,
Principal_Name VARCHAR(30) UNIQUE NOT NULL,
Team VARCHAR(30) NULL,
Age INT NOT NULL,
Nationality VARCHAR(30) NOT NULL,
Seasons INT NOT NULL DEFAULT 0,
PRIMARY KEY (Principal_No),
FOREIGN KEY (Team) REFERENCES Teams(Team_Name));


Here's the insert statement. The Principals table has already been populated with the principal names and their corresponding teams



INSERT INTO Teams (Principal) 
SELECT Principal_Name
FROM Principals
WHERE Team IN(
SELECT Team_Name
FROM Teams);









share|improve this question

























  • Is auto increment of primary key working?

    – CS_noob
    Nov 23 '18 at 13:38











  • Sorry, I should have mentioned I'm new to SQL. So I'm not sure what you mean?

    – Clank
    Nov 23 '18 at 13:44






  • 1





    Hi there, welcome to SO. "but for some reason it won't work" this should be more specific. What was (is) the sample data you used and what was the expected output? Was there any error output or messages?

    – TrebuchetMS
    Nov 23 '18 at 13:44
















0












0








0








I have a F1 database and I'm trying to insert the team principal's name (from Principals table) into the Teams table, but for some reason it won't work. I'm not sure if my insert into statement is wrong, but I can't see why it would be. The code is below





CREATE TABLE Teams (
Team_Name VARCHAR(30) NOT NULL,
Driver_1 INT NULL,
Driver_2 INT NULL,
Nation VARCHAR(30) NOT NULL,
Seasons INT NOT NULL,
No_Titles INT NOT NULL DEFAULT 0,
Principal VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (Team_Name));

CREATE TABLE Principals (
Principal_No INT NOT NULL,
Principal_Name VARCHAR(30) UNIQUE NOT NULL,
Team VARCHAR(30) NULL,
Age INT NOT NULL,
Nationality VARCHAR(30) NOT NULL,
Seasons INT NOT NULL DEFAULT 0,
PRIMARY KEY (Principal_No),
FOREIGN KEY (Team) REFERENCES Teams(Team_Name));


Here's the insert statement. The Principals table has already been populated with the principal names and their corresponding teams



INSERT INTO Teams (Principal) 
SELECT Principal_Name
FROM Principals
WHERE Team IN(
SELECT Team_Name
FROM Teams);









share|improve this question
















I have a F1 database and I'm trying to insert the team principal's name (from Principals table) into the Teams table, but for some reason it won't work. I'm not sure if my insert into statement is wrong, but I can't see why it would be. The code is below





CREATE TABLE Teams (
Team_Name VARCHAR(30) NOT NULL,
Driver_1 INT NULL,
Driver_2 INT NULL,
Nation VARCHAR(30) NOT NULL,
Seasons INT NOT NULL,
No_Titles INT NOT NULL DEFAULT 0,
Principal VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (Team_Name));

CREATE TABLE Principals (
Principal_No INT NOT NULL,
Principal_Name VARCHAR(30) UNIQUE NOT NULL,
Team VARCHAR(30) NULL,
Age INT NOT NULL,
Nationality VARCHAR(30) NOT NULL,
Seasons INT NOT NULL DEFAULT 0,
PRIMARY KEY (Principal_No),
FOREIGN KEY (Team) REFERENCES Teams(Team_Name));


Here's the insert statement. The Principals table has already been populated with the principal names and their corresponding teams



INSERT INTO Teams (Principal) 
SELECT Principal_Name
FROM Principals
WHERE Team IN(
SELECT Team_Name
FROM Teams);






mysql mysql-workbench






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 13:36









TrebuchetMS

2,64811023




2,64811023










asked Nov 23 '18 at 13:30









ClankClank

32




32













  • Is auto increment of primary key working?

    – CS_noob
    Nov 23 '18 at 13:38











  • Sorry, I should have mentioned I'm new to SQL. So I'm not sure what you mean?

    – Clank
    Nov 23 '18 at 13:44






  • 1





    Hi there, welcome to SO. "but for some reason it won't work" this should be more specific. What was (is) the sample data you used and what was the expected output? Was there any error output or messages?

    – TrebuchetMS
    Nov 23 '18 at 13:44





















  • Is auto increment of primary key working?

    – CS_noob
    Nov 23 '18 at 13:38











  • Sorry, I should have mentioned I'm new to SQL. So I'm not sure what you mean?

    – Clank
    Nov 23 '18 at 13:44






  • 1





    Hi there, welcome to SO. "but for some reason it won't work" this should be more specific. What was (is) the sample data you used and what was the expected output? Was there any error output or messages?

    – TrebuchetMS
    Nov 23 '18 at 13:44



















Is auto increment of primary key working?

– CS_noob
Nov 23 '18 at 13:38





Is auto increment of primary key working?

– CS_noob
Nov 23 '18 at 13:38













Sorry, I should have mentioned I'm new to SQL. So I'm not sure what you mean?

– Clank
Nov 23 '18 at 13:44





Sorry, I should have mentioned I'm new to SQL. So I'm not sure what you mean?

– Clank
Nov 23 '18 at 13:44




1




1





Hi there, welcome to SO. "but for some reason it won't work" this should be more specific. What was (is) the sample data you used and what was the expected output? Was there any error output or messages?

– TrebuchetMS
Nov 23 '18 at 13:44







Hi there, welcome to SO. "but for some reason it won't work" this should be more specific. What was (is) the sample data you used and what was the expected output? Was there any error output or messages?

– TrebuchetMS
Nov 23 '18 at 13:44














2 Answers
2






active

oldest

votes


















1














In your INSERT statement



INSERT INTO Teams (Principal) 
-- ^


you're only inserting a value for the field Principal into the table Teams. But the Teams table has other fields as well... not inserting values into them will default them to NULL. This will lead to a contradiction in the arguments. In your Teams table:



Team_Name VARCHAR(30) NOT NULL
Nation VARCHAR(30) NOT NULL
Seasons INT NOT NULL


these fields, by definition, can't be NULL. But since no values are inserted with the INSERT statement, these will default to NULL, clashing with the definition and causing an error.



You may want to consider setting default values for the above fields or modifying your INSERT statement to accommodate those fields.



INSERT INTO Teams (Principal, Team_Name, Nation, Seasons)
SELECT
-- four columns ...





share|improve this answer


























  • Ahh I see now. Thanks a lot! I appreciate it

    – Clank
    Nov 23 '18 at 14:33











  • @Clank when an answer worked for you; you can consider marking it as accepted answer. Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

    – Madhur Bhaiya
    Nov 23 '18 at 19:16



















0














Your insert statement tries to assign null values to columns that have 'not null' property. Check these columns 'not null' -> false or assign default value or define values in your insert statement. I hope this help






share|improve this answer
























  • Thanks for the answer! But where exactly does it try to assign null values? All my values for Principal are set to their proper names, so should it not only be assigning initialised values?

    – Clank
    Nov 23 '18 at 13:53











  • INSERT INTO Teams (Principal) this expression assign to only column 'principal' and tries to assign NULL value to others.

    – ahmet_y
    Nov 23 '18 at 14:17











  • Is there a way of inserting just Principals without trying to insert NULL everywhere else?

    – Clank
    Nov 23 '18 at 15:21











  • when a row is inserted, it is inserted with all columns of that table. You define (in insert statement) specific value, or not define(in insert statement) and let it use its default value already defined in table construction or not define(in insert statement) and let it to be NULL(if columns 'NOT NULL' property is not checked )

    – ahmet_y
    Nov 24 '18 at 23:05











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447627%2fmysql-insert-into-statement-giving-error-code-1062%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














In your INSERT statement



INSERT INTO Teams (Principal) 
-- ^


you're only inserting a value for the field Principal into the table Teams. But the Teams table has other fields as well... not inserting values into them will default them to NULL. This will lead to a contradiction in the arguments. In your Teams table:



Team_Name VARCHAR(30) NOT NULL
Nation VARCHAR(30) NOT NULL
Seasons INT NOT NULL


these fields, by definition, can't be NULL. But since no values are inserted with the INSERT statement, these will default to NULL, clashing with the definition and causing an error.



You may want to consider setting default values for the above fields or modifying your INSERT statement to accommodate those fields.



INSERT INTO Teams (Principal, Team_Name, Nation, Seasons)
SELECT
-- four columns ...





share|improve this answer


























  • Ahh I see now. Thanks a lot! I appreciate it

    – Clank
    Nov 23 '18 at 14:33











  • @Clank when an answer worked for you; you can consider marking it as accepted answer. Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

    – Madhur Bhaiya
    Nov 23 '18 at 19:16
















1














In your INSERT statement



INSERT INTO Teams (Principal) 
-- ^


you're only inserting a value for the field Principal into the table Teams. But the Teams table has other fields as well... not inserting values into them will default them to NULL. This will lead to a contradiction in the arguments. In your Teams table:



Team_Name VARCHAR(30) NOT NULL
Nation VARCHAR(30) NOT NULL
Seasons INT NOT NULL


these fields, by definition, can't be NULL. But since no values are inserted with the INSERT statement, these will default to NULL, clashing with the definition and causing an error.



You may want to consider setting default values for the above fields or modifying your INSERT statement to accommodate those fields.



INSERT INTO Teams (Principal, Team_Name, Nation, Seasons)
SELECT
-- four columns ...





share|improve this answer


























  • Ahh I see now. Thanks a lot! I appreciate it

    – Clank
    Nov 23 '18 at 14:33











  • @Clank when an answer worked for you; you can consider marking it as accepted answer. Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

    – Madhur Bhaiya
    Nov 23 '18 at 19:16














1












1








1







In your INSERT statement



INSERT INTO Teams (Principal) 
-- ^


you're only inserting a value for the field Principal into the table Teams. But the Teams table has other fields as well... not inserting values into them will default them to NULL. This will lead to a contradiction in the arguments. In your Teams table:



Team_Name VARCHAR(30) NOT NULL
Nation VARCHAR(30) NOT NULL
Seasons INT NOT NULL


these fields, by definition, can't be NULL. But since no values are inserted with the INSERT statement, these will default to NULL, clashing with the definition and causing an error.



You may want to consider setting default values for the above fields or modifying your INSERT statement to accommodate those fields.



INSERT INTO Teams (Principal, Team_Name, Nation, Seasons)
SELECT
-- four columns ...





share|improve this answer















In your INSERT statement



INSERT INTO Teams (Principal) 
-- ^


you're only inserting a value for the field Principal into the table Teams. But the Teams table has other fields as well... not inserting values into them will default them to NULL. This will lead to a contradiction in the arguments. In your Teams table:



Team_Name VARCHAR(30) NOT NULL
Nation VARCHAR(30) NOT NULL
Seasons INT NOT NULL


these fields, by definition, can't be NULL. But since no values are inserted with the INSERT statement, these will default to NULL, clashing with the definition and causing an error.



You may want to consider setting default values for the above fields or modifying your INSERT statement to accommodate those fields.



INSERT INTO Teams (Principal, Team_Name, Nation, Seasons)
SELECT
-- four columns ...






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 14:16

























answered Nov 23 '18 at 14:09









TrebuchetMSTrebuchetMS

2,64811023




2,64811023













  • Ahh I see now. Thanks a lot! I appreciate it

    – Clank
    Nov 23 '18 at 14:33











  • @Clank when an answer worked for you; you can consider marking it as accepted answer. Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

    – Madhur Bhaiya
    Nov 23 '18 at 19:16



















  • Ahh I see now. Thanks a lot! I appreciate it

    – Clank
    Nov 23 '18 at 14:33











  • @Clank when an answer worked for you; you can consider marking it as accepted answer. Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

    – Madhur Bhaiya
    Nov 23 '18 at 19:16

















Ahh I see now. Thanks a lot! I appreciate it

– Clank
Nov 23 '18 at 14:33





Ahh I see now. Thanks a lot! I appreciate it

– Clank
Nov 23 '18 at 14:33













@Clank when an answer worked for you; you can consider marking it as accepted answer. Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

– Madhur Bhaiya
Nov 23 '18 at 19:16





@Clank when an answer worked for you; you can consider marking it as accepted answer. Please see: How to accept an answer for closure. You get points for it as well. Thanks :)

– Madhur Bhaiya
Nov 23 '18 at 19:16













0














Your insert statement tries to assign null values to columns that have 'not null' property. Check these columns 'not null' -> false or assign default value or define values in your insert statement. I hope this help






share|improve this answer
























  • Thanks for the answer! But where exactly does it try to assign null values? All my values for Principal are set to their proper names, so should it not only be assigning initialised values?

    – Clank
    Nov 23 '18 at 13:53











  • INSERT INTO Teams (Principal) this expression assign to only column 'principal' and tries to assign NULL value to others.

    – ahmet_y
    Nov 23 '18 at 14:17











  • Is there a way of inserting just Principals without trying to insert NULL everywhere else?

    – Clank
    Nov 23 '18 at 15:21











  • when a row is inserted, it is inserted with all columns of that table. You define (in insert statement) specific value, or not define(in insert statement) and let it use its default value already defined in table construction or not define(in insert statement) and let it to be NULL(if columns 'NOT NULL' property is not checked )

    – ahmet_y
    Nov 24 '18 at 23:05
















0














Your insert statement tries to assign null values to columns that have 'not null' property. Check these columns 'not null' -> false or assign default value or define values in your insert statement. I hope this help






share|improve this answer
























  • Thanks for the answer! But where exactly does it try to assign null values? All my values for Principal are set to their proper names, so should it not only be assigning initialised values?

    – Clank
    Nov 23 '18 at 13:53











  • INSERT INTO Teams (Principal) this expression assign to only column 'principal' and tries to assign NULL value to others.

    – ahmet_y
    Nov 23 '18 at 14:17











  • Is there a way of inserting just Principals without trying to insert NULL everywhere else?

    – Clank
    Nov 23 '18 at 15:21











  • when a row is inserted, it is inserted with all columns of that table. You define (in insert statement) specific value, or not define(in insert statement) and let it use its default value already defined in table construction or not define(in insert statement) and let it to be NULL(if columns 'NOT NULL' property is not checked )

    – ahmet_y
    Nov 24 '18 at 23:05














0












0








0







Your insert statement tries to assign null values to columns that have 'not null' property. Check these columns 'not null' -> false or assign default value or define values in your insert statement. I hope this help






share|improve this answer













Your insert statement tries to assign null values to columns that have 'not null' property. Check these columns 'not null' -> false or assign default value or define values in your insert statement. I hope this help







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 13:49









ahmet_yahmet_y

1078




1078













  • Thanks for the answer! But where exactly does it try to assign null values? All my values for Principal are set to their proper names, so should it not only be assigning initialised values?

    – Clank
    Nov 23 '18 at 13:53











  • INSERT INTO Teams (Principal) this expression assign to only column 'principal' and tries to assign NULL value to others.

    – ahmet_y
    Nov 23 '18 at 14:17











  • Is there a way of inserting just Principals without trying to insert NULL everywhere else?

    – Clank
    Nov 23 '18 at 15:21











  • when a row is inserted, it is inserted with all columns of that table. You define (in insert statement) specific value, or not define(in insert statement) and let it use its default value already defined in table construction or not define(in insert statement) and let it to be NULL(if columns 'NOT NULL' property is not checked )

    – ahmet_y
    Nov 24 '18 at 23:05



















  • Thanks for the answer! But where exactly does it try to assign null values? All my values for Principal are set to their proper names, so should it not only be assigning initialised values?

    – Clank
    Nov 23 '18 at 13:53











  • INSERT INTO Teams (Principal) this expression assign to only column 'principal' and tries to assign NULL value to others.

    – ahmet_y
    Nov 23 '18 at 14:17











  • Is there a way of inserting just Principals without trying to insert NULL everywhere else?

    – Clank
    Nov 23 '18 at 15:21











  • when a row is inserted, it is inserted with all columns of that table. You define (in insert statement) specific value, or not define(in insert statement) and let it use its default value already defined in table construction or not define(in insert statement) and let it to be NULL(if columns 'NOT NULL' property is not checked )

    – ahmet_y
    Nov 24 '18 at 23:05

















Thanks for the answer! But where exactly does it try to assign null values? All my values for Principal are set to their proper names, so should it not only be assigning initialised values?

– Clank
Nov 23 '18 at 13:53





Thanks for the answer! But where exactly does it try to assign null values? All my values for Principal are set to their proper names, so should it not only be assigning initialised values?

– Clank
Nov 23 '18 at 13:53













INSERT INTO Teams (Principal) this expression assign to only column 'principal' and tries to assign NULL value to others.

– ahmet_y
Nov 23 '18 at 14:17





INSERT INTO Teams (Principal) this expression assign to only column 'principal' and tries to assign NULL value to others.

– ahmet_y
Nov 23 '18 at 14:17













Is there a way of inserting just Principals without trying to insert NULL everywhere else?

– Clank
Nov 23 '18 at 15:21





Is there a way of inserting just Principals without trying to insert NULL everywhere else?

– Clank
Nov 23 '18 at 15:21













when a row is inserted, it is inserted with all columns of that table. You define (in insert statement) specific value, or not define(in insert statement) and let it use its default value already defined in table construction or not define(in insert statement) and let it to be NULL(if columns 'NOT NULL' property is not checked )

– ahmet_y
Nov 24 '18 at 23:05





when a row is inserted, it is inserted with all columns of that table. You define (in insert statement) specific value, or not define(in insert statement) and let it use its default value already defined in table construction or not define(in insert statement) and let it to be NULL(if columns 'NOT NULL' property is not checked )

– ahmet_y
Nov 24 '18 at 23:05


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447627%2fmysql-insert-into-statement-giving-error-code-1062%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen