tailieunhanh - Expert SQL Server 2008 Development- P2

Tham khảo tài liệu 'expert sql server 2008 development- p2', công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | CHAPTER 2 BEST PRACTICES FOR DATABASE PROGRAMMING Identify Hidden Assumptions in Your Code One of the core tenets of defensive programming is to identify all of the assumptions that lie behind the proper functioning of your code. Once these assumptions have been identified the function can either be adjusted to remove the dependency on them or explicitly test each condition and make provisions should it not hold true. In some cases hidden assumptions exist as a result of code failing to be sufficiently explicit. To demonstrate this concept consider the following code listing which creates and populates a Customers and an Orders table CREATE TABLE Customers CustID int Name varchar 32 Address varchar 255 INSERT INTO Customers CustID Name Address VALUES 1 Bob Smith Flat 1 27 Heigham Street 2 Tony James 87 Long Road GO CREATE TABLE Orders OrderID INT CustID INT OrderDate DATE INSERT INTO Orders OrderID CustID OrderDate VALUES 1 1 2008-01-01 2 1 2008-03-04 3 2 2008-03-07 GO Now consider the following query to select a list of every customer order which uses columns from both tables SELECT Name Address OrderID FROM Customers c JOIN Orders o ON GO 29 CHAPTER 2 BEST PRACTICES FOR DATABASE PROGRAMMING The query executes successfully and we get the results expected Bob Smith Flat 1 27 Heigham Street 1 Bob Smith Flat 1 27 Heigham Street 2 Tony James 87 Long Road 3 But what is the hidden assumption The column names listed in the SELECT query were not qualified with table names so what would happen if the table structure were to change in the future Suppose that an Address column were added to the Orders table to enable a separate delivery address to be attached to each order rather than relying on the address in the Customers table ALTER TABLE Orders ADD Address varchar 255 GO The unqualified column name Address specified in the SELECT query is now ambiguous and if we attempt to run the original query again we receive an error Msg 209 Level 16 State 1 Line 1

TỪ KHÓA LIÊN QUAN