Download presentation
Presentation is loading. Please wait.
1
Relational Data Model Part I
2
Outline Relational data model basics Integrity rules (完整性規則)
Rules about referenced rows (被參照的記錄) Relational Algebra (關聯代數) Relational model basics: - Tables - Columns and data types - Matching values - Alternative terminology - SQL CREATE TABLE statement Integrity rules: primary and foreign keys Referenced rows: actions when referenced rows are modified Relational algebra - Cover simple operators - Provide separate slide shows for join, outer join, and division operators - May want to mix relational algebra coverage with SQL 3-2
3
What is a data model? Data model (資料模式、模型)
A collection of integrated concepts for describing data, relationships between data, and constraints on the data used by a database. 簡單的來說: 一套描述資料庫的方式就稱為資料模式(Data model)
4
What is a data model? A data model comprises three components
A Structure Part How the database is to be constructed A Manipulative Part Types of operations (updating or retrieving data, and changing the structure of the database) that are allowed on the data A set of integrity rules ensuring that the data is accurate.
5
Relational Database (關聯式資料庫)
A relational database is a collection of interrelated relations (or tables). Database Relational Database: Introduction Oracle Database is a relational database management system (RDBMS). A relational database uses relations or two-dimensional tables to store information. The relational database model consists of three components: a collection of objects or relations (tables), a set of operators to manage the tables, and data integrity rules. For example, you may want to store information about all the employees in your company. In a relational database, you can create several tables, such as an employee table, a department table, and a salary table, to store different pieces of information about your employees. Table Name: EMPLOYEES Table Name: DEPARTMENTS … … 3-5
6
Tables Each table has a heading (標題) and a body (內容). Heading Body
Definition Part Table name and column (資料欄) names Body Content Part (data part) Rows (資料列) of the table Partial Student table: - 5 columns - 3 rows - Real student table: 10 to 50 columns; thousands of rows Convention: - Table names begin with uppercase - Mixed case for column names - First part of column name is an abbreviation for the table name - Upper case for data heading part StdSSN StdLastName StdMajor StdClass WELLS IS FR NORBERT FIN JR KENDALL ACCT body heading part Table Name: Student 3-6
7
Naming Rules In general, table names and column names:
Must begin with a letter Must be 1–30 characters long Must contain only A–Z, a–z, 0–9, _, $, and # Must not duplicate the name of another object owned by the same user of a database Must not be a reserved word Naming Rules You name database tables and columns according to the standard rules for naming any Oracle database object: Table names and column names must begin with a letter and be 1–30 characters long. Names must contain only the characters A–Z, a–z, 0–9, _ (underscore), $, and # (legal characters, but their use is discouraged). Names must not duplicate the name of another object owned by the same Oracle server user. Names must not be an Oracle server reserved word. Naming Guidelines Use descriptive names for tables and other database objects. Note: Names are case-insensitive. For example, EMPLOYEES is treated as the same name as eMPloyees or eMpLOYEES. For more information, see “Object Names and Qualifiers” in the Oracle Database SQL Reference. 3-7
8
Data Type Each column of a table is associated with a data type.
Each data type define a set of values and permissible (可允許的) operations on a column of a table. Each data type has a name and usually a length specification. CHAR(6) DECIAML(6,2) Naming Rules You name database tables and columns according to the standard rules for naming any Oracle database object: Table names and column names must begin with a letter and be 1–30 characters long. Names must contain only the characters A–Z, a–z, 0–9, _ (underscore), $, and # (legal characters, but their use is discouraged). Names must not duplicate the name of another object owned by the same Oracle server user. Names must not be an Oracle server reserved word. Naming Guidelines Use descriptive names for tables and other database objects. Note: Names are case-insensitive. For example, EMPLOYEES is treated as the same name as eMPloyees or eMpLOYEES. For more information, see “Object Names and Qualifiers” in the Oracle Database SQL Reference. 3-8
9
Common Data Types Data Type Description CHAR(size) VARCHAR(size)
Fixed-length character data of length size bytes (Default and minimum size is 1; Most DBMS has an upper limit on the length.) VARCHAR(size) Variable-length character data (A maximum size must be specified: minimum size is 1; Most DBMS has an upper limit on the length.) DECIMAL(W,R) For numeric data with a fixed precision (The W value indicates the total number of digits, and the R value indicates the number of digits to the right of the decimal point.) DATE/TIME These data types are not standard across various DBMSs. INTEGER For columns containing whole numbers FLOAT(P) For columns containing numeric data with a floating precision (The P value indicates the number of significant digits(有效數字)). Most DBMS has an upper limit on P such as 38. BOOLEAN For columns containing data with two values such as true/false or yes/no Data Types When you identify a column for a table, you need to provide a data type for the column. There are several data types available: 3-9
10
CREATE TABLE Statement (SQL)
CREATE TABLE Student ( StdSSN CHAR(10), StdFirstName VARCHAR(50), StdLastName VARCHAR(50), StdCity VARCHAR(50), StdState CHAR(2), StdZip CHAR(10), StdMajor CHAR(6), StdClass CHAR(6), StdGPA DECIMAL(3,2) ) Define table name, column names, and column data types Other clauses added later in the lecture Data type: - Set of values - Permissible values - Vary by DBMS - CHAR: fixed length character strings - VARCHAR: variable length character strings - DECIMAL: fixed precision numbers - Table 3-2 lists common data types 3-10
11
An GUI Example of Table Definition
10 Access relationship window 5 tables (student, enrollment, course, offering, faculty): faculty_1 is not a real table (details later) Relationships: lines connecting tables (faculty to offering); not all tables are directly connected Must define the tables and relationships before entering data and retrieving data Table definition window in MS Access 1.2 Features of DBMS 1-14
12
DEFAULT Option (SQL) Specify a default value for a column in an insert
operation. The default data type must match the column data type. CREATE TABLE hire_dates (id NUMBER(8), . . . hire_date DATE DEFAULT SYSDATE, ) DEFAULT Option When you define a table, you can specify that a column be given a default value by using the DEFAULT option. This option prevents null values from entering the columns if a row is inserted without a value for the column. The default value can be a literal, an expression, or a SQL function (such as SYSDATE or USER), but the value cannot be the name of another column or a pseudocolumn (such as NEXTVAL or CURRVAL). The default expression must match the data type of the column. Note: CURRVAL and NEXTVAL are explained later in this lesson. 3-11
13
Properties of Relational Tables (1/2)
The name of a table is distinct from all other table names in a database. Each cell of a table contains exactly one value. A table satisfying this property is said to be normalized or in first normal form. Storing several telephone numbers for a single department in a single cell is wrong DB usage. 3-12
14
Properties of Relational Tables (2/2)
Each column has a distinct name within a table. Data values of a column are all from the same data type. Order of columns has no significance. Order of records has no significance. Each record is distinct. There are no duplicate records. 3-13
15
Connections Among Tables
To understand a relational database, relationships among tables must be understood. The rows in a table are usually related to rows in other tables. Matching values (對應的值) indicate relationships between tables.
16
Relationships Among Tables
Table Name: Student StdSSN StdLastName StdMajor StdClass StdGPA WELLS IS FR 3.00 NORBERT FIN JR 2.70 KENDALL ACCT 3.50 Table Name: Enrollment(選課) OfferNo StdSSN EnrGrade 1234 3.3 3.5 4321 3.2 Table Name: Offering (開課) OfferNo CourseNo OffLocation OffTime OffDays 1111 IS320 BLM302 10:30 AM MW 1234 TTH 3333 BLM214 8:30 AM 4321 3:30 PM 4444 5678 IS480 如何知道學生WELLS所選課程的上課時間與地點 ? 如何知道週二早上10:30在教室BLM302上課的學生名字 ?
17
Connections among Tables
matching values matching values Shown by matching values - First Student row ( ) related to 1st and 3rd rows of Enrollment table - First Offering row (1234) related to 1st two rows of Enrollment table Combine tables using matching values Relational databases can have many tables (hundreds) Follow matching values to combine tables: - Combine Student and Enrollment where StdSSN matches - Join operation 3-16
18
Alternative Terminology (替代的術語)
Table-oriented Set-oriented Record-oriented Table Relation (關聯) Record-type, file Row Tuple (值組) Record Column Attribute (屬性) Field End Users Academic Researchers Information System Professionals Table-oriented: familiar Set-oriented: mathematical Record-oriented: IS staff Terminology is often mixed: table, record, field 3-17
19
Alternative Terminology
Column Attribute Field Relational Database Terminology A relational database can contain one or many tables. A table is the basic storage structure in an RDBMS. A table holds all the data necessary about something in the real world, such as employees, invoices, or customers. The slide shows the contents of the EMPLOYEES table or relation. The following terms are illustrated in the slide: 1. A single row or table representing all data required for a particular employee. Each row in a table must be identified by a primary key, which allows no duplicate rows. The order of rows is insignificant; specify the row order when the data is retrieved. 2. A column or attribute containing the employee number. The employee number identifies a unique employee in the EMPLOYEES table. In this example, the EMPLOYEE_ID column containing the employee number is designated as the primary key. A primary key must contain a value, and the value must be unique. 3. A column that is not a key value. A column represents one kind of data in a table; in the example, the salary of all the employees. Column order is insignificant when storing data; specify the column order when the data is retrieved. Row Tuple Record 3-18
20
Relational Keys (關聯索引、鍵)
Superkey (超索引、鍵) Candidate key(候選索引、鍵) Primary key(主索引、鍵) Alternate key(替代索引、鍵) Foreign key(外索引、鍵) 3-19
21
Superkey (超索引) A column, or a set of columns, that can uniquely identify a record within a table. Note that each record in a table must be unique in a relational database. May contain additional columns that are not necessary for unique identification We are only interested in the ones with minimum number of columns necessary for unique identification for a table. 3-20
22
Candidate Key (候選索引) Enrollment A minimal superkey
A superkey that contains only the minimum number of columns necessary for unique identification in a table. Uniqueness(唯一性) For each record, the value of a candidate key uniquely identifies the record Irreducibility(不可減少性) If removing any column from a candidate key makes it no longer unique Enrollment 3-21
23
Candidate Key (候選索引鍵) There may be many candidate keys for a table.
Since a table has no duplicate records, it’s always possible to identify each record uniquely. When a key consists of more than one column, we call it a composite key(複合索引). 3-22
24
Primary Key vs. Alternate Key ( 主索引 vs. 替代索引)
One of the candidate keys Selected to identify records uniquely for a table. A table always has a primary key in a relational database. The Worst Case: The entire set of columns serve as the primary key Alternate Key Other candidate key(s) that is(are) not selected to be the primary key 3-23
25
Rules to Select Primary Key
A candidate key with a minimal set of attributes Always have values Less likely to have its values changed (or never changed) Less likely to lose uniqueness in the future With fewest characters (for textual attribute) With the smallest maximum value (for numerical attribute) Easiest to use from the users’ point of view 3-24
26
Foreign Key (外索引鍵) Enrollment
A column, or a set of columns, in a table that matches (對應到) the candidate key of some (possibly the same) table. When a column appears in more than one table, its appearance represents a relationship between records of the two tables A foreign key must have the same data type as its associated (相對應的) candidate key Enrollment 資料表紀錄間的關係是透過某些稱為外鍵 (Foreign Key) 的欄位來表示,外鍵必須參考到某一個資料表的主鍵(Primary Key) 3-25
27
Relating Multiple Tables
Each row of a table is uniquely identified by a primary key (PK). We can logically relate data from multiple tables using foreign keys (FK). Relating Multiple Tables Each table contains data that describes exactly one entity. For example, the EMPLOYEES table contains information about employees. Because data about different entities is stored in different tables, you may need to combine two or more tables to answer a particular question. For example, you may want to know the location of the department where an employee works. In this scenario, you need information from the EMPLOYEES table (which contains data about employees) and the DEPARTMENTS table (which contains information about departments). With an RDBMS, you can relate the data in one table to the data in another by using the foreign keys. A foreign key is a column or a set of columns that refer to a primary key in the same table or another table. You can use the ability to relate data in one table to data in another to organize information in separate, manageable units. Employee data can be kept logically distinct from department data by storing it in a separate table. Enrollment 3-26
28
Relating Two Tables 機構分行資料表 Robert的經理是誰 ? 有哪些員工在Seattle上班 ? 員工資料表 3-27
29
Integrity Rules (完整性規則)
Constraints that must be obeyed in a database Entity Integrity(EI, 個體的完整性) Referential Integrity(RI, 參照的完整性) Other rules Null Business Rules
30
Null (空值、虛值) Denotes the absence of data for a column and is not the same as zero or spaces Represents a value for a column, that is currently unknown or is not applicable for a record. Deals with incomplete or exceptional data. Examples A branch temporarily without a manager 3-29
31
Examples of Null (Business Rules)
… NOT NULL Constraint The NOT NULL constraint ensures that the column contains no null values. Columns without the NOT NULL constraint can contain null values by default. NOT NULL constraints must be defined at the column level. No row can contain a null value for this column. (business rule) No row can contain a null value for this column. (business rule) Any row can contain a null value for this column. (business rule) 3-30
32
Entity Integrity Rule Entity integrity:
Each table must have a column or combination of columns with unique values (PK is unique). Unique means that no two rows of a table can have a same value. Ensures that entities are uniquely identified in a table. Ensures entities are easily traceable in a table. No null values in any part of a primary key Entity integrity rule: each table must have a primary key Referential integrity: foreign keys are valid references except when null 3-31
33
Application Examples of Entity Integrity
DEPARTMENTS PRIMARY KEY … PRIMARY KEY Constraint A PRIMARY KEY constraint creates a primary key for the table. Only one primary key can be created for each table. The PRIMARY KEY constraint is a column or set of columns that uniquely identifies each row in a table. This constraint enforces uniqueness of the column or column combination and ensures that no column that is part of the primary key can contain a null value. Note: Because uniqueness is part of the primary key constraint definition, the Oracle server enforces the uniqueness by implicitly creating a unique index on the primary key column or columns. Not allowed (DEPARTMENT is null value) : R4 INSERT (a new department) case 1 : case 2 : Not allowed (DEPARTMENT 50 already exists) : R2 3-32
34
Referential Integrity Constraints
The FK column values in one table must match the values of some column (CK) in its related table(s). Ensures that the related tables of a database contains valid connections. Only two kinds of values can be stored in a foreign key A value matching a candidate key value in some row of a related table. A null value. But, it is not common for foreign keys to have null values. In CREATE TABLE statement of SQL, foreign key is associated with primary key only, not with alternate key. 參考完整限制指的是紀錄裡的外鍵值,如果不是空值,則該值必須存在於其所參考的資料表之主鍵值裡 3-33
35
Referential Integrity Constraints
PRIMARY KEY DEPARTMENTS 90 FOREIGN KEY EMPLOYEES FOREIGN KEY Constraint The FOREIGN KEY (or referential integrity) constraint designates a column or combination of columns as a foreign key and establishes a relationship between a primary key or a unique key in the same table or a different table. In the example in the slide, DEPARTMENT_ID has been defined as the foreign key in the EMPLOYEES table (dependent or child table); it references the DEPARTMENT_ID column of the DEPARTMENTS table (the referenced or parent table). Guidelines A foreign key value must match an existing value in the parent table or be NULL. Foreign keys are based on data values and are purely logical, rather than physical, pointers. Not allowed (9 does not exist) : R1 INSERT (a new employee) case 1: case 2: Allowed 3-34
36
1-M (One-to-Many) Relationships
Connections are from the PK to FK. Parent table: the PK table Child table: the FK table a 1-M Relationship Child Table (開課資料表) Visual representation is easier to comprehend than CREATE TABLE statements 1 and symbols: - 1-M relationships - Student can have many enrollments - Student is the parent (1) table - Enrollment is the child (M) table - Foreign key is shown near the symbol Meaning of the Faculty_1 table - Access representation for a self referencing relationship - Faculty_1 is not a real table (placeholder for self referencing relationship) one many Parent Table (課程資料表) 3-36
37
M-N (Many-to-Many) Relationships
Rows of two table are related to multiple rows of each other. Not directly represented in the relational model Represented by Two 1-M relationships and An associative table (關聯組合資料表) Example: The linking table Enrollment and its relationship with Student and Offering tables represents an M-N Relationship between the Student and Offering tables Example: - Student and Offering tables - Student can take many offerings - Offering can have many enrolled students - Enrollment table and 1-M relationships represent this M-N relationship 3-37
38
Graphical Representation of M-N Relationships
The associative table Enrollment and its relationships with Student and Offering represents an M-N relationship between Student and Offering Associative Table a 1-M Relationship a 1-M Relationship
39
作業 下一週第一堂上課時交 HW#1 第三章 69~70頁 Questions: 3, 4, 5, 8, 13, 14
共交三題 : #1-1、#1-2、#1-3 #1-1.作業題號mapping是你的學號除以6的商 #1-2.作業題號mapping是你的學號除以6的餘數 #1-3.任選一題(不可與#1-1, #1-2重複) 作業題號mapping如下 : 0). 3 1). 4 2). 5 3). 8 4). 13 5). 14
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.