Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > it.comp.www.php > #22645

Re: [OT] MySQL/MariaDB db per gusti utenti

From Alessandro Pellizzari <shuriken@amiran.it>
Newsgroups it.comp.www.php
Subject Re: [OT] MySQL/MariaDB db per gusti utenti
Date 2019-04-22 10:37 +0100
Message-ID <gi5gahF2ut7U1@mid.individual.net> (permalink)
References <q9i9g9$1i7b$1@gioia.aioe.org>

Show all headers | View raw


On 21/04/2019 18:32, ^Bart wrote:

> A questo punto converrebbe gli ingredienti metterli tutti nello stesso 
> campo evitando quindi la FK?

In SQL hai relativamente poca scelta, una volta che decidi di
normalizzare i DB.

1:1 -> Metti tutto nella stessa tabella

1:n -> Hai una tabella con gli "1", e una tabella con gli "n".

n:m -> Hai 3 tabelle: una con gli "n", una con gli "m" e una con solo
due colonne: fk_n e fk_m

Nel tuo caso hai una relazione n:m
Un ingrediente può appartenere a diversi stili.
Per esempio, il latte è vegetariano e onnivoro, ma non vegano, mentre il
radicchio appartiene a tutti e tre, ma non al carnivoro.

Quindi la soluzione è una tabella con gli stili, una con gli ingredienti
e una tabella di link:

create table foodstyle (
  id int not null auto_increment primary key,
  name varchar(255)
)

create table ingredient (
  id int not null auto_increment primary key,
  name varchar(255),
)

create table foodstyle_ingredient (
  foodstyle int not null,
  ingredient int not null,
  foreign key fk_foodstyle(foodstyle) references foodstyle (id),
  foreign key fk_ingredient(ingredient) references ingredient (id)
)

Quindi la tua seconda soluzione è quella giusta.

Bye.

Back to it.comp.www.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[OT] MySQL/MariaDB db per gusti utenti ^Bart <gabriele1NOSPAM@hotmail.com> - 2019-04-21 19:32 +0200
  Re: [OT] MySQL/MariaDB db per gusti utenti Alessandro Pellizzari <shuriken@amiran.it> - 2019-04-22 10:37 +0100
    Re: [OT] MySQL/MariaDB db per gusti utenti ^Bart <gabriele1NOSPAM@hotmail.com> - 2019-04-22 18:01 +0200
      Re: [OT] MySQL/MariaDB db per gusti utenti Alessandro Pellizzari <shuriken@amiran.it> - 2019-04-22 17:23 +0100
        Re: [OT] MySQL/MariaDB db per gusti utenti ^Bart <gabriele1NOSPAM@hotmail.com> - 2019-04-22 19:13 +0200

csiph-web