THIẾT KẾ ỨNG DỤNG 1. Giới thiệu chức năng Ứng dụng quản lý sản xuất nhiều phân xưởng được áp dụng để minh họa về những gì tìm hiểu được về cơ sở dữ liệu phân tán áp dụng vào thực tế. Chương trình QL Sản xuất Quản lý nhân viên Quản lý SP xuất xưởng Cập nhập thông tin Thống kế tình hình SX Cập nhật SP xuất xưởng Sơ đồ chức năng hệ thống quản lý sản xuất nhiều phân xưởng. 2. Các bảng cho CSDL tập trung Hệ thống gồm 4 bảng với các quan hệ như hình sau: Các bảng cho CSDL tập trung cho hệ thống quản lý sản xuất nhiều phân xưởng. Giả sử ứng dụng cần thống kê tính toán tình hình sản xuất tại các phân xương thuộc một địa phương TinhTP) 3. Thiết kế phân mảnh ngang Giả sử ứng dụng được đặt ở 2 nơi Hà Nội và HCM là 2 nơi đặt phân xưởng sản xuất. Ta thiết kế Phanxuong phân mảnh ngang dựa theo TinhTP giả sử TinhTP có 2 giá trị HN, HCM đại diện cho 2 nơi. Nhanvien được phân mảnh ngang dẫn xuất theo Phanxuong dựa vào MaPX Sanxuat được phân mảnh ngang dẫn xuất theo Phanxuong dựa vào MaPX Sanpham được nhân bản ở 2 nơi và được đồng bộ dữ liệu. Ta có các mảnh ngang như sau: PhanxuongHCM=σ TinhTP= HCM Phanxuong) PhanxuongHN=σ TinhTP= HN Phanxuong) 1
NhanvienHCM=Nhanvien < PhanxuongHCM Phép nữa kết semi Join) NhanvienHN=Nhanvien < PhanxuongHN SanxuatHCM=Sanxuat < PhanxuongHCM SanxuatHN=Sanxuat < PhanxuongHN Vẽ cây phân mảnh dẫn xuất PhanxuongHCM PhanxuongHN NhanvienHCM SanxuatHCM NhanvienHN SanxuatHN 4. Cài đặt CSDL tại các Server 4.1 Cài đặt Server databe cho ứng dụng trên ServerHCM ServerHCM cài đặt CSDL QLSanxuatHCM gồm 4 bảng : use mter if existsselect name from sysdatabes where name='qlsanxuathcm') drop databe QLSanxuatHCM create databe QLSanxuatHCM use QLSanxuatHCM Create Table PhanxuongHCM MaPX char4) constraint pk_mapx primary key, TenPX nvarchar50) not null, DiachiPX nvarchar100) null, TinhTP nvarchar20) constraint chk_noi checktinhtp='hcm') ) 2
Create table NhanvienHCM MaNV char6) constraint pk_manv primary key, HotenNV nvarchar50) not null, DiachiNV nvarchar100) null, MaPX char4) constraint fk_mapx1 references PhanxuongHCMMaPX), Trinhdo nvarchar30) not null ) Create table Sanpham MaSP char6) constraint pk_masp Primary key, TenSP nvarchar50) not null, Mota nvarchar100) null ) Create table SanxuatHCM MaPX char4) constraint fk_mapx2 references PhanxuongHCMMaPX), MaSP char6) constraint fk_masp references SanphamMaSP), Ngay smalldatetime not null, Soluong integer constraint chk_soluong CheckSoluong>=0), constraint pk_sanxuat primary keymapx,masp,ngay) ) 4.2 Cài đặt Server Databe cho ứng dụng trên ServerHN ServerHCM cài đặt CSDL QLSanxuatHCM gồm 4 bảng như sau : use mter if existsselect name from sysdatabes where name='qlsanxuathn') drop databe QLSanxuatHN create databe QLSanxuatHN use QLSanxuatHN 3
Create Table PhanxuongHN MaPX char4) constraint pk_mapx primary key, TenPX nvarchar50) not null, DiachiPX nvarchar100) null, TinhTP nvarchar20) constraint chk_noi checktinhtp='hn') ) Create table NhanvienHN MaNV char6) constraint pk_manv primary key, HotenNV nvarchar50) not null, DiachiNV nvarchar100) null, MaPX char4) constraint fk_mapx1 references PhanxuongHNMaPX), Trinhdo nvarchar30) not null ) Create table Sanpham MaSP char6) constraint pk_masp Primary key, TenSP nvarchar50) not null, Mota nvarchar100) null ) Create table SanxuatHN MaPX char4) constraint fk_mapx2 references PhanxuongHNMaPX), MaSP char6) constraint fk_masp references SanphamMaSP), Ngay smalldatetime not null, Soluong integer constraint chk_soluong CheckSoluong>=0), constraint pk_sanxuat primary keymapx,masp,ngay)) 4.3 Cài đặt linkserver Server đặt tại TP.HCM có tên PHET cài đặt Databe QLSanxuatHCM Chứa thông tin quản lý sản xuất tại TP.HCM) gồm 4 bảng : PhanxuongHCM,NhanvienHCM, SanxuatHCM, Sanpham Server đặt tại TP.HN có tên KHOA cài đặt Databe QLSanxuatHN Chứa thông tin quản lý sản xuất tại Hà Nội ) gồm 4 bảng : PhanxuongHN, NhanvienHN, SanxuatHN, Sanpham Tại ServerHCM PHET) cài đặt linkserver đến ServerHN KHOA) bằng đoạn mã sau Thực thi trong Query Analyzer) use mter Exec sp_addlinkedserver @server='khoa', @srvproduct='sqlserver OLEDB Provider', @provider='sqloledb', 4
@datrc='khoa' Exec sp_addlinkedsrvlogin 'Khoa','false',NULL,'sa','sa' ---Để kiểm tra select * from Khoa.QLSanxuatHN.dbo.PhanxuongHN Tương tự tại ServerHN KHOA) cài đặt linkserver đến ServerHCM PHET) bằng đoạn mã sau Thực thi trong Query Analyzer) use mter Exec sp_addlinkedserver @server='phet', @srvproduct='sqlserver OLEDB Provider', @provider='sqloledb', @datrc='phet' Exec sp_addlinkedsrvlogin 'PHET','false',NULL,'sa','sa' ---Để kiểm tra select * from Khoa.QLSanxuatHCM.dbo.PhanxuongHCM 5. Cài đặt các khung nhìn phân tán 5.1. Trên serverhcm Phet) Cài đặt các khung nhìn phân tán use QLSanxuatHCM Create View Phanxuong Select * from PhanxuongHCM Union Select * from Khoa.QLSanxuatHN.dbo.PhanxuongHN ----------------------------------------------------------------------------------- Create View Nhanvien Select * from NhanvienHCM Union Select * from Khoa.QLSanxuatHN.dbo.NhanvienHN Create View Sanxuat Select * from SanxuatHCM Union Select * from Khoa.QLSanxuatHN.dbo.SanxuatHN -------------------------------------------------------------- Create View SanphamSX Select Sp.MaSp,TenSP,Mota,MaPX,Ngay,Soluong from Sanpham SP, Sanxuat SX Where Sp.MaSP=SX.MaSP 5.2. Trên serverhn Khoa) Cài đặt các khung nhìn phân tán use QLSanxuatHN 5
Create View Phanxuong Select * from PhanxuongHN Union Select * from Khoa.QLSanxuatHCM.dbo.PhanxuongHCM ----------------------------------------------------------------------------------- Create View Nhanvien Select * from NhanvienHN Union Select * from Khoa.QLSanxuatHCM.dbo.NhanvienHCM Create View Sanxuat Select * from SanxuatHN Union Select * from Khoa.QLSanxuatHCM.dbo.SanxuatHCM -------------------------------------------------------------- Create View SanphamSX Select Sp.MaSp,TenSP,Mota,MaPX,Ngay,Soluong from Sanpham SP, Sanxuat SX Where Sp.MaSP=SX.MaSP 6. Cài đặt các Stored Procedure xử lý 6.1. Trên serverhcm Phet) Cài đặt các Stored Procedure xử lý a. Nhập TinhTP, MaSP, Ngay cho biết số lượng sản phẩm đã sản xuất trên các phân xưởng theo ngày. Create Proc TimSLSanxuat @Noi varchar10), @MaSP Char6), @Ngay Varchar20) If @Noi='HCM' --Local Select SP.MaSP, TenSP, SoLuong From Sanpham SP, SanxuatHCM SX Where SP.MaSP=SX.MaSP and SP.MaSP=@MaSP and Ngay=@Ngay If @Noi='HN' --Remote Select SP.MaSP, TenSP, SoLuong From Sanpham SP, Khoa.QLSanxuatHN.dbo.SanxuatHN SX Where SP.MaSP=SX.MaSP and SP.MaSP=@MaSP and Ngay=@Ngay b. Thêm vào bảng Sanxuat thông tin gồm: MaSP, MaPX, Ngay, Soluong Create Proc NhapSPXuatxuong @Noi varchar10),@mapx Char4), @MaSP Char6), @Ngay Varchar20), @Soluong Integer If @Noi='HCM' --Local Insert Into SanxuatHCMMaPX,MaSP,Ngay,Soluong) 6
values @MaPX, @MaSP, @Ngay, @Soluong) If @Noi='HN' --Remote Insert Into Khoa.QLSanxuatHN.dbo.SanxuatHNMaPX,MaSP,Ngay,Soluong) values @MaPX, @MaSP, @Ngay, @Soluong) c. Xóa 1 dòng ra khỏi bảng Sanxuat Create Proc XoaSPXuatxuong @Noi varchar10),@mapx Char4), @MaSP Char6), @Ngay Varchar20) If @Noi='HCM' --Local Delete From SanxuatHCM Where MaPX=@MaPX and MaSP=@MaSP and Ngay=@Ngay If @Noi='HN' --Remote Delete From Khoa.QLSanxuatHN.dbo.SanxuatHN Where MaPX=@MaPX and MaSP=@MaSP and Ngay=@Ngay d. Cập nhật dữ liệu trong bảng sản xuất MaSP, MaPX, Ngay, Soluong) Create Proc SuaSPXuatxuong @Noi varchar10),@mapxcu Char4), @MaSPCu Char6), @NgayCu Varchar20), @Soluong Integer, @MaPXMoi Char4), @MaSPMoi Char6), @NgayMoi Varchar20) If @Noi='HCM' --Local Update SanxuatHCM Set MaPX=@MaPXMoi,MaSP=@MaSPMoi, Ngay=@NgayMoi,Soluong=@Soluong PXCu and MaSP=@MaSPCu and Ngay=@NgayCu If @Noi='HN' --Remote Update Khoa.QLSanxuatHN.dbo.SanxuatHN Set MaPX=@MaPXMoi,MaSP=@MaSPMoi, Ngay=@NgayMoi,Soluong=@Soluong PXCu and MaSP=@MaSPCu and Ngay=@NgayCu e. Thêm vào bảng Nhanvien MaNV, HoTenNV, DiachiNV, MaPX, Trinhdo) Create Proc NhapNV @MaNV Char6), @HotenNV nvarchar50), @MaPX varchar4),@trinhdo nvarchar30) Declare @Noi varchar20) --Lay gia tri Noi cua phan xuong trong Khung nhin phan tan Select @Noi=TinhTP From Phanxuong PX If @Noi='HCM' --Local Insert Into NhanvienHCMMaNV,HotenNV,DiachiNV,MaPX,Trinhdo) values @MaNV, @HotenNV, @DiachiNV, @MaPX, @Trinhdo) If @Noi='HN' --Remote Insert Into NhanvienHNMaNV,HotenNV,DiachiNV,MaPX,Trinhdo) values @MaNV, @HotenNV, @DiachiNV, @MaPX, @Trinhdo) f. Cập nhật thông tin phân xưởng 7
Create Proc DieuchinhPX @Ma Char4), @Noi varchar20), @Ten nvarchar50),@diachi nvarchar100) Declare @Noicu varchar20), @Tencu nvarchar50), @Diachicu nvarchar100) --Lay gia tri Noicu, Tencu,Diachicu Select @Noicu=TinhTP, @Tencu = TenPX, @Dichicu=DiachiPX From Phanxuong --Khung nhin phan tan Where MaPX=@Ma If@Noi=@Noicu) --chi cap nhat TenPX,DiachiPX tren khung nhin phan tan If@Ten<>@Tencu) Update Phanxuong Set TenPX=@Ten else Begin If@Noi='HCM' --Chuyen tu ServerHNKhoa) sang ServerHCMPhet) Begin ---Them vao Bang PhanxuongHCM Insert into PhanxuongHCMMaPX,TenPX,DiachiPX,TinhTP) Values@Ma, @Ten, @Diachi, 'HCM') ---Them vao Bang NhanvienHCM Insert into NhanvienHCMMaNV,HotenNV,DichiNV, MaPX, Trinhdo) Select MaNV,HotenNV,DiachiNV,@Ma, trinhdo From Khoa.QLsanxuatHN.dbo.NhanvienHN ---Them vao Bang SanxuatHCM Insert into SanxuatHCMMaPX,MaSP,Ngay,Soluong) Select @Ma,MaSp,Ngay,Soluong From Khoa.QLsanxuatHN.dbo.SanxuatHN ---Xoa ra khoi Bang NhanvienHN Delete From Khoa.QLSanxuatHN.dbo.NhanvienHN ---Xoa ra khoi Bang SanxuatHN Delete From Khoa.QLSanxuatHN.dbo.SanxuatHN End Else --Chuyen tu ServerHCMPhet) sang ServerHNKhoa) Begin ---Them vao Bang PhanxuongHN Insert into PhanxuongHNMaPX,TenPX,DiachiPX,TinhTP) Values@Ma, @Ten, @Diachi, 'HN') ---Them vao Bang NhanvienHN Insert into NhanvienHNMaNV,HotenNV,DichiNV, MaPX, Trinhdo) Select MaNV,HotenNV,DiachiNV,@Ma, trinhdo From Phet.QLsanxuatHN.dbo.NhanvienHN ---Them vao Bang SanxuatHCM Insert into SanxuatHNMaPX,MaSP,Ngay,Soluong) Select @Ma,MaSp,Ngay,Soluong 8
From Phet.QLsanxuatHCM.dbo.SanxuatHCM ---Xoa ra khoi Bang NhanvienHCM Delete From Phet.QLSanxuatHCM.dbo.NhanvienHCM where MaPX=@Ma ---Xoa ra khoi Bang SanxuatHCM Delete From Phet.QLSanxuatHCM.dbo.SanxuatHCM where MaPX=@Ma End End 6.2. Trên serverhcm Phet) Cài đặt các Procedure xử lý a. Nhập TinhTP, MaSP, Ngay cho biết số lượng sản phẩm đã sản xuất trên các phân xưởng theo ngày. Create Proc TimSLSanxuat @Noi varchar10), @MaSP Char6), @Ngay Varchar20) If @Noi='HN' --Local Select SP.MaSP, TenSP, SoLuong From Sanpham SP, SanxuatHCM SX Where SP.MaSP=SX.MaSP and SP.MaSP=@MaSP and Ngay=@Ngay If @Noi='HCM' --Remote Select SP.MaSP, TenSP, SoLuong From Sanpham SP, Phet.QLSanxuatHCM.dbo.SanxuatHCM SX Where SP.MaSP=SX.MaSP and SP.MaSP=@MaSP and Ngay=@Ngay b. Thêm vào bảng Sanxuat thông tin gồm : MaSP, MaPX, Ngay, Soluong Create Proc NhapSPXuatxuong @Noi varchar10),@mapx Char4), @MaSP Char6), @Ngay Varchar20), @Soluong Integer If @Noi='HN' --Local Insert Into SanxuatHNMaPX,MaSP,Ngay,Soluong) values @MaPX, @MaSP, @Ngay, @Soluong) If @Noi='HCM' --Remote Insert Into Phet.QLSanxuatHCM.dbo.SanxuatHCMMaPX,MaSP,Ngay,Soluong) values @MaPX, @MaSP, @Ngay, @Soluong) c. Xóa 1 dòng ra khỏi bảng Sanxuat Create Proc XoaSPXuatxuong @Noi varchar10),@mapx Char4), @MaSP Char6), @Ngay Varchar20) If @Noi='HN' --Local Delete From SanxuatHN Where MaPX=@MaPX and MaSP=@MaSP and Ngay=@Ngay If @Noi='HCM' --Remote 9
Delete From Phet.QLSanxuatHCM.dbo.SanxuatHCM Where MaPX=@MaPX and MaSP=@MaSP and Ngay=@Ngay d. Cập nhật dữ liệu trong bảng sản xuất MaSP, MaPX, Ngay, Soluong) Create Proc SuaSPXuatxuong @Noi varchar10),@mapxcu Char4), @MaSPCu Char6), @NgayCu Varchar20), @Soluong Integer, @MaPXMoi Char4), @MaSPMoi Char6), @NgayMoi Varchar20) If @Noi='HN' --Local Update SanxuatHN Set MaPX=@MaPXMoi,MaSP=@MaSPMoi,Ngay=@NgayMoi,Soluong=@Soluong PXCu and MaSP=@MaSPCu and Ngay=@NgayCu If @Noi='HCM' --Remote Update PHET.QLSanxuatHCM.dbo.SanxuatHCM Set MaPX=@MaPXMoi,MaSP=@MaSPMoi,Ngay=@NgayMoi,Soluong=@Soluong PXCu and MaSP=@MaSPCu and Ngay=@NgayCu e. Thêm vào bảng Nhanvien MaNV, HoTenNV, DiachiNV, MaPX, Trinhdo) Create Proc NhapNV @MaNV Char6), @HotenNV nvarchar50), @MaPX varchar4),@trinhdo nvarchar30) Declare @Noi varchar20) --Lay gia tri Noi cua phan xuong trong Khung nhin phan tan Select @Noi=TinhTP From Phanxuong PX If @Noi='HCM' --Local Insert Into NhanvienHCMMaNV,HotenNV,DiachiNV,MaPX,Trinhdo) values @MaNV, @HotenNV, @DiachiNV, @MaPX, @Trinhdo) If @Noi='HN' --Remote Insert Into NhanvienHNMaNV,HotenNV,DiachiNV,MaPX,Trinhdo) values @MaNV, @HotenNV, @DiachiNV, @MaPX, @Trinhdo) f. Cập nhật thông tin phân xưởng Create Proc DieuchinhPX @Ma Char4), @Noi varchar20), @Ten nvarchar50),@diachi nvarchar100) Declare @Noicu varchar20), @Tencu nvarchar50), @Diachicu nvarchar100) --Lay gia tri Noicu, Tencu,Diachicu Select @Noicu=TinhTP, @Tencu = TenPX, @Dichicu=DiachiPX From Phanxuong --Khung nhin phan tan Where MaPX=@Ma If@Noi=@Noicu) --chi cap nhat TenPX,DiachiPX tren khung nhin phan tan If@Ten<>@Tencu) Update Phanxuong Set TenPX=@Ten else 10
Begin If@Noi='HCM' --Chuyen tu ServerHNKhoa) sang ServerHCMPhet) Begin ---Them vao Bang PhanxuongHCM Insert into PhanxuongHCMMaPX,TenPX,DiachiPX,TinhTP) Values@Ma, @Ten, @Diachi, 'HCM') ---Them vao Bang NhanvienHCM Insert into NhanvienHCMMaNV,HotenNV,DichiNV, MaPX, Trinhdo) Select MaNV,HotenNV,DiachiNV,@Ma, trinhdo From Khoa.QLsanxuatHN.dbo.NhanvienHN ---Them vao Bang SanxuatHCm Insert into SanxuatHCMMaPX,MaSP,Ngay,Soluong) Select @Ma,MaSp,Ngay,Soluong From Khoa.QLsanxuatHN.dbo.SanxuatHN ---Xoa ra khoi Bang NhanvienHN Delete From Khoa.QLSanxuatHN.dbo.NhanvienHN ---Xoa ra khoi Bang SanxuatHN Delete From Khoa.QLSanxuatHN.dbo.SanxuatHN End Else --Chuyen tu ServerHCMPhet) sang ServerHNKhoa) Begin ---Them vao Bang PhanxuongHN Insert into PhanxuongHNMaPX,TenPX,DiachiPX,TinhTP) Values@Ma, @Ten, @Diachi, 'HN') ---Them vao Bang NhanvienHN Insert into NhanvienHNMaNV,HotenNV,DichiNV, MaPX, Trinhdo) Select MaNV,HotenNV,DiachiNV,@Ma, trinhdo From Phet.QLsanxuatHN.dbo.NhanvienHN ---Them vao Bang SanxuatHCM Insert into SanxuatHNMaPX,MaSP,Ngay,Soluong) Select @Ma,MaSp,Ngay,Soluong From Phet.QLsanxuatHCM.dbo.SanxuatHCM ---Xoa ra khoi Bang NhanvienHCM Delete From Phet.QLSanxuatHCM.dbo.NhanvienHCM 11
End End 7. Giao diện chương trình ---Xoa ra khoi Bang SanxuatHCM Delete From Phet.QLSanxuatHCM.dbo.SanxuatHCM 12
13
8. Những vấn đề chưa giải quyết Vấn đề khi một server chứa Phân xưởng bị hỏng thì hệ thống cũng không hoạt động được vấn đề nhân bản dữ liệu) Chưa giải quyết vần đề các giao dịch đồng thời xảy ra Chưa quan tâm đến việc Commit các giao dịch thành công và Rollback các giao dịch thất bại Và còn một số vấn đề khác nữa Trên đây là những vần đề quan trọng khi thực hiện một ứng dụng phân tán, nhưng do thời gian hạn hẹp nên chỉ thực hiện dược một ứng dụng nhỏ đơn giản: Giải quyết được các vần đề truy xuất dữ liệu, cập nhật dữ liệu giữa các mảnh. Trong tương lai sẽ phát triển hoàn thiện hơn. 14