创建数据库
..
附加数据库
设计表
架构
Char和varchar数据类型比较
创建XML 架构集合
创建表
计算列
向现有表中添加计算列
稀疏列
创建带主键约束的表
先现有表添加主键
alter table Orders.OrderDetail add constraint pk_orderdetail Primary key (OrderDetailID) go
唯一性约束
DocumentID uniqueidentifier rowguidcol unique,
CHECK 约束
创建check约束表
ProductCost money not null check (ProductCost>0),
默认值约束
OrderDate date not null constraint df_orderdate default getdate(),
外键
向现有表中添加外键
alter table LookupTables.StateProvince add constraint fk_countrytostateprovince foreign key (CountryID) references LookupTables.Country(CountryID) go
数据关系图
索引
创建聚集索引
--ShipDate/ FinalShipDate列上创建新的群集索引 CREATE CLUSTERED INDEX icx_finalshipdate ON Orders.OrderHeader (FinalShipDate) GO CREATE CLUSTERED INDEX icx_shipdate ON Orders.OrderDetail (ShipDate) GO
创建非聚集索引
GO CREATE NONCLUSTERED INDEX idx_lastnamefirstname ON HumanResources.Employee(LastName,FirstName) GO CREATE NONCLUSTERED INDEX idx_productname ON Products.Product(ProductName) GO
覆盖索引
DROP INDEX idx_city ON Customers.CustomerAddress GO CREATE NONCLUSTERED INDEX idx_cityaddressline1 ON Customers.CustomerAddress(City) INCLUDE(AddressLine1) GO
筛选索引
创建筛选索引
create nonclustered index idx_lastnamefirstname on Customers.Customer(LastName,FirstName) where LastName is not null go
XML索引
Xml索引创建
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