日期:2014-05-16  浏览次数:20462 次

微软认证考试70-461-Create Database Objects(创建数据库对象)-24%比重

附注:微软认证考试70-461范围

  1. Create Database Objects创建数据库对象 (24%)
  2. Work with Data数据处理 (27%)
  3. Modify Data数据修改 (24%)
  4. Troubleshoot & Optimize故障排解及SQL优化 (25%)

本文是第一节Create Database Objects创建数据库对象

第一部分:Create and alter tables using T-SQL syntax (simple statements).May include but not limited to: create tables without using the built in tools; ALTER; DROP; ALTER COLUMN; CREATE 用T-SQL语句创建修改表(简易叙述)。可能包含并不仅限于:不使用内建工具创建,修改,删除表;创建,修改,删除列;

    创建表:CREATE TABLE Test(ID INT IDENTITY(1,1) PRIMARY KEY,Name VARCHAR(100))

    删除表:DROP TABLE Test3

    修改表增加列:ALTER TABLE Test2 ADD Email VARCHAR(200)
    修改表修改列:ALTER TABLE Test2 ALTER COLUMN Name VARCHAR(200)
    重命名表:EXEC SP_RENAME 'Test2','Test3'
    重命名列: EXEC SP_RENAME 'Test3.Name','Name2'

    修改表删除列:alter table test drop column name

参考:http://msdn.microsoft.com/en-us/library/ms174979(v=sql.110).aspx

http://msdn.microsoft.com/en-us/library/ms173790(v=sql.110).aspx

http://msdn.microsoft.com/en-us/library/ms190273(v=sql.110).aspx

第二部分:Create and alter views (simple statements). May include but not limited to: create indexed views; create views without using the built in tools; CREATE, ALTER, DROP 创建修改视图(简易叙述)。可能包含并不仅限于:不使用内建工具创建,修改,删除视图;创建带索引视图。

创建修改视图:

CREATE/ALTER VIEW TestView
AS
SELECT id,testid FROM test

删除视图:

DROP VIEW TestView

为视图创建索引:

CREATE UNIQUE CLUSTERED INDEX NIX_TestView_testid
ON TestView
(
    testid
)

注意:视图需要设置WITH SCHEMABINDING

ALTER VIEW TestView
WITH SCHEMABINDING
AS
SELECT id,testid,name FROM test
(error: Cannot schema bind view 'TestView' because name 'test' is invalid for schema binding. Names must be in two-part format and an object cannot reference itself.
)

更改视图设置选项WITH SCHEMABINDING出错:此视图不能设置SCHEMABINDING。表名必须是两部分格式并且对象不能引用本身。

正确语句:

VIEW TestView
WITH SCHEMABINDING
AS
SELECT id,testid FROM dbo.test

删除视图索引:DROP INDEX 视图名.索引名,如DROP INDEX TestView.NIX_TestView_id

参考:http://msdn.microsoft.com/en-us/library/ms187956(v=sql.110).aspx

http://msdn.microsoft.com/en-us/library/ms173846(v=sql.110).aspx

http://msdn.microsoft.com/en-us/library/ms173492(v=sql.110).aspx

http://msdn.microsoft.com/en-us/library/ms191432(v=sql.110).aspx