Bài giảng Cơ sở dữ liệu - Bảng dữ liệu (tables) cung cấp cho người học các kiến thức: Khái niệm bảng dữ liệu, cách tạo bảng, các kiểu dữ liệu, các thuộc tính của trường (cột). Mời các bạn cùng tham khảo nội dung chi tiết.
Trang 1BẢNG DỮ LIỆU (TABLES)
Trang 2NỘ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
Trang 3 Khóa chính (primary key)
Khóa ngoại (foreign key)
Trang 4VÍ DỤ
Trang 5KHÓ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.
Trang 6KHÓ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
Trang 7TẠO BẢNG BẰNG TABLE DESIGN(1/4)
Trang 8TẠO BẢNG BẰNG TABLE DESIGN(2/4)
Field Properties
Trang 9TẠ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
Trang 10TẠ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
Trang 11TẠO BẢNG TRONG CHẾ ĐỘDATASHEET
VIEW (1/2)
Trang 12TẠO BẢNG TRONG CHẾ ĐỘDATASHEET
VIEW (2/2)
Trang 13CÁC KIỂU DỮ LIỆU (DATA TYPES)(1/4)
Trang 14DATA TYPES (2/4)
Text Use for alphanumeric character Up to 255 characters
Memo Use for text greater than 255
characters in length Up to 65,535 charactersNumber 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)
Trang 15DATA TYPES (3/4)
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
Trang 16Note: 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
Trang 17FIELD PROPERTIES (1/6)
FIELD PROPERTIES ASSOCIATED DATA TYPES USE
Field Size
Text NumberAutoNumber
Set the maximum size for storeddata
Format
Text Memo Number Date/Time CurrencyAutoNumber Yes/No
Hyperlink
Customize the way that the field appears by default when it is displayed or printed
Decimal Places NumberCurrency
Specify the number of decimal places to use when displaying numbers
Trang 18FIELD 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
Trang 19FIELD 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
Trang 20FIELD PROPERTIES (4/6)
FIELD PROPERTIES ASSOCIATED DATA TYPES USE
Required All data types except
Allow entry (by setting to Yes) of a length string (“”)
zero-Indexed
Text Memo Number Date/Time CurrencyAutoNumber Yes/No
Hyperlink
Speed up read access to data in this field
by creating and using an index
Trang 21FIELD 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
Trang 22FIELD PROPERTIES (6/6)
FIELD PROPERTIES ASSOCIATED DATA TYPES USE
Smart Tags
Text Memo Number Date/Time CurrencyAutoNumber Hyperlink
Attach a smart tag to the field
Append Only Memo
Hyperlink Track field value historyText Format Memo Select Rich Text to store field data as
HTML, Plan Text to store only textText Align All data types except
Attachment Specify default alignment of textShow Date Picker Date/Time Display a date picker
Trang 23FIELD PROPERTIES – FIELD SIZE
significant digits Replication ID Globally unique identifier for replication (16 bytes for
storage) Decimal From -10 28 -1 to 10 28 -1, and up to 20 significant digits
AutoNumber Long integer
Trang 24FIELD PROPERTIES – FORMAT
Custom text format
Ví dụ: @”.com”; “no link”[red]
Trang 25FIELD PROPERTIES – FORMAT
Predefined Number format
Trang 26FIELD PROPERTIES – FORMAT
Custom Number format (1/2)
Trang 27FIELD PROPERTIES – FORMAT
Custom Number format (2/2)
Custom number formats can have one to four sections
with semicolons (;)
Example
Trang 28FIELD PROPERTIES – FORMAT
Predefined Date/Time format (1/4)
Trang 29FIELD PROPERTIES – FORMAT
Custom Date/Time format (2/4)
Trang 30FIELD PROPERTIES – FORMAT
Custom Date/Time format (3/4)
Trang 31FIELD PROPERTIES – FORMAT
Custom Date/Time format (4/4)
Example:
Define a custom format to display the date as:
15-April- 2010
Trang 32FIELD 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
Trang 33FIELD PROPERTIES – INPUT MASK
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
Trang 34FIELD PROPERTIES – INPUT MASK
Các ký tự sử dụng trong Input Mask
Trang 35FIELD PROPERTIES – INPUT MASK
Ví dụ 1
Ví dụ 2
Tạo Input Mask cho trường Số điện thoại dưới dạng
### - ### - ####
Trang 36FIELD 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
Trang 37FIELD PROPERTIES – VALIDATION RULE
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”
Trang 38FIELD PROPERTIES – VALIDATION RULE
Trang 39FIELD PROPERTIES – VALIDATION TEXT
Trang 40BÀI TẬP 1
Tạo CSDL tên QLSV.ACCDB
TABLE NAME: LOP
• Field size = 10
• Format: Chữ hoa, màu xanh
• Caption: Mã lớpTenLop Text Tên lớp • Field size = 30
• Caption: Tên lớp
nhiệm • Field size = 30
Trang 41BÀI TẬP 1
TABLE NAME: MONHOC
• Field size = 10
• Format: Chữ hoa, màu đỏ
• Caption: Mã môn họcTenMH Text Tên môn học • Field size = 30
• Caption: Tên môn học
• Field size = byte
• Caption: Số tín chỉ
• Validation rule: Số tín chỉ nằm trong khoảng (1, 5)
Trang 42BÀI TẬP 1
TABLE NAME: SINHVIEN
• Field size = 10
• Format: Chữ hoa, màu đỏ
• Caption: Mã sinh viênHoSV Text Họ sinh viên • Field size = 30
• Caption: Họ sinh viênTenSV Text Tên sinh viên • Field size = 50
• Caption: Tên sinh viênGioiTinh Yes/No Giới tinh • Format: Nam/Nữ
• Caption: Giới tính
Trang 43BÀ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ảng1/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
• Field size = 10
• Format: chữ hoa, màu xanh
• Caption: Mã lớp
Trang 44BÀ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ênMaMH
Text (Lookup Wizard)
Mã môn học
• Field size = 10
• Format: Chữ hoa, màu đỏ
• Caption: Mã môn họcDiemLan1 Number Điểm lần 1
• Field size: double
Trang 45BÀI TẬP 1
Trang 46BÀI TẬP 1
Trang 47BÀI TẬP 1
Trang 48BÀI TẬP 1
Bảng SINHVIEN (tiếp)
Trang 49BÀI TẬP 1
Trang 50BÀI TẬP 1
Trang 51BÀ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
• Field size = 30
• Format: chữ hoa, màu xanh
• Caption: Họ nhân viên
• Field size = 50
• Format: chữ hoa, màu xanh
• Caption: Tên nhân viênGioiTinh Yes/No Giới tinh • Format: Nam/Nữ
• Caption: Giới tính
Trang 52BÀ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 sinhDiaChi Text Địa chỉ • Caption: Địa chỉ
DienThoai Text Điện thoại • Input mask: (###) #######
• Caption: Điện thoại
Trang 53BÀ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àngTenNV Text Tên nhân viên • Format: Chữ hoa, màu xanh
• Caption: Tên khách hàngDiaChi 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
Trang 54BÀI TẬP 2
TABLE NAME: SANPHAM
Field Name Data Type Description Field Properties
• Caption: Đơn vị tínhDonGia Currency Đơn giá
• Format: Currency
• Caption: Đơn giá
• Validation text: Giá phải lớn hơn 0
Trang 55BÀI TẬP 2
TABLE NAME: HOADON
Field Name Data Type Description Field Properties
• Field size = 5
• Format: Chữ hoa, màu đỏ
• Caption: Mã hóa đơn
• Field size: long integer
• Caption: Mã nhân viên
• Required: yes
Trang 56BÀI TẬP 2
TABLE NAME: HOADON (tiếp)
Field Name Data Type Description Field Properties
NgayLapHD Date/Time Ngày lập
• Format: short date
• Caption: Ngày giao hàng
• Input mask 00/00/00
• Validation rule: ngày giao hàng phảisau ngày lập hóa đơn
Trang 57BÀ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: YesMaSP
Number(LookupWizard)
Mã sản phẩm
• Field size = Integer
• Caption: Mã sản phẩm
• Required: YesSoLuong Number Số lượng
• Field size = Integer
• Format: standard
• Caption: Số lượng
• Validation rule: Số lượng phải >0DonGiaBan Currency Đơn giá bán • Caption: Đơn giá bán
Trang 58BÀI TẬP 2
Nhập dữ liệu vào file Excel sau đó Import vào
CSDL này
Trang 59BÀI TẬP 2
Trang 60BÀI TẬP 2
Trang 61BÀI TẬP 2
Trang 62BÀI TẬP 2
Trang 63BÀI TẬP 2