can anyone help me with this error? When I try to do an insert in the "attendances" table I get that my foreign key does not have a default value, but shouldn't it be automatic? I leave my code, thanks!
USE dbfinal3;
CREATE TABLE alumnos (
dni int(20) NOT NULL UNIQUE primary key,
apellido varchar(15) NOT NULL,
nombre varchar(20) NOT NULL,
direccion varchar(25) NOT NULL,
curso int(2) NOT NULL,
division varchar(5) NOT NULL,
estado varchar(35) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE asistencias (
idAsistencia int NOT NULL auto_increment primary key,
dniAlumnos int NOT NULL,
fecha date NOT NULL,
constraint fk_dniAlumnos foreign key (dniAlumnos) references alumnos(dni)
ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
I also leave the inserts that I have been testing!
insert into `alumnos` (dni,apellido,nombre,direccion,curso,division,estado) values
(40441958,'Perez','Javier','Martelli 234','3','A','Regular');
insert into `alumnos` (dni,apellido,nombre,direccion,curso,division,estado) values
(50987187,'Martinez','Juan','Belgrano 534','2','B','Irregular');
insert into `asistencias` (fecha) values
('1998-10-19')
your table
asistencias
has two perforce required fieldsfecha
anddniAlumnos
(that means theNOT NUll
)when you run
You are not inserting the student ID. It should be something like
either
Or any other valid ID of a student.
By telling you that it does not have a default value, it means that the database will first check the value you chose, if there is not, it will verify the default and since it does not have, then it marks the error
Apparently you don't put the field
dni
of the students table asauto_increment
.The correct way would be: