-1
I am learning a little mysql and decided to do a market system, I have the following tables:
CREATE TABLE IF NOT EXISTS cliente(cliente_cpf BIGINT NOT NULL PRIMARY
KEY,cliente_nome VARCHAR(100) NOT NULL,cliente_sexo BIT NOT
NULL,cliente_nascimento DATE NOT NULL,cliente_email
VARCHAR(50),cliente_telefone BIGINT,cliente_celular BIGINT);
CREATE TABLE IF NOT EXISTS produto(produto_id INT AUTO_INCREMENT PRIMARY
KEY,produto_nome VARCHAR(100) NOT NULL,produto_preco FLOAT(5,2) NOT NULL);
CREATE TABLE IF NOT EXISTS item_pedido(item_pedido_id INT NOT
NULL,item_pedido_produto_id INT NOT NULL,item_pedido_produto_quantidade
TINYINT NOT NULL,FOREIGN KEY(item_pedido_produto_id) references
produto(produto_id));
CREATE TABLE IF NOT EXISTS pedido(pedido_id INT NOT NULL AUTO_INCREMENT
PRIMARY KEY,pedido_cliente_cpf BIGINT NOT NULL,pedido_item_pedido_id INT NOT
NULL DEFAULT 0,pedido_data_compra TIMESTAMP DEFAULT
CURRENT_TIMESTAMP(),pedido_valor_total FLOAT(6,2),FOREIGN KEY
(pedido_item_pedido_id) REFERENCES item_pedido(item_pedido_id),FOREIGN KEY
(pedido_cliente_cpf) REFERENCES cliente(cliente_cpf));
I am having a problem on the requested table with regards to add to set pedido_item_pedido_id in the requested table, everything else works right and if I do not add this Foreign key to the requested table also works, the error is ERROR 1215 (HY000): Cannot add Foreign key Constraint, I researched and I didn’t see why to give this mistake, someone can help me?
I came to think about it but as I need a value to repeat several times in item_pedido_id in the item_request table I did not do this, in this case this would be the only form of resolution?
– Erick R. Diogo
Yes. It is illogical to make a reference in another table if it returns more than 1 record. The solution would be to change the structure of the tables apparently. If you want help, open another question, showing how your bank’s structure, your "problem" with it, and possible solutions. Surely the people will help you.
– rbz