Save several values in the field of the mysql table

Asked

Viewed 46 times

-1

I wanted to find a way to save, for example, the codes of some doctors who participated in a consultation of an animal.

I would like to keep more than one veterinarian in the consultation. (as many as is necessary)

create table if not exists consulta(
codConsulta character(8) not null primary key,
crv character(5) ,
codAnimal character(8) not null,
diagnostico text not null,
constraint fk_crv foreign key (crv) references veterinario(crv),
constraint fk_codAnimal foreign key (codAnimal) references animal(codAnimal))engine=INNODB default charset = utf8;
  • 3

    Read about Data Normalization (Nornal Forms) , you are on the wrong track believe.

1 answer

2

The best way would be to have three tables (consultation, veterinary and veterinary consultation ). The veterinary table_query would be n to n keeping the codes of consultation and veterinary

N to N (explanation)

https://fmhelp.filemaker.com/help/fmp/pt/index.html#page/Fmp_help/Many-to-Many-relationships.html

I made an example working on http://sqlfiddle.com/#! 9/b94c3c/5

CREATE TABLE IF NOT EXISTS `veterinario` (
  `id` int(6) unsigned NOT NULL,
  `nome` varchar(80) NOT NULL,  
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;

INSERT INTO `veterinario` (`id`, `nome`) VALUES
  ('1', 'Carlos Manga'),
  ('2', 'Leonardo Pires'),  
  ('3', 'João Duarte');
  
CREATE TABLE IF NOT EXISTS `cliente` (
  `id` int(6) unsigned NOT NULL,
  `nome` varchar(80) NOT NULL,  
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;

INSERT INTO `cliente` (`id`, `nome`) VALUES
  ('1', 'Fernando'),
  ('2', 'Maria Inês'),  
  ('3', 'Carmem');
  
CREATE TABLE IF NOT EXISTS `consulta` (
  `id_consulta` int(6) unsigned NOT NULL,
  `id_cliente` int(6) NOT NULL,
  `dt_consulta` date NOT NULL,
  PRIMARY KEY (`id_consulta`)
) DEFAULT CHARSET=utf8;

INSERT INTO `consulta` (`id_consulta`, `id_cliente`, `dt_consulta`) VALUES
  ('1', '1','2020-05-05'),
  ('2', '1','2020-05-13'),  
  ('3', '3','2020-05-27'),
  ('4', '2','2020-06-02'),
  ('5', '4','2020-06-30'),  
  ('6', '5','2020-07-16');
  
CREATE TABLE IF NOT EXISTS `veterinario_consulta` (
  `id_consulta` int(6) unsigned NOT NULL,
  `id_veterinario` varchar(80) NOT NULL,  
  PRIMARY KEY (`id_consulta`,`id_veterinario`)
) DEFAULT CHARSET=utf8;

INSERT INTO `veterinario_consulta` (`id_consulta`, `id_veterinario`) VALUES
  ('1', '2'),
  ('1', '3'),  
  ('2', '1'),    
  ('3', '2'),
  ('3', '3'),  
  ('4', '2'),
  ('5', '1'),  
  ('3', '1');

Example query

select c.id_consulta, l.nome cliente, v.nome veterinario
  from consulta c,
       veterinario_consulta vc,
       cliente l,
       veterinario v
 where c.id_consulta    = vc.id_consulta
  and  v.id             = vc.id_veterinario
  and  c.id_cliente     = l.id

You can return veterinarians in a row also using group_concat

https://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php

select c.id_consulta, l.nome cliente, group_concat(v.nome) veterinario
  from consulta c,
       veterinario_consulta vc,
       cliente l,
       veterinario v
 where c.id_consulta    = vc.id_consulta
  and  v.id             = vc.id_veterinario
  and  c.id_cliente     = l.id
  group by c.id_consulta, l.nome
  • but if the consultation has more than one veterinarian ?

  • In the example I did above http://sqlfiddle.com/#! 9/b94c3c/5 the first consultation has two veterinarians, according to the first two lines in the return.

  • 1

    I think it is interesting to make it clear that "better" is completely relative. Normalization is not always, in fact, the best way. It is quite common, including, we see the process of denormalization of banks for optimization. Perhaps suggesting normalization initially for ease of maintenance issues makes more sense.

  • Whether you must know the Normal Ways to decide not to use them. In this case then I see no reason not to be conservative, malnutrition will only generate queries and complicated records for nothing.

Browser other questions tagged

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