Working with Tables, Indexes, and ConstraintsUse the ˛ CREATE TABLE statement to define a table by listing columns in the table along with corresponding data types.. One clustered index
Trang 1Working with Tables, Indexes, and Constraints
Use the
˛ CREATE TABLE statement to define a table by listing columns
in the table along with corresponding data types
Indexes are useful for fast searching and sorting data One clustered index
˛
is allowed per table, and the underlying table data is stored in the order of
the clustered index Nonclustered indexes are separate lookup structures
that point to the table heap or the clustered index
Full-text indexes are used for specialized querying using functions like
˛
FREETEXT Only one full-text index is allowed per table.
Indexes and constraints can be defined separately and are bound to an
˛
existing table
Viewing and Modifying Data
A view is a
˛ SELECT statement saved with a name A view can be updated
if it is based on a single table, or if it has INSTEAD OF triggers defined
on it Indexes can be created on a view as well as on a table
A stored procedure is any
˛ Transact-SQL statement saved with a name
Stored procedures can update data by using DML statements.
A function is a
˛ Transact-SQL statement that usually performs a calculation
Functions must return a value Functions that return a single value are
known as scalar functions, whereas functions that return a table-valued
expression are known as table-valued functions
A trigger is a statement that runs automatically, when data in a particular
˛
table or view is modified Triggers can cancel transactions by using the
ROLLBACK TRANSACTION statement Triggers can be specified as
FOR, AFTER, or INSTEAD OF You can access special INSERTED and
DELETED tables within the trigger to find out the old and new values of
rows that are being updated
Trang 2Exam Objectives
Frequently Asked Questions
Q: When should I use the TRUNCATE TABLE statement rather than the
DELETE statement?
A: You should use the TRUNCATE TABLE statement if you want to quickly and
indiscriminately empty the table A performance advantage is achieved with the
TRUNCATE TABLE statement when the database recovery model is set to
Bulk-Logged The DELETE statement allows you to restrict the data you will
be deleting using a WHERE clause.
Q: Does the TRUNCATE TABLE statement remove the table from the database?
A: No, the TRUNCATE TABLE statement removes all data from the table, but the
table structure and its definition remain intact
Q: What is the best indexing strategy?
A: Indexing strategies vary depending on your data access pattern and parameters such as the size of your table As a rule of thumb, it is recommended that you
create a clustered index on the PRIMARY KEY column and multiple
nonclus-tered indexes for other frequently searched-on columns
Q: When should I use FILLFACTOR and PAD_INDEX options?
A: Use these options when creating or rebuilding an index Bear in mind that
FILLFACTOR and PAD_INDEX optimize write performance, but slightly
decrease read performance because more data pages have to be accessed Do not bother padding indexes based on identity columns In these cases, new values will never go in the middle of a page, always at the end
Q: Why would I use the FREETEXT search function instead of multiple LIKE
‘%value%’ comparisons?
A: Using LIKE comparisons is a highly time- and resource-consuming operation that always requires a table scan The FREETEXT utilizes the full-text index structure and delivers much better performance than the LIKE comparison.
Trang 3Q: What is the advantage of using an updateable view over updating the table
directly, given that the updateable view will by definition always be based on a
single table?
A: A view is more flexible For example, you may wish to restructure the
under-lying tables without the need to change your client applications You will only
need to update the view in this case, not everything that referenced the tables
being restructured
Q: Why should I use stored procedures in preference to functions?
A: Stored procedures are usually used to perform an action, like update, insert, or
delete, whereas functions are usually used to perform calculations Functions
cannot be used to execute DML statements.
Q: If I define multiple triggers on a single table, what is the order of the triggers
firing?
A: The triggers will execute in random order; you cannot rely on the order of
triggers firing
Trang 4Self Test
1 You are creating a view named WeeklySales This view is used to create a sales report that is presented to management at the beginning of each week You want to ensure that the underlying tables on which this view is based are not accidentally modified causing the report to break What is the easiest way to implement this?
A Use a CREATE VIEW WITH CHECK constraint to create a view.
B Use a CREATE VIEW WITH SCHEMABINDING statement to create
the view
C Do nothing When a view is based on a table, the underlying table cannot
be modified until the view is dropped
D Use a DDL trigger to roll back any statement that attempts to modify the table that the view depends on
2 You have a view named YearlySales that lists all sales for the year The reporting application your organization uses allows you to query the YearlySales view by CustomerName or by OrderDate You receive unfavorable feedback from users that report generation is painfully slow What is the best way to optimize report performance?
A Create indexes on CustomerName and OrderDate columns
B Create a UNIQUE constraint on CustomerName and OrderDate columns.
C Create a DEFAULT constraint on CustomerName and OrderDate columns.
D Create a full-text index on CustomerName and OrderDate columns
3 You have a very large table containing documents stored as a column of varbi-nary data type The table is named Documents and is not referenced by
FOREIGN KEY constraints What is the most efficient way of removing all
records from this table, while leaving the table ready for inserting new records (select all that apply)?
A TRUNCATE TABLE Documents
B DELETE Documents
C DELETE FROM Documents
D DROP TABLE Documents
Trang 5E Set the Database Recovery Model to Full
F Set the Database Recovery Model to Bulk-Logged
4 You have a table named Products, which contains the ProductID, ProductName,
Model, and Color columns The ProductID is marked as IDENTITY You wish
to ensure that there are never two products with the same combination of
name, model, and color What is the easiest way to achieve this?
A Create a PRIMARY KEY constraint on the ProductName, Model, and
Color columns
B Create a DEFAULT constraint on the ProductName, Model, and Color
columns
C Create a UNIQUE constraint on the ProductName, Model, and Color
columns
D Create a trigger FOR INSERT that checks that there is not already a
combination of name, model, and color in the table
5 You have two tables: PrizeDraw and Employees Both tables have a
PersonName column You must ensure that employees cannot enter the prize
draw A record with the name that already exists in the Employees table cannot
be inserted into the PrizeDraw table What is the best way to achieve this?
A Create a CHECK constraint on the PrizeDraw table.
B Create a CHECK constraint on the Employees table.
C Create a trigger on the Employees table
D Create a trigger on the PrizeDraw table
6 You are tasked with creating a Reseller table, with the Commission column
containing the commission percent When a new reseller is added, the default
commission level is 30% What is the easiest way to implement this rule?
A Create a FOR INSERT trigger on the Reseller table.
B Create an INSTEAD OF INSERT trigger on the Reseller table.
C Create a DEFAULT constraint on the Commission column.
D Create a CHECK constraint on the Commission column.
7 You have a table named EmployeePhoto This table is not referenced by any
FOREIGN KEY constraints What is the most efficient way of deleting the
EmployeePhoto table entirely, including data and structure?