Appendix:附录: Getting started起步

The purpose of this appendix is to help you get started and set up your environment so that you have everything you need to get the most out of this book.本附录的目的是帮助你开始并设置你的环境,这样你就拥有了充分利用本书所需的一切。

You can run all code samples in this book on a Microsoft SQL Server box product, and most of the examples can be run on Microsoft Azure SQL Database. 您可以在Microsoft SQL Server box产品上运行本书中的所有代码示例,大多数示例都可以在Microsoft Azure SQL数据库上运行。For details about the differences between the flavors, see the section “The ABCs of Microsoft RDBMS flavors” in Chapter 1, “Background to T-SQL querying and programming.”有关两种风格之间差异的详细信息,请参阅第1章T-SQL查询和编程背景”中的“Microsoft RDBMS风格的基础知识”一节。

The first section, “Getting started with Azure SQL Database,” provides a link to the website where you can find the information you need to get started with Azure SQL Database.第一部分“开始使用Azure SQL数据库”提供了一个指向网站的链接,您可以在其中找到开始使用Azure SQL数据库所需的信息。

The second section, “Installing a SQL Server box product,” assumes you want to connect to a SQL Server box product instance to run the code samples in this book, and that you don't have an instance to connect to already. 第二部分“安装SQL Server box产品”假设您想要连接到SQL Server box产品实例以运行本书中的代码示例,并且您还没有要连接的实例。This section walks you through the installation process for a SQL Server 2016 instance. 本节将引导您完成SQL Server 2016实例的安装过程。If you already have an instance of SQL Server to connect to, feel free to skip this section.如果您已经有一个SQL Server实例要连接,请跳过本节。

The third section, “Downloading and installing SQL Server Management Studio,” provides instructions to download and install SQL Server Management Studio (SSMS).第三部分“下载和安装SQL Server Management Studio”提供了下载和安装SQL Server Management Studio(SSMS)的说明。

The fourth section, “Downloading source code and installing the sample database,” points you to the website where you can get the downloadable source code for the book and provides instructions for installing the book's sample database on both a SQL Server box product and Azure SQL Database.第四部分“下载源代码并安装示例数据库”指向网站,在那里可以获得本书的可下载源代码,并提供在SQL Server box产品和Azure SQL数据库上安装本书示例数据库的说明。

The fifth section, “Working with SQL Server Management Studio,” explains how to develop and execute T-SQL code in SQL Server by using SSMS.第五部分“与SQL Server Management Studio合作”介绍了如何使用SSMS在SQL Server中开发和执行T-SQL代码。

The last section, “Working with SQL Server Books Online,” describes SQL Server Books Online and explains its importance in helping you get information about T-SQL.最后一节,“使用SQL Server联机丛书”介绍了SQL Server联机丛书,并解释了它在帮助您获取有关T-SQL的信息方面的重要性。

Getting started with Azure SQL DatabaseAzure SQL数据库入门

If you want to run the code samples in this book on Azure SQL Database, you need access to an Azure SQL Database server, with an account that has privileges to create a new database (or ask an administrator to create the sample database for you). 如果你想在Azure SQL数据库上运行本书中的代码示例,你需要访问Azure SQL数据库服务器,并拥有创建新数据库的权限(或要求管理员为你创建示例数据库)。If you don't already have access to Azure SQL Database, you can find useful information on how to get started on the Microsoft Azure main page at https://azure.microsoft.com.如果您还没有访问Azure SQL数据库的权限,可以在Microsoft Azure主页上找到有关如何开始的有用信息,网址为https://azure.microsoft.com

You need a Microsoft account to create a Microsoft Azure subscription. 您需要Microsoft帐户才能创建Microsoft Azure订阅。If you don't already have an account, you can create one at https://signup.live.com. 如果你还没有账户,你可以在https://signup.live.com创建账户。When you have a Microsoft Azure subscription, you can connect to the Microsoft Azure Portal at https://portal.azure.com, from which you can manage your Azure SQL Database servers and databases.当您拥有Microsoft Azure订阅时,您可以通过以下地址连接到Microsoft Azure门户:https://portal.azure.com,您可以从中管理Azure SQL数据库服务器和数据库。

The Microsoft Azure main page offers different options for getting started (by buying a subscription or getting a free trial), and it provides access to resources such as the management portal, community, and support.Microsoft Azure主页提供了不同的入门选项(通过购买订阅或获得免费试用),并提供对管理门户、社区和支持等资源的访问。

When you have access to Azure SQL Database, proceed to the instructions on how to download the source code and install the sample database later in this appendix.当您有权访问Azure SQL数据库时,请继续阅读本附录后面有关如何下载源代码和安装示例数据库的说明。

Installing a SQL Server box product安装SQL Server box产品

This section is relevant for those who want to run the code samples in this book and practice the exercises against a SQL Server box product and don't already have access to one. 本节适用于那些希望运行本书中的代码示例,并针对SQL Server box产品练习这些练习,但还没有访问该产品的人。You can use any edition of SQL Server 2016 or later. 您可以使用SQL Server 2016或更高版本的任何版本。Assuming you don't already have an instance of SQL Server to connect to, the following sections describe where you can obtain SQL Server and how to install it.假设您尚未连接到SQL Server实例,以下各节将介绍从何处获取SQL Server以及如何安装它。

1. Obtain SQL Server获取SQL Server

As I mentioned, you can use any edition of SQL Server 2016 or later to practice the materials in this book. 正如我提到的,您可以使用SQL Server 2016或更高版本的任何版本来练习本书中的内容。If you have a subscription to the Microsoft Developer Network (MSDN), you can use the SQL Server 2016 Developer edition for learning purposes. 如果您订阅了Microsoft开发者网络(MSDN),可以使用SQL Server 2016开发者版进行学习。You can download it from https://msdn.microsoft.com/subscriptions/downloads. 你可以从https://msdn.microsoft.com/subscriptions/downloads下载到它。The SQL Server Developer edition is also available for free for Visual Studio Dev Essentials members. SQL Server Developer edition也可免费提供给Visual Studio Dev Essentials成员。For details, see https://blogs.technet.microsoft.com/dataplatforminsider/2016/03/31/microsoft-sql-server-developer-edition-is-now-free. 有关详细信息,请参阅https://blogs.technet.microsoft.com/dataplatforminsider/2016/03/31/microsoft-sql-server-developer-edition-is-now-freeAnother option is to use the free trial software of SQL Server 2016, which you can download from https://www.microsoft.com/sql. 另一个选择是使用SQL Server 2016的免费试用软件,您可以从https://www.microsoft.com/sql下载它。In this appendix, I demonstrate the installation of the SQL Server 2016 Evaluation edition.在本附录中,我将演示SQL Server 2016评估版的安装。

2. Install the database engine安装数据库引擎

Assuming you have the SQL Server installation software available, you can proceed to installing the product.假设您有可用的SQL Server安装软件,则可以继续安装该产品。

To install the database engine

1. Start the setup.exe program from the SQL Server installation folder. 从SQL Server安装文件夹启动setupexe程序。You should see the SQL Server Installation Center dialog box shown in Figure A-1.您应该会看到“SQL Server安装中心”对话框,如图A-1所示。

Image

FIGURE A-1 SQL Server Installation Center.安装中心。

2. In the left pane, choose Installation. Note that the screen changes.在左侧窗格中,选择安装。请注意,屏幕会发生变化。

3. In the right pane, choose New SQL Server Stand-Alone Installation Or Add Features To An Existing Installation. 在右侧窗格中,选择新建SQL Server单机安装或向现有安装添加功能。The Product Key dialog box appears, as shown in Figure A-2.此时会出现产品密钥对话框,如图A-2所示。

Image

FIGURE A-2 The Product Key dialog box.“产品密钥”对话框。

4. Make sure that Evaluation is selected in the Specify A Free Edition list box, and click Next to continue. 确保在“指定免费版本”列表框中选择了“评估”,然后单击“下一步”继续。The License Terms dialog box appears.此时会出现“许可条款”对话框。

5. Confirm that you accept the license terms, and click Next to continue. 确认您接受许可条款,然后单击“下一步”继续。The Microsoft Update dialog box appears.此时会出现“Microsoft更新”对话框。

6. Confirm that you want to use Microsoft Update to check for updates (recommended), and click Next to continue. 确认要使用Microsoft Update检查更新(推荐),然后单击“下一步”继续。The Install Rules dialog box appears.将显示“安装规则”对话框。

7. Ensure that no problems are indicated. Click Next to continue. 确保没有显示任何问题。单击“下一步”继续。The Feature Selection dialog box appears. 此时会出现“功能选择”对话框。Select the features to install, as shown in Figure A-3.选择要安装的功能,如图A-3所示。

Image

FIGURE A-3 The Feature Selection dialog box.“功能选择”对话框。

8. Select the Database Engine Services feature. 选择数据库引擎服务功能。For the purposes of this book, you don't need any of the other features.就本书而言,您不需要任何其他功能。

9. When you're done, click Next to continue. 完成后,单击“下一步”继续。The Instance Configuration dialog box appears, as shown in Figure A-4.此时会出现实例配置对话框,如图A-4所示。

Image

FIGURE A-4 The Instance Configuration dialog box.“实例配置”对话框。

If you're not familiar with the concept of SQL Server instances, you can find details in Chapter 1, in the “SQL Server architecture” section.如果您不熟悉SQL Server实例的概念,可以在第1章的“SQL Server体系结构”部分中找到详细信息。

10. If a default instance of SQL Server is not installed on your computer and you would like to configure the new instance as the default, simply confirm that the Default Instance option is selected. 如果您的计算机上没有安装SQL Server的默认实例,并且希望将新实例配置为默认实例,只需确认选择了默认实例选项。If you want to configure the new instance as a named instance, make sure the Named Instance option is selected and that you specify a name for the new instance (for example, SQL2016). 如果要将新实例配置为命名实例,请确保已选中“命名实例”选项,并为新实例指定名称(例如SQL2016)。When you later connect to SQL Server, you'll specify only the computer name for a default instance (for example, MERU), and the computer name\instance name for a named instance (for example, MERU\SQL2016).稍后连接到SQL Server时,将仅为默认实例指定计算机名(例如,MERU),并为命名实例指定计算机名\实例名(例如,MERU\SQL2016)。

11. When you're done, click Next to continue. 完成后,单击“下一步”继续。The Server Configuration dialog box appears.此时会出现“服务器配置”对话框。

For the purposes of this book, you do not need to change the default choices in the Service Accounts and Collation dialog boxes. 在本书中,您无需更改“服务帐户和排序规则”对话框中的默认选项。If you want to know more about collation, you can find details in Chapter 2, “Single-table queries,” in the “Working with character data” section.如果您想了解有关排序规则的更多信息,可以在第2章单表查询”的“使用字符数据”部分中找到详细信息。

12. Click Next to continue. 单击“下一步”继续。The Database Engine Configuration dialog box appears.此时会出现“数据库引擎配置”对话框。

13. On the Server Configuration tab, ensure that under Authentication Mode the Windows Authentication Mode option is selected. 在服务器配置选项卡上,确保在身份验证模式下选择了Windows身份验证模式选项。Under Specify SQL Server Administrators, click Add Current User to assign the current logged-on user with the System Administrator (sysadmin) server role, as shown in Figure A-5. 在Specify SQL Server Administrators(指定SQL Server管理员)下,单击“添加当前用户”,为当前登录的用户分配系统管理员(sysadmin)服务器角色,如图A-5所示。SQL Server administrators have unrestricted access to the SQL Server database engine.SQL Server管理员可以不受限制地访问SQL Server数据库引擎。

Image

FIGURE A-5 The Database Engine Configuration dialog box.“数据库引擎配置”对话框。

Of course, in your case, your current user name will appear instead of MERU\Gandalf.当然,在您的情况下,将显示您当前的用户名,而不是MERU\Gandalf

If you want to change the setup program's defaults in terms of data directories, you can do so on the Data Directories tab. 如果要更改安装程序在数据目录方面的默认设置,可以在“数据目录”选项卡上进行更改。For the purposes of the book, you don't need to configure anything on the TempDB and FILESTREAM tabs.就本书而言,您不需要在TempDB和FILESTREAM选项卡上配置任何内容。

14. Click Next to continue. 单击“下一步”继续。The Ready To Install dialog box appears with a summary of the installation choices.此时会出现“准备安装”对话框,其中包含安装选项的摘要。

15. Ensure that the summary indicates your choices correctly, and click Install to start the actual installation process. 确保摘要正确显示了您的选择,然后单击“安装”开始实际的安装过程。The Installation Progress dialog box appears and remains open throughout the remainder of the installation process. 此时会出现“安装进度”对话框,并在安装过程的其余部分保持打开状态。This dialog box provides a general progress bar as well as indicating the status of each feature that is being installed. 此对话框提供常规进度条,并指示正在安装的每个功能的状态。((See Figure A-6.)见图A-6。)

Image

FIGURE A-6 The Installation Progress dialog box.“安装进度”对话框。

16. When the installation is complete, the Complete dialog box appears, as shown in Figure A-7.安装完成后,会出现“完成”对话框,如图A-7所示。

Image

FIGURE A-7 The Complete dialog box.完整的对话框。

This dialog box should indicate the successful completion of the installation.此对话框应指示安装已成功完成。

17. Click Close to finish.单击“关闭”完成。

Downloading and installing SQL Server Management Studio下载和安装SQL Server Management Studio

Whether you plan to work with SQL Server or Azure SQL Database as the database engine, you need to download and install SQL Server Management Studio (SSMS)—the client tool you use to develop and execute your T-SQL code against the database engine. 无论您计划使用SQL Server还是Azure SQL Database作为数据库引擎,都需要下载并安装SQL Server Management Studio(SSMS)——您用来开发和执行针对数据库引擎的T-SQL代码的客户端工具。You can download the installation software from https://msdn.microsoft.com/en-us/library/mt238290.aspx. 您可以从https://msdn.microsoft.com/en-us/library/mt238290.aspx下载安装软件。Once the download completes, run the setup program. 下载完成后,运行安装程序。No user input is required other than clicking the Install button to initiate the installation and the Close button when it's done.无需用户输入,只需单击“安装”按钮即可启动安装,完成后单击“关闭”按钮即可。

Downloading source code and installing the sample database下载示例数据库并安装源代码

To download the source code, visit the book's companion website here: 要下载源代码,请访问本书的配套网站:http://tsql.solidq.com/books/tf3. This page has a link to download a single compressed file with the book's source code, as well as a script file called TSQLV4.sql that creates the sample database. 这个页面有一个链接,可以下载一个带有本书源代码的压缩文件,以及一个名为TSQLV4.sql的脚本文件,该文件创建了示例数据库。Decompress the files to a local folder (for example, C:\TSQLFundamentals).将文件解压缩到本地文件夹(例如,C:\TSQLFundamentals)。

You'll find up to three .sql script files associated with each chapter of the book:您可以找到与本书每一章相关联的三个sql脚本文件:

Image One file contains the source code for the corresponding chapter. 一个文件包含相应章节的源代码。It's provided for your convenience, in case you don't want to type the code that appears in the book. 它是为方便您而提供的,以防您不想键入书中出现的代码。The name of this file matches the title of the corresponding chapter.此文件的名称与相应章节的标题匹配。

Image A second file contains the exercises for the chapter. 第二个文件包含本章的练习。The name of this file also matches the title of the corresponding chapter but includes the suffix “Exercises.”该文件的名称也与相应章节的标题匹配,但包含后缀“Exercises”。

Image A third file contains the solutions to the chapter's exercises. 第三个文件包含本章练习的解决方案。The name of this file matches the title of the corresponding chapter but includes the suffix “Solutions.”此文件的名称与相应章节的标题匹配,但包含后缀“Solutions”。

You use SSMS to open the files and run the code they contain. 您可以使用SSM打开文件并运行其中包含的代码。If you don't have SSMS installed already, make sure you install it first by following the instructions in the section “Downloading and installing SQL Server Management Studio.” 如果尚未安装SSMS,请确保先按照“下载和安装SQL Server Management Studio”一节中的说明进行安装。The next section explains how to work with SSMS.下一节将解释如何使用SSMS。

You'll also find a text file called orders.txt, which you can use when practicing the materials from Chapter 8, “Data modification.” 您还可以找到一个名为orders.Text的文本文件,在练习第8章数据修改”中的材料时可以使用该文件。Also included is a script file called TSQLV4.sql, which creates the book's sample database, TSQLV4.还包括一个名为TSQLV4.sql的脚本文件,它创建了本书的示例数据库TSQLV4

To create the sample database in an instance of a SQL Server box product, you simply need to run this script file while you're connected to the target SQL Server instance. 要在SQL Server box产品的实例中创建示例数据库,只需在连接到目标SQL Server实例时运行此脚本文件。If you aren't familiar with running script files in SQL Server, you can follow these steps to complete the database creation.如果不熟悉在SQL Server中运行脚本文件,可以按照以下步骤完成数据库创建。

To create and populate the sample database in a SQL Server box product在SQL Server box产品中创建和填充示例数据库

1. Double-click the TSQLV4.sql file name in File Explorer to open the file in SSMS. 双击文件资源管理器中的TSQLV4sql文件名,在SSMS中打开该文件。The Connect To Database Engine dialog box appears.此时会出现“连接到数据库引擎”对话框。

2. In the Server Name box, ensure that the name of the instance you want to connect to appears. 在“服务器名称”框中,确保显示要连接到的实例的名称。For example, you would type the name MERU if your instance was installed as the default instance in a computer called MERU, or MERU\SQL2016 if your instance was installed as a named instance called SQL2016 in a computer called MERU.例如,如果您的实例作为默认实例安装在一台名为MERU的计算机上,则键入MERU;如果您的实例作为命名实例安装在一台名为MERU的计算机上,则键入MERU\SQL2016

3. In the Authentication box, make sure Windows Authentication is selected. 在“身份验证”框中,确保选择了Windows身份验证。Click Connect.单击“连接”。

4. When you're connected to SQL Server, press F5 to run the script. 连接到SQL Server后,按F5运行脚本。When the execution is done, the Command(s) Completed Successfully message should appear in the Messages pane. 执行完成后,“消息”窗格中应显示“命令成功完成”消息。You should see the TSQLV4 database in the Available Databases box.您应该在“可用数据库”框中看到TSQLV4数据库。

5. When you're done, you can close SSMS.完成后,可以关闭SSMS。

To create and populate the sample database in Azure SQL Database在Azure SQL数据库中创建并填充示例数据库

1. Double-click the file name in File Explorer to open the file in SSMS. 双击文件资源管理器中的文件名,在SSMS中打开该文件。The Connect To Database Engine dialog box appears.此时会出现“连接到数据库引擎”对话框。

2. In the Server Name box, ensure that the name of the Azure SQL Database server you want to connect to appears—for example, myserver.database.windows.net.在“服务器名称”框中,确保显示要连接的Azure SQL数据库服务器的名称,例如myserver.database.windows.net

3. In the Authentication box, make sure SQL Authentication is selected and the correct logon name and password are entered. 在“身份验证”框中,确保选择了SQL身份验证,并输入了正确的登录名和密码。Click Options.单击“选项”。

4. On the Connection Properties tab, type master in the Connect To Database text box, and then click Connect.在“连接属性”选项卡上,在“连接到数据库”文本框中键入master,然后单击“连接”。

5. Skip the instructions under Section A in the script (for a SQL Server box product), and follow the instructions under Section B in the script (for Azure SQL Database). 跳过脚本中A部分下的说明(对于SQL Server box产品),并遵循脚本中B部分下的说明(对于Azure SQL数据库)。The most important instruction is the one telling you to run the following command to create the TSQLV4 database:最重要的指令是告诉您运行以下命令来创建TSQLV4数据库:

CREATE DATABASE TSQLV4;

6. Right-click any empty area in the query pane, and choose Connection | Change Connection. 右键单击查询窗格中的任何空白区域,然后选择“连接”|“更改连接”。The Connect To Database Engine dialog box appears. 此时会出现“连接到数据库引擎”对话框。Specify TSQLV4 as the database to connect to, and click Connect. TSQLV4指定为要连接的数据库,然后单击“连接”。You should see the TSQLV4 database in the Available Databases box.您应该在“可用数据库”框中看到TSQLV4数据库。

As an alternative, you can simply select the TSQLV4 database from the Available Databases box.作为替代方案,您只需从“可用数据库”框中选择TSQLV4数据库即可。

7. Highlight the code in Section C (beginning with Create Schemas and continuing all the way to the end of the script file). 突出显示C部分中的代码(从创建模式开始,一直到脚本文件的末尾)。Press F5 to run the script. When the execution is done, the Command(s) Completed Successfully message should appear in the Messages pane. F5运行脚本。执行完成后,“消息”窗格中应显示“命令成功完成”消息。Note that on slow connections it might take the code a few minutes to complete.请注意,在连接速度较慢的情况下,代码可能需要几分钟才能完成。

8. When you're done, you can close SSMS.完成后,可以关闭SSMS。

The data model of the TSQLV4 database is provided in Figure A-8 for your convenience.为了方便起见,图A-8提供了TSQLV4数据库的数据模型。

Image

FIGURE A-8 The data model of the TSQLV4 database.TSQLV4数据库的数据模型。

Working with SQL Server Management Studio使用SQL Server Management Studio

SQL Server Management Studio (SSMS) is the client tool you use to develop and execute T-SQL code against SQL Server. SQL Server Management Studio(SSMS)是用于针对SQL Server开发和执行T-SQL代码的客户端工具。The purpose of this section is not to provide a complete guide to working with SSMS, but rather just to help you get started.本节的目的不是提供使用SSM的完整指南,而是帮助您开始。


Image Note

SSMS is updated periodically, so your experience might vary from the screenshots in this appendix.SSMS会定期更新,因此您的体验可能与本附录中的截图有所不同。


To start working with SSMS开始与SSMS合作

1. Start SSMS from the Microsoft SQL Server program group.从Microsoft SQL Server程序组启动SSMS。

2. If this is the first time you have run SSMS, I recommend specifying the startup options so that the environment is set up the way you want it.如果这是您第一次运行SSMS,我建议您指定启动选项,以便按照您想要的方式设置环境。

a. If a Connect To Server dialog box appears, click Cancel for now.如果出现“连接到服务器”对话框,请暂时单击“取消”。

b. Choose the Tools | Options menu item to open the Options dialog box. Under Environment | Startup, set the At Startup option to Open Object Explorer And Query Window. 选择“工具”|“选项”菜单项打开选项对话框。在“环境”|“启动”下,设置启动时选项以打开对象资源管理器和查询窗口。This choice tells SSMS that whenever it starts, it should open the Object Explorer and a new query window.这个选项告诉SSMS,无论何时启动,它都应该打开对象资源管理器和一个新的查询窗口。

The Object Explorer is the tool you use to manage SQL Server and graphically inspect object definitions, and a query window is where you develop and execute T-SQL code against SQL Server. 对象浏览器是用于管理SQL Server和以图形方式检查对象定义的工具,查询窗口是针对SQL Server开发和执行T-SQL代码的地方。Feel free to navigate the tree to explore the options you can set, but few of them are likely to mean much at this point. 您可以随意浏览树,探索您可以设置的选项,但在这一点上,这些选项中几乎没有什么意义。After you gain some experience with SSMS, you'll find many of the options more meaningful and probably want to change some of them.在你获得了一些SSM的经验之后,你会发现许多选项更有意义,并且可能想要改变其中的一些。

c. When you're done exploring the Options dialog box, click OK to confirm your choices.浏览完“选项”对话框后,单击“确定”确认您的选择。

3. Close SSMS and start it again to verify that it actually opens the Object Explorer and a new query window. 关闭SSMS并再次启动,以验证它是否确实打开了对象资源管理器和一个新的查询窗口。You should see the Connect To Server dialog box, as shown in Figure A-9.您应该会看到“连接到服务器”对话框,如图A-9所示。

Image

FIGURE A-9 The Connect To Server dialog box.“连接到服务器”对话框。

4. In this dialog box, you specify the details of the SQL Server instance you want to connect to.在此对话框中,您可以指定要连接到的SQL Server实例的详细信息。

5. Type the name of the server you want to connect to in the Server Name box, or select it from the list if you've already connected to it successfully in the past. 在“服务器名称”框中键入要连接的服务器的名称,或者如果您过去已成功连接到该服务器,请从列表中选择该服务器。For Azure SQL Database you will need to specify the full DNS server name in the form yourserver.database.windows.net (replacing yourserver with your server name).对于Azure SQL数据库,您需要以yourserver.database.windows.net的形式指定完整的DNS服务器名称(用您的服务器名称替换yourserver)。

6. Choose the authentication mode in the Authentication list box according to the type of login you are connecting with (Windows Authentication or SQL Server Authentication). 根据要连接的登录类型(Windows身份验证或SQL Server身份验证),在“身份验证”列表框中选择身份验证模式。If you use the former (recommended), you don't need to specify the login name and password. 如果使用前者(推荐),则无需指定登录名和密码。If you use the latter, specify the SQL authenticated login name and password information. 如果使用后者,请指定经过SQL验证的登录名和密码信息。For Azure SQL Database, also click Options and specify TSQLV4 in the Connect To Dataset box in the Connection Properties dialog box.对于Azure SQL数据库,还要单击“选项”,并在“连接属性”对话框的“连接到数据集”框中指定TSQLV4

7. Click Connect. 单击“连接”。SSMS should start, as shown in Figure A-10.SSMS应启动,如图A-10所示。

Image

FIGURE A-10 The opening screen of SSMS.SSMS的开放屏幕。

The Object Explorer window appears on the left, the query window appears to the right of Object Explorer, and the Properties window is to the right of the query window. 对象资源管理器窗口显示在左侧,查询窗口显示在对象资源管理器的右侧,属性窗口显示在查询窗口的右侧。You can hide the Properties window by clicking the Auto Hide button (in the upper-right corner of the window, to the left of the X). 通过单击“自动隐藏”按钮(在窗口的右上角,X的左侧),可以隐藏“属性”窗口。Adjust the sizes of the Object Explorer dialog box and the query window as convenient to you. 尽可能方便地调整“对象资源管理器”对话框和查询窗口的大小。Although the focus of this book is on developing T-SQL code and not SQL Server management, I urge you to explore the Object Explorer by navigating the tree and by right-clicking the various nodes. 虽然本书的重点是开发T-SQL代码,而不是SQL Server管理,但我敦促您通过导航树和右键单击各个节点来探索对象资源管理器。You'll find the Object Explorer to be a convenient tool for graphically inspecting your databases and database objects, as shown in Figure A-11.您会发现Object Explorer是一个方便的工具,可以以图形方式检查数据库和数据库对象,如图a-11所示。

Note that you can drag items from the Object Explorer to the query window.请注意,您可以将项目从对象资源管理器拖动到查询窗口。


Image Tip

If you drag the Columns folder of a table from the Object Explorer to the query window, SQL Server will list all table columns separated by commas.如果将表的Columns文件夹从对象资源管理器拖动到查询窗口,SQL Server将列出所有以逗号分隔的表列。


Image

FIGURE A-11 The Object Explorer.对象资源管理器。

In the query window, you develop and execute T-SQL code. 在查询窗口中,开发并执行T-SQL代码。The code you run is executed against the database you're connected to. 运行的代码是针对所连接的数据库执行的。You can choose the database you want to connect to from the Available Databases combo box, as shown in Figure A-12.您可以从“可用数据库”组合框中选择要连接的数据库,如图A-12所示。

Image

FIGURE A-12 The Available Databases combo box.“可用数据库”组合框。

8. Make sure you're currently connected to the TSQLV4 sample database.确保当前已连接到TSQLV4示例数据库。

Note that, at any point, you can change the server and database you're connected to by right-clicking an empty area in the query window and then choosing Connection | Change Connection.请注意,在任何时候,您都可以通过右键单击查询窗口中的空白区域,然后选择“连接”|“更改连接”来更改所连接的服务器和数据库。

9. You're now ready to start developing T-SQL code. 现在可以开始开发T-SQL代码了。Type the following code into the query window:在查询窗口中键入以下代码:

SELECT orderid, orderdate FROM Sales.Orders;

10. Press F5 to execute the code. F5执行代码。Alternatively, you can click Execute (the icon with the red exclamation point). 或者,您可以单击执行(带有红色感叹号的图标)。You'll get the output of the code in the Results pane, as shown in Figure A-13.您将在结果窗格中获得代码的输出,如图A-13所示。

Image

FIGURE A-13 Executing the first query.执行第一个查询。

You can control the target of the results from the Query | Results To menu item or by clicking the corresponding icons in the SQL Editor toolbar. 您可以从“查询”|“结果输出到”菜单项或通过单击SQL编辑器工具栏中的相应图标来控制结果的目标。You have the following options: Results To Grid (default), Results To Text, and Results To File.您有以下选项:“结果输出到网格”(默认)、“结果输出到文本”和“结果输出到文件”。

Note that if some of the code is highlighted, as shown in Figure A-14, when you execute the code, SQL Server executes only the selected part. 请注意,如果突出显示了某些代码,如图A-14所示,则在执行代码时,SQL Server仅执行选定的部分。SQL Server executes all code in the script only if no code is highlighted.只有在没有突出显示代码的情况下,SQL Server才会执行脚本中的所有代码。

Image

FIGURE A-14 Executing only selected code.只执行选定的代码。


Image Tip

If you press and hold the Alt button before you start highlighting code, you can highlight a rectangular block that doesn't necessarily start at the beginning of the lines of code, for purposes of copying or executing, as shown in Figure A-15. 如果在开始高亮显示代码之前按住Alt按钮,可以高亮显示不一定从代码行开头开始的矩形块,以便复制或执行,如图a-15所示。Pressing Tab or Shift+Tab will shift the whole rectangle forward or backward, respectively. 按Tab键或Shift+Tab键将分别向前或向后移动整个矩形。If at this point you start typing something, what you type gets repeated in all highlighted lines. 如果此时您开始键入内容,您键入的内容会在所有高亮显示的行中重复。Try it!试试看!


Image

FIGURE A-15 Highlighting a rectangular block.突出显示矩形块。

Finally, before I leave you to your own explorations, I'd like to remind you that all the source code for the book is available for download from the book's website. 最后,在我让你们自己探索之前,我想提醒你们,这本书的所有源代码都可以从这本书的网站上下载。The previous section in this appendix, “Downloading source code and installing the sample database,” provides the details. 本附录的前一节,“下载源代码并安装示例数据库”提供了详细信息。Assuming you downloaded the source code and extracted the compressed files to a local folder, you can open the script file you want to work with from File | Open | File or by clicking the Open File icon on the standard toolbar. 假设您下载了源代码并将压缩文件解压缩到本地文件夹中,您可以从“文件”|“打开”|“文件”或单击标准工具栏上的“打开文件”图标打开要使用的脚本文件。Alternatively, you can double-click the script file's name in File Explorer to open the script file within SSMS.或者,可以在文件资源管理器中双击脚本文件的名称,在SSMS中打开脚本文件。

Working with SQL Server Books Online使用SQL Server联机丛书

Microsoft SQL Server Books Online is the online documentation that Microsoft provides for SQL Server. Microsoft SQL Server联机丛书是Microsoft为SQL Server提供的联机文档。It contains a huge amount of useful information. 它包含了大量有用的信息。When you're developing T-SQL code, think of Books Online as your best friend—besides this T-SQL fundamentals book, of course.当然,在开发T-SQL代码时,除了这本T-SQL基础知识书之外,还可以把网上的书当成你最好的朋友。

You can access Books Online from the Help menu in SSMS by clicking View Help. 通过单击“查看帮助”,可以从SSMS的“帮助”菜单访问联机丛书。By default, SSMS goes to the Internet to get help content. 默认情况下,SSMS会上网获取帮助内容。You can also install and access help locally from the Help Viewer, which you start by choosing Add and Remove Help Content from the Help menu, or by clicking Ctrl+Alt+F1. 也可以从“帮助查看器”本地安装和访问帮助,首先从“帮助”菜单中选择“添加和删除帮助内容”,或者单击Ctrl+Alt+F1Install new content from the Manage Content tab. 从“管理内容”选项卡安装新内容。I installed all the items with SQL in their name.我安装了所有以SQL命名的项目。

Learning to use Books Online is not rocket science, and I don't want to insult anyone's intelligence by explaining the obvious. 学习在线用书不是火箭科学,我不想通过解释显而易见的事实来侮辱任何人的智慧。Dedicating a section to Books Online in the “Getting started” appendix is more about making you aware of its existence and emphasizing its importance rather than explaining how to use it. 在“起步”附录中专门列出一节在线图书,更多的是让你意识到它的存在并强调它的重要性,而不是解释如何使用它。Too often, people ask others for help about a topic related to SQL Server when they can easily find the answer if they only put a little effort into searching for it in Books Online.很多时候,当人们只要花一点精力在网上的书籍中搜索,就可以轻松找到答案时,他们会向其他人寻求有关SQL Server主题的帮助。

I'll explain a few of the ways to get information from Books Online. 我将解释一些从网上图书中获取信息的方法。One of the windows I use most in Help Viewer to search for information is the Index tab, shown in Figure A-16.我在Help Viewer中搜索信息最常用的窗口之一是“索引”选项卡,如图A-16所示。

Type what you're looking for in the search box. 在搜索框中键入要查找的内容。As you type the letters of the subject you're looking for (for example, window function), Help Viewer places the first qualifying item at the top of the sorted list of subjects. 当您键入要查找的主题的字母(例如,“窗口函数”)时,“帮助查看器”会将第一个符合条件的项目放在已排序的主题列表的顶部。You can type T-SQL keywords for which you need syntax information, for example, or any other subject of interest.例如,您可以键入需要语法信息的T-SQL关键字或任何其他感兴趣的主题。

Image

FIGURE A-16 The Help Viewer Index window.帮助浏览器索引窗口。

You can add the topic to the Help Favorites by clicking the Add To Favorites button from the toolbar, making it easy to get back to later. 通过单击工具栏上的“添加到集合夹”按钮,可以将主题添加到“帮助集合夹”中,以便稍后返回。You can also sync the current help item with the respective topic on the Content tab by clicking the Show Topic In Contents button.还可以通过单击“在内容中显示主题”按钮,将当前帮助项目与“内容”选项卡上的相应主题同步。

You can also look for an item through the Contents tab by navigating the tree of topics, as shown in Figure A-17.您还可以通过导航主题树,通过内容选项卡查找项目,如图A-17所示。

Image

FIGURE A-17 The Help Viewer Contents window.“帮助查看器内容”窗口。

If you just want to explore what's available in T-SQL, navigate the Transact-SQL Reference folder tree, which can be found under the SQL Shared Language Reference folder.如果您只想探索T-SQL中的可用内容,请浏览Transact-SQL引用文件夹树,该文件夹位于SQL共享语言引用文件夹下。

Another useful tool is the Help Viewer's Search window, which is shown in Figure A-18.另一个有用的工具是帮助查看器的搜索窗口,如图A-18所示。

Image

FIGURE A-18 The Help Viewer Search window.“帮助查看器搜索”窗口。

You use the search box when looking for articles that contain words you're looking for. 在查找包含您要查找的单词的文章时,可以使用搜索框。This search is more abstract than a search on the Index tab—somewhat similar to a search performed by an Internet search engine. 这种搜索比“索引”选项卡上的搜索更抽象,有点类似于互联网搜索引擎执行的搜索。Note that if you want to find a certain word in an open article, click the Find In Topic button on the toolbar or press Ctrl+F to activate the Find bar.请注意,如果要在打开的文章中查找某个单词,请单击工具栏上的“在主题中查找”按钮,或按Ctrl+F激活“查找”栏。


Image Tip

Finally, let me add a last tip. 最后,让我补充最后一点。If you need help with a syntax element while writing code in SQL Server Management Studio, make sure your cursor is positioned somewhere in that code element and then press F1. 如果在SQL Server Management Studio中编写代码时需要有关语法元素的帮助,请确保光标位于该代码元素中的某个位置,然后按F1This will load Books Online and open the syntax page for that element, assuming that such a Help item exists.这将加载联机书籍并打开该元素的语法页面,假设存在这样的帮助项。