sql server programming:
If the two are always to be the same, why duplicate the value? Just keep the one copy in Products. -- Tom --------------------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA SQL Server MVP Columnist, SQL Server Professional Toronto, ON Canada www.pinnaclepublishing.com [quoted text, click to view] "Emre Guldogan" <ask me please...> wrote in message news:e8lhJHI5EHA.1204@TK2MSFTNGP10.phx.gbl...
Hi , i need some help . My problem is referencing a column from another tables column let say these two are my db tables . I want pro_price in Products to be taken from price in Products . Whenever price in Products is updated , pro_price in Products is updated too . How can i do ? CREATE TABLE Products ( ProductID INTEGER IDENTITY(0,1) NOT NULL, UnitCost INTEGER NOT NULL, price INTEGER, CONSTRAINT PK_Products PRIMARY KEY (ProductID) ) GO CREATE TABLE ShoppingCart ( sopid INTEGER IDENTITY(0,1) NOT NULL, ProductID INTEGER, pro_price INTEGER, CONSTRAINT PK_ShoppingCart PRIMARY KEY (sopid) ) GO ALTER TABLE ShoppingCart ADD FOREIGN KEY (ProductID) REFERENCES Products (ProductID) GO
I'm never happy when this happens to me, especially if it's an airline ticket that has gone up a lot in 5 minutes, but depending on what else is going on, it could be to prevent people from creating a shopping cart on sale day and then checking out months later when the actual price is much higher. Refreshing prices when the customer checks out might be a better option, but I'm sure the real situation has more constraints than we see here. (For example, shopping carts that can hold more than one item.) Emre - how about trying this: CREATE TABLE Products ( ProductID INTEGER IDENTITY(0,1) NOT NULL, UnitCost INTEGER NOT NULL, price INTEGER, CONSTRAINT uq_Products UNIQUE (ProductID), CONSTRAINT PK_Products PRIMARY KEY (ProductID, price) ) GO CREATE TABLE ShoppingCart ( sopid INTEGER IDENTITY(0,1) NOT NULL, ProductID INTEGER, pro_price INTEGER, CONSTRAINT PK_ShoppingCart PRIMARY KEY (sopid) ) GO ALTER TABLE ShoppingCart ADD FOREIGN KEY (ProductID, pro_price) REFERENCES Products (ProductID, price) ON UPDATE CASCADE GO DROP TABLE ShoppingCart, Products Steve Kass Drew University Steve Kass Drew University [quoted text, click to view] David Portas wrote: >See the UPDATE I posted. But your requirement doesn't make much sense to me. >Are your customers really happy to have prices change after they have added >them to the cart? Shouldn't the price be populated at INSERT and then be >unchanged? > >
UPDATE ShoppingCart SET pro_price = (SELECT price FROM Products WHERE productid = ShoppingCart.productid) -- David Portas SQL Server MVP --
See the UPDATE I posted. But your requirement doesn't make much sense to me. Are your customers really happy to have prices change after they have added them to the cart? Shouldn't the price be populated at INSERT and then be unchanged? -- David Portas SQL Server MVP --
Reference? A trigger doesn't seem appropriate to me for the reason I explained in my second post. Yes, a trigger on Products is one possibility: UPDATE ShoppingCart SET pro_price = (SELECT price FROM Inserted WHERE productid = ShoppingCart.productid) WHERE EXISTS (SELECT * FROM Inserted WHERE productid = ShoppingCart.productid) -- David Portas SQL Server MVP --
Hi , i need some help . My problem is referencing a column from another tables column let say these two are my db tables . I want pro_price in Products to be taken from price in Products . Whenever price in Products is updated , pro_price in Products is updated too . How can i do ? CREATE TABLE Products ( ProductID INTEGER IDENTITY(0,1) NOT NULL, UnitCost INTEGER NOT NULL, price INTEGER, CONSTRAINT PK_Products PRIMARY KEY (ProductID) ) GO CREATE TABLE ShoppingCart ( sopid INTEGER IDENTITY(0,1) NOT NULL, ProductID INTEGER, pro_price INTEGER, CONSTRAINT PK_ShoppingCart PRIMARY KEY (sopid) ) GO ALTER TABLE ShoppingCart ADD FOREIGN KEY (ProductID) REFERENCES Products (ProductID) GO
for performans reasons . This is not the only price that i have to calculate .. Is there any way to do this ? thanks [quoted text, click to view] "Tom Moreau" <tom@dont.spam.me.cips.ca> wrote in message news:ulZ6eKI5EHA.1596@tk2msftngp13.phx.gbl... > If the two are always to be the same, why duplicate the value? Just keep > the one copy in Products. > > -- > Tom > > --------------------------------------------------------------- > Thomas A. Moreau, BSc, PhD, MCSE, MCDBA > SQL Server MVP > Columnist, SQL Server Professional > Toronto, ON Canada > www.pinnaclepublishing.com > > > "Emre Guldogan" <ask me please...> wrote in message > news:e8lhJHI5EHA.1204@TK2MSFTNGP10.phx.gbl... > Hi , i need some help . My problem is referencing a column from another > tables column > let say these two are my db tables . I want pro_price in Products to be > taken from price in Products . > Whenever price in Products is updated , pro_price in Products is updated > too . > How can i do ? > > CREATE TABLE Products ( > ProductID INTEGER IDENTITY(0,1) NOT NULL, > UnitCost INTEGER NOT NULL, > price INTEGER, > CONSTRAINT PK_Products PRIMARY KEY (ProductID) > ) > GO > CREATE TABLE ShoppingCart ( > sopid INTEGER IDENTITY(0,1) NOT NULL, > ProductID INTEGER, > pro_price INTEGER, > CONSTRAINT PK_ShoppingCart PRIMARY KEY (sopid) > ) > GO > ALTER TABLE ShoppingCart > ADD FOREIGN KEY (ProductID) REFERENCES Products (ProductID) > GO > >
Sir , this is not a reference so the solution you just offered does not solve the problem . Maybe someone would say put this it on a update trigger for Products , but again it is not a reference thanks [quoted text, click to view] "David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote in message news:EbCdnfN0OYvty17cRVn-iA@giganews.com... > UPDATE ShoppingCart > SET pro_price = > (SELECT price > FROM Products > WHERE productid = ShoppingCart.productid) > > -- > David Portas > SQL Server MVP > -- > >
I like Steve's suggestion if denormalization is a requirement. Emre mentioned that this was for "performance reasons" but didn't give any details. If the volume is 1000 orders per day then maybe your table is small enough that the redundant column won't harm performance but in the long run I suggest that this approach is unlikely to scale well. As always, test, test, test. :-) -- David Portas SQL Server MVP --
Dear David , It is a requirement for us to keep customers shopping cart on db! , and when they come back after one month price must be updated (if still exists). Our system is a real time system , which gets about 1000 orders per a day . it doesn't make much sense to me what you have offered [if u offer something ] Anyway , thanks for your answer . Hasan Ozgur Computer Engineer [quoted text, click to view] "David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote in message news:lNqdnRAda93ixF7cRVn-tQ@giganews.com... > See the UPDATE I posted. But your requirement doesn't make much sense to me. > Are your customers really happy to have prices change after they have added > them to the cart? Shouldn't the price be populated at INSERT and then be > unchanged? > > -- > David Portas > SQL Server MVP > -- > >
Dear Sir , We are not happy too but this is a must for us to do . You analysed what the post means correctly. Thanks for your answer [quoted text, click to view] "Steve Kass" <skass@drew.edu> wrote in message news:41C36FA4.6050300@drew.edu... > I'm never happy when this happens to me, especially if > it's an airline ticket that has gone up a lot in 5 minutes, > but depending on what else is going on, it could be to > prevent people from creating a shopping cart on sale day > and then checking out months later when the actual price > is much higher. Refreshing prices when the customer > checks out might be a better option, but I'm sure the > real situation has more constraints than we see here. > (For example, shopping carts that can hold more > than one item.) > > Emre - how about trying this: > > CREATE TABLE Products ( > ProductID INTEGER IDENTITY(0,1) NOT NULL, > UnitCost INTEGER NOT NULL, > price INTEGER, > CONSTRAINT uq_Products UNIQUE (ProductID), > CONSTRAINT PK_Products PRIMARY KEY (ProductID, price) > ) > GO > CREATE TABLE ShoppingCart ( > sopid INTEGER IDENTITY(0,1) NOT NULL, > ProductID INTEGER, > pro_price INTEGER, > CONSTRAINT PK_ShoppingCart PRIMARY KEY (sopid) > ) > GO > ALTER TABLE ShoppingCart > ADD FOREIGN KEY (ProductID, pro_price) > REFERENCES Products (ProductID, price) > ON UPDATE CASCADE > GO > > DROP TABLE ShoppingCart, Products > > Steve Kass > Drew University > > > > > > > Steve Kass > Drew University > > David Portas wrote: > > >See the UPDATE I posted. But your requirement doesn't make much sense to me. > >Are your customers really happy to have prices change after they have added > >them to the cart? Shouldn't the price be populated at INSERT and then be > >unchanged? > > > > > >
I forgot to tell shopping card id is also kept at cookie not in a session ;) maybe it would help you to analyse correctly [quoted text, click to view] "hoz" <ask@me.com> wrote in message news:#vOP1KR5EHA.1408@TK2MSFTNGP10.phx.gbl... > Dear David , > It is a requirement for us to keep customers shopping cart on db! , and when > they come back after one month price must be updated (if still exists). > Our system is a real time system , which gets about 1000 orders per a day .. > it doesn't make much sense to me what you have offered [if u offer > something ] > Anyway , thanks for your answer . > > Hasan Ozgur > Computer Engineer > > > "David Portas" <REMOVE_BEFORE_REPLYING_dportas@acm.org> wrote in message > news:lNqdnRAda93ixF7cRVn-tQ@giganews.com... > > See the UPDATE I posted. But your requirement doesn't make much sense to > me. > > Are your customers really happy to have prices change after they have > added > > them to the cart? Shouldn't the price be populated at INSERT and then be > > unchanged? > > > > -- > > David Portas > > SQL Server MVP > > -- > > > > > >
Don't see what you're looking for? Try a search.
|