The old 1970's Sybase syntax is unpredictable, not portable and conceptually wrong.
CREATE TABLE Orders
(order_nbr INTEGER NOT NULL PRIMARY KEY,
some_col DECIMAL (9,2) NOT NULL);
INSERT INTO Orders
VALUES (1, 0), (2, 0), (3, 0);
CREATE TABLE Order_Details
(order_nbr INTEGER NOT NULL,
sku INTEGER NOT NULL,
item_price DECIMAL (9,2) NOT NULL,
PRIMARY KEY(order_nbr, sku),
-- FOREIGN KEY(sku) REFERENCES Products(sku)
FOREIGN KEY(order_nbr) REFERENCES Orders(order_nbr));
INSERT INTO Order_Details VALUES (1, 1, 500.00);
INSERT INTO Order_Details VALUES (1, 2, 205.00);
INSERT INTO Order_Details VALUES (2, 1, 490.95);
INSERT INTO Order_Details VALUES (3, 1, 480.00);
SELECT * FROM Orders;
UPDATE Orders
SET Orders.some_col =Order_Details.item_price
FROM Orders
INNER JOIN
Order_Details
ON Orders.order_nbr =Order_Details.order_nbr;
/* results -- see item #1; last physical value
1 205.00 - where is the $500.00?
2 490.95
3 480.00
*/
--repeat with new physical ordering
DELETE FROM Order_Details;
DELETE FROM Orders;
DROP INDEX Order_Details.foobar;
-- index will change the execution plan
CREATE INDEX foobar ON Order_Details (order_nbr, item_price);
INSERT INTO Orders VALUES (1, 0);
INSERT INTO Orders VALUES (2, 0);
INSERT INTO Orders VALUES (3, 0);
INSERT INTO Order_Details VALUES (1, 2, 205.00);
INSERT INTO Order_Details VALUES (1, 1, 500.00);
INSERT INTO Order_Details VALUES (2, 1, 490.95);
INSERT INTO Order_Details VALUES (3, 1, 480.00);
UPDATE Orders
SET Orders.some_col = Order_Details.item_price
FROM Orders
INNER JOIN
Order_Details
ON Orders.order_nbr = Order_Details.order_nbr;
SELECT * FROM Orders;
/*
Results
1 500.00
2 490.95
3 480.00
*/
What is the first property that you must have in an INDEX? It cannot change the results of a statement, only the performance. See the problem?
Can you get an industry standard for the region codes?
We have the ANSI/ISO Standard MERGE statement which will catch this and other problems. Use it instead.