Bài giảng Hệ quản trị cơ sở dữ liệu - Chương 2: Bảng dữ liệu (Tables)

KHÁI NIỆM Là thành phần cơ bản trong CSDL của Access, dùng để lưu trữ dữ liệu Thành phần Tên bảng Cột/ trường (field): lưu trữ một thuộc tính của đối tượng Dòng/ mẩu tin (record): là một thể hiện dữ liệu của các trường trong bảng Khóa chính (primary key) Khóa ngoại (foreign key) KHÓA CHÍNH (PRIMARY KEY)  Là tập hợp của một hay nhiều cột  Dùng để phân biệt giữa các dòng khác nhau trong cùng một bảng  Khóa chính phải là duy nhất, không được trùng và không phép rỗng KHÓA NGOẠI (FOREIGN KEY) Là tập hợp của 1 hay nhiều cột, các cột này phải là khóa chính của một bảng khác Dùng để tạo quan hệ giữa các bảng trong CSDL

pdf63 trang | Chia sẻ: thanhle95 | Lượt xem: 558 | Lượt tải: 1download
Bạn đang xem trước 20 trang tài liệu Bài giảng Hệ quản trị cơ sở dữ liệu - Chương 2: Bảng dữ liệu (Tables), để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
BẢNG DỮ LIỆU (TABLES) 1ttdung@utc2.edu.vn NỘI DUNG KHÁI NIỆM1 CÁCH TẠO BẢNG2 CÁC KIỂU DỮ LIỆU3 CÁC THUỘC TÍNH CỦA TRƯỜNG(CỘT)4 2 2 ttdung@utc2.edu.vn KHÁI NIỆM Là thành phần cơ bản trong CSDL của Access, dùng để lưu trữ dữ liệu Thành phần Tên bảng Cột/ trường (field): lưu trữ một thuộc tính của đối tượng Dòng/ mẩu tin (record): là một thể hiện dữ liệu của các trường trong bảng Khóa chính (primary key) Khóa ngoại (foreign key) 3 ttdung@utc2.edu.vn VÍ DỤ 4ttdung@utc2.edu.vn KHÓA CHÍNH (PRIMARY KEY)  Là tập hợp của một hay nhiều cột  Dùng để phân biệt giữa các dòng khác nhau trong cùng một bảng  Khóa chính phải là duy nhất, không được trùng và không phép rỗng. 5 ttdung@utc2.edu.vn KHÓA NGOẠI (FOREIGN KEY) Là tập hợp của 1 hay nhiều cột, các cột này phải là khóa chính của một bảng khác Dùng để tạo quan hệ giữa các bảng trong CSDL 6 ttdung@utc2.edu.vn TẠO BẢNG BẰNG TABLE DESIGN(1/4) 7ttdung@utc2.edu.vn TẠO BẢNG BẰNG TABLE DESIGN(2/4) Field Properties 8 ttdung@utc2.edu.vn TẠO BẢNG BẰNG TABLE DESIGN(3/4) Xác định khóa chính  Lựa chọn thuộc tính (hoặc các thuộc tính) được sử dụng làm khóa chính Chọn nút Primary Key trên Table tools Hoặc R-click trên tên thuộc tính, chọn Primary Key 9 ttdung@utc2.edu.vn TẠO BẢNG BẰNG TABLE DESIGN(4/4) Lưu bảng vừa tạo  Sử dụng nút Save trên Quick Access Toolbar, hoặc Save trong tab File, hoặc Ctrl+S Đặt tên cho bảng Chú ý: Nếu bảng không có khóa chính 10 ttdung@utc2.edu.vn TẠO BẢNG TRONG CHẾ ĐỘDATASHEET VIEW (1/2) 11ttdung@utc2.edu.vn TẠO BẢNG TRONG CHẾ ĐỘDATASHEET VIEW (2/2) 12ttdung@utc2.edu.vn CÁC KIỂU DỮ LIỆU (DATA TYPES)(1/4) 1. Text 2. Memo 3. Number 4. Date/time 5. Currency 6. AutoNumber 7. Yes/No 8. OLE Object 9. Attachment 10. Hyperlink 11. Lookup wizard 12. Calculated 13. Rich text 13ttdung@utc2.edu.vn DATA TYPES (2/4) DATA TYPES USE SIZE Text Use for alphanumeric character Up to 255 characters Memo Use for text greater than 255 characters in length Up to 65,535 characters Number Use for storing numeric values 1, 2, 4, 8 or 12 bytes (16 bytes when used for replication ID) Date/Time Use for storing date and time values 8 bytes Currency Use for storing monetary values 8 bytes AutoNumber Use for generating unique values that can be used as a primary key 4 bytes (16 bytes when used for replication ID) 14 ttdung@utc2.edu.vn DATA TYPES (3/4) DATA TYPES USE SIZE Yes/No Use for Boolean values 1 bit OLE Object Use to hold a graphic or other objects. Objects can be linked or embedded Up to 1 GB Attachment Use to attach a file to a record (similar to file attachment in email) 2 GB for compressed attachments, ~700KB for uncompressed ones Hyperlink Use for storing hyperlink Up to 65,535 characters 15 ttdung@utc2.edu.vn DATA TYPES (4/4) DATA TYPES USE SIZE Lookup Wizard * Use to start the Lookup Wizard so that we can create a field that uses a combo box to look up a value in another table, query, or list of values Note: Lookup Wizard is not an actual data type • If the lookup field is bound to a table or a query, the size of the bound column is used • Otherwise, the size of text field is used Calculated * Use to hold the results of a calculation based on other fields in the same tables Rich text Use for text fields that require up to 65,535 alphanumeric characters with character formatting 16 ttdung@utc2.edu.vn FIELD PROPERTIES (1/6) FIELD PROPERTIES ASSOCIATED DATA TYPES USE Field Size Text Number AutoNumber Set the maximum size for stored data Format Text Memo Number Date/Time Currency AutoNumber Yes/No Hyperlink Customize the way that the field appears by default when it is displayed or printed Decimal Places Number Currency Specify the number of decimal places to use when displaying numbers 17 ttdung@utc2.edu.vn FIELD PROPERTIES (2/6) FIELD PROPERTIES ASSOCIATED DATA TYPES USE New Values AutoNumber Incremented or random value is used Input Mask Text Number Date/Time Currency Display editing characters to guide data entry Caption All data types Set default labels for forms, reports, and queries Default Value Text Memo Number Date/Time Currency Yes/No Hyperlink Automatically assign the specified value to a field when a new record is added 18 ttdung@utc2.edu.vn FIELD PROPERTIES (3/6) FIELD PROPERTIES ASSOCIATED DATA TYPES USE Validation Rule Text Memo Number Date/Time Currency Yes/No Hyperlink Supply an expression that must be true to add or change the value in this field Validation Text Text Memo Number Date/Time Currency Yes/No Hyperlink Enter text that appears when a value entered this field violates the expression in the Validation Rule box 19 ttdung@utc2.edu.vn FIELD PROPERTIES (4/6) FIELD PROPERTIES ASSOCIATED DATA TYPES USE Required All data types except AutoNumber Require that this field must contain a value in every record Allow Zero Length Text Memo Hyperlink Allow entry (by setting to Yes) of a zero- length string (“”) Indexed Text Memo Number Date/Time Currency AutoNumber Yes/No Hyperlink Speed up read access to data in this field by creating and using an index 20 ttdung@utc2.edu.vn FIELD PROPERTIES (5/6) FIELD PROPERTIES ASSOCIATED DATA TYPES USE Unicode Compression Text Memo Hyperlink Compress the data in this field when fewer than 4,096 characters IME Mode Text Memo Date/Time Hyperlink Control conversion of characters in East-Asian versions of Windows IME Sentence Mode Text Memo Date/Time Hyperlink Control conversion of sentences in East-Asian versions of Windows 21 ttdung@utc2.edu.vn FIELD PROPERTIES (6/6) FIELD PROPERTIES ASSOCIATED DATA TYPES USE Smart Tags Text Memo Number Date/Time Currency AutoNumber Hyperlink Attach a smart tag to the field Append Only Memo Hyperlink Track field value history Text Format Memo Select Rich Text to store field data as HTML, Plan Text to store only text Text Align All data types except Attachment Specify default alignment of text Show Date Picker Date/Time Display a date picker 22 ttdung@utc2.edu.vn FIELD PROPERTIES – FIELDSIZE Number DATA TYPES VALUE Expressed data Text 1 - 255 Byte Integers from 0 to 255 Integer Integers from -32,768 to 32,767 Long integer Integers from -2,147,483,648 to 2,147,483,647 Single Float from -3.4x1038 to 3.4x1038 and up to 7 significantdigits Double Float from -1.797x10308 to 1.797x10308 and up to 15 significant digits ReplicationID Globally unique identifier for replication (16 bytes for storage) Decimal From -1028-1 to 1028-1, and up to 20 significant digits AutoNumber Long integer ReplicationID 23 ttdung@utc2.edu.vn FIELD PROPERTIES – FORMAT Custom text format Ví dụ: @”.com”; “no link”[red] 24 ttdung@utc2.edu.vn FIELD PROPERTIES – FORMAT Predefined Number format 25 ttdung@utc2.edu.vn FIELD PROPERTIES – FORMAT Custom Number format (1/2) 26 ttdung@utc2.edu.vn FIELD PROPERTIES – FORMAT Custom Number format (2/2) Custom number formats can have one to four sections with semicolons (;) Example 27 ttdung@utc2.edu.vn FIELD PROPERTIES – FORMAT Predefined Date/Time format (1/4) 28 ttdung@utc2.edu.vn FIELD PROPERTIES – FORMAT Custom Date/Time format (2/4) 29 ttdung@utc2.edu.vn FIELD PROPERTIES – FORMAT Custom Date/Time format (3/4) 30 ttdung@utc2.edu.vn FIELD PROPERTIES – FORMAT Custom Date/Time format (4/4) Example: Define a custom format to display the date as: 15-April- 2010 31 ttdung@utc2.edu.vn FIELD PROPERTIES – FORMAT Predefined format of a Yes/No field Yes/No, True/False, On/Off Custom format of a Yes/No field Contain up to three sections Example: Define custom format ◦ Display the word “Always” in blue text for Yes, True, or On ◦ And the word “Never” in red text for No, False, or Off 32 ttdung@utc2.edu.vn FIELD PROPERTIES – INPUTMASK Quy định khuôn dạng của dữ liệu được nhập vào Khi quy đinh Input Mask cho bảng, các Input Mask này sẽ được áp dụng cho cả biểu mẫu (form), truy vấn (query), và báo cáo (report) Các ký tự sử dụng trong Input Mask 33 ttdung@utc2.edu.vn FIELD PROPERTIES – INPUTMASK Các ký tự sử dụng trong Input Mask 34 ttdung@utc2.edu.vn FIELD PROPERTIES – INPUTMASK Ví dụ 1 Ví dụ 2 Tạo Input Mask cho trường Số điện thoại dưới dạng ### - ### - #### 35 ttdung@utc2.edu.vn FIELD PROPERTIES – VALIDATION RULE Types of validation rule  Field validation: Access uses the validation rule to test an entry when you attempt to leave the field Record validation: Access uses the rule to test the contents of more than one field when you attempt to leave the record Create a validation rule A validation rule is an expression  Logical, comparison and arithmetic operators are used to build the expression 36 ttdung@utc2.edu.vn FIELD PROPERTIES – VALIDATION RULE  Common operators  Logical operators ◦ AND/OR/NOT  Comparison operators ◦ , =, =,  Arithmetic operators ◦ +, -, *, /  Example  Thiết lập validation rule cho trường ngày sinh, sao cho ngày sinh được nhập vào nằm trong khoảng 1/1/2000 đến 1/1/2014  Nếu ngày sinh nhập vào nằm ngoài khoảng quy đinh xuất hiện hộp thoại yêu cầu nhập lại với lời nhắn “Nhập ngày sinh trong khoảng 1/03/2014 đến 31/03/2014” 37 ttdung@utc2.edu.vn FIELD PROPERTIES – VALIDATION RULE 38ttdung@utc2.edu.vn FIELD PROPERTIES – VALIDATION TEXT ttdung@utc2.edu.vn 39 BÀI TẬP 1 Tạo CSDL tên QLSV.ACCDB TABLE NAME: LOP Field Name Data Type Description Field Properties MaLop Text Mã lớp • Field size = 10 • Format: Chữ hoa, màu xanh • Caption: Mã lớp TenLop Text Tên lớp • Field size = 30 • Caption: Tên lớp GVCN Text Giáo viên chủ nhiệm • Field size = 30 40 ttdung@utc2.edu.vn BÀI TẬP 1 TABLE NAME: MONHOC Field Name Data Type Description Field Properties MaMH Text Mã môn học • Field size = 10 • Format: Chữ hoa, màu đỏ • Caption: Mã môn học TenMH Text Tên môn học • Field size = 30 • Caption: Tên môn học SoTC Number Số tín chỉ • Field size = byte • Caption: Số tín chỉ • Validation rule: Số tín chỉ nằm trong khoảng (1, 5) 41 ttdung@utc2.edu.vn BÀI TẬP 1 TABLE NAME: SINHVIEN Field Name Data Type Description Field Properties MaSV Text Mã sinh viên • Field size = 10 • Format: Chữ hoa, màu đỏ • Caption: Mã sinh viên HoSV Text Họ sinh viên • Field size = 30 • Caption: Họ sinh viên TenSV Text Tên sinh viên • Field size = 50 • Caption: Tên sinh viên GioiTinh Yes/No Giới tinh • Format: Nam/Nữ • Caption: Giới tính 42 ttdung@utc2.edu.vn BÀI TẬP 1 TABLE NAME: SINHVIEN (tiếp) Field Name Data Type Description Field Properties NgaySinh Date/Time Ngày sinh • Format: Short date • Input mask: 00/00/00 • Caption: Ngày sinh • Validation rule: Sinh trong khoảng 1/1/1980 đến 31/12/1995 DiaChi Text Địa chỉ • Caption: Địa chỉ DienThoai Text Điện thoại • Input mask: (###) ####### • Caption: Điện thoại MaLop Text Mã lớp • Field size = 10 • Format: chữ hoa, màu xanh • Caption: Mã lớp 43 ttdung@utc2.edu.vn BÀI TẬP 1 TABLE NAME: KETQUA Field Name Data Type Description Field Properties MaSV Text (Lookup Wizard) Mã sinh viên • Field size = 10 • Format: chữ hoa, màu xanh • Caption: Mã sinh viên MaMH Text (Lookup Wizard) Mã môn học • Field size = 10 • Format: Chữ hoa, màu đỏ • Caption: Mã môn học DiemLan1 Number Điểm lần 1 • Field size: double • Caption: Điểm lần 1 • Validation rule: Điểm nằm trong đoạn [0, 10] DiemLan2 Number Điểm lần 2 • Field size: double • Caption: Điểm lần 2 • Validation rule: Điểm nằm trong đoạn [0, 10] 44 ttdung@utc2.edu.vn BÀI TẬP 1 45ttdung@utc2.edu.vn BÀI TẬP 1 46ttdung@utc2.edu.vn BÀI TẬP 1 47ttdung@utc2.edu.vn BÀI TẬP 1 Bảng SINHVIEN (tiếp) 48 ttdung@utc2.edu.vn BÀI TẬP 1 49ttdung@utc2.edu.vn BÀI TẬP 1 50ttdung@utc2.edu.vn BÀI TẬP 2 Tạo tập tin CSDL tên HOADON.ACCDB TABLE NAME: NHANVIEN Field Name Data Type Description Field Properties MaNV AutoNumber Mã nhân viên • Caption: Mã nhân viên HoNV Text Họ nhân viên • Field size = 30 • Format: chữ hoa, màu xanh • Caption: Họ nhân viên TenNV Text Tên nhân viên • Field size = 50 • Format: chữ hoa, màu xanh • Caption: Tên nhân viên GioiTinh Yes/No Giới tinh • Format: Nam/Nữ • Caption: Giới tính 51 ttdung@utc2.edu.vn BÀI TẬP 2 TABLE NAME: NHANVIEN (tiếp) Field Name Data Type Description Field Properties NgaySinh Date/Time Ngày sinh • Format: Short date • Input mask: 00-00-00 • Caption: Ngày sinh DiaChi Text Địa chỉ • Caption: Địa chỉ DienThoai Text Điện thoại • Input mask: (###) ####### • Caption: Điện thoại MaLop Text Mã lớp • Field size = 10 • Format: chữ hoa, màu xanh • Caption: Mã lớp Hình OLE Object Hình 52 ttdung@utc2.edu.vn BÀI TẬP 2 TABLE NAME: KHACHHANG Field Name Data Type Description Field Properties MaKH Text Mã khách hàng • Field size = 10 • Format: Chữ hoa, màu đỏ • Caption: Mã khách hàng TenNV Text Tên nhân viên • Format: Chữ hoa, màu xanh • Caption: Tên khách hàng DiaChi Text Địa chỉ • Field size = 50 • Caption: Địa chỉ ThanhPho Text Thành phố • Field size = 20 • Caption: Thành phố DienThoai Text Điện thoại • Input mask: (###) ####### • Caption: Điện thoại 53 ttdung@utc2.edu.vn BÀI TẬP 2 TABLE NAME: SANPHAM Field Name Data Type Description Field Properties MaSP Number Mã sản phẩm • Field size = Integer • Caption: Mã sản phẩm • Required: Yes TenSP Text Tên sản phẩm • Field size = 40 • Format: Chữ hoa, màu xanh • Caption: Tên sản phẩm DonViTinh Text Đơn vị tính • Field size = 10 • Caption: Đơn vị tính DonGia Currency Đơn giá • Format: Currency • Caption: Đơn giá • Validation text: Giá phải lớn hơn 0 54 ttdung@utc2.edu.vn BÀI TẬP 2 TABLE NAME: HOADON Field Name Data Type Description Field Properties MaHD Text Mã hóa đơn • Field size = 5 • Format: Chữ hoa, màu đỏ • Caption: Mã hóa đơn • Required: Yes MaKH Text Mã khách hàng • Field size = 10 • Format: Chữ hoa, màu đỏ • Caption: Mã khách hàng MaNV Number Mã nhân viên • Field size: long integer • Caption: Mã nhân viên • Required: yes 55 ttdung@utc2.edu.vn BÀI TẬP 2 TABLE NAME: HOADON (tiếp) Field Name Data Type Description Field Properties NgayLapHD Date/Time Ngày lập hóa đơn • Format: short date • Caption: Ngày lập HĐ • Input mask 00/00/00 NgayGiaoHang Date/Time Ngày giao nhận hàng • Format: short date • Caption: Ngày giao hàng • Input mask 00/00/00 • Validation rule: ngày giao hàng phải sau ngày lập hóa đơn 56 ttdung@utc2.edu.vn BÀI TẬP 2 TABLE NAME: CHITIETHOADON Field Name Data Type Description Field Properties MaHD Text (Lookup Wizard) Mã hóa đơn • Field size = 5 • Format: Chữ hoa, màu đỏ • Caption: Mã hóa đơn • Required: Yes MaSP Number (Lookup Wizard) Mã sản phẩm • Field size = Integer • Caption: Mã sản phẩm • Required: Yes SoLuong Number Số lượng • Field size = Integer • Format: standard • Caption: Số lượng • Validation rule: Số lượng phải >0 DonGiaBan Currency Đơn giá bán • Caption: Đơn giá bán 57 ttdung@utc2.edu.vn BÀI TẬP 2 Nhập dữ liệu vào file Excel sau đó Import vào CSDL này 58 ttdung@utc2.edu.vn BÀI TẬP 2 59ttdung@utc2.edu.vn BÀI TẬP 2 60ttdung@utc2.edu.vn BÀI TẬP 2 61ttdung@utc2.edu.vn BÀI TẬP 2 62ttdung@utc2.edu.vn BÀI TẬP 2 63ttdung@utc2.edu.vn
Tài liệu liên quan