I have a web application that uses these 3 tables:
Client:
CREATE TABLE CLIENTE(
ID_CLIENTE INT PRIMARY KEY,
CLIENTE VARCHAR(50),
ENDERECO VARCHAR(50),
CIDADE VARCHAR(30),
CEP VARCHAR(9),
UF CHAR(2)
);
Order:
CREATE TABLE PEDIDO (
NUM_PEDIDO INT PRIMARY KEY,
ID_CLIENTE INT FOREIGN KEY REFERENCES CLIENTE(ID_CLIENTE),
ID_VENDEDOR INT FOREIGN KEY REFERENCES VENDEDOR(ID_VENDEDOR),
PRAZO_ENTREGA VARCHAR (50)
);
Order_id:
CREATE TABLE ITEM_PEDIDO(
ID_ITEM_PEDIDO INT PRIMARY KEY IDENTITY (1,1),
NUM_PEDIDO INT FOREIGN KEY REFERENCES PEDIDO(NUM_PEDIDO),
ID_PRODUTO INT FOREIGN KEY REFERENCES PRODUTO(ID_PRODUTO),
QUANTIDADE INT
);
Because of the Referential Integrity , whenever I go delete a client, I must exclude not only the requests made by him, but also the items of that request.
But, as seen in the above code, the REQUIRED ITEM table does not have the relationship with Customer , so it is first necessary to identify all records that are related to that customer's request. My question is: How do I get the primary key of this request, storing it inside a int
and then, using as a parameter to delete the order items?
An example of my goal:
Cliente c = new Cliente();
c.ID_Cliente = 1;
int PK = "select PEDIDO.NUM_PEDIDO where ID_Cliente = "+c.ID_Cliente;
SqlDataSource1.DeleteCommand = "delete from ITEM_PEDIDO where NUM_PEDIDO = "+PK;
SqlDataSource1.Delete();