tailieunhanh - Beginning C# 2008 Databases From Novice to Professional phần 3

Cơ sở dữ liệu mô hình hoạt động như một khuôn mẫu cho các cơ sở dữ liệu mới được tạo ra, nói cách khác, thiết lập cơ sở dữ liệu mô hình sẽ được áp dụng để tạo ra cơ sở dữ liệu tất cả người dùng. • Cơ sở dữ liệu msdb được sử dụng bởi SQL Server Agent cho công việc lập kế hoạch và cảnh báo. | CHAPTER 5 MANIPULATING DATABASE DATA 75 Figure 5-2. Query results pane How It Works You ask the database to return the data for all columns and you get exactly that. If you scroll to the right you ll find all the columns in the Employees table. Most of the time you should limit queries to only relevant columns. When you select columns you don t need you waste resources. To explicitly select columns enter the column names after the SELECT keyword as shown in the following query and click Execute. Figure 5-3 shows the results. Select employeeid firstname lastname from employees This query selects all the rows from the Employees table but only the EmployeeID FirstName and LastName columns. 76 CHAPTER 5 MANIPULATING DATABASE DATA Figure 5-3. Selecting specific columns Using the WHERE Clause Queries can have WHERE clauses. The WHERE clause allows you to specify criteria for selecting rows. This clause can be complex but we ll stick to a simple example for now. The syntax is as follows WHERE column1 operator column2 Value Here operator is a comparison operator for example or . Table 5-1 later in the chapter lists the T-SQL comparison operators. Try It Out Refining Your Query In this exercise you ll see how to refine your query. 1. Add the following WHERE clause to the query in Figure 5-3. Where country USA 2. Run the query by pressing F5 and you should see the results shown in Figure 5-4. CHAPTER 5 MANIPULATING DATABASE DATA 77 Figure 5-4. Using a WHERE clause Caution SQL keywords and table and column names aren t case sensitive but string literals enclosed in single quotes are. This is why we use USA not usa for this example. How It Works The new query returns the data for columns EmployeelD FirstName and LastName from the Employees table but only for rows where the Country column value equals USA