T-SQL管理数据库对象

1、架构的含义
  架构是对象的容器,包含的主要对象有XML集合、表、视图、过程、函数、聚合函数、约束、同义词、队列和统计信息
  架构位于数据库内部,而数据库位于服务器内部
  标识:两部分标识:schema_name.object_name;三部分标识:database_name.schema_name.object_name
2、创建架构
  Transact-SQL命令创建架构
  create schema schema_name_clause [ <schema_element> [ . . . n] ]
  <schema_name_clause> :: =
  {
    schema_name
   |  authorization owner_name
   |  schema_name schema_name
  }
  <schema_element> :: =
  {
    table_definition | view_definition | grant_statement
    revoke_statement | deny_statement
  }
3、用Transact-SQL命令移动对象到新的架构
  alter schema schema_name(目标架构) transfer securable_name(源架构)
4、删除架构(不能使用master数据库)
  drop schema schema_name
完整示例:
  create schema BookSchema
  authorization dbo
  create table MyBooks
  (
     [bookid] [int] identity(1,1) not null,
     [bookname] [nvarchar](50) null,
     [bookisbn] [nchar](20) null,
     [bookprice] [int] null,
     [bookauthor] [nchar](10) null,
     [bookPress] [nchar](10) null
  )
5、视图的含义
  由select语句组成的查询定义的虚拟表,原始数据库中数据的一种变换,是查看表中数据的另一种方式
  数据库中只存在视图的定义,而数据仍然存放在原来的基本表中
6、创建视图
  create view [ schema_name . ] view_name [ (column) [ , . . . n ] ]
  [ with <view_attribute> [ , . . . n] ]
  as select_statement
  [ with check option ] [ ; ]
  <view_attribute> :: =
  {
    [ encryption ]
    [ schemabinding ]
    [ view_metadata ]
  }
7、管理视图
  查看视图:
8、索引的定义
  索引是一种可以加快数据检索速度的数据结构,主要用于提高数据库查询数据性能
  索引类型:
    聚集索引
      聚集索引将数据行的键值在表内排序存储对应的数据记录,使得表的物理顺序与索引顺序一致
      查询优化器非常适合聚集索引,因为聚集索引的叶级页而不是数据页  
    非聚集索引
      非聚集索引的数据存储在一个位置,索引存储在另一个位置,索引带有指针指向数据的存储位置
      每一个表中最多可有249个非聚集索引
  查询:select * from tableName with (index = indexName) where conditions
9、

posted @ 2011-09-28 22:18  常伟华  阅读(376)  评论(0编辑  收藏  举报