> 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));
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sena-ceet.gitbook.io/2252819-trimestre-2/base-de-datos/bases-de-datos/relacion/muchos-a-muchos.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
