The problem
I'm currently trying to insert a new employee to the employees
table of the test database with the same name.
If I try to add a new employee specifying the emp_no
it's done with success:
insert into employees.employees(
emp_no,
birth_date,
first_name,
last_name,
gender,
hire_date
)
values(
500500,
date('1970-05-12'),
'John',
'Smith',
'M',
date('1991-10-08')
);
But when I try removing the emp_no
, it won't be incremented by the database.
I'm looking for a way of making the database increment the emp_no itself but Im' not able to find it by myself.
The database
Here's the link to the title database itself for the reference.
What I tried so far
I tried with a native query as follows:
insert into employees.employees(
birth_date,
first_name,
last_name,
gender,
hire_date
)
values(
date('1970-05-12'),
'John',
'Smith',
'M',
date('1991-10-08')
);
But I'm getting the following error:
Error Code: 1364. Field 'emp_no' doesn't have a default value
So according to the solutions here, here or here I tried to enable auto incrementing the emp_no
with
ALTER TABLE employees.employees MODIFY emp_no int NOT NULL AUTO_INCREMENT;
But this gave me another error
Error Code: 1833. Cannot change column 'emp_no': used in a foreign key constraint 'dept_emp_ibfk_1' of table 'employees.dept_emp'
I don't even know if that's what causes the problem and whether I should still go this way or maybe not. Maybe the problem itself lies somewhere else? I'll be very grateful for any help.