Resolved: Introduction to JOINS - Exercise
In Task 2, I followed the code for Insert into Dept_manager_dup but the output displays the Error 1364 : Field Dept_no doesn't have a default value.
Can you please suggest what is wrong in my method that displays such an error.
7 answers ( 1 marked as helpful)
Hi Mohammad!
Thanks for reaching out.
Can you please share the CREATE statement query you've executed prior to the INSERT Statements you've shared? There it should be seen if a default value has been set and what it is.
Looking forward to your answer.
Kind regards,
Martin
Kind regards,
Martin
Hi Martin,
Apologies for my late reply.
Well, you are right that I didn't set the default value for dept_no but I set it later and it didn't work. Lemme send you the screenshot of my Create statement.
Can you kindly guide me why the default value needs to be set and how in that Create statement. I looked up to your example in the lesson and there wasn't any default value set by you??
What should be the default value here?
Awaiting your response.
Regards,
BURAIR
Hi Mohammad!
Thanks for reaching out.
Please accept my apologies for the delayed reply.
Thank you for sharing the screenshot! In fact, the issue may simply stem from the fact that you have not executed all queries you have in the given order. In other words, at the moment of execution of th INSERT statement that leads you to an error message, there might be an issue in terms of the correct state of the dept_manager_dup table.
Therefore, once you've set the employees as your database, can you please execute all of the following queries in the given order?
DROP TABLE IF EXISTS dept_manager_dup;
CREATE TABLE dept_manager_dup (
emp_no int(11) NOT NULL,
dept_no char(4) NULL,
from_date date NOT NULL,
to_date date NULL
);
INSERT INTO dept_manager_dup
SELECT * FROM dept_manager;
INSERT INTO dept_manager_dup (emp_no, from_date)
VALUES (999904, '2017-01-01'),
(999905, '2017-01-01'),
(999906, '2017-01-01'),
(999907, '2017-01-01');
DELETE FROM dept_manager_dup
WHERE
dept_no = 'd001';
CREATE TABLE dept_manager_dup (
emp_no int(11) NOT NULL,
dept_no char(4) NULL,
from_date date NOT NULL,
to_date date NULL
);
INSERT INTO dept_manager_dup
SELECT * FROM dept_manager;
INSERT INTO dept_manager_dup (emp_no, from_date)
VALUES (999904, '2017-01-01'),
(999905, '2017-01-01'),
(999906, '2017-01-01'),
(999907, '2017-01-01');
DELETE FROM dept_manager_dup
WHERE
dept_no = 'd001';
Hope this helps.
Kind regards,
Kind regards,
Hi Martin,
Thanks for your response.
I executed the code you mentioned above but still it displayed Error Code: 1136 - Column count doesn't match value count at row 1.
This error occurred when I executed the insert statement with values (999904, '2017-01-01')... Still the code didn't work.
Can you kindly guide me what went wrong.
Screenshot is attached here for your reference.
Hi Mohammad!
Thanks for your reply.
Because, you have designated four column names and then provided values for just two. As we point out in the lecture, the number of designated columns should equal the number of values provided for each row.
For the given example, you don't need
INSERT INTO dept_manager_dup (emp_no, dept_no, from_date, to_date)
you only need
INSERT INTO dept_manager_dup (emp_no, from_date)
Hope this helps.
Kind regards,
Martin
Kind regards,
Martin
Hi Martin!
Big Oops!😬
I was subconsciously inserting all columns of Dept_Manager_Dup while putting only two values!!!
It's a very small mistake which I kept overlooking.
Thank you so much for the assistance.
Hi Mohammad!
Great! Thank you for letting us know!
Please feel free to post another question should you need further assistance.
Please feel free to post another question should you need further assistance.
Kind regards,
Martin
Martin