Mastering Microsoft Visual Basic 2008 phần 8 pps

115 183 0
Mastering Microsoft Visual Basic 2008 phần 8 pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Petroutsos c21.tex V3 - 01/28/2008 6:33pm Page 770 770 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES tables, and set up relations between them. And only then can we code against the database. It’s not uncommon to add a table to an existing database at a later stage, but this reveals some flaw in the initial database design. Working with R elationships, Indices, and Constraints To manipulate relationships, indices, and constraints, open one of the tables in design mode. The Table Designer menu contains a list of tasks you can perform with the table, including changing the relationships, indexes, and constraints. Relationships Relationships are the core of a relational database, because they relate tables to one another. To create a relationship, double-click a table’s name in Server Explorer and then choose Table Designer  Relationships, which displays the Foreign Key Relationships dialog box shown in Figure 21.9. This figure shows that there is already a relationship between the Categories table and the Products table. The relationship is called FK Products Categories, and it relates the pri- mary and foreign keys of the two tables (field CategoryID). The names of the two related tables appear in two read-only boxes. When you create a new relationship, you can select a table from a drop-down list. Under each table’s name, you see a list of fields. Here you select the matching fields in the two tables. Most relationships are based on a single field, which is common to both tables. However, you can relate two tables based on multiple fields (in which case, all pairs must match in a relationship). The check boxes at the bottom of the page specify how the DBMS will handle the relationship (they are discussed shortly). Figure 21.9 The Foreign Key Rela- tionships dialog box for the Categories table To create a new relationship with another table, click the Add button. A new relationship will be added with a default name, which you can change. Like all other objects, relationships have unique names, too. Expand the Tables And Columns Specification entry and click the ellipses button in this field. You’ll see the Tables And Columns dialog box. In the Primary Key Table column, you can select the name of the table that has the primary key in the relationship. The Foreign Key Table column always defaults to the current table, so when you create a relationship, you must create it in the table where the foreign key will appear. The default relationship names Petroutsos c21.tex V3 - 01/28/2008 6:33pm Page 771 SERVER EXPLORER 771 starts with the string FK (which stands for foreign key), followed by an underscore character and the name of the foreign table, then followed by another underscore and the name of the primary table. You can change the relationship’s name to anything you like. If the relationship is based on a compound key, select all the fields that make up the primary and foreign keys, in the same order. After you select the fields, click OK to create the relationship. At the right side of the Foreign Key Relationships dialog box, you see a few options that you can change: Check Existing Data On Creation Or Re-enabling If the existing data violate the relation- ship, the new relationship won’t be established. You will have to fix the data and then attempt to establish the relationship again. Enforce For Replication The relationship is enforced when the database is replicated. Enforce Foreign Key Constraint The relationship is enforced when you add new data or update existing data. If you attempt to add data that violate the relationship, the new data (or the update) will be rejected. Update Rule, Delete Rule When you change the primary key in one table, or delete it, some rows of a related table may be left with an invalid foreign key. If you delete a publisher, for example, all the titles that pointed to this publisher will become invalid after you change the publisher’s ID. If you change a publisher’s key, you may leave some books without a pub- lisher. You can set this option to take no action at all, automatically cascade the change (or deletion), set the affected data to Null, or set the data to the default value that you selected as part of the design. You can also create relationships on a database diagram by dragging the primary key field from its table and dropping it onto the foreign key of the related table. Just click the gray header of the primary key to select it, not the name of the field. To view or edit the details of a relationship, right-click the line that represents the relationship, and you will see the following commands: Delete Relationship From Database This command removes the relationship between the two tables. Properties This command brings up the Properties pages of the primary table, in which you can specify additional relationships or constraints. Earlier in this chapter, you saw that you couldn’t remove a row from the Categories table because this action conflicted with the FK Products Categories constraint. If you open the first diagram you created in this section and examine the properties of the relation between the Product and Categories tables, you’ll see that the FK Products Categories relationship is enforced. If you want to be able to delete categories, you must delete all the products that are associated with the specific category first. SQL Server can take care of deleting the related rows for you if you select Cascade option in the Delete Rule field. This is a rather dangerous practice, and you shouldn’t select it without good reason. In the case of the Products table, you shouldn’t enable cascade deletions. The products are also linked to the Order Details table, which means that the corresponding detail lines would also disappear from the database. Allowing cascade deletion in a database such as Northwind will result in loss of valuable information irrevocably. There are other situations in which cascade deletion is not such a critical issue. You can enable cascade deletions in the Pubs database, for instance, so that each time you delete a title, the cor- responding rows in the TitleAuthor table will also be removed. When you delete a book, you obviously don’t need any information about this book in the TitleAuthor table. Petroutsos c21.tex V3 - 01/28/2008 6:33pm Page 772 772 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES Indices/Keys You created a few tables and have entered some data into them. Now the most important thing you can do with a database is extract data from it (or else, why store the information in the first place?). We rarely browse the rows of a single table. Instead, we’re interested in summary infor- mation that will help us make business decisions. We need answers to questions such as ‘‘What’s the most popular product in California?’’ or ‘‘What month has the largest sales for a specific prod- uct?’’ To retrieve this type of information, you must combine multiple tables. To answer the first question, you must locate all the customers in California, retrieve their orders, sum the quantities of the items they have purchased, and then select the product with the largest sum of quantities. As you can guess, a DBMS must be able to scan the tables and locate the desired rows quickly. Computers use a special technique, called indexing, to locate information quickly. This tech- nique requires that the data be maintained in some order. The indexed rows need not be in a specific physical order, as long as you can retrieve them in a specific order. Indeed, an index is an ordering of the rows, and you can maintain the same rows sorted in many different ways. Depend- ing on the operation, the DBMS will select the appropriate index to speed up the operation. If you want to retrieve the name of the category of a specific product, the rows of the Categories table must be ordered according to the CategoryID field, which is the value that links each row in the Products table to the corresponding row in the Categories table. The DBMS retrieves the Catego- ryID field of a specific product and then instantly locates the matching row in the Categories table because the rows of this table are indexed according to their CategoryID field. Fortunately, you don’t have to maintain the rows of the tables in any order yourself. All you have to do is define the order, and the DBMS will maintain the indices for you. Every time a new row is added or an existing row is deleted or edited, the table’s indices are automatically updated. To speed up the searches, you can maintain an index for each field you want to search. Of course, although indexing will help the search operations, maintaining too many indices will slow the insertion and update operations. At any rate, all columns that are used in joins must be indexed, or else the selection process will be very slow. Use the Table Designer  Indexes/Keys command to display the Indexes/Keys dialog box for the Categories table. Figure 21.10 shows the properties of the PK Categories index of the Categories table. This index is based on the column CategoryID of the table and maintains the rows of the Categories table in ascending order according to their ID. The prefix PK stands for primary key. To specify that an index is also the table’s primary key, you must set the Is Unique property to Yes. You can create as many indices as necessary for each table, but only one of them can be the primary key. The Is Unique property in Figure 21.10 is disabled because the primary key is involved in one or more relationships — therefore, you can’t change the table’s primary key because you’ll break some of the existing relationships with other tables. To create a new index, click the Add button. Specify the column on which the new index will be based by clicking the ellipses in the Columns property and choosing the columns in the Index Columns dialog box. Enter a name for the new index (or accept the default one) using the (Name) property. Check Constraints A constraint is another important object of a database. The entity represented by a field can be subject to physical constraints. The Discount field, for example, should be a positive value no greater than 1 (or 100, depending on how you want to store it). Prices are also positive values. Other fields are subject to more-complicated constraints. The DBMS can make sure that the values assigned to those fields do not violate the constraints. Otherwise, you’d have to make sure that all the applications that access the same fields conform to the physical constraints. Petroutsos c21.tex V3 - 01/28/2008 6:33pm Page 773 STRUCTURED QUERY LANGUAGE 773 Figure 21.10 The Indexes/Keys tab of the Properties pages To make a constraint part of the database, open the table that contains the field on which you want to impose a constraint, in design view. Use the Table Designer  Check Constraints com- mand to display the Check Constraints dialog box. The names of the constraints start with the CK prefix, followed by an underscore, the name of the table, another underscore, and finally the name of the field to which the constraint applies. The CK Products UnitPrice constraint is the expression that appears in the Expression property (the UnitPrice field must be positive): ([UnitPrice]>=(0)) Constraints have a syntax similar to the syntax of SQL restrictions and are quite trivial. (I’ll get into SQL in the following section.) Another interesting constraint exists in the Employees table, and it’s expressed as follows: ([BirthDate]<GetDate()) This constraint prevents users and programs from inserting an employee that hasn’t been born yet. GetDate()is a built-in function that returns the current date and time. So far, you should have a good idea about how databases are organized, what the relation- ships are for, and why they’re so critical for the integrity of the data stored in the tables. Now you’ll look at ways to retrieve data from a database. To specify the rows and columns you want to retrieve from one or more tables, you must use SQL statements, which are the topic of the following section. Structured Query Language Structured Query Language (SQL) is a universal language for manipulating tables. Almost every DBMS supports it, so you should invest the time and effort to learn it. You can generate SQL statements with point-and-click operations (the Query Builder is a visual tool for generating SQL statements), but this is no substitute for understanding SQL and writing your own statements. The visual tools are nothing more than a user-friendly interface for specifying SQL statements. In the Petroutsos c21.tex V3 - 01/28/2008 6:33pm Page 774 774 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES background, they generate the appropriate SQL statement, and you will get the most out of these tools if you understand the basics of SQL. I will start with an overview of SQL and then I’ll show you how to use the Query Builder utility to specify a few advanced queries. By the way, the SQL version of SQL Server is called T-SQL, which stands for Transact-SQL. T-SQL is a superset of SQL and provides advanced programming features that are not available with SQL. I’m not going to discuss T-SQL in this book, but once you have understood SQL you’ll find it easy to leverage this knowledge to T-SQL. SQL is a nonprocedural language, which means that SQL doesn’t provide traditional program- ming structures such as If statements or loops. Instead, it’s a language for specifying the operation you want to perform against a database at a high level. The details of the implementation are left to the DBMS. SQL is an imperative language, like Language Integrated Query (LINQ), as opposed to a traditional programming language, such as VB. Traditional languages are declarative: The statements you write tell the compiler how to perform the desired actions. This is good news for nonprogrammers, but many programmers new to SQL might wish it had the structure of a more traditional language. You will get used to SQL and soon be able to combine the best of both worlds: the programming model of VB and the simplicity of SQL. Besides, there are many similarities between SQL and LINQ, and you’ll be able to leverage your skills in any of the two areas. SQL Is Not Case-Sensitive SQL is not case-sensitive, but it’s customary to use uppercase for SQL statements and keywords. In the examples in this book, I use uppercase for SQL statements. This is just a style to help you distin- guish between the SQL keywords and the table/field names of the query. Also, unlike VB, SQL literals must be embedded in single quotes, not double quotes. To retrieve all the company names from the Customers table of the Northwind database, you issue a statement like this one: SELECT CompanyName FROM Customers To select customers from a specific country, you must use the WHERE clause to limit the selected rows, as in the following statement: SELECT CompanyName FROM Customers WHERE Country = ’Germany’ The DBMS will retrieve and return the rows you requested. As you can see, this is not the way you’d retrieve rows with Visual Basic. With a procedural language such as VB, you’d have to write loops to scan the entire table, examine the value of the Country column, and either select or reject the row. Then you would display the selected rows. With SQL, you don’t have to specify how the selection operation will take place; you simply specify what you want the database to do for you — not how to do it. SQL statements are divided into two major categories, which are actually considered separate languages: the statements for manipulating the data, which form the Data Manipulation Language (DML), and the statements for defining database objects, such as tables or their indexes, which form the Data Definition Language (DDL). The DDL is not of interest to every database developer, Petroutsos c21.tex V3 - 01/28/2008 6:33pm Page 775 STRUCTURED QUERY LANGUAGE 775 and we will not discuss it in this book. The DML is covered in depth because you’ll use these statements to retrieve data, insert new data into the database, and edit or delete existing data. The statements of the DML part of the SQL language are also known as queries, and there are two types of queries: selection queries and action queries. Selection queries retrieve information from the database. A selection query returns a set of rows with identical structure. The columns can come from different tables, but all the rows returned by the query have the same number of columns. Action queries modify the database’s objects, or create new objects and add them to the database (new tables, relationships, and so on). Executing SQL Statements If you are not familiar with SQL, I suggest that you follow the examples in this chapter and exper- iment with the sample databases. To follow the examples, you have two options: the SQL Server Management Studio (SSMS) and the Query Designer of Visual Studio. The SSMS helps you man- age databases in various ways, including creating queries to extract data. The Query Designer is an editor for SQL statements that also allows you to execute them and see the results. In addition to the Query Designer, you can also use the Query Builder, which is part of the SSMS and Visual Studio. The Query Builder lets you build the statements with visual tools and you don’t have to know the syntax of SQL in order to create queries with the Query Builder. After a quick overview of the SQL statements, I will describe the Query Builder and show you how to use its interface to build fairly elaborate queries. Using the SQL Server Management Studio (SSMS) One of the applications installed with SQL Server is the SSMS. To start it, choose Start  Programs  SQL Server  SQL Server Management Studio. When this application starts, you see the Connect To Server dialog box (Figure 21.11). Choose Database Engine in the Server Type field so you can work with databases on your system. Select the server you want to use in the Server Name field. Provide your credentials and click Connect. Figure 21.11 SSMS provides access to all the database engine objects, including databases. After you’re connected, right-click the database you want to use and choose New Query from the context menu. Enter the SQL statement you want to execute in the blank query that SSMS Petroutsos c21.tex V3 - 01/28/2008 6:33pm Page 776 776 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES creates. The SQL statement will be executed against the selected database when you press Ctrl+E or click the Execute button (it’s the button with the exclamation point icon). Alternatively, you can prefix the SQL statement with the USE statement, which specifies the database against which the statement will be executed. To retrieve all the Northwind customers located in Germany, enter this statement: USE Northwind SELECT CompanyName FROM Customers WHERE Country = ’Germany’ The USE statement isn’t part of the query; it simply tells SSMS the database against which it must execute the query. I’m including the USE statement with all the queries so you know the database used for each example. If you’re executing the sample code from within Visual Studio, you need not use the USE statement, because all queries are executed against the selected database. Actually, the statement isn’t supported by the Query Designer of Visual Studio. The results of the query, known as the result set, will appear in a grid in the lower pane. An action query that updates a table (adds a new row, edits, or deletes an existing row) doesn’t return any rows; it simply displays the number of rows affected on the Messages tab. To execute another query, enter another statement in the upper pane, or edit the previous statement and press Ctrl+E again. You can also save SQL statements into files, so that you won’t have to type them again. To do so, open the File menu, choose Save As or Save, and enter the name of the file in which the contents of the Query pane will be stored. The statement will be stored in a text file with the extension .sql. Using Visual Studio To execute the same queries with Visual Studio, open the Server Explorer window and right-click the name of the database against which you want to execute the query. From the context menu, choose New Query, and a new query window will open. You will also see a dialog box prompting you to select one or more tables. For the time being, close this dialog box, because you will supply the names of the tables in the query; later in this chapter, you’ll learn how to use the visual tools to build queries. The Query Designer of Visual Studio is made up of four panes (Figure 21.12). The upper pane (which is the Table Diagram pane) displays the tables involved in the query, their fields, and the relationships between the tables — if any. The next pane shows the fields that will be included in the output of the query. Here you specify the output of the query, as well as the selection criteria. This pane is the Query Builder, the tool that lets you design queries visually. It’s discussed later in this chapter. In the next pane, the SQL pane, you see the SQL statement produced by the visual tools. If you modify the query with the visual tools, the SQL statement is updated automatically; likewise, when you edit the query, the other two panes are updated automatically to reflect the changes. The last pane, the Results pane, contains a grid with the query’s output. Every time you execute the query by clicking the button with the exclamation mark in the toolbar, the bottom pane is populated with the results of the query. For the examples in this section, ignore the top two panes. Just enter the SQL statements in the SQL pane and execute them. Using Selection Queries We’ll start our discussion of SQL with the SELECT statement. After you learn how to express the criteria for selecting the desired rows with the SELECT statement, you can apply this information to other data-manipulation statements. The simplest form of the SELECT statement is Petroutsos c21.tex V3 - 01/28/2008 6:33pm Page 777 STRUCTURED QUERY LANGUAGE 777 SELECT fields FROM tables Figure 21.12 Executing queries with Visual Studio where fields and tables are comma-separated lists of the fields you want to retrieve from the database and the tables they belong to. The list of fields following the SELECT statement is referred to as the selection list. To select the contact information from all the companies in the Customers table, use this statement: USE Northwind SELECT CompanyName, ContactName, ContactTitle FROM Customers To retrieve all the fields, use the asterisk (*). The following statement selects all the fields from the Customers table: SELECT * FROM Customers As soon as you execute a statement that uses the asterisk to select all columns, the Query Designer will replace the asterisk with the names of all columns in the table. Limiting the Selection with WHERE The unconditional form of the SELECT statement used in the previous section is quite trivial. You rarely retrieve data from all rows in a table. Usually you specify criteria, such as ‘‘all companies Petroutsos c21.tex V3 - 01/28/2008 6:33pm Page 778 778 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES from Germany,’’ ‘‘all customers who have placed three or more orders in the last six months,’’ or even more-complicated expressions. To restrict the rows returned by the query, use the WHERE clause of the SELECT statement. The most common form of the SELECT statement is the following: SELECT fields FROM tables WHERE condition The fields and tables arguments are the same as before, and condition is an expression that limits the rows to be selected. The syntax of the WHERE clause can get quite complicated, so we’ll start with the simpler forms of the selection criteria. The condition argument can be a relational expression, such as the ones you use in VB. To select all the customers from Germany, use the following condition: WHERE Country = ’Germany’ To select customers from multiple countries, use the OR operator to combine multiple conditions: WHERE Country = ’Germany’ OR Country = ’Austria’ You can also combine multiple conditions with the AND operator. Selecting Columns from Multiple Tables It is possible to retrieve data from two or more tables by using a single statement. (This is the most common type of query, actually.) When you combine multiple tables in a query, you can use the WHERE clause to specify how the rows of the two tables will be combined. Let’s say you want a list of all product names, along with their categories. For this query, you must extract the product names from the Products table and the category names from the Categories table and specify that the ProductID field in the two tables must match. The statement USE Northwind SELECT ProductName, CategoryName FROM Products, Categories WHERE Products.CategoryID = Categories.CategoryID retrieves the names of all products, along with their category names. Here’s how this statement is executed. For each row in the Products table, the SQL engine locates the matching row in the Categories table and then appends the ProductName and CategoryName fields to the result. If a product has no category, that product is not included in the result. If you want all the products, even the ones that don’t belong to a category, you must use the JOIN keyword, which is described later in this chapter. Using the WHERE clause to combine rows from multiple tables might lead to unexpected results because it can combine rows only with matching fields. If the foreign key in the Products table is Null, this product is not selected. This is a fine point in combining multiple tables, and many programmers abuse the WHERE clause. As a result, they retrieve fewer rows from the database and don’t even know it. Petroutsos c21.tex V3 - 01/28/2008 6:33pm Page 779 STRUCTURED QUERY LANGUAGE 779 Resolving Column Names When fields in two tables have the same names, you must prefix them with the table’s name to remove the ambiguity. When you execute a SQL statement, the Query Builder automatically prefixes all column names with the name of the table they belong to. Also, some field names might contain spaces. These field names must appear in square brackets. The Publishers table of the Pubs sample database contains a field named Publisher Name. To use this field in a query, enclose it in brackets: Publishers.[Publisher Name]. The table prefix is optional (no other table contains a column by that name), but the brackets are mandatory. To retrieve all the titles published by a specific publisher, the New Moon Books publisher, use a statement like the following: USE pubs SELECT titles.title FROM titles, publishers WHERE titles.pub id = publishers.pub id AND publishers.pub name = ’New Moon Books’ This statement combines two tables and selects the titles of a publisher specified by name. To match titles and publisher, it requests the following: ◆ The publisher’s name in the Publishers table should be New Moon Books. ◆ The pub id field in the Titles table should match the pub id field in the Publishers table. Knowing WHERE You’re Going If you specify multiple tables without the WHERE clause, the SQL statement will return an enormous set of rows, which is known as a cursor. If you issue the following statement, you will not get a line for each product name followed by its category: SELECT ProductName, CategoryName FROM Categories, Products You will get a cursor with 616 rows, which are all possible combinations of product names and cate- gory names. In this example, the Categories table has 8 rows, and the Products table has 77 rows, so their cross-product contains 616 rows. It’s extremely rare to request the cross-product of two tables. If the two tables have many rows, you will have to stop the execution of the query by clicking the round button with the red square in the status bar of the Query Designer window, next to the number of selected rows. Notice that we did not specify the publisher’s name (field pub name) in the selection list. All the desired books have the same publisher, so we need not include the publisher’s names in the result set. [...]... *(1 - Discount)) AS Revenue FROM [Order Details] GROUP BY ProductID ORDER BY ProductID Here are the first few lines returned by the preceding query: Product 1 2 Invoices 38 44 Units Sold 82 8 1057 Revenue 12 788 .1000595092 16355.96004 486 08 You should try to revise the preceding statement so that it displays product names instead of IDs, by adding another join to the query as explained already Limiting Groups... select Sum Make sure that 799 80 0 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES the Output column is selected and then run the query You’ll have the same results as before, only this time with an extra column, which is the revenue generated by the corresponding product: Alice Mutton Aniseed Syrup Boston Crab Meat 9 78 3 28 1103 37 12 41 326 98. 379 981 994629 3044 17910.62 989 2349243 The SQL statement generated... Syrup Boston Crab Meat Camembert Pierrot Carnarvon Tigers Total Revenues 326 98. 38 3044.0 17910.63 46927. 48 29171 .87 If you omit the GROUP BY clause, the query will generate an error message indicating that the ProductName column in the selection list is not involved in an aggregate or a GROUP BY clause 789 790 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES You can also combine multiple aggregate... procedure, the year 19 98 will be used automatically The AS keyword marks the beginning of the stored procedure The first IF statement makes sure that the year is a valid one (from 1996 to 19 98) If not, it will use the year 19 98 The BEGIN and END keywords mark the beginning and end of the IF block (the same block that’s delimited by the If and End If statements in VB code) 80 1 80 2 CHAPTER 21 BASIC CONCEPTS OF... with matching values in the two related tables Master It How will you relate two tables with a many-to-many relationship? Utilize the data tools of Visual Studio Visual Studio 20 08 provides visual tools for working with databases The Server Explorer is a visual representation of the databases you can access from your computer and their data You can create new databases, edit existing ones, and manipulate... the query This time you’ll get one line per product The Alice Mutton item has been ordered 37 times, and the total items sold are 9 78, as in the preceding query Alice Mutton Aniseed Syrup Boston Crab Meat Camembert Pierrot 9 78 3 28 1103 1577 37 12 41 51 797 7 98 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES Figure 21.14 A query with totals The SELECT statement generated by the Query Builder is the... Revenues] STRUCTURED QUERY LANGUAGE FROM [Order Details] GROUP BY ProductID ORDER BY ProductID The preceding statement produces the following output: ProductID 1 2 3 4 5 6 7 Total Revenues 12 788 .10 16355.96 3044.0 85 67 .89 5347.20 7137.0 22044.29 The aggregate functions work in tandem with the GROUP BY clause (when there is one) to produce subtotals The GROUP BY clause groups all the rows with the same values... is a visual tool for building SQL statements, and it’s available with both SQL Server Management Studio (SSMS) and Visual Studio It’s a highly useful tool that generates SQL statements for you — you just specify the data you want to retrieve with point-and-click operations, instead of typing complicated expressions A basic understanding of SQL is obviously required, which is why I described the basic. .. OD.Quantity) * (1 - OD.Discount)) AS Revenue FROM Customers AS C INNER JOIN Orders AS O ON C.CustomerID = O.CustomerID INNER JOIN [Order Details] AS OD ON O.OrderID = OD.OrderID GROUP BY C.CompanyName 787 788 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES Query 2 SELECT C.CompanyName, SUM((OD.UnitPrice * OD.Quantity) * (1 - OD.Discount)) AS Revenue FROM Customers AS C INNER JOIN Orders AS O ON C.CustomerID... languages such as Visual Basic, which specifies how to perform the operation The details of the implementation are left to the DBMS SQL consists of a small number of keywords and is optimized for selecting, inserting, updating, and deleting data Master It How would you write a SELECT statement to retrieve data from multiple tables? 80 3 Chapter 22 Programming with ADO.NET In Chapter 21, ‘ Basic Concepts . ON O.OrderID = OD.OrderID GROUP BY C.CompanyName Petroutsos c21.tex V3 - 01/ 28 /20 08 6:33pm Page 788 788 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES Query 2 SELECT C.CompanyName, SUM((OD.UnitPrice. include the publisher’s names in the result set. Petroutsos c21.tex V3 - 01/ 28 /20 08 6:33pm Page 780 780 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES Aliasing Table Names To avoid typing long. values that are (or are not) Null. To locate the Petroutsos c21.tex V3 - 01/ 28 /20 08 6:33pm Page 782 782 CHAPTER 21 BASIC CONCEPTS OF RELATIONAL DATABASES rows of the Customers table that have

Ngày đăng: 12/08/2014, 21:20

Tài liệu cùng người dùng

Tài liệu liên quan