Why don’t you create Foreign Key?

Asked

Viewed 31 times

0

I ran the commands in Mysql Workbench, but did not create the Foreign Key connection, which is wrong in querys?

create table Computador (
    ID int NOT NULL primary key AUTO_INCREMENT,
    Nome varchar(45) NOT NULL,
    Marca varchar(45) NOT NULL,
    Modelo varchar(45) NOT NULL,
    Processador varchar(45) NOT NULL,
    MemoriaRam smallint NOT NULL,
    TamanhoTotal int NOT NULL
);

create table MaquinaVirtual (
    ID int NOT NULL primary key AUTO_INCREMENT,
    Nome varchar(70) NOT NULL,
    EspacoEmUso int NOT NULL, 
    EspacoTotal int NOT NULL,
    IdComputador int NOT NULL,
    foreign key (IdComputador) references Computador (ID)
);

1 answer

0


You need to set the table engine to Innodb:

create table Computador (
    ID int NOT NULL primary key AUTO_INCREMENT,
    Nome varchar(45) NOT NULL,
    Marca varchar(45) NOT NULL,
    Modelo varchar(45) NOT NULL,
    Processador varchar(45) NOT NULL,
    MemoriaRam smallint NOT NULL,
    TamanhoTotal int NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

create table MaquinaVirtual (
    ID int NOT NULL primary key AUTO_INCREMENT,
    Nome varchar(70) NOT NULL,
    EspacoEmUso int NOT NULL, 
    EspacoTotal int NOT NULL,
    IdComputador int NOT NULL,
    foreign key (IdComputador) references Computador (ID)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.