1 / 41

Data Models

Data Models . Amandeep Kaur Lecturer GPC Khunimajra Call at 9646229005 Email aman12484@gmail.com. Data Models. A Database models some portion of the real world. Data Model is link between user’s view of the world and bits stored in computer. Many models have been proposed.

aspen
Download Presentation

Data Models

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Models AmandeepKaur Lecturer GPC Khunimajra Call at 9646229005 Email aman12484@gmail.com

  2. Data Models • A Database models some portion of the real world. • Data Model is link between user’s view of the world and bits stored in computer. • Many models have been proposed. • We will concentrate on the Relational Model. • Student (sid: string, name: string, login: string, age: integer, gpa:real) 1010111101

  3. What is a Data Model? • A notation for describing data or information • Description consists of 3 parts: 1) Structure of the data -Arrays and structures or objects 2) Operations on the data -Queries (Operations that retrieve information) -Modifications (Operations that change the database) 3) Constraints on the data -Limitations

  4. A Brief History of Data Models • 1950s file systems, punched cards • 1960s hierarchical • IMS • 1970s network • CODASYL, IDMS • 1980s relational • INGRES, ORACLE, DB2, Sybase • Paradox, dBase • 1990s object oriented and object relational • O2, GemStone, Ontos

  5. Relational Model • Sets • collections of items of the same type • no order • no duplicates • Mappings domain range 1:many many:1 1:1 many:many

  6. Describing Data: Data Models • A data modelis a collection of concepts for describing data. • Adatabase schemais a description of a particular collection of data, using a given data model. • The relational model of datais the most widely used model today. • Main concept: relation, basically a table with rows and columns. • Every relation has a schema, which describes the columns, or fields.

  7. Basics of the Relational Model • Represent data as a two-dimensional table called a relation

  8. The Relational Model • Based on tables • The structure portion resemble an array of structs in C • Column headers Field names • Each row values of struct in the array

  9. Relational Model • General • Simple • Flexible • Easy to query declaratively without programming • But..... • Good design essential • Integrity essential • Poor semantics • Relationships based on ‘value-matching’

  10. Informal guidelines • Semantics of the attributes • easy to explain relation • doesn’t mix concepts • Reducing the redundant values in tuples • Choosing attribute domains that are atomic • Reducing the null values in tuples • Disallowing spurious tuples

  11. Relational Model Notes • no duplicate tuples in a relation • a relation is a set of tuples • no ordering of tuples in a relation • a relation is a set • attributes of a relation have an implied ordering • but used as functions and referenced by name, not position • every tuple must have attribute values drawn from all of the domains of the relation or the special value NULL • all a domain’s values and hence attribute’s values are atomic.

  12. Comparative Terms Formal Oracle • NotationCourse (courseno, subject, equipment)Student(studno,name,hons)Enrol(studno,courseno,labmark) Relation schema Table description Relation Table Tuple Row Attribute Column Domain Value set

  13. Keys • SuperKey • a set of attributes whose values together uniquely identify a tuple in a relation • Candidate Key • a superkey for which no proper subset is a superkey…a key that is minimal . • Can be more than one for a relation • Primary Key • a candidate key chosen to be the main key for the relation. • One for each relation • Keys can be composite

  14. Foreign Key • a set of attributes in a relation that exactly matches a (primary) key in another relation • the names of the attributes don’t have to be the same but must be of the same domain • a foreign key in a relation A matching a primary key in a relation B represents a • many:one relationship between A and B Student(studno,name,tutor,year) Staff(lecturer,roomno,appraiser)

  15. Referential Integrity • Student(studno,name,tutor,year) • Staff(lecturer,roomno,appraiser) • CASCADE • delete all matching foreign key tuples eg. STUDENT • RESTRICT • can’t delete primary key tuple STAFF whilst a foreign key tuple STUDENT matches • NULLIFY • foreign key STUDENT.tutor set to null if the foreign key ids allowed to take on null

  16. Entity Integrity and Nulls No part of a key can be null • Attribute values • Atomic • Known domain • Sometimes can be null THREE categories of null values 1. Not applicable 2. Not known 3. Absent (not recorded) STUDENT

  17. Definitions • Cartesian Product The cartesian product () between n sets is the set of all possible combinations of the elements of those sets. • Domain set of all possible values for an attribute; for attribute A, the domain is represented as dom(A). A domain has a format and a base data type. • Relation Schema denoted by R(A1, A2, …, An), is made up of relation name R and list of attributes A1, A2, …, An. • Relation a subset of the cartesian product of its domains. Given a relation schema R, a relation on that schema r, a set of attributes A1..An for that relation then r(R) (dom(A1)  dom(A2)  ...  dom(An)) • Attribute a function on a domain foreach instance of the mapping or tuple • Attribute Value the result of the attribute function. Each instance of the mapping is represented by one attribute value drawn from each domain or a special NULL value. Given a tuple t and an attribute A for a relation r, t[A]--> a, where a is the attribute’s value for that tuple.

  18. (N)-tuple a set of (n) attribute-value pairs representing a single instance of a relation’s mapping between its domains. • Degree the number of attributes a relation has. • Cardinality a number of tuples a relation has. • Roles several attributes can have the same domain; the attributes indicate different roles in the relation. • Key (SuperKey) a set of attributes whose values together uniquely identify every tuple in a relation. Let t1 and t2 be two tuples on relation r of relation schema R, and sk be a set of attributes whose values are the key for the relation schema R, then t1[sk]  t2[sk]. • (Candidate) Key a (super)key that is minimal, i.e. has no proper subsets that still uniquely identify every tuple in a relation. There can be several for one relation. • Primary Key a candidate key chosen to be the main key for the relation. There is only one for each relation. • Foreign Key a candidate key of relation A situated in relation B. • Database a set of relations.

  19. Comparison of Modeling Approaches • Semistructured models have more flexibility than relations • Nevertheless, the relational model is still preferred • Because database are large, -efficiency of access to data -efficiency of modifications to data -ease of use • These all goals can be achieved with a model, particularly the relational model

  20. Comparison of Modeling Approaches • Relational model provides - A simple, limited approach to structuring data, yet is reasonably flexible so anything can be modeled - A limited, yet useful, collection of operations on data

  21. View 1 View 2 View 3 Conceptual Schema Physical Schema DB Users Levels of Abstraction • Views describe how users see the data. • Conceptual schema defines logical structure • Physical schema describes the files and indexes used. • (sometimes called the ANSI/SPARC model)

  22. View 1 View 2 View 3 Conceptual Schema Physical Schema DB Data Independence:The Big Breakthrough of the Relational Model • A Simple Idea: Applications should be insulated from how data is structured and stored. • Logical data independence: Protection from changes in logical structure of data. • Physical data independence: Protection from changes in physical structure of data. • Q: Why are these particularly important for DBMS?

  23. Why Study the Relational Model? • Most widely used model currently. • DB2, MySQL, Oracle, PostgreSQL, SQLServer, … • Note: some “Legacy systems” use older models • e.g., IBM’s IMS • Object-oriented concepts have recently merged in • object-relational model • Informix, IBM DB2, Oracle 8i • Early work done in POSTGRES research project at Berkeley • XML (semi-structured)models emerging?

  24. Relational Database: Definitions • Relational database:a set of relations. • Relation: made up of 2 parts: • Schema :specifiesname of relation, plus name and type of each column. • E.g. Students(sid: string, name: string, login: string, age: integer, gpa: real) • Instance : a table, with rows and columns. • #rows = cardinality • #fields = degree / arity • Can think of a relation as a setof rows or tuples. • i.e., all rows are distinct

  25. View 1 View 2 View 3 Conceptual Schema Physical Schema DB Example: University Database • Conceptual schema: • Students(sid: string, name: string, login: string, age: integer, gpa:real) • Courses(cid: string, cname:string, credits:integer) • Enrolled(sid:string, cid:string, grade:string) • External Schema (View): • Course_info(cid:string,enrollment:integer) • One possible Physical schema : • Relations stored as unordered files. • Index on first column of Students.

  26. Ex: An Instance of Students Relation Cardinality = 3, Arity = 5 All rows must be unique (set semantics) • Q: Do all values in each column of a relation instance have to be Unique? • Q: Is “Cardinality” a schema property? • Q: Is “Arity” a schema property?

  27. SQL - A language for Relational DBs • SQL (a.k.a. “Sequel”), • “Intergalactic Standard for Data” • Stands for Structured Query Language Two sub-languages: • Data Definition Language (DDL) • create, modify, delete relations • specify constraints • administer users, security, etc. • Data Manipulation Language (DML) • Specify queries to find tuples that satisfy criteria • add, modify, remove tuples

  28. SQL Overview • CREATE TABLE <name> ( <field> <domain>, … ) • INSERT INTO <name> (<field names>) VALUES (<field values>) • DELETE FROM <name> WHERE <condition> • UPDATE <name> SET <field name> = <value> WHERE <condition> • SELECT <fields> FROM <name> WHERE <condition>

  29. Creating Relations in SQL • Creates the Students relation. • Note: the type (domain) of each field is specified, and enforced by the DBMS whenever tuples are added or modified. CREATE TABLE Students (sid CHAR(20), name CHAR(20), login CHAR(10), age INTEGER, gpa FLOAT)

  30. Table Creation (continued) • Another example: the Enrolled table holds information about courses students take. CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2))

  31. Adding and Deleting Tuples INSERT INTO Students (sid, name, login, age, gpa) VALUES (‘53688’, ‘Smith’, ‘smith@ee’, 18, 3.2) • Can insert a single tuple using: • Can delete all tuples satisfying some condition (e.g., name = Smith): DELETE FROM Students S WHERE S.name = ‘Smith’ Powerful variants of these commands are available; more later!

  32. Keys • Keys are a way to associate tuples in different relations • Keys are one form of integrity constraint (IC) Enrolled Students PRIMARY Key FORIEGN Key

  33. Primary Keys • A set of fields is a superkey if: • No two distinct tuples can have same values in all key fields • A set of fields is a candidate keyfor a relation if : • It is a superkey • No subset of the fields is a superkey • what if >1 key for a relation? • one of the candidate keys is chosen (by DBA) to be the primary key. E.g. • sid is a key for Students. • What about name? • The set {sid, gpa} is a superkey.

  34. CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid), UNIQUE (cid, grade)) CREATE TABLE Enrolled (sid CHAR(20) cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid)) vs. Primary and Candidate Keys in SQL • Possibly many candidate keys(specified using UNIQUE), one of which is chosen as the primary key. • Keys must be used carefully! • “For a given student and course, there is a single grade.” “Students can take only one course, and no two students in a course receive the same grade.”

  35. Foreign Keys, Referential Integrity • Foreign key : Set of fields in one relation that is used to `refer’ to a tuple in another relation. • Must correspond to the primary key of the other relation. • Like a `logical pointer’. • If all foreign key constraints are enforced, referential integrity is achieved (i.e., no dangling references.)

  36. Enrolled Students 11111 English102 A Foreign Keys in SQL • E.g. Only students listed in the Students relation should be allowed to enroll for courses. • sid is a foreign key referring to Students: CREATE TABLE Enrolled (sid CHAR(20),cid CHAR(20),grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students )

  37. Enforcing Referential Integrity • Consider Students and Enrolled; sid in Enrolled is a foreign key that references Students. • What should be done if an Enrolled tuple with a non-existent student id is inserted? (Reject it!) • What should be done if a Students tuple is deleted? • Also delete all Enrolled tuples that refer to it? • Disallow deletion of a Students tuple that is referred to? • Set sid in Enrolled tuples that refer to it to a default sid? • (In SQL, also: Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown’ or `inapplicable’.) • Similar issues arise if primary key of Students tuple is updated.

  38. Integrity Constraints (ICs) • IC: condition that must be true for any instance of the database; e.g., domain constraints. • ICs are specified when schema is defined. • ICs are checked when relations are modified. • A legalinstance of a relation is one that satisfies all specified ICs. • DBMS should not allow illegal instances. • If the DBMS checks ICs, stored data is more faithful to real-world meaning. • Avoids data entry errors, too!

  39. Relational Model: Summary • A tabular representation of data. • Simple and intuitive, currently the most widely used • Object-relational variant gaining ground • Integrity constraints can be specified by the DBA, based on application semantics. DBMS checks for violations. • Two important ICs: primary and foreign keys • In addition, we always have domain constraints. • Powerful query languages exist. • SQL is the standard commercial one • DDL - Data Definition Language • DML - Data Manipulation Language

  40. Thanks

More Related