mcdba 数据库设计学习blog
2004-4-14
规划存储管理
1 有且只能有一个主数据文件
2 数据和事务日志永远不能在同一个文件中
3 两个数据库之间不能共享一个文件组
4 当估算数据库大小时,应该使用表和索引.
5 无限文件增长只受操作系统和物理磁盘容量的限制.
6 文件组,就是把一组文件象单个文件一样的处理,这些文件的组称为文件组,
通过它实现了一个数据库存放在多个磁盘可能,通过练习理解了文件组的概念。
练习:
use master
go
create database examnotes
on primary
(name =examnotes_data,filename=d:\testdata\examnotes_data.mdf ,
size=4mb,
maxsize=16mb,
filegrowth=2mb),
filegroup success
(name =examnotes1_data1,filename=d:\testdata\examnotes1_data1.ndf,
size=4mb,
maxsize=8mb,
filegrowth =500kb),
(name =examnotes1_data2,filename=d:\testdata\examnotes2_data2.ndf,
size=4mb,filegrowth=10%)
log on(name =examnotes1_log,
filename=d:\testdata\examnotes_log.ldf,
size=4mb,maxsize=16mb)
向数据库中添加由两个文件组成的文件组
alter database exam
add filegroup examfg1
go
alter database exam
add file
( name = exam_data3,
filename = d:\testdata\exam_data3.ndf,
size = 5mb,
maxsize = 100mb,
filegrowth = 5mb),
( name = exam_data4,
filename = d:\testdata\exam_data4.ndf,
size = 5mb,
maxsize = 100mb,
filegrowth = 5mb)
to filegroup examfg1
alter database exam
modify filegroup examfg1 default
go
欲删除文件组,必先删除该组文件
alter database exam
remove file exam_data4
alter database exam
remove file exam_data3
alter database exam
remove filegroup examfg1
一个有用的sp:
exec sp_spaceused usertablename
查出某个表的记录数和使用的空间大小,因为在实际应用中会出现某个表超大的情形.
在数据库中为表指定文件组的用法如on后面子句,注意on [primary]中的[primary]是主文件组而非主键.
create table [acc_paymethod_mstr] (
[paymethodid] [nvarchar] (10) collate sql_latin1_general_cp1_ci_as not null ,
[paymethodname] [nvarchar] (40) collate sql_latin1_general_cp1_ci_as not null ,
[stampusername] [stampusername] null ,
[stampdatetime] [stampdatetime] null
) on [primary]
相应的改变默认文件的语法为:
alter database <database name>
modify filegroup <filegroup name> default
确保数据完整性
实现约束的6种
alter table <table name>
add constraint <constraint name>
<constraint type><constraint define>
1 unique key
alter table product
add constraint uk_product
unique (supplierid,productname)
2 primary key
alter table product
add constraint pl_product
primary key (productid)
3 foreign key
alter table product
add constraint fk_product_suppliers
foreign key (productid) references suppliers (id)
4 check 约束
alter table product
add constraint df_product_unitsinstock
check (unitsinstock >=0 or unitsinstock is null)
5 not null 约束
alter table product
alter column discontinued bit not null
6 默认约束
alter table product
add constraint df_products_unitprice
default 0 for unitprice
