hello I have these tables created in SQL SERVER with the following problem which I could not solve no matter how many times I gave it. I clarify I am a total novice in databases.
create table sesion(
idSesion int not null Primary key identity(1,1),
horarioLog timestamp not null,
);
go
go
create table tipoUsuario(
idTipoUsuario int not null Primary key identity(1,1),
descripcion varchar (10) not null,
);
go
go
create table persona(
idPersona int not null Primary key identity(1,1),
nombre varchar(30) not null,
apellido varchar(20) not null,
email varchar(256) not null,
dni int not null,
);
go
go
create table usuario(
idUsuario int not null Primary key identity(1,1),
nombre varchar (20) not null,
contrasena varchar (256) not null,
idSesion int not null,
idTipoUsuario int not null,
idPersona int not null,
constraint FK_sesion_usuario foreign key(idSesion) references sesion(idSesion),
constraint FK_tipoUsuario_usuario foreign key(idTipoUsuario) references tipoUsuario(idTipoUsuario),
constraint FK_persona_usuario foreign key(idPersona) references persona(idPersona),
constraint UK_persona_usuario unique(idPersona),
);
go
I want to insert records and I get this error. Could you tell me what am I doing wrong?
use pruebas;
INSERT INTO persona (nombre, apellido, email, dni) VALUES ('antonio','Perez','[email protected]', '37370935');
INSERT INTO tipoUsuario VALUES ('Secretario');
INSERT INTO sesion (horarioLog) VALUES (DEFAULT);
INSERT INTO usuario (nombre, contrasena) VALUES ('AniPz', 'afaushuf@3647');
Based on your own comment that you're a newbie and the comments, I see you got the first 3
INSERT
correct, however I guess the last one you don't know how to insert.To be able to do it you have to call all the missing columns that you have declared in your first scripts
CREATE
, that is, the following fields:So the query (in principle) looks like this:
Then, I suppose, you think that the id's will be added automatically or something like that and it's not, there are a thousand ways to do it, but if you don't want to complicate yourself you can use
IDENT_CURRENT
, select the table and refer to the last ID inserted in that table.The final query would look like this: