1. Trang chủ
  2. » Công Nghệ Thông Tin

Hướng dẫn học Microsoft SQL Server 2008 part 16 docx

10 388 0
Tài liệu đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 872,78 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

If you upgrade to SQL Server 2008 and just keep using it the way you used Management Studio 2005, you’re missing out: ■ Object Explorer Details has dozens of additional columns that may

Trang 1

The Security node is used to manage server-wide security:

■ Logins: Server-level authentication of logins

■ Server Roles: Predefined security roles

■ Credentials: Lists credentials

■ Cryptographic Providers: Used for advanced data encryption

■ Audits: Part of SQL Audit, collects data from Extended Events

■ Server Audit Specifications: Defines a SQL Audit for a server-level audit

The Server Objects node holds server-wide items:

■ Backup Devices: Organizes tapes and files

■ Endpoints: HTTP endpoints used by database mirroring, Service Broker, SOAP, and T-SQL

■ Linked Servers: Lists predefined server credentials for distributed queries

■ Triggers: Contains server-level DDL triggers

The Replication node is used to set up and monitor replication:

■ Local Publications: Lists publications available from this server

■ Local Subscriptions: Lists subscriptions this server subscribes to from other servers

The Management node contains several server-wide administration tools:

■ Policy Management: SQL Server’s new policy-based management

■ Data Collection: Define data collection points for SQL Server’s Management Data Warehouse

■ Resource Governor: Control Enterprise Edition’s CPU and Memory Resource Governor

■ Maintenance Plans: Create and manage maintenance plans

■ SQL Server Logs: SQL Server creates a new log with every restart View them all here

■ Database Mail: Configure and monitor Database Mail

■ Distributed Transaction Coordinator: Manage DTC for transactions involving multiple

servers

■ Legacy: Contains deprecated objects such as DTS packages and older database maintenance

plans The final node links to SQL Server Agent tools (if SQL Server Agent is running):

■ Jobs: Control SQL Server Agent jobs

■ Job Activity Monitor: View job activity

■ Alerts: Configure SQL Server Agent alerts

■ Operators: Set up SQL Server Agent operators

■ Proxies: Manage SQL Server Agent externally

■ Error Logs: View SQL Server Error Logs

Trang 2

Because Management Studio and SQL Server are communicating as client and server, the

two processes are not always in sync Changes on the server are often not immediately

reflected in Management Studio unless Management Studio is refreshed, which is why nearly every tool

has a refresh icon, and refresh is in nearly every context menu.

Filtering Object Explorer

Some databases are huge To ease navigating these objects, Microsoft has included a filter for portions

of the tree that include user-defined objects, such as tables or views The filter icon is in the toolbar at

the top of the Object Explorer The icon is only enabled when the top node for a type of user-defined

object is selected For example, to filter the tables, select the tree node and then click on the filter icon

or right-click to open the tree’s context menu, and select Filter➪ Filter Settings

The Filter Settings dialog box enables you to filter the object by name, schema, or creation date To

remove the filter, use the same context menu, or open the Filter Settings dialog box and choose Clear

Filter Note that the filter accepts only single values for each parameter; Boolean operators are not

permitted

System objects are organized in their own folder, but if you prefer to hide them altogether,

you can do so by selecting ToolsOptions – EnvironmentGeneral tab.

Object Explorer Details

The Object Explorer Details page is completely redesigned and now sports several way cool features If

you upgrade to SQL Server 2008 and just keep using it the way you used Management Studio 2005,

you’re missing out:

■ Object Explorer Details has dozens of additional columns that may be added to the grid

Right-click on the grid headers to select additional columns

■ The columns can be rearranged and the rows sorted by any column

■ Data can be selected (highlighted) and copied to the clipboard (Ctrl+C) in a tabbed format

with header columns — perfect for pasting into Excel and graphing

■ The pane below the grid displays several properties depending on the size of the pane The

sort order of those properties was hand-picked by Buck Woody (real DBA and SSMS 2008

Product Manager)

The Object Explorer Details Search is one of the best-kept secrets of SQL Server:

■ If Object Explorer is at the server-node level, then the Object Explorer Details Search searches

every object in the server

■ If Object Explorer is at any node at or under the database node level, then it searches the

current database

■ The Object Explorer Details page is rather object-type generic and so is its context menu The

best solution is to use the synchronize toolbar button or context menu command to quickly

jump to the object in Object Explorer

■ If the back button in Object Explorer Details returns to search results, it will automatically

re-execute the search to ensure that the list is as up-to-date as possible

Trang 3

The Table Designer

Creating a new table, or modifying the design of an existing table, is easy with the Table Designer

The Table Designer, shown in Figure 6-5, is very similar to MS Access and other database design tool

interfaces

Create a new table by selecting the Tables node in the tree and then selecting New Table from the

con-text menu The design of existing tables may be altered by selecting the table, right-clicking, and

select-ing Design from the context menu

FIGURE 6-5

Tables may be created or their designs edited using the Table Designer tool

Columns may be individually selected and edited in the top pane The column properties for the

selected column are listed in the bottom pane Dialog boxes for modifying foreign keys and indexes can

be opened using the Table Designer menu or toolbar

Trang 4

Although I’m a code guy myself and prefer Query Editor to the GUI tools, I must admit that the Table

Designer page is a clean, straightforward UI, and it generates scripts for every modification I

recom-mend opening the Properties window as well, because some table properties are only visible there

The logical design of tables and columns is covered in Chapter 3, ‘‘Relational Database

Design.’’ The realities of implementing the logical design, and how to script tables using

DDL, are discussed in Chapter 20, ‘‘Creating the Physical Database Schema.’’

Building database diagrams

The Database Diagram tool takes the Table Designer up a notch by adding custom table design views

(see Figure 6-6) and a multi-table view of the foreign-key relationships The Database Diagram tool has

its own node under each database, and each database may contain multiple diagrams, which makes

working with very large databases easier because each module, or schema, of the database may be

represented by a diagram

FIGURE 6-6

The OBXKites database relationships viewed with the Database Diagram tool The Location table has

been changed to Standard view

Trang 5

Personally, I like the Database Diagram tool (as far as GUI tools go) Although I don’t develop using the

tool, sometimes it’s useful to visually explore the schemas of very large databases Unfortunately, the

Database Diagram tool suffers from a few clumsy issues:

■ It makes sense to create a separate diagram for each section of a large database, and databases

can be organized by schemas (as AdventureWorks is); unfortunately, the Database Diagram tool is schema unaware

■ It does not display the table schema in the diagram To view the schema, open the Property

window and select the table in the diagram

■ There’s no way to select all of the tables of a schema and add them to the diagram as a set

Object Explorer will not permit selecting multiple tables The Object Explorer Details page allows multiple table selection, but it does not permit dragging the tables to the design Even worse, the Add Table dialog box in the Database Diagram tool does not sort by the table’s schema The Add Related Tables option on the table’s context menu helps solve this problem

■ Relationship lines have the frustrating tendency to become pretzels when tables or lines are

moved

Best Practice

If your goal is to print the database diagram, be sure to check the page breaks and arrange the tables first,

or chances are good you’ll end up wasting a lot of paper To view the page breaks, use the tool’s context

menu or the Database Diagram menu

The Query Designer

The Query Designer is a popular tool for data retrieval and modification, although it’s not the easiest

tool to find within Management Studio You can open it two ways:

■ Using Object Explorer, select a table Using the context menu, choose Edit Top 200 Rows This

will open the Query Designer, showing the return from a ‘‘select top(200)’’ query in the results pane The other panes may now be opened using the Query Designer menu or the toolbar

■ When using the Query Editor, use the Query Designer button on the toolbar, use the Query ➪

Design Query in Editor menu command, or use the Query Editor’s own context menu

Note that when the Query Designer is opened from the Query Editor, it’s a modal dialog box and the results pane is disabled

If editing 200 rows, or viewing 1000 rows, seems like too many (or not enough) for your application, you can edit those values in the OptionsSQL Server Object Explorer

Command tab.

Unlike other query tools that alternate between a graphic view, a SQL text view, and the query results,

Management Studio’s Query Designer simultaneously displays multiple panes (see Figure 6-7), as

selected with the view buttons in the toolbar:

■ Diagram pane: Multiple tables or views may be added to the query and joined together in

this graphic representation of theSELECTstatement’sFROMclause

Trang 6

■ Grid pane: Lists the columns being displayed, filtered, or sorted

■ SQL pane: The raw SQLSELECTstatement may be entered or edited in this pane

■ Results pane: When the query is executed with the Run button (!), the results are captured

in the results pane If the results are left untouched for too long, Management Studio requests

permission to close the connection

One of my favorite features in Management Studio is the capability to create and

graphi-cally join derived tables within Query Designer’s Diagram pane Way cool!

FIGURE 6-7

Object Explorer’s Query Designer

The Query Designer can perform Data Manipulation Language (DML) queries other thanSELECT

The Change Type drop-down list in the Query Designer toolbar can change the query from a default

SELECTquery to the following queries: Insert Results, Insert Values, Update, Delete, or Make Table

However, the Query Designer is no substitute for the Query Editor Unlike the Query Editor, it cannot

perform batches or non-DML commands Nor can it execute SQL statements using F5 Table and

col-umn names can’t be dragged from the Object Explorer to the SQL pane

Trang 7

The Query Designer may be used to edit data directly in the results pane — a quick-and-dirty way to

correct or mock up data

Navigating the Query Designer should feel familiar to experienced Windows users While Books Online

lists several pages of keyboard shortcuts, most are standard Windows navigation commands The one

worth mentioning here is Ctrl+0, which enters anullinto the result pane

Object Explorer reports

No section on Object Explorer would be complete without mentioning the dozens of great reports

hid-den within it, one of which is shown in Figure 6-8 These reports can be found in the context menus of

the Server, Database, and Security➪ login nodes While I won’t list every report here, they’re an

excel-lent resource and one of the most underused features of Management Studio

FIGURE 6-8

The server or database standard reports are a great way to quickly investigate your SQL Server

Custom reports can be installed in any Object Explorer node by placing the report definition file in

the following directory: \Documents and Settings\{user}\Documents\SQL Server Management

Studio\Custom Reports

For more details, seehttp://msdn2.microsoft.com/en-us/library/bb153684.aspx

Trang 8

Using the Query Editor

The Query Editor carries on the legacy of SQL Server’s historic Query Analyzer as the primary UI for

database developers While SQL Server 2005 introduced Management Studio and the Query Editor, with

SQL Server 2008, it rocks!

Opening a query connecting to a server

The Query Editor can maintain multiple open query documents and connections within the tabbed

doc-ument area In fact, different queries may be connected as different users, which is very useful for testing

security In addition, the Query Editor can open and work with a.sqlfile even when not connected to

a server

When Query Editor first opens, it prompts for an initial login To make further connections, use the

File➪ New Connection menu command

The New Query toolbar button opens a new Query Editor document There’s some intelligence in how

it selects the current database for the new query If Object Explorer had focus before the New Query

button is pressed, then the new query is connected to Object Explorer’s currently selected database If

the Query Editor had focus, then the new query opens to the same database as the Query Editor’s

cur-rent query

You can also switch a query’s connection to another server using the Query➪ Connection menu, the

Change Connection toolbar button, or the Query Editor’s context menu

By default, the Query tab displays the current SQL Server and database name merged with the filename;

but it’s too long to fit, so it’s cut off in the middle

Don’t forget that with SQL Server 2008, you can now open a new query connected to multiple servers

using the Registered Server’s server group context menu

In some extreme cases, if SQL Server cannot seem to accept new connections, it listens

on a dedicated port for a special diagnostic connection and tries to make a connection.

A Dedicated Administrator Connection (DAC) is only possible if you are a member of the server’s

sysadmin role To attempt a DAC connection using Query Editor, connect to the server with a prefix

of admin: before the server name For example, my notebook’s name is Maui, so connecting to it as

admin:maui opens a DAC connection DAC connections are also possible using the SQLCMD utility For

more details about the DAC connection, see Chapter 42, ‘‘Maintaining the Database.’’

You can set the display color of the Query Editor’s connection bar (at the bottom of the

Query Editor) per connected server This is a great visual cue I recommend setting the

development server to green and the production server to red When connecting to a server, open the

connection dialog’s options and select Use Custom Color to set the color for that server.

Opening a sql file

There are multiple ways to open a saved query batch file, and one huge trap you want to avoid:

■ If Management Studio is not open, then double-clicking a sqlfile in Windows File Explorer

will launch Management Studio, prompt you for a connection, and open the file Here’s the

Trang 9

gotcha: If you select multiple sqlfiles in Windows File Explorer and open them as a group, Windows will launch a separate instance of Management Studio for each file — not a good thing You’ll end up running several copies of Management Studio

■ If Management Studio is already open, then double-clicking will open the file or selected files

in a Query Editor document Each file will prompt you for a connection

■ Multiple sqlfiles may be dragged from Windows File Explorer and dropped on Management

Studio Each file will open a Query Editor after prompting for a connection

■ The most recently viewed files are listed in the Files ➪ Recent Files menu Selecting a file will

open it in the Query Editor

■ The File ➪ File Open menu or toolbar command will open a dialog box to select one or more

files

Real-World Developing with the Query Editor

Iadmit it, I dream in T-SQL — so here are my favorite tips for using the Query Editor as a developer:

■ View multiple scripts at the same time in Query Editor by right-clicking on one of the

documents and selecting New Vertical Tab Group The selected document is the one

that becomes the new tab to the right

■ I usually develop using three scripts The first script contains schema, triggers, indexes,

and stored procedures The second script is the inserts for the unit test sample data,

and the third script — called ProcTest — executes every stored procedure I like

dragging the documents so that the tabs are in the correct order to run the three scripts

in sequence

■ I use bookmarks liberally to save points in the script to which I’ll need to refer back

For example, I’ll bookmark a table’s DDL code and the CRUD stored procedures for

that table while I’m working on the stored procedures

■ I begin every script with use database and set nocount on (these commands

are covered in Chapter 21, ‘‘Programming with T-SQL.’’) Every script ends with use

tempdb That way, if I run all the scripts, no script stays in the user database and the

initial create script can easily drop and recreate the database

■ Line numbers can be a good thing when navigating a long script They can be turned

on by using Options – Text Editor➪All Languages➪General

■ Maybe it’s just my eyes, but the default text is way too small I find Consolas 14-point

to be far more readable than the default

■ When there are more documents than can be displayed as tabs, the easy way to select

the correct tab is to use the Active Documents drop-down list, at the far right of the

continued

Trang 10

Query Editor by the close document ‘‘x’’ button This is also the best way to determine

whether a script is still executing, but it does sometimes reorder the tabs

■ If there’s an error in the script, double-clicking on the error message jumps to a spot

near the error

■ I’m compulsive about indenting, and I like indents of two spaces I uppercase all

reserved words in the outer query, and then use PascalCase (sometimes called

CamelCase) for user-defined objects and reserved words in subqueries

■ IntelliSense rocks! Finally

■ I sometimes use code outlining to collapse large sections of code The Code Outliner

can collapse multi-line statements

■ IntelliSense and Code Outlining can be turned off in Options – Text Editor➪

Transact-SQL➪Advanced

■ The Query Editor provides a quick visual indicator of lines that have been edited The

Track Changes Indicator displays a thin, yellow bar to the left of the line if the text is

modified, and a green bar if that change has been saved

■ Use the SQLCMD toolbar button or the Query➪SQLCMD Mode menu command to

switch the editor to work with SQLCMD utility scripts

■ While working with T-SQL code in the Query Editor, you can get Books Online (BOL)

keyword help by pressing F1 Alternately, the dynamic help window in Management

Studio will follow your work and display appropriate help topics as you move though

the code (which is actually a bit spooky)

Out of the box, Management Studio’s Query Editor does not provide automatic formatting

of T-SQL There are some free websites that enable you to submit a SQL statement and will then format the code, but I’ve been using SQL Prompt from Red Gate and I’ve come to depend on it for

consistent formatting Highly recommended.

Shortcuts and bookmarks

Bookmarks are a great way to navigate large scripts Bookmarks can be set manually, or automatically set

using the Find command Bookmarks work with double Control key combinations For example,

hold-ing down the Ctrl key and presshold-ing K and then N moves to the next bookmark The Ctrl+K keys also

control some of the other editing commands, such as commenting code Bookmarks are also controlled

using the Edit➪ Bookmarks menu or the bookmark next and previous toolbar buttons Table 6-1 lists

the shortcuts I find especially useful

The Bookmark window displays a list of all bookmarks and offers tools to control bookmarks, navigate

bookmarks, and even change the name of a bookmark

Bookmarks are lost if the file is simply saved as a.sqlfile, but if the query is saved within a solution

in the Solution Explorer, then bookmarks are saved from session to session

Ngày đăng: 04/07/2014, 09:20

TỪ KHÓA LIÊN QUAN