0
I’m having problems in the relationship of my tables products and stock. The stock table is responsible for storing the quantity of each item in the products table and the date when the product went into stock. Each item in the product table can only be registered once in the stock table. So I was using the following structure:
create table produtos(
id_produto int unsigned auto_increment not null,
marca int unsigned not null,
categoria int unsigned not null,
nome varchar(100) not null,
valor double not null,
descricao varchar(250) not null,
tamanho varchar(2),
cor varchar(15),
sexo varchar(1),
constraint pk_produto primary key (id_produto),
constraint fk_marca foreign key (marca) references marcas(id_marca),
constraint fk_categoria foreign key(categoria) references categorias(id_categoria)
);
create table estoque(
id_estoque int unsigned auto_increment not null,
produto int unsigned not null unique,
qtd int unsigned not null,
datarep date not null,
constraint pk_estoque primary key(id_estoque),
constraint fk_produto foreign key(produto) references produtos(id_produto)
);
The problem is that this way, I can register the same product several times in stock. So, I ended up thinking about some alternatives:
- Create a field Unic not null to identify the products in the stock table, and consequently prevent the product with the same code to be registered.
- Use product key as primary key of stock table, but I have my doubts whether this would be a good practice.
- Join the stock table in the product table through the quantity and replenishment attributes, for example. In this case, I would need to update the table with each purchase and arrival of a new stock.
What would be the best option for this case? If none of them are good practice, what could I do? Obs: I’m using mysql.
1... The id would already be your Unique field, you can use a hash function based on the id to generate a unique hash for each product. 2... With this you would prevent there being more of the same product registered in the stock, I honestly do not see much harm, unless you want to keep a history of when the product entered and when it ended, in this case would be to have a created field_em, and when the product in stock ended up updating a completed field_em with the current timestamp. , in the next reset create a new row in the table.
– MarceloBoni
If you already have the
constraint fk_produto foreign key(produto) references produtos(id_produto)
I don’t see much point in still havingproduto int unsigned not null unique,
– MarceloBoni
Hello, Marcelo. Would you have an example of using this hash? I believe I would need a field to generate and store this hash, correct? Despite having this Constraint, I can still register the same product in stock more than once, so I added this Picnic to see how it would look, but I did not test.
– Ryan Santos
You can search for something inside the mysql itself, or use some external tool, when your record is created, get the
last_id_insert
and use that one library for example, to generate a hash– MarceloBoni