3
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)
);
Request:
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)
);
Item_request:
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 Integrity Reference, where I am deleting a customer, I shall exclude not only the orders made by him, but also the items in that order.
But, as seen in the code above, the table ITEM_PEDIO has no relationship with Client, It is therefore necessary to first identify all the records that are related to that customer’s order. My question is as follows: How to get the primary key of this request by storing it within a int
and then, using as a parameter for deleting 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();
You want to do this using SQL itself, as in the answer below, or do it straight from C#?
– Grupo CDS Informática