Thuc Hanh CSDL

Embed Size (px)

DESCRIPTION

NỘI DUNG PHẦN THỰC HÀNH Bài thực hành 1: Thiết kế CSDL trong MS Access  Nội dung: ♦ Tạo CSDL trong MS Access ♦ Tạo Table ♦ Khai báo khóa chính, khóa ngoại, miền giá trị ♦ Nhập dữ liệu vào các bảng Bài thực hành 2: Truy xuất dữ liệu  Nội dung: ♦ Tạo select query đơn giản ♦ Tạo select query có tạo filed mới ♦ Tạo select query có thống kê theo group Bài thực hành 3: Thiết kế report  Nội dung: ♦ Tạo report bằng wizard ♦ Thiết kế report trong design view ♦ Tạo Main/subreport Bài thực hành 4: Thiết

Text of Thuc Hanh CSDL

NI DUNG PHN THC HNH Bi thc hnh 1: Thit k CSDL trong MS Access Ni dung: To CSDL trong MS Access To Table Khai bo kha chnh, kha ngoi, min gi tr Nhp d liu vo cc bng Bi thc hnh 2: Truy xut d liu Ni dung: To select query n gin To select query c to filed mi To select query c thng k theo group Bi thc hnh 3: Thit k report Ni dung: To report bng wizard Thit k report trong design view To Main/subreport Bi thc hnh 4: Thit k form Ni dung: To Form cp nht d liu trn mt table To Form nhp liu cho nhiu table To Form tm kim thng tin. Bi thc hnh 5: To menu, toolbar Ni dung: To menu To toolbar Gn toolbar vo form hay report

NI DUNG PHN THC HNH Bi thc hnh 1: Thit k CSDL trong MS Access Ni dung: To CSDL trong MS Access To cc Table Khai bo kha chnh, kha ngoi, min gi tr Nhp d liu vo cc bng

CSDL QUN L BN HNGTable: KHACHHANG Name MaKH TenCty DiaChi ThanhPho DienThoai Table: LOAISANPHAM Name Type MaLoai Number TenLoai Text MoTa Memo Hnh OLE Object Table: NHANVIEN Name Type MaNV Number Ho Text Ten Text Nu Yes/No NgaySinh Date/Time NoiSinh Text DiaChi Text DienThoaiNha Text ChucVu Text NgayNV Date/Time DTDiDong Text Hinh OLE Object GhiChu Memo MaNVQL Number Table: SANPHAM Name Type MaSP AutoNumbe r TenSP Text DonViTinh Text DonGia Currency SoTon Number MaLoai Number Table: HOADON Name MaHD MaKH MaNV NgayLapHD NgayNhanHang

Type Text Text Text Text Text Size Long 15 -

Size 10 50 60 20 10

Din gii M khch Tn cng ty a ch Thnh ph in thoi

Din gii M loi sn phm Tn loi M t Hnh loi sn phm

Size Din gii Long M nhn vin 30 H 7 Tn L phi n Ngy sinh 15 Ni sinh 60 a ch 10 in thoi nh 20 Chc v Ngy nhn vic 50 in thoi di ng Hnh Ghi ch Long M cn b qun l trc tip Size 20 20 Din gii M sn phm-Kha chnh

Tn sn phm n v tnh n gi Double S lng tn kho Long M loi sn phm Size 10 Long Din gii M ha n-Kha chnh M khch M nhn vin Ngy lp ha n Ngy khch nhn hng

Type AutoNumber Text Number Date/Time Date/Time

Table: CHITIETHD Name Type MaHD Number MaSP Number SoLuong Number Dongia Currency

Size Long Long Byte

Din gii M ha n M sn phm S lng n gi bn

Kha chnh

Bi thc hnh 2: Truy xut d liu Ni dung: To select query n gin To select query c to filed mi To select query c thng k theo group 1- Hy lit k cc chi tit ha n gm cc thng tin: MaHD, MaKH, MaNV, MaSP,

SoLuong, DonGia, ThanhTien: SoLuong*DonGia. Sp th t theo MaHD. Lu vi tn "Qry_Tien CTHD".2- To Query "Qry_ThanhToan" c cc Field sau:MaHD, TenSP, DonGia, SoLuong, TienSP, SoNgay, TienTraTruoc, ConLai. Sp th t tng dn theo MaHD. Trong : SoNgay: l s ngy t ngy lp ha n n ngy nhn hng. TienTraTruoc c tnh nh sau: nu SoNgay < 10 th tin tr trc l 20% ca TinSP, nu 10 SoNgay < 20 th tin tr trc l 30% ca TienSP, v t 20 tr ln tin phi tr trc l 40% ca TienSP. ConLai: l s tin cn li phi tr nhng phi gim i 5% 3- To Query "Qry_TienQuyDoi" ( Tin quy i ) xem bng tnh tin cho tng sn phm theo tng thi im gm cc thng tin sau: MaHD, ngy lp, tn sn phm, n gi, s lng, thnh tin VND, thnh tin USD. Bit rng: t gi 1 USD trong nm 2000 l 13500VND, 6 thng u nm 2001 l 14500 v 6 thng cui nm 2001 n nay l 15000. Bit rng n gi trong bng sn phm c tnh l VND. 4- Lit k danh sch cc nhn vin n gm cc thng tin:M nhn vin, h v tn, Ngy nhn vic. Sp th t theo ngy nhn vic. Lu query vi tn Qry_Nhan Vien Nu 5- Lit k danh sch ha n mua mt hng Ru trong nm 2001 gm cc thng tin: M ha n, M khch, Tn cng ty, Ngy lp ha n. Sp th t theo tng khch hng. 6- Lit k danh sch cc ha n khng c ngy nhn hng gm cc thng tin: MaHD, M khch, Tn cng ty, Ngy lp ha n. Sp th t theo tng khch hng v ngy lp. Lu vi Hoa Don Chua Nhan Hang. 7- Lit k danh sch cc khch hng c ha n trong nm 2000. Vi cc thng tin: M khch, TnCty. Yu cu khng c hai record trng nhau. Lu vi tn Qry_Khach mua 2000 8- To Query tnh tin cho mi ha n, gm cc thng tin: MaHD, MaKH, MaNV, Ngy Lp, Ngy nhnhng, m s lng chi tit ha n (So CT), Tin ha n (TienHD). Lu vi tn "Qry_TienHoaon" 9- Cho bit mi Khch hng t mua nhng sn phm no, tng s lng v tng s tin mua cho nhng loi sn phm ca khch. Gm cc thng tin: MaKH, MaSP, Tng s lng (TongSoLuong), Tng tin (TongTien). Lu vi tn Qry_San Pham Khach Mua. 10- Cho bit s lng v doanh thu ca tng sn phm bn trong mi nm, gm cc ct:Nm, MaSP, TenSP, TongSoLuong, TongTien. Lu vi tn Doanh Thu San Pham Moi Nam. 11- To Query cho bit thng tin ca nhng khch hng khng c ha n trong nm 2001. Vi cc thng tin: M khch, TnCty, a ch, S in thai. Lu vi tn Qry_Khach khong mua hang 2001

12- Cho bit tng s nhn vin, s nam v s n trong cng ty. Gm cc ct: Si So, So Nam, So Nu 13- To query tnh tin cho cc ha n, gm cc thng tin: MaHD, MaKH, MaNV, ngy lp, ngy nhn hng, s dngchi tit trong mi ha n (SoCT), Tin ha n (TienHD). Lu vi tn Tin Ha n.

14- Cho bit doanh thu tng mt hng trong nm 2001, sp theo th t gim ca s tin thu. Gm cc ct:MaSP,TenSP, Hinh, n gi, Tng lng hng bn (TongSL), Tng tin bn sn phm (TongTien). Lu vi tn DsSP Nam 2001 15- Cho bit doanh thu tng mt hng mi thng trong nm 2001, sp theo th t gim ca s tin thu trong tng thng. Gm cc ct: MaSP, TenSP, Thang, s lng bn(TongSL), TienBan. Lu vi tn San Pham Ban Moi Thang Nam 2001

16- To Crosstab Query cho bit s lng tng sn phm bn trong tng nm gm cc ct: MaSP,TnSP, TngDoanh s, Cc nm.

17- Thit k Crosstab Query cho bit doanh thu ca mi nhn vin trong tng nm. gm cc ct: MaNV, HoTenNV,Tng Doanh s, cc nm. 18- Cho bit doanh s bn cc sn phm theo cc thng lp ha n trong mt nm no gm cc ct: MaSP, Tn SP, Tng Doanh s, Tng s lng, Thng 1, Thng 2, Thng 3,.. .. Trong , Nm cn tng hp c nhp vo khi thi hnh Query. 19- Cho bit doanh s bn hng ca tng nhn vin theo tng loi sn phm da trn cc ha n bn trong nm 2000, gm cc ct: MaNV,H tn nhn vin, Tng Doanh s, Tin bn tng loi sn phm. MaNV H Tn NV Tng DS Bnh Kem C Hp ... Ru 0001 Nguyn Vn A 7000000 1200000 700000 ... 1500000 ... .... .... .... ... ... ..... 20- Cho bit doanh s bn hng ca mi nhn vin trong nm 2002 theo tng qy gm cc ct: MaNV,H tn, Tng Doanh s, Quy 1, Quy 2,... Lu tn Qry Doanh So Nhan Vien Tung Quy2002 21- Cho bit doanh s bn hng ca mi nhn vin trn tng mt hng theo tng qy trong nm 2002 gm cc ct: MaNV,MaSP,TenSP, Tng Doanh s, Quy 1, Quy 2,., Quy 4. Lu vi tn Qry DSo Nhan Vien ban SP Tung Quy2002

Bi thc hnh 3: Thit k report Ni dung: To report bng wizard Thit k report trong design view To Main/Subreport1- Dng AutoReport:Tabular to bo co "Danh Mc Sn Phm" nh sau:

To report bng wizard

2- Dng AutoReport:Columnar to bo co "rptTheNhanVien" nh sau:

3- S dng AutoReport Columnar vi ngun d liu t bng Khach hang to bo co rptDanhMucKhachHang trn kh giy A4 nh hnh mu.

4- To report "San Pham Khach Mua Moi Nam" in danh sch cc sn phm khch mua, phn theo tng nm. Cui mi nm v mi khch hng c dng tnh tng tin mua.

Hng dn: D liu trn bo co gm thng tin v Khch hng trong table Khach Hang lin kt mtnhiu vi cc sn phm khch mua mi nm thng qua M khch. Do , cn to query tng hp s lng , tin mua mi sn phm trong tng nm. To query San Pham Khach Mua Moi Nam bng Simple Query Wizard: Chn cc fields trong: San Pham, query Lien Ket CTHD. Tnh tng ct soLuong v Thanh tien. Sau , sa li tn cc Fields nh sau:Field MaKH Nam: Year(NgayLapHD) TenSP DonVi Tinh TongSL: SoLuong Tienmua: ThanhTien Table San Pham San Pham Total Group by Group by Group by Group by Sum Sum

To Report bng Report Wizard, click Ok HT1: Chn cc Fields trong: + Table Khach Hang: MaKh, TenCty, Diachi

+ Query San Pham Khach Mua Moi Nam: chn ht cc fields tr field MaKH HT2: Chn kiu hin th d liu theo table Khach Hang (by Khach Hang), click nt Next HT3: Chn Nam lm chun phn nhm cc sn phm khch mua trong nm. Click nt Next. Do you want to add grouping levels? Click nt Summary Options, chn tnh tng Sum ct TienMua cc sn phm khch mua trong tng nm. Click OK, v click Next. 5- To Report Doanh Thu San Pham Moi Nam dng Groups/Totals lit k cc s lng, tin bn tng sn phm trong tng nm. C tnh tng s lng v tng tin bn trong mi Nm v ca tt c cc Nm.

Hng dn: D liu trn bo co gm: Nm, tn sn phm, n v tnh, s lng v tin bn sn phm trong nm. To query Doanh Thu San Pham Moi Nam FieldNam: Year(NgayLapHD) MASP TenSP TongSL: SoLuong TienBan: SoLuong*DonGia Table San Pham TotalGroup by Group By Group By Sum Sum

To Report bng Report Wizard, vi bng ngun l query Doanh Thu San Pham Moi Nam. HT1: Chn cc Field: Nam, TenSP, DonViTinh, Soluong, TienBan. V click nt Next HT2: Chn field Nam phn nhm cc mu tin sn phm, click nt Next HT3: Click nt Summary Options, chn tnh tng Sum mi nm cho ct SoLuong v TienBan. Click OK, v click Next.

6- To report Doanh Thu Moi Nam lit k tin bn mi nm.

Hng dn: D liu trn bo co gm c: Nm v tin bn sn phm trong nm. Nn to query Doanh Thu San Pham Moi Nam lm ngun cho bo co. To Report bng Report Wizard, vi bng ngun l query Doanh Thu San Pham Moi Nam. HT1: Chn cc Field: Nam, TienBan. V click nt Next HT2: Chn field Nam phn nhm cc mu tin sn phm, click nt Next HT3: Click nt Summary Options, chn tnh tng Sum mi nm cho ct TienBan v Summary Only ch hin dng tng cui nm khng hin chi tit tng sn phm trong nm nh cu 3. Click OK, v click Next.

1- To report Doanh Thu SP Mt Qy bo co s lng v tin bn mi thng ca tng sn phm trong mt qy.Trong , Qy v Nm c nhp khi m Report. Mu bo co sau y hin th thng tin ca qu 1 nm 2002 ( chn hnh ty ) DOANH THU SN PHM QU 1 NM 2002 ISTT Sn Phm Tng s Tng Tin Ngy lp bo co: 15/04/2002 Thng Th Nht Thng Th Hai Thng Th Ba S Tin S Tin S Tin lng lng lng

Thit k report trong design view

1 2

Bnh kem

Bnh m

131 ...

$ 1310 ...

55 ...

$ 550 ...

0 ...

$0 ...

76 ...

$ 760 ...

Cng c Qu : Xxx xxx xxx xxx Xxx xxx xxx xxx Hng dn: To Query ngun cho Report: Add: Hoa Don, Chi Tiet HD, San Pham Field TenSP Tongso: Tongtien: TienThang1:iif(Month( SLThang1: Soluong Soluong* NgayLHD) Mod 3 = iif(Month(NgayLHD) Dongia 1 ,Soluong*Dongia,0) Mod 3 = 1, Soluong, 0) Table SPham Total G. By Sum Sum Sum Sum Criteria ..... .. Nam: Year(NgayLapHD) Quy:Datepart(q,NgayLapHD) Hoa Don Where Where [N