教程:编写 Transact-SQL 语句Tutorial: Writing Transact-SQL Statements

适用对象: yesSQL ServeryesAzure SQL 数据库yesAzure SQL 数据仓库yes并行数据仓库APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure SQL Data Warehouse yesParallel Data Warehouse

欢迎学习“编写 Transact-SQLTransact-SQL 语句”教程。Welcome to the Writing Transact-SQLTransact-SQL Statements tutorial. 本教程适用于对编写 SQL 语句不熟悉的用户。This tutorial is intended for users who are new to writing SQL statements. 本教程通过回顾一些用于创建表和插入数据的基本语句,帮助新用户入门。It will help new users get started by reviewing some basic statements for creating tables and inserting data. 本教程使用 Transact-SQLTransact-SQL,后者是 SQL 标准的 MicrosoftMicrosoft 实现。This tutorial uses Transact-SQLTransact-SQL, the MicrosoftMicrosoft implementation of the SQL standard. 本教程旨在简介 Transact-SQLTransact-SQL 语言,但不是要取代 Transact-SQLTransact-SQL 课程。This tutorial is intended as a brief introduction to the Transact-SQLTransact-SQL language and not as a replacement for a Transact-SQLTransact-SQL class. 本教程特意选用了简单的语句,因此它们不能代表标准生产数据库中存在的语句的复杂程度。The statements in this tutorial are intentionally simple, and are not meant to represent the complexity found in a typical production database.

注意:如果你是初学者,可能会发现使用 SQL Server Management StudioSQL Server Management Studio 比编写 Transact-SQLTransact-SQL 语句更容易。NOTE: If you are a beginner you might find it easier to use SQL Server Management StudioSQL Server Management Studio instead of writing Transact-SQLTransact-SQL statements.

查找详细信息Finding More Information

若要查找有关任何特定语句的详细信息,请在 SQL Server 联机丛书中按名称搜索该语句,或使用“目录”浏览在 Transact-SQL 引用(数据库引擎)下按字母顺序列出的 1,800 个语言元素。To find more information about any specific statement, either search for the statement by name in SQL Server Books Online, or use the Contents to browse the 1,800 language elements listed alphabetically under Transact-SQL Reference (Database Engine). 另一种查找信息的好办法是搜索与您感兴趣的主题相关的关键字。Another good strategy for finding information is to search for key words that are related to the subject matter you are interested in. 例如,如果想要知道如何返回日期的一部分(例如月份),请在索引中搜索 dates [SQL Server] ,然后选择 datepartsFor example, if you want to know how to return a part of a date (such as the month), search the index for dates [SQL Server], and then select dateparts. 这会让你转到主题日期部分 (Transact-SQL)This takes you to the topic DATEPART (Transact-SQL). 作为另一个示例,若要了解如何使用字符串,请搜索 string functionsAs another example, to find out how to work with strings, search for string functions. 这会让你转到主题字符串函数 (Transact-SQL)This takes you to the topic String Functions (Transact-SQL).

学习内容What You Will Learn

本教程将介绍如何创建数据库、在数据库中创建表、将数据插入到表中、更新数据、读取数据、删除数据,然后删除表。This tutorial shows you how to create a database, create a table in the database, insert data into the table, update the data, read the data, delete the data, and then delete the table. 您将创建视图和存储过程,并为数据库和数据配置用户。You will create views and stored procedures and configure a user to the database and the data.

本教程分为三课:This tutorial is divided into three lessons:

第 1 课:创建数据库对象Lesson 1: Creating Database Objects
在本课中,将介绍如何创建数据库、在数据库中创建表、将数据插入到表中、更新数据,然后读取数据。In this lesson, you create a database, create a table in the database, insert data into the table, update the data, and read the data.

第 2 课:配置数据库对象的权限Lesson 2: Configuring Permissions on Database Objects
在本课中,将介绍如何创建登录名和用户。In this lesson, you create a login and user. 还将创建视图和存储过程,再将用户权限授予存储过程。You will also create a view and a stored procedure, and then grant the user permission to the stored procedure.

第 3 课:删除数据库对象Lesson 3: Deleting Database Objects
在本课中,将介绍如何删除对数据的访问权限、从表中删除数据、删除表,然后删除数据库。In this lesson, you remove access to data, delete data from a table, delete the table, and then delete the database.

要求Requirements

为了完成本教程,您不必知道 SQL 语言,但是应该理解基本的数据库概念,如表。To complete this tutorial, you do not have to know the SQL language, but you should understand basic database concepts such as tables. 在学习本教程的过程中,将创建一个数据库,并创建一个 Windows 用户。During this tutorial, you will create a database and create a Windows user. 这些任务需要高级别的权限;因此,您应该以管理员身份登录到计算机。These tasks require a high level of permissions; therefore, you should log in to the computer as an administrator.

您的系统必须安装以下软件:Your system must have the following installed: