Sql Server 2005 Concepts

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Sql Server 2005 Concepts as PDF for free.

More details

  • Words: 16,206
  • Pages: 65
Microsoft SQL Server - Concepts 2 005

1

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

SQL Server Overview

SQL Server 2005 Overview 2

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005 Microsoft SQL Server 2005 is a database platform for large-scale Online Transaction Processing (OLTP) and Online Analytical Processing (OLAP). The following table lists the components that provide various database services in SQL Server 2005: Component

Description The SQL Server Database Engine is a service that you can use to store, process, and secure your data in a relational or XML format. Starting an instance of SQL Server Database Engine starts the SQL Server service which allows users to connect to the server. SQL Server Database Engine includes features such as:

SQL Server Database Engine

• • • • • •

Controlled access Replication Full-text search Relational data management tools XML data management tools Rapid transaction processing for data-consuming applications



Outstanding support to sustain availability

SQL Server Analysis Services (SSAS) provides Online Analytical Processing (OLAP) and data mining functionality for business intelligence solutions. You can use SSAS to design, create, maintain, and visualize: SQL Server Analysis Services



OLAP multi-dimensional structures that contain aggregated data from relational databases or additional sources.



Data mining models that are constructed from data sources by using industrystandard algorithms.

SQL Server Reporting Services is an extensible, web-enabled platform that includes server and client components that allow you to:

• •

Create, manage, and deploy a variety of reports such as: o Tabular reports o Matrix reports o Graphical reports o Free-form reports Publish reports in a variety of formats Develop report applications



Manage security and subscriptions centrally



SQL Server Reporting Services

SQL Server Notification Services 3

SQL Server Notification Services allow you to develop and deploy applications that generate and send notifications. You can use SQL Server Notification Services to:

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005



Generate and send timely, personalized messages to millions of subscribers



Deliver messages and notifications to a wide variety of devices

SQL Server Integration Services (SSIS) is a platform for building high-performance data integration solutions. SSIS provides tools and programmable objects for extracting, loading, and transforming (ELT) services for data integration and workflow solutions including updating data warehouses, mining data, and processing analytic objects. SSIS includes:

SQL Server Integration Services



Tools and wizards for building and debugging packages A management service An Integrations service Tasks for performing workflow functions such as: o FTP operations o Executing SQL statements o Sending e-mail messages Application programming interfaces for programming the Integration Services object model Data sources and destinations for extracting and loading data



Transformers for cleaning, aggregating, merging, and copying data

• • • •



4

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005 SQL Server Editions SQL Server 2005 has different editions to meet the needs of businesses of varying sizes and varying database requirements. The table below describes the various SQL Server 2005 editions. Edition

Description Enterprise Edition includes the complete set of enterprise data management and business intelligence features. SQL Server Enterprise offers the highest levels of scalability and availability. Enterprise Edition supports:

SQL Server 2005 Enterprise Edition

• • •

Data warehousing systems Enterprise online transaction processing (OLAP) Large Web sites

Enterprise Edition is optimized to run on x64- and Itanium-based servers and is typically used on a production database server. SQL Server 2005 Standard Edition

Standard Edition is an affordable option for small- and medium-sized organizations. It includes the core functionality needed for non-critical e-commerce, data warehousing, and line-ofbusiness solutions. Standard Edition is optimized to run on win32, x64, and Itanium-based servers.

SQL Server 2005 Workgroup Edition

Workgroup Edition is the data management solution for small organizations or workgroups within larger entities. It includes all the core database features needed for data management. Workgroup Edition is available only on a 32-bit platform.

SQL Server 2005 Express Edition

Express Edition is a replacement for Microsoft Desktop Engine (MSDE). SQL Server Express is free and can be redistributed (subject to agreement). It functions as the client database, as well as a basic server database. Express Edition is available only on a 32-bit platform.

SQL Server 2005 Developer Edition

Developer Edition is designed for application developers. It includes all the functionality of Enterprise Edition. License restrictions limit the use of this version to application development and testing only. It is not for use as a production server. Developer Edition runs on win32, x64, and Itanium-based servers.

SQL Server 2005 Mobile Edition

Mobile Edition allows organizations to extend enterprise data management and business intelligence capabilities to mobile devices. Mobile Edition can replicate to a SQL 2005 Enterprise or Standard Edition environment. Mobile Edition is only available on a 32-bit platform.

5

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

Ma na gem ent T ool s

SQL Tool Facts 6

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005 SQL Server 2005 has enhanced tools for easier, more efficient management. SQL Server 2005 tools can manage all features of SQL Server 2000; however, you cannot use SQL Server 2000 tools to manage SQL Server 2005. When SQL Server 2005 and SQL Server 2000 are installed on the same computer, both of their respective tool sets remain fully functional. The following table identifies SQL Server 2005 management tools and their respective features. It also identifies any SQL Server 2000 tools that have been replaced. Tool

Description SQL Server Management Studio provides an integrated environment to manage a SQL Server implementation. Using its graphical interface and script editors, you can use SQL Server Management Studio to perform such tasks as:

SQL Server Management Studio

• • • • • •

Create databases, tables, indexes, views, etc. Perform database maintenance Perform server maintenance on servers Manage security Register multiple servers Build SQL Server projects

SQL Server Management Studio replaces the following tools in previous versions of SQL Server: • •

SQL Server Enterprise Manager SQL Query Analyzer



Analysis Manager

SQL Server Business Intelligence Development Studio provides tools, wizards, and templates to enable organizations to gather business intelligence and create business solutions. It integrates into Microsoft Visual Studio with project types specifically designed for business intelligence. The primary components of SQL SQL Server Business Server Business Intelligence Development Studio are: Intelligence Development Studio • Analysis Services • Integration Services •

SQL Server Configuration Manager

7

Reporting Services

SQL Server Configuration Manager enables you to manage SQL Server services. SQL Server Configuration Manager is a Microsoft Management Console snap-in that is available from the Start menu, or can be added to any other Microsoft Management Console display. Tasks you can perform with SQL Server Configuration Manager are:

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

• • •

Change the account used by the SQL Server or SQL Server Agent services. Configure network protocols. Manage network connectivity configuration from SQL Server client computers.

SQL Server Configuration Manager replaces the following SQL Server tools: • •

Server Network Utility Client Network Utility



Service Manager

SQL Server Profiler provides a graphical user interface to SQL Trace. It is a valuable tool for analyzing database performance issues. You can use it for: SQL Server Profiler



Monitoring an instance of the SQL Server Database Engine or Analysis Services. Analyzing query patterns.



Capturing event data to a file or table.



Surface area refers to the exposure of the SQL System by enabled components, services, protocols, and open ports. Using the SQL Server Surface Area Configuration Tool you can: SQL Server Surface Area Configuration Tool

Database Engine Tuning Advisor



Enable and disable features, services, and network protocols for remote connections to help reduce the surface area of your SQL Server installations.



Export SQL surface area settings created using SQL Server Surface Area Configuration Tool to another server. The SQL Server Surface Area Configuration Tool works in conjunction with the Surface Area Configuration (SAC) utility to export settings.

The Database Engine Tuning Advisor analyzes the performance effects of workloads ran against one or more databases and provides recommendations to improve performance. Tuning your databases with Database Engine Tuning Advisor requires no expertise in database structure, workloads, or the internal workings of SQL Server. Database Engine Tuning Advisor provides two interfaces: •

8

A standalone graphical user interface tool for tuning databases and for

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

viewing tuning recommendations and reports. •

A command-line utility program, dta.exe, for Database Engine Tuning Advisor functionality in software programs and scripts.

Report Manager is a Web-based report access and management tool that you can use to manage a report server over an HTTP connection. You can use Report Manager to:

Report Manager

• • • • • •

9

View, search, and subscribe to reports. Configure site properties and defaults. Configure role-based security that determines access to items and operations. Configure report execution properties, report history, and report parameters. Create report models. Create linked reports to reuse and repurpose an existing report in different ways.

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

Database Concepts

Relational Database Concepts Relational databases apply mathematical theories to efficiently organize and retrieve data within a database. Database information is stored in tables, which are made up of rows and columns. The following graphic shows several components of a database.

10

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

The following table describes common relational database components. Component Description A table is a collection or set of related data within a database and is known as a database object. A database can contain one or more tables. Data within the table is organized as follows: •



Table





The table itself defines an object class. Data within the table is a collection of discrete items that share common characteristics. In the example above, the table contains data related to employees. Rows represent unique records in the database. In the above example, each row identifies a distinct employee. Columns represent fields within the record. Columns identify attributes that describe, define, or are associated with the record. In this example, each employee has common attributes of EmployeeID, LastName, FirstName, and Extension. Values for each row and column identify the exact characteristics of each record. For example, the LastName of the second employee in the table is King.

A primary key is a column guaranteed to have a unique value for each row in a table. A primary key can be multiple columns which together have a unique value. In the table Primary Key above, the EmployeeID column is the primary key. The primary key column in each row identified will contain a unique ID number for each employee. A foreign key defines the relationship and correspondence between a column in one table and primary key in another table. For example, another table might use the Extension Foreign key value as its primary key. In this situation, the Extension value would be a foreign key for the table in the example. Database Objects

The database organizes logical entities into database objects such as tables, indexes, views, and other objects. •

11

Tables, indexes, text data, ntext data, or image data are associated with a filegroup.

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

The data that is part of the database object is then striped across all physical files in the filegroup.



In addition to the database itself, a relational database system includes a Database Management System (DBMS). The role of the DBMS is to define the database structure, maintain relationships within the database, control data access and modification, interact with physical server resources, and define data recovery methods. The following table describes several common features of a DBMS. Feature

Description A query is a statement containing one or more commands for a database. The commands typically are a request to view or modify data. Queries are:



Written in a structured language that is understood by the DBMS. For example, SQL server uses Transact-SQL as the query language. Submitted by client applications to the DBMS. For example, database management tools are client applications that connect to and submit queries to the DBMS.



Executed by the DBMS and returns the result to the client.



Queries

A search is performed by the DBMS for information requested in a query. The data Searching and might be stored in multiple rows within a table or across tables or databases. An index Indexing is used by a DBMS to organize data for frequent searches to improve performance and data access. Locking is used to coordinate changes in data management systems that support multiple user access to a single database. At any time, the host system can have multiple clients connected, all accessing different portions of the same database. Locking: Locking

Transactions

• •

Prevents multiple users from changing the same piece of data at the same time. Allows only one client at a time to make changes to a table or row. Other clients will be unable to modify the data while it is locked, although in many cases they will still be able to see it while it is locked.



Is released after the changes are made, at which time other clients can lock the data for modification.

A transaction is a group of actions that are treated as a single unit. Transactions ensure data integrity. The success of the entire transaction depends on the success of each of the actions that are part of a transaction. None of the changes are made if errors are encountered in any part of the transaction. When using transactions: • •

12

The application signals the start of the transaction. The application submits the series of actions that are part of the transaction.

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005





Each action is executed by the DBMS. All actions performed by the application are considered part of the same transaction. The application signals the end of the transaction. If the end of the transaction has been reached without error, the transaction is committed and the changes become permanent. If there is an error during the transaction, the DBMS rolls back the changes, putting the database in its original (unchanged) state.

Transactions are particularly useful in processing changes that are made across databases or across servers. For example, if you were transferring money from one bank account to another account, the transaction might involve servers at two different banks. The transaction would not be completed unless the withdrawal from one account and the deposit into the other account were both successful.

SQL Databases SQL Server has two types of databases: • •

System databases are used to store information about SQL Server and are essential for the operation of a server instance. User databases are the databases created by users for storing their unique data.

The following table identifies specific databases used by SQL Server. 13

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005 Database Type

System

Database Name

Description

master

The master database records all system-level information for the SQL Server system, including the existence of all other databases and the location of their files, login account information, and system configuration settings. A backup of this database should always be available.

model

The model database is used as the template for all new databases. A backup of this database should always be available.

msdb

The msdb database is used by SQL Server Agent for recording operators and scheduling jobs and alerts. This database also contains backup and restore history tables. A backup of this database should always be available.

resource (RDB)

The resource database contains all objects that ship with SQL Server. You must use a filebased or disk-based backup for this database and you cannot restore the file using SQL Server restore.

tempdb

The tempdb database is a temporary database that is used as a holding area for all temporary tables, stored procedures, and temporary worktables used by the relational database engine. This database is recreated each time SQL Server is started–no data is saved from one session to another.

The distribution database stores metadata and history data used in replication. A distribution database is not created during installation–instead, it is created when SQL Server is Distribution configured for replication activities. In some instances, multiple distribution databases may be used. A backup of this database should always be available. User databases are created for use in your organization and hold data that you supply. A sample user database, AdventureWorks, is provided during installation as a learning tool and model for creating user databases. AdventureWorks contains sample sales data for a fictitious multinational manufacturing company. These databases and their tables are frequently used as examples in SQL Server Books Online.

User

Database Objects The various components of a database are known as objects. Databases can contain the following objects: Object

Description

Tables

Database tables typically consist of columns and rows of data. The rows represent unique records in the database, while the columns represent fields within the record.

Views

Views identify the specific data that is displayed at any given time. Views can: 14

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

• •

Specify that only certain rows or columns are visible. Aggregate (summarize) data.



Collect data from more than one table and present it as though it were from a single table.

A database index is a structured list of the values in the table or view and the row location of the value. •

Indexes





Indexes can be clustered, where the data is sorted and stored based on the index key. A nonclustered index is separate from the data rows. Row locators in the index point to the location of the data in either a clustered index key or in a heap. A table that is created with no index is called a heap, and the data rows are stored in no particular order.

Stored procedures

A stored procedure is a group of Transact-SQL statements compiled into a single execution plan. Stored procedures are typically used to achieve consistent implementation, improved performance, and a single point of control for commonly performed tasks.

Constraints

Constraints allow you to define the rules for the values allowed in columns. While there are other methods of enforcing data integrity (i.e., triggers, rules, and defaults), using constraints is the standard method for enforcing data integrity.

Defaults

Defaults specify the values to be used in a column when no value is specified when a row is inserted.

Roles

A role is a profile that defines the security access controls and permissions. Assigning a user to a role, gives that user the security privileges for the role.

Assemblies

An assembly is a .dll file created using .NET Framework common language runtime (CLR). Assemblies are used in an instance of SQL Server to deploy functions, stored procedures, triggers, user-defined aggregates, and user-defined types.

Triggers

Triggers are a special class of stored procedures defined to execute automatically whenever an UPDATE, INSERT, or DELETE statement is issued against a table or view. Tables can have multiple triggers. Triggers can be used to enforce business rules and automate processing for a company.

User-defined data types

User-defined data types are based on pre-defined SQL Server 2005 data types. Userdefined data types are typically used when columns holding similar classes of data must have the same base data type, length, and nullability. User-defined data types are not supported in table variables.

User-defined functions

User-defined functions are uniquely-named subroutines made up of one or more Transact-SQL statements customized by the user for a specific purpose.

Additional objects are used by Analysis Services. The following table lists these objects. 15

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005 Object

Description

A data source is used by Analysis Services to identify the connection string that defines Data Sources how Analysis Services connects to a physical data store using a managed Microsoft .NET Framework or native OLE DB provider. Data Source Views

A data source view is a metadata definition of the schema objects used by Analysis Services. These schema elements are used by Unified Dimensional Model (UDM) and by the mining structures.

Cubes

A cube is an arrangement of data in an array that allows for fast analysis. The arrangement of data into cubes avoids a limitation of relational databases which are not well suited for analysis of large amounts of data almost instantaneously.

Dimensions

A database dimension is a collection of attributes. These attributes provide information about fact data in one or more cubes. The attributes are bound to one or more columns in one or more tables in a data source view.

A data mining model defines the algorithm used for data mining and how each column is used by the model. A mining model inherits the values of the properties that are Mining Model defined by the mining structure. The model can use all the columns that the mining structure contains or a subset of the columns. Mining Structures

A mining structure defines the data domain from which mining models are built. A single mining structure can contain multiple mining models that share the same domain.

Roles

A role is a profile that defines the security access controls and permissions. Assigning a user to a role, gives that user the security privileges for the role.

Assemblies

An assembly is a .dll file created using .NET Framework common language runtime (CLR). Assemblies are used in an instance of SQL Server to deploy functions, stored procedures, triggers, user-defined aggregates, and user-defined types.

Measures

A measure represents a column of data (usually numeric) that can be aggregated -processed as a set of values rather than as individual data values.

Measure groups

A measure group contains multiple measures that associate dimensions with measures. You can also use measure groups for measures that have distinct count as their aggregation behavior. You can optimize aggregation processing by putting each distinct count measure into its own measure group.

Hierarchies

A hierarchy establishes the priority of entities used. Hierarchies can be established for attributes, data types, dimensions, cubes, etc.

16

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

Data Type Facts As you define tables and other database objects and as you construct Transact SQL statements, the data type is identified by using a keyword. The following table lists many common data type keywords. Note: This list is not comprehensive. Class

Integer

17

Keyword

Description

bigint

Integer (whole number) data from -263 (-9,223,372,036,854,775,808) through 263-1 (9,223,372,036,854,775,807).

int

Integer (whole number) data from -231 (-2,147,483,648) through 231-1 (2,147,483,647).

smallint

Integer data from -215 (-32,768) through 215-1 (32,767).

tinyint

Integer data from 0 through 255.

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

Bit, Decimal, Numeric, and Approximate Numerics

bit

Integer data with either a 1 or 0 value.

decimal

Fixed precision and scale numeric data from -1038+1 through 1038-1.

numeric

Fixed precision and scale numeric data from -1038+1 through 1038-1.

float

Floating precision number data with the following valid values: -1.79E + 308 through -2.23E - 308, 0 and 2.23E + 308 through 1.79E + 308.

real

Floating precision number data with the following valid values: -3.40E + 38 through -1.18E - 38, 0 and 1.18E - 38 through 3.40E + 38.

money

Monetary data values from -263 (-922,337,203,685,477.5808) through 263-1 (+922,337,203,685,477.5807), with accuracy to a ten-thousandth of a monetary unit.

smallmoney

Monetary data values from -214,748.3648 through +214,748.3647, with accuracy to a ten-thousandth of a monetary unit.

datetime

Date and time data from January 1, 1753, through December 31, 9999, with an accuracy of three-hundredths of a second, or 3.33 milliseconds.

smalldatetime

Date and time data from January 1, 1900, through June 6, 2079, with an accuracy of one minute.

char

Fixed-length non-Unicode character data with a maximum length of 8,000 characters.

varchar

Variable-length non-Unicode data with a maximum of 8,000 characters.

text

Variable-length non-Unicode data with a maximum length of 2311 (2,147,483,647)

nchar

Fixed-length Unicode data with a maximum length of 4,000 characters.

nvarchar

Variable-length Unicode data with a maximum length of 4,000 characters. sysname is a system-supplied user-defined data type that is functionally equivalent to nvarchar(128) and is used to reference database object names.

ntext

Variable-length Unicode data with a maximum length of 230-1 (1,073,741,823) characters.

Money

Date/Time

Character Strings and Characters

18

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

Binary Strings

Other Data Types

binary

Fixed-length binary data with a maximum length of 8,000 bytes.

varbinary

Variable-length binary data with a maximum length of 8,000 bytes.

image

Variable-length binary data with a maximum length of 231-1 (2,147,483,647) bytes.

cursor

A reference to a cursor.

sql_variant

A data type that stores values of various SQL Server-supported data types, except text, ntext, timestamp, and sql_variant.

table

A special data type used to store a result set for later processing.

timestamp

A database-wide unique number that gets updated every time a row gets updated.

uniqueidentifier A globally unique identifier (GUID). In addition to the system-supplied data types, user-defined data types, which are based on system data type, can be created for specific needs. When you create a column, you can specify the maximum size of the data type using precision and scale. • • • •

Precision is the total number of digits in a number. Scale is the number of digits to the right of the decimal point in a number. For example, the number 531.24 has a precision of 5 and a scale of 2. A column created as a numeric data type containing values similar to 531.24 would be designated as Col_name (5,2). A column created as an integer containing values similar to 5324 would be designated as Col_name (4).

Note: The primary and foreign keys of a table must be the same data type.

Transact-SQL Overview

19

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

Query Facts A query is a request for data from a database. Client computers, whether running on the same computer as SQL Server 2005 or on a separate client computer, do not access SQL Server directly. Instead, they use applications written to access the data stored in SQL Server databases. Client applications submit queries to SQL Server using one of the following formats: Format

Description

Transact-SQL

The primary database query and programming language used by SQL Server.

Extensible Markup Language (XML)

A hypertext programming language used to return data from queries and stored procedures using URLs or templates over HTTP. Also used to insert, delete, and update database values. The results from these queries are returned as XML documents.

Multidimensional Expressions

Syntax used to define multidimensional objects and queries and

20

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005 (MDX)

manipulate this data in an OLAP database.

OLE DB and ODBC APIs

Programming interfaces used to send Transact-SQL commands to a database. ADO wraps OLE DB for use in other programming languages; used to access data in OLTP databases.

ActiveX Data Objects (ADO) and ActiveX Data Objects ADO MD is a language-independent set of object-based data access (Multidimensional) (ADO MD) interfaces optimized for multidimensional data applications; used to access data in Analysis Services data cubes. Application type using an automation API that lets users use natural language questions instead of more complex queries to access database information.

English Query

Transact-SQL statements are a common way to interact with SQL server, view or modify data, or change the configuration of the server. Queries are built using a series of statements, much like lines of programming code. Even when you use a graphical management tool or submit queries using another supported language, the queries are translated into Transact-SQL statements and then executed. SQL Server 2005 uses the SQL-92 standard. Queries can be simple or quite complex. The following example shows a simple query statement that selects all columns from the Customers table for rows that match USA as the country: SELECT * FROM Customers WHERE Country = 'USA'

Note: The example shown above is a single query statement. Statements can be written all on one line or broken into several lines as shown above. When SQL server parses the query, it identifies all elements that are part of the statement and executes one statement at a time. Query statements can contain the following elements: Component Description Commands perform functions on the server or the database. Each statement begins with a command. Four common commands are: Commands

Keywords 21

• • •

SELECT retrieves rows from a table. UPDATE changes data in a table. INSERT adds a row to a table or a view.



DELETE removes rows from a table.

Keywords are commands or command parameters used by SQL. In the example shown above, the FROM and WHERE are keywords. Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005

Note: Within documentation, keywords are written in all capital letters. When you write Transact-SQL statements, case does not matter. Identifiers

Identifiers are names of objects such as the server, a database, a table, or an index. In the example above, Customers is an identifier that identifies the table used within the query. Functions are collections of commands defined on the SQL server. When functions are executed, they return a value. SQL Server comes with pre-defined functions (called system functions), or you can define your own functions. Examples of functions include:

Functions

• • • • •

DB_ID and DB_NAME return the database ID or name. OBJECT_ID and OBJECT_NAME return the object ID or name. COL_NAME returns the column name. COL_LENGTH returns the length of a column. INDEX_COL returns an index column name.

When calling functions, follow the function name with parenthesis ( ). Inside include any parameters that must be passed to the function. For example: •

The SELECT SUSER_NAME() statement returns the currently logged on user.



The SELECT DB_ID('AdventureWorks') statement returns the ID of the AdventureWorks database.

An expression is a combination of identifiers, values, and operators that SQL server can Expressions evaluate to get a result. In example above, both * and Country = 'USA' are expressions. Operators

Operators perform actions on data. For example, you can use arithmetic operators to perform mathematical operations on the data.

Datatypes

Datatypes define the format of the data used by table columns or variables. Each data type is identified by a keyword. In some cases, you have to identify the data type of any variables used within the statement.

Comments Comments are nonexecuting remarks in a statement. Comments are indicated in one of two ways: •



Double hyphens ( -- ). Any text on a line following the double hyphen will not be included as part of the statement. Text on a new line that is not preceded by the double hyphens is not commented. Forward slash-asterisk pairs ( /* ... */ ). Any text inside the character pair will not be executed, even if it spans multiple lines.

For example, the following statement: --get the list of names SELECT Firstname /*, Lastname */ FROM Customers

22

Taught by: Aiman Mobarak Elkhalifa

Microsoft SQL Server - Concepts 2 005 WHERE Language = 'Spanish' --test for language

With the comments removed becomes: SELECT Firstname FROM Customers WHERE Language = 'Spanish'

Be aware of the following when working with command syntax in documentation: • • • • • • • • • •

Keywords are in all capital letters. Italics indicate identifiers, expressions, or functions. Items in brackets [ ] are optional. Do not type the brackets. Item in braces { } are required. Do not type the braces. Bolded items indicate items that must be typed exactly as shown such as databases, tables, stored procedures, and utilities. Underline indicates the default value applied. Vertical bar | separates keywords enclosed in brackets or braces. You select only one. [,...n] indicates the preceding item can be repeated n number of times, separated by commas. [...n] indicates the preceding item can be repeated n number of times, separated by blank spaces.

Related Documents