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

The Real MTCS SQL Server 2008 Exam 70/432 Prep Kit- P127 pot

5 69 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 5
Dung lượng 208,45 KB

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

Nội dung

The SQL Server 2008 Management Studio user interface simply allows you to create an underlying DDL statement using the appropriate GUI component.. The CREATE statement creates a SQL Serv

Trang 1

SQL Server 2008 is a mature enterprise data platform, providing objects for storing, accessing, and modifying data In this chapter you will discover how to create tables, indexes, stored procedures, functions, and triggers Designing database objects and writing scripts for their creation is usually the role of database developers However, database administrators must understand the concepts behind database objects and have the skills necessary to create and modify them This chapter will provide you with these key skills

Database objects are divided into two broad categories: storage and

programma-bility Tables are units of data storage, structured by columns and rows Each column in

a table stores data of a specific data type Many built-in data types are available for you to choose from, or you can create custom user-defined data types if necessary

Tables can store large amounts of data, which can take a long time to query

To maximize query performance, you can create fast lookup structures known as

indexes Indexes are created on frequently searched on columns and enable quick

traversal when looking for particular values within a table, similar to the index

you will find at the end of a printed book You can associate constraints with table

columns Constraints define the rules to which data in a particular column or columns must adhere For example, you can use a constraint to specify that values

stored in the EMailAddress field are of a particular format Unique constraints

ensure that data for a particular column is unique across the table For example, you may enforce that product names are always unique in the Products table

Constraints can also define relationships between tables, such as the necessity to have a Customer entity associated with every Order entity These are known as

foreign key constraints.

Programmability objects allow you to define Transact-SQL statements that can

be reused again and again Views are based on Transact-SQL SELECT statements

They represent a way of viewing a data set and show data from one or more underlying tables Views can be updated, allowing you to write data to the view and

to update underlying tables Stored procedures are compiled Transact-SQL statements

that perform particular actions Stored procedures can accept parameters and return

values Functions are similar to stored procedures, except that they always return a value and never update data Triggers are actions defined on tables that will execute

every time data in a table changes

Together these database objects compose a database system Techniques and best practices for designing a database system are beyond the scope of this book However you must understand the concepts behind database systems so that you can maintain them correctly and effectively

Trang 2

Understanding DDL

and DML Language Elements

Transact-SQL is the language used to create objects and access data in SQL Server

Data Manipulation Language, (DML) is part of the Transact-SQL language that

allows you to insert, modify, and delete data in SQL Server tables The core

state-ments that constitute DML are INSERT, UPDATE, DELETE, and MERGE

In this chapter you will only use these statements in a basic way, as they are outside

the scope of this book and exam

Data Definition Language (DDL) is a subset of Transact-SQL that deals with

creating database objects such as tables, constraints, and stored procedures You will

examine these statements in depth as they are mapped directly to the exam objectives

SQL Server 2008 Management Studio provides a rich user interface for creating

these database objects However, not all functionality is available within the user

interface, and often you will use DDL scripts to create your database objects

The SQL Server 2008 Management Studio user interface simply allows you to

create an underlying DDL statement using the appropriate GUI component

Figure 14.1 shows the user interface for creating a table The DDL statement for

creating the same table is shown in Example 14.1

Figure 14.1 SQL Server Management Studio User Interface

Trang 3

The key DDL statements are CREATE, ALTER, and DROP The CREATE

statement creates a SQL Server database object, like a table, view, or stored procedure Example 14.1 creates a new table named Produce and a new view

named Fruits In this example we also use the INSERT DML statement to add

three rows into our new table

TesT Day Tip

Remember that the user interface provided by SQL Server 2008

Management Studio allows you to visually design DDL statements

Any task that is available in SQL Server 2008 Management Studio can

be completed using a DDL script, but not all options available within

a DDL script are available within the user interface.

Example 14.1 Using the CREATE DDL

Statement to Create a New Table and View

USE AdventureWorks;

GO

Use the CREATE DDL statement to create a new table named Produce CREATE TABLE Produce

(ProductID int PRIMARY KEY,

ProductName varchar(50),

ProductType varchar(20))

Use the INSERT DML statement to add rows to the Produce table

INSERT Produce VALUES

(1, 'Tomato', 'Vegetable'),

(2, 'Pear', 'Fruit'),

(3, 'Kiwifruit', 'Fruit');

GO

Use the CREATE DDL statement to create a new view named Fruit that shows us only produce of type 'Fruit'

CREATE VIEW Fruit AS

SELECT * FROM Produce WHERE ProductType = 'Fruit';

GO

Trang 4

The ALTER DDL statement changes an existing object and it can be used to

add or remove columns from a table You can also use this statement to change the

definition of a view, stored procedure, trigger, or function Example 14.2 adds a

Price column to the Produce table we have created in Example 14.1 In this

example we also redefine the view to include the new Price column Do not

confuse the ALTER statement, which changes an object definition, with the

UPDATE statement, which changes data in a table.

Use the SELECT statement to view the data in the Fruit View

SELECT * FROM Fruit

Results:

Example 14.2 Using the ALTER DDL Statement

to Add a New Column to a Table and Redefine a View

Add a new column

ALTER TABLE Produce

ADD Price Money;

GO

Use the UPDATE statement to set prices

UPDATE Produce SET Price = 2.50 WHERE ProductID = 1;

UPDATE Produce SET Price = 3.95 WHERE ProductID = 2;

UPDATE Produce SET Price = 4.25 WHERE ProductID = 3;

GO

Redefine the view

ALTER VIEW Fruit AS

SELECT ProductID, ProductName, Price FROM Produce WHERE ProductType =

'Fruit';

GO

SELECT * FROM Fruit

Results:

Trang 5

The DROP DDL statement removes an object from the database If other

objects depend on the object you are attempting to drop, this statement will not succeed and an error will be raised Example 14.3 deletes data from the Produce table, and then removes both the Fruit view and the Produce table from the database In this example, we also attempt to drop the Person.Contact table This operation will fail, as other objects depend on Person.Contact Do not

confuse the DROP statement, which removes an object from the database, with the DELETE statement, which deletes data from a table.

Example 14.3 Using the DROP DDL

Statement to Remove Tables and Views from a Database

DELETE FROM Produce;

SELECT * FROM Fruit;

Results:

- (0 row(s) affected)

DROP VIEW Fruit;

GO

DROP TABLE Produce;

GO

DROP TABLE Person.Contact;

Results:

Msg 3726, Level 16, State 1, Line 1

Could not drop object 'Person.Contact' because it is referenced by

a FOREIGN KEY constraint.

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

TỪ KHÓA LIÊN QUAN