Sql - Modeling Database

1

I would appreciate help. I do not know how to create tables in the SQL database

My problem is this:

I have a user with a request with multiple items.

I already have the users and products table

How do I create this table that contains an order and various products related to it?

Thank you

    
asked by anonymous 11.04.2017 / 01:02

1 answer

2

Considering that your already created tables contain the following structure:

Users Table

PK idUsuarios int
Nome varchar(30)
NomeCompleto varchar(255)

Products Table

PK idProdutos int
Descricao varchar(255)
ValorUnit decimal(10,4)

The new desired tables

Orders Table

PK idPedidos int
FK idUsuarios int
DataPedido datetime
ValorTotal decimal(10,4)

SQL Code

create table Pedidos
(idPedidos int identity,
 idUsuarios int,
 DataPedido datetime,
 ValorTotalPedido decimal(10,4))

Product Order Table - > Link of various order products

PK idPedidos int
PK idProdutos int
Qtde decimal(7,4) -> Decimal para produtos fracionados por exemplo

SQL Code

create table PedidosProdutos
(idPedidos int,
 idProdutos int,
 Qtde decimal(7,4))

Please note that multiple products can be registered using the same order ID. Ex:

+---------------------------+
| PEDIDO | PRODUTO |  QTDE  |
+---------------------------+
| 1      | 1029    | 1.0000 | 
| 2      | 1023    | 3.0000 |
| 3      | 1993    | 8.3100 |  
+---------------------------+
    
11.04.2017 / 15:36