0
create table Pessoa (
id int not null primary key auto_increment,
nome varchar(150) not null,
cidade varchar(100) not null,
cpf char(14) not null unique,
rg int not null unique,
INDEX idx_nome(nome)
);
create table conta(
id int not null primary key auto_increment,
numero int not null,
agencia int not null,
saldo decimal(20,2) not null,
pessoa_id int not null,
foreign key(pessoa_id) references Pessoa(id)
ON UPDATE CASCADE,
INDEX idx_numero_agencia(numero, agencia)
);
create table medico(
pessoa_id int primary key not null,
especialidade varchar(100)not null,
FOREIGN KEY(pessoa_id) references Pessoa(id)
ON UPDATE CASCADE
);
create table paciente (
pessoa_id int primary key not null,
doenca varchar(100) not null,
FOREIGN KEY(pessoa_id) references Pessoa(id)
ON UPDATE CASCADE
ON DELETE RESTRICT
);
create table consulta(
medico_id int,
paciente_id int,
horario datetime not null,
FOREIGN KEY(medico_id) references medico(pessoa_id)
ON UPDATE CASCADE
ON DELETE RESTRICT,
FOREIGN KEY(paciente_id) references paciente(pessoa_id)
ON UPDATE CASCADE
ON DELETE RESTRICT,
INDEX idx_medico_horario(medico_id,horario),
INDEX idx_paciente_horario(paciente_id,horario)
);
select Pessoa.nome `Nome do Médico`,
Pessoa.cpf `CPF do Médico`,
Pessoa.nome `Nome do Paciente`,
Pessoa.cpf `CPF do Paciente`,
consulta.horario `Horário da Consulta`
from Pessoa inner join consulta on (consulta.medico_id = Pessoa.id)
inner join medico on (consulta.medico_id = Pessoa.id)
inner join paciente on (consulta.paciente_id= Pessoa.id)
order by consulta.horario asc;
I created a bank where a person can be a doctor or a patient, but when I show the table that shows the doctor’s name and the doctor’s Cpf, and the patient’s name and Cpf, gives error.
What was the error? I created the tables and ran the query and it worked. The only difference was in using ' ' and not `
– Claudio Lopes
Use left in place of Inner Join.
– Procópio
Your query has no syntax error apparently. It could post the error that is occurring?
– Edgar Muniz Berlinck
The objective is to show the following columns: Doctor’s Name, Doctor’s CPF, Patient’s Name, Patient’s CPF and Appointment Hours. But every time I run, the table goes blank
– sKILE