代码改变世界

《SQL Server 2008 从入门到精通》 学习笔记 第二天

2012-02-02 15:41  CodeCy  阅读(486)  评论(0编辑  收藏  举报

创建数据库

..

clip_image002

附加数据库

clip_image003

clip_image005

设计表

架构

clip_image007

Char和varchar数据类型比较

clip_image009

创建XML 架构集合

clip_image011

创建表

clip_image013

计算列

clip_image015

向现有表中添加计算列

clip_image017

稀疏列

clip_image019

创建带主键约束的表

clip_image021

先现有表添加主键

clip_image023

 

 

alter table Orders.OrderDetail

add constraint pk_orderdetail Primary key (OrderDetailID)

go

 

唯一性约束

clip_image025

DocumentID uniqueidentifier rowguidcol unique,

 

CHECK 约束

创建check约束表

clip_image027

ProductCost money not null check (ProductCost>0),

 

默认值约束

clip_image029

OrderDate date not null constraint df_orderdate default getdate(),

 

外键

向现有表中添加外键

clip_image031

alter table LookupTables.StateProvince

add constraint fk_countrytostateprovince foreign key (CountryID)

references LookupTables.Country(CountryID)

go

 

数据关系图

clip_image033

索引

创建聚集索引

clip_image035

--ShipDate/ FinalShipDate列上创建新的群集索引

CREATE CLUSTERED INDEX icx_finalshipdate ON Orders.OrderHeader (FinalShipDate)

GO

CREATE CLUSTERED INDEX icx_shipdate ON Orders.OrderDetail (ShipDate)

GO

 

创建非聚集索引

clip_image037

GO

CREATE NONCLUSTERED INDEX idx_lastnamefirstname ON HumanResources.Employee(LastName,FirstName)

GO

CREATE NONCLUSTERED INDEX idx_productname ON Products.Product(ProductName)

GO

 

覆盖索引

clip_image039

DROP INDEX idx_city ON Customers.CustomerAddress

GO

CREATE NONCLUSTERED INDEX idx_cityaddressline1 ON Customers.CustomerAddress(City)

INCLUDE(AddressLine1)

GO

 

筛选索引

创建筛选索引

clip_image041

create nonclustered index idx_lastnamefirstname on Customers.Customer(LastName,FirstName)

where LastName is not null

go

 

XML索引

Xml索引创建

clip_image043

create primary xml index ipxml_productdescription on Products.Product(ProductDescription)

go

create xml index ispxml_productdescription on Products.Product(ProductDescription)

using xml index ipxml_productdescription

for path

go

create xml index isvxml_productdescription on Products.Product(ProductDescription)

using xml index ipxml_productdescription

for value

go

 

创建聚集索引 执行 create clustered index

创建非聚集索引 执行 create noclustered index

在索引中添加列,且不受900字 在非聚集索引中增加include子句

针对行子集创建索引 在非聚集索引中增加where子句

在索引中预留空间供未来增加值用 在创建索引时指定小于100的填充因子

消除索引碎片 执行alter index… rebuild / alter index… reorganize

禁用索引 执行alter index…disable

创建xml索引 执行create primary xml index…以创建主xml索引

执行create xml index 以创建辅助xml索引

创建空间索引 执行create sptial index