> For the complete documentation index, see [llms.txt](https://sena-ceet.gitbook.io/2252819-trimestre-2/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sena-ceet.gitbook.io/2252819-trimestre-2/base-de-datos/bases-de-datos/relacion/muchos-a-muchos.md).

# Muchos a Muchos

## Diseño conceptual

![](/files/-M_0wxsuJh1jwm6Y2uL4)

En el diseño conceptual se puede expresar con en la imagen anterior pero cuando se va a programar se genera una tablan intermedia o también se coloca en el diseño físico.

## &#x20;Diseño Físico

### Llaves naturales

![](/files/-M_11YUITOV29bwiXP9e)

```sql
CREATE TABLE factura (
  numero_factura int4 NOT NULL, 
  anio           int4 NOT NULL, 
  forma_pago     varchar(100) NOT NULL, 
  total          float8 NOT NULL, 
  PRIMARY KEY (numero_factura, 
  anio));
  
  CREATE TABLE producto (
  codigo_barras   varchar(255) NOT NULL, 
  nombre_producto varchar(255) NOT NULL, 
  precio          float8 NOT NULL, 
  cantidad        int4 NOT NULL, 
  PRIMARY KEY (codigo_barras));

CREATE TABLE detalle_factura (
  numero_factura int4 NOT NULL, 
  anio_factura   int4 NOT NULL, 
  codigo_barras  varchar(255) NOT NULL, 
  precio         float8 NOT NULL, 
  cantidad       int4 NOT NULL, 
  PRIMARY KEY (numero_factura, 
  anio_factura, 
  codigo_barras));
ALTER TABLE detalle_factura ADD CONSTRAINT fk_factura FOREIGN KEY (numero_factura, anio_factura) REFERENCES factura (numero_factura, anio);
ALTER TABLE detalle_factura ADD CONSTRAINT fk_producto FOREIGN KEY (codigo_barras) REFERENCES producto (codigo_barras);

```

### Llaves sustitutas&#x20;

![](/files/-M_1HJgWthAkYUgyETAu)

```sql
CREATE TABLE factura (
  id             SERIAL NOT NULL, 
  numero_factura int4 NOT NULL, 
  anio           int4 NOT NULL, 
  forma_pago     varchar(100) NOT NULL, 
  total          float8 NOT NULL, 
  PRIMARY KEY (id), 
  CONSTRAINT uc_factura 
    UNIQUE (numero_factura, anio));
    
    
CREATE TABLE producto (
  id              SERIAL NOT NULL, 
  codigo_barras   varchar(255) NOT NULL, 
  nombre_producto varchar(255) NOT NULL, 
  precio          float8 NOT NULL, 
  cantidad        int4 NOT NULL, 
  PRIMARY KEY (id), 
  CONSTRAINT uc_producto 
    UNIQUE (codigo_barras));
    
CREATE TABLE detalle_factura (
  id          SERIAL NOT NULL, 
  id_factura  int4 NOT NULL, 
  id_producto int4 NOT NULL, 
  cantidad    int4 NOT NULL, 
  precio      float8 NOT NULL, 
  PRIMARY KEY (id), 
  CONSTRAINT uc_detalle_factura 
    UNIQUE (id_factura, id_producto));
```
