07 Computer Applications M3B: Relational Databases and Working with SQL (1) M3B: Relational Databases and Working with SQL (1) Basic Concepts of a Relational Database Softcopy of handouts can be found at http://www.clsmss.edu.hk/~whlau/07CA/ 1. Relational database model a) Entity: An entity is something that has a distinct, separate existence, though it need not be a material existence. It can be a person, a place, a device, an event or a concept. b) Relation: Relation is a named two-dimensional table, containing a set of named attribute (columns) and an arbitrary number of unnamed tuple (rows). c) Attribute: An attribute is a named column of a relation. Each attribute in an relation must have a different name. d) Domain: Data domain is the set of possible values for a given attribute. It involves data type, size (length), and the allowable values. e) Each tuple (row) of a relation corresponds to a record that contains attribute values for a single entity. f) The order of tuples in a relation is not important. Ref: http://en.wikipedia.org/wiki/Database http://en.wikipedia.org/wiki/Relational_database http://en.wikipedia.org/wiki/Entity 2. Keys: a) A key is a kind of constraint which requires that the object, or critical information about the object, isn’t duplicated. b) Candidate key: A candidate key is set of attributes such that at all times there are no two distinct tuples with the same values for these attributes and there is no proper subset of the attributes. c) Compound key: A compound key (also called a composite key or concatenated key) is a key that consists of 2 or more attributes. d) Primary key: A primary key is a value that can be used to identify a unique row in a table. It is always chosen from one of the candidate key in an relation. e) Note that in most cases, primary key should be immutable, meaning that its value should not be changed during the course of normal operations of the database. This avoids the problem of dangling references or orphan records created by other relations referring to a tuple whose primary key has changed. f) Foreign key: A foreign key (FK) is a reference to a key in another table. Usually a foreign key in one table refers to the primary key (PK) of another table. This way references can be made to link information together and it is an essential part of database normalization. Ref: http://en.wikipedia.org/wiki/Primary_key http://en.wikipedia.org/wiki/Compound_key [06.09.20] CLSMSS/0607/S7CA/M3B(01) P. 1/2 07 Computer Applications M3B: Relational Databases and Working with SQL (1) http://en.wikipedia.org/wiki/Candidate_key http://en.wikipedia.org/wiki/Foreign_key 3. Table Relationships a) One-to-one relationship: Example, student to locker. Each student is assigned a locker, whilst each locker belongs to one student only. b) One-to-many relationship: Example, department to employees. One department has many employees, but each employee is assigned to one department only. c) Many-to-many relationship: Example, customers to products. A customer may order many different products, and each product may be ordered by different customers. An intermediary table (e.g. order table) is needed to hold the relationship. 4. Constraints: a) Key constraint: All tuples (records) in a relation must be distinct. No two tuples can have the same combination of values for all their attributes. b) Entity integrity constraint: No primary key can be NULL. c) Domain integrity constraint: Value of each attribute must be an atomic value from the domain for that attribute. E.g. Age: integer, 17 is acceptable but 17.4 is not. d) Referential integrity constraint: Referential integrity is consistency between related tables. It is usually enforced by the combination of a primary key or candidate key and a foreign key. i. The relational database management system (RDBMS) enforces referential integrity, normally either by deleting the foreign key rows as well to maintain integrity, or by returning an error and not performing the delete. ii. For example, the department number in employee table is a foreign key referring to the key field in the department table. Deleting the record of a department will break referential integrity. Ref: http://en.wikipedia.org/wiki/Referential_integrity 5. Students should be able to identify these basic elements in examples taken from everyday applications 6. Other references: http://www.cbe.wwu.edu/ganz/mis421web/421chap06.ppt http://www.cs.nthu.edu.tw/~yishin/Courses/CS6710/Handout/Session3%20-%20Relational%20Model.pdf http://joung.im.ntu.edu.tw/teaching/hospital_DB/2002/tables.ppt (Chinese) [06.09.20] CLSMSS/0607/S7CA/M3B(01) P. 2/2