Tài liệu Using SQL phần 1 doc - Pdf 87

Using SQL
SQL (pronounced sequel) is the standard language for accessing relational databases. As
you'll see in this chapter, SQL is easy to learn and use. With SQL, you tell the database
what data you want to access, and the database software figures out exactly how to get
that data.
There are many types of SQL statements, but the most commonly used types of SQL
statements are these:

Data Manipulation Language (DML) statements

Data Definition Language (DDL) statements
DML statements allow you to retrieve, add, modify, and delete rows stored in the
database. DDL statements allow you to create database structures such as tables.
Before you learn the basics of DML statements, you need to know how you can enter and
run SQL statements. You can enter and run SQL statements against a SQL Server
database using the Query Analyzer tool, and you'll learn about this next.

Note As you'll see later in the "Accessing a Database Using Visual Studio .NET" section,
you can also use Visual Studio .NET to create SQL statements. Visual Studio .NET
enables you to create SQL statements visually, as well as entering them manually.
Using Query Analyzer
You use Query Analyzer to enter and run SQL statements. You start Query Analyzer by
selecting Start ➣ Microsoft SQL Server ➣ Query Analyzer. In the following sections,
you'll learn how to connect to a SQL server instance, enter and run a SQL statement, save
a SQL statement, and load one.
Connecting to a SQL Server Instance
When you start Query Analyzer, the first thing it displays is the Connect to SQL Server
dialog box, as shown in Figure 3.1
. In the SQL Server field, you enter the name of the
SQL Server instance to which you want to connect. You can click the drop-down list and
select an instance of SQL Server, or you can click the ellipsis button (three dots …) to the

window, and the results retrieved from the database are displayed in the bottom part. You
specify the database to access with the USE statement, and you retrieve rows from the
database using the SELECT statement.
Tip You can also specify the database to access by using the drop-down list on the
toolbar.
If you want to follow along with this example, go ahead and enter the following USE
statement into your Query window:
USE Northwind
This USE statement indicates that you want to use the Northwind database. Next, on a
separate line, enter the following SELECT statement:
SELECT CustomerID, CompanyName FROM Customers;
This SELECT statement indicates that you want to retrieve the CustomerID and
CompanyName columns from the Customers table.

Note SELECT and FROM are SQL keywords. Although SQL isn't case sensitive, I use
uppercase when specifying SQL keywords and mixed case when specifying column
and table names. You may terminate a SQL statement using a semicolon (;),
although this isn't mandatory.
You can run the SQL statement entered in the Query window in five ways:

Selecting Execute from the Query menu

Clicking the Execute Query button (green triangle) on the toolbar

Pressing the F5 key on the keyboard

Pressing Ctrl+E on the keyboard

Pressing Alt+X on the keyboard
Once you run the SQL statement, your statement is sent to the database for execution.

INSERT Adds one or more new rows to a table.

UPDATE Modifies one or more rows in a table.

DELETE Removes one or more rows from a table.
You'll learn how to use these four statements in the following sections.
Retrieving Rows From a Single Table
You use the SELECT statement to retrieve rows from tables. The SELECT statement has
many forms, and the simplest version allows you to specify a list of columns and the
table name. For example, the following SELECT statement retrieves the CustomerID,
CompanyName, ContactName, and Address columns from the Customers table:
SELECT CustomerID, CompanyName, ContactName, Address
FROM Customers;
The columns to retrieve are specified after the SELECT keyword, and the table is
specified after the FROM keyword.
If you want to retrieve all columns from a table, specify the asterisk character (*)
immediately after the SELECT keyword.
Tip To avoid retrieving more information than you need, rather than use *, list only the
columns you actually want.
For example, the following SELECT statement retrieves all the columns from the
Customers table using *:
SELECT *
FROM Customers;
Figure 3.3
shows the results of this SELECT statement.

Figure 3.3: Using a SELECT statement to retrieve rows from the Customers table


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status