1、分布式数据库一、实验目的:(1)通过实验加深对课程的理论部分的理解,培养学生理论联系实际的能力。(2)在实验系统的设计与实现上给学生留有较大的自由发挥空间,鼓励与培养学生的创新能力。(3)通过合作完成一个实验系统,培养学生的团队精神与协同工作的习惯。二、实验内容:(1)第一部分:建表及数据量要求Student (SID char(4) primary key, SName char(20) not null,Class char(20) not null);1000Course (CID char(3) primary key, CName char(20 ) not null);30Teac
2、her (TID char(4) primary key, TName char(20) not null , CID char(3) ,Class char(20) not null);30Exam (SID char(4) primary key, CID char(3) primary key, Mark double );2000 设一个教师只上一门课,一门课可由多个教师上。(2)第二部分:数据分布要求:垂直和水平划分Student 表:根据某种规则,将 Student 表水平分片:S1,S2,S3,分别存放在三个不同的站点SITE1, SITE2,SITE3 。Teacher 表:根
3、据某种规则,将 Teacher 表垂直分片:T1,T2,分别存放在两个不同的站点SITE1, SITE2。Exam 表:根据某种规则,将 Exam 表水平分片:E1,E2,E3,分别存放在三个不同的站点SITE1, SITE2,SITE3 。Course 表:不划分,分配到 site1(3)第三部分:数据查询、插入、删除和更新要求在任意站点可实现以上功能。(4)第四部分:系统性能讨论讨论你所采用 DBMS 的分布式特性,以及本系统的安全性、可靠性、数据一致性措施和效果。三、设计基本思想1拟采用 MS SQLServer 数据库实现本系统,数据库的分片的透明性在数据库的层次上实现,建立一个全局视
4、图,尽量避免使用各个表的分片。2在 SQLServer 内部水平分片透明主要利用分区视图来实现。分区视图在一个或多个服务器间水平连接一组成员表中的分区数据,使数据看起来就象来自一个表。在实现分区视图之前,必须先水平分区表。原始表被分成若干个较小的成员表。每个成员表包含与原始表相同数量的列,并且每一列具有与原始表中的相应列同样的特性(如数据类型、大小、排序规则) 。如果正在创建分布式分区视图,则每个成员表分别位于不同的成员服务器上。3垂直分片的透明性的实现:可以为这些表建立视图,为了保证数据的可更新,再针对此视图建立相关的触发器。3在 SQLServer 内部为了远程服务器的访问可以通过 sp_
5、addlinkedserver 建立连接服务器,然后用 sp_addlinkedsrvlogin 建立此服务器的登录。四、实现步骤1首先安装三个 SQLServer 服务器,分别命名为 site1,site2,site3,在其上面建立数据库对应为 db1,db2,db3。2在 site1 的 db1 数据库中建立如下个表:create table S1(SID char(4) not null,SName char(20) not null,Classroom char(20) not null CHECK (Classroom =计 0201),primary key(SID,Classro
6、om)create table Course(CID char(3) not null,CName char(20) not null,Primary key(CID)create table T1(TID char(4) not null,TName char(20) not null,primary key(TID)create table E1 (SID char(4) not null, CID char(3) not null, Mark float not null CHECK (Mark =60 and Mark=80) ,primary key(SID,CID,Mark)3为每
7、个服务器建立其它服务器的连接。Site1 中执行:exec sp_addlinkedserver server=site2 ,srvproduct =SQL SERVEREXEC sp_addlinkedsrvlogin site2, false, null, sa, jia123exec sp_addlinkedserver server=site3 ,srvproduct =SQL SERVEREXEC sp_addlinkedsrvlogin site3, false, null, sa, jia123Site2 中执行:exec sp_addlinkedserver server=si
8、te1 ,srvproduct =SQL SERVEREXEC sp_addlinkedsrvlogin site1, false, null, sa, jia123exec sp_addlinkedserver server=site3 ,srvproduct =SQL SERVEREXEC sp_addlinkedsrvlogin site3, false, null, sa, jia123Site3 中执行:exec sp_addlinkedserver server=site1 ,srvproduct =SQL SERVEREXEC sp_addlinkedsrvlogin site1
9、, false, null, sa, jia123exec sp_addlinkedserver server=site2 ,srvproduct =SQL SERVEREXEC sp_addlinkedsrvlogin site2, false, null, sa, jia1234建立各个分区视图。Site1 中执行:-建立 Student 视图create view Student asselect * from S1union allselect * from site2.db2.dbo.S2union allselect * from site3.db3.dbo.S3go-建立 Exa
10、m 视图create view Exam asselect * from E1union allselect * from site2.db2.dbo.E2union allselect * from site3.db3.dbo.E3go-建立 Teacher 视图create view t2 as select * from site2.db2.dbo.t2gocreate view Teacher asselect t1.TID,t1.Tname ,t2.CID,t2.Classroom from t1,t2where t1.tid=t2.tidSite2 中执行:-建立 Student
11、视图create view Student asselect * from S2union allselect * from site1.db1.dbo.S1union allselect * from site3.db3.dbo.S3go-建立 Course 视图create view Course asselect * from site1.db1.dbo.Coursego-建立 Exam 视图create view Exam asselect * from E2union allselect * from site1.db1.dbo.E1union allselect * from si
12、te3.db3.dbo.E3go-建立 Teacher 视图create view t1 as select * from site1.db1.dbo.t1gocreate view Teacher asselect t1.TID,t1.Tname ,t2.CID,t2.Classroom from t1,t2where t1.tid=t2.tidSite3 中执行:create view Student asselect * from S3union allselect * from site2.db2.dbo.S2union allselect * from site1.db1.dbo.S
13、1go-建立 Course 视图create view Course asselect * from site1.db1.dbo.Coursego-建立 Exam 视图create view Exam asselect * from E3union allselect * from site2.db2.dbo.E2union allselect * from site1.db1.dbo.E1go-建立 Teacher 视图create view t1 as select * from site1.db1.dbo.t1gocreate view t2 as select * from site2
14、.db2.dbo.t2gocreate view Teacher asselect t1.TID,t1.Tname ,t2.CID,t2.Classroom from t1,t2where t1.tid=t2.tid5在每个数据库中建立 Teacher 的触发器。drop trigger Trig_INS_Teacher gocreate trigger Trig_INS_Teacher ON Teacherinstead of insert asbegininsert into t1 select tid,tname from insertedinsert into t2 select ti
15、d ,cid ,classroom from insertedend godrop trigger Trig_DEL_Teachergocreate trigger Trig_DEL_Teacher ON Teacherinstead of deleteasbegindelete t1 where t1.tid in(select tid from deleted)delete t2 where t2.tid in(select tid from deleted)end drop trigger Trig_upd_Teachergodrop trigger Trig_upd_Teachergo
16、create trigger Trig_upd_Teacher ON Teacherinstead of updateasbegindelete t1 where t1.tid in(select tid from deleted)delete t2 where t2.tid in(select tid from deleted)insert into t1 select tid,tname from insertedinsert into t2 select tid ,cid ,classroom from insertedend附:SQL Server2005 中分区表创建文件组ALTER
17、 DATABASE DeanDB ADD FILEGROUP FG1ALTER DATABASE DeanDB ADD FILE ( NAME = NFG1, FILENAME = NC:DeanDataFG1.ndf , SIZE = 3072KB , FILEGROWTH = 1024KB ) TO FILEGROUP FG1类似的建立四个文件和文件组,并把每一个存储数据的文件放在不同的磁盘驱动器里。创建分区函数:CREATE PARTITION FUNCTION SendSMSPF(datetime) AS RANGE RIGHT FOR VALUES (20070401, 20070701, 20071001)创建分区方案:CREATE PARTITION SCHEME SendSMSPS AS PARTITION SendSMSPF TO (FG1, FG2, FG3, FG4)创建分区表:CREATE TABLE SendSMSLog(ID int IDENTITY(1,1) NOT NULL,IDNum nvarchar(50) NULL,SendContent text NULLSendDate datetime NOT NULL,) ON SendSMSPS(SendDate)