Presentation is loading. Please wait.

Presentation is loading. Please wait.

VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation Mapping RDB Schema to.

Similar presentations


Presentation on theme: "VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation Mapping RDB Schema to."— Presentation transcript:

1 VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation Mapping RDB Schema to XML DTD Lecturer : Dr. Pavle Mogin

2 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 1 Plan For Mapping RDB To XML Motives for mapping a relational schema to an XML DTD Basic principles Selecting “top” elements Mapping tables Mapping columns A mapping algorithm Some examples –Reading: Lee D., Mani M., Chu W. “Schema Conversion Methods between XML and Relational Models”, Knowledge Transformation for the Semantic Web, B. Omelayenko, M. Klein, IOS Press, Amsterdam, 2003

3 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 2 Motives Although XML became an industry standard for data exchange on the World Wide Web, the most of the operational business data is yet stored in relational database systems That situation is likely to remain the same in the foreseeable future Accordingly, some mechanisms are needed to publish relational data in the form of XML documents

4 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 3 Motives To publish relational as XML data, we need: –To extract data from a relational database, –To structure data into a hierarchy, and –To perform tagging While a relational database bears data and information about tag names, a DTD bears information about the structure of XML documents If the DTD is not known in advance, the structure of the target XML document has to be established first by a relational (RDB) schema to XML DTD or XML Schema mapping We suppose the relational to XML mapping is needed

5 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 4 Basic Principles of RM to XML Mapping Relational database schema name is mapped to a complex element that represents the root element Each table is mapped to a complex XML element A table that does not reference any other table within the schema is mapped to an element reference in the content model of the root element Table columns (except foreign keys) are mapped either to simple elements in the content model of their table element, or to attributes Referential integrity constraints carry information about the parent-child relationships of a DTD

6 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 5 Root Element An XML document can have only one root element To satisfy this requirement the mapping algorithm maps a database schema name to a DTD root element This way a corresponding XML document will have a unique root element even in the case when a database schema contains a set of unrelated tables Also, if the database schema contains only one table the corresponding XML document will have a unique root element

7 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 6 Single Table To XML Student StudIdName 007James 131Susan 555Susan 007 James 131 Susan 555 Susan  Students Database

8 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 7 Set Of Unrelated Tables To XML Student StudIdName 007James 555Susan 007 James … c302 DBSys …  Course CourIdTitle C302DBSys M114Math StudAndCour Database

9 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 8 Top Tables We refer to tables that map in the content model of the root element as top tables Top tables are: –Tables that do not have any foreign key, –Tables whose all foreign keys are nullable, and –If tables T 1,…, T n make a circular structure using referential integrity constraints T 1 [X 1 ]  T 2 [Y 2 ],…, T n [X n ]  T 1 [Y 1 ], and no one of tables T 1,…, T n references any other table, then one of them should be pronounced a top table

10 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 9 Mapping Tables and Columns Each table T is mapped to a complex element T and its columns are mapped into its content model Each column A, except columns belonging to a foreign key, of a table T is mapped into either a simple element A and a reference to it in the content model of T, or to an attribute with the name A, whose owner is T Foreign key columns are not mapped into content model of an owner element, since they represent properties of the owner element

11 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 10 Mapping Columns In principle, table columns are mapped into simple elements Exceptions to the rule above could be: –A column having CHECK constraint that can be represented as a DTD attribute of the choice type, and –Columns having a default value that could be represented as DTD attributes These columns could map to DTD attributes, but we shall not do it All database data types map to #PCDATA, or CDATA,

12 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 11 Repetition Rates The repetition rates of elements in the content model of a complex element are determined by applying the following rules: NOT NULL columns are mapped to elements with default cardinality, or to attributes having #REQUIRED default, Nullable columns are mapped to “ ? ” elements, or attributes having #IMPLIED as a default

13 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 12 Mapping Referencing Tables An element P representing a referencing table P is a (complex) child of an element Q representing referenced table Q The table P is mapped either as an element in the content model of the element Q, or an attribute of the IDREF type is assigned to the element P and an attribute of the ID type to the element Q The referencing table P may reference more than one table Q To avoid redundancy element P may belong to the content model of at most one element Q So, only one referential integrity constraint with a NOT NULL foreign key is mapped by placing the element P in the content model of the element Q, all the other referential integrities are mapped using ID/IDREF attributes All referential integrity constraints having nullable foreign keys are mapped using ID/IDREF attributes

14 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 13 Nullable Foreign Keys If any column of a foreign key value is nullable that fact conveys the information that the parent instance may be not known If a table with a nullable foreign key were mapped as an element in the content model of the parent element, information contained in a tuple with null valued foreign key can not be contained in an XML document since it has no parent If the relationship between a table with the null valued foreign key and the referenced table is mapped using ID/IDREF attributes, the loss of information is avoided

15 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 14 Rules for Mapping Referencing Tables Foreign Key One of RI ConstraintsAll Other RI Constraints not null unique not null null unique null

16 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 15 Ref_Int Graph A Ref_Int graph G = (V, E ) consists of a node set V and a set of directed edges E For each table T k in a relational schema S, there is a node V k  V, and for each distinct referential integrity T k [X ]  T m [Y ], there exist an edge E mk  E Orientation of an edge is from the referenced node T m to the referencing node T k

17 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 16 Use of Ref_Int Graph The Ref_Int graph is traversed in the mapping process using Breadth-First Search (BFS) to reach referencing tables of each table and to include them as children in the content model of the table The BFS starts from each top table

18 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 17 RDB Schema Classes CREATE TABLE Class (classId int PRIMARY KEY, name varchar NOT NULL, no_of_std varchar); CREATE TABLE Lecturer(classId int NOT NULL REFERENCES Class, LecId int PRIMARY KEY, lecname varchar NOT NULL); CREATE TABLE Auditor(classId int REFERENCES Class, AudId int PRIMARY KEY, audname varchar NOT NULL); CREATE TABLE Student (classId int NOT NULL REFERENCES Class, StudId int PRIMARY KEY, studname varchar NOT NULL, no_of_pts varchar, origin varchar NOT NULL); CREATE TABLE Assignment (StudId int NOT NULL REFERENCES Student, AssId int PRIMARY KEY, no int NOT NULL, marks decimal (4,2) NOT NULL);

19 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 18 Ref_Int Graph of the Classes Schema Class Lecturer Auditor Student Assignment Legend : NOT NULL foreign key nullabel foreign key

20 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 19 RDB Schema To DTD Mapping Algorithm (1) Input: A RDB Schema S Output: A DTD 1.Create a DTD root element with the name S 2.Identify top tables and put them in a set T and put the corresponding elements in the content model of the root element S 3.Set U =  (U is the set of mapped tables) 4.Map each table P from the relational schema S into a complex element P 5.Construct a Ref_Int graph for the schema S

21 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 20 RDB Schema To DTD Mapping Algorithm (2) 6.Do while T   i.Select a table Q from T and set T = T \ {Q } ii.Put the non foreign key columns of the table Q into the content model of the element Q iii.Set U = U  {Q } iv.Perform a BFS in the Ref_Int graph starting from the table Q For each referential integrity constraint P m [X ]  P n [Y ] found in BFS: a.If P m  U and (  A  X )(A “is not null”), then: Put the non foreign key columns of the table P m into the content model of the element P m Add the element P m into the content model of the element P n Set U = U  {P m } b.If P m  U or (  A  X )(A “is nullable”), then associate an attribute of the ID type with the element P n (unless the ID attribute already exists) and an attribute of the IDREF type with the element P m

22 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 21 RDB Schema To DTD Mapping Algorithm (3) 7.Set repetition rates of elements and default values of attributes according to the repetition rules for columns and mapping rules for referencing tables 8.Insert #PCDATA declarations for simple elements

23 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 22 Mapping Classes Schema (Steps 1-5) Step 1 Step 2 Step 3 Step 4 –<! ELEMENT Class () –

24 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 23 Mapping Classes Schema (Step 6) <!ELEMENT Class (classId, name, no_of_stud, Lecturer, Student)>

25 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 24 Mapping Classes Schema (Step 6) <!ELEMENT Student (StudId, studname, no_of_pts, origin, Assignment)> <!- -U = {Class, Lecturer, Auditor, Student, Assignment}- ->

26 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 25 Mapping Classes Schema (Steps 7&8) <! ELEMENT Class (classId, name, no_of_stud?, Lecturer*, Student*) >

27 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 26 Mapping Classes (Steps 7&8) <! ELEMENT Student (StudId, studname, no_of_pts?, origin, Assignment*) >

28 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 27 Mapping a M:N RDB Schema Schema Student_Course CREATE TABLE Course (CourseId char(7) PRIMARY KEY, Course_Name varchar); CREATE TABLE Grades (CourseId char(7) NOT NULL REFERENCES Course, StudentId int NOT NULL REFERENCES Student, Grade char(2), PRIMARY KEY (CourseId, StudentId)); CREATE TABLE Student (StudentId int PRIMARY KEY, Stud_Name varchar);

29 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 28 Ref_Int Graph of the Student_Course Grades Course Student Legend : NOT NULL foreign key nullabel foreign key

30 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 29 Mapping Student_Course (Steps 1 – 5) Step 1 Step 2 <!ELEMENT Student_Courses (Student, Course)> Step 3 Step 4

31 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 30 Mapping Student_Course Step (6)

32 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 31 Mapping Student_Course (Steps 7&8) <! ELEMENT Student (StudentId, Student_Name?, Grades*) >

33 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 32 Mapping a NULL Referential Integrity Schema Student_City CREATE TABLE City (CityId char(7) PRIMARY KEY, City_Name varchar NOT NULL); CREATE TABLE Student (StudentId int PRIMARY KEY, Stud_Name varchar, CityId REFERENCES City); CREATE TABLE Phone (PhoneNo varchar PRIMARY KEY, Phone_Type varchar, StudentId NOT NULL REFERENCES Student);

34 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 33 Ref_Int Graph of the Student_City Student City Legend : NOT NULL foreign key nullabel foreign key Phone

35 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 34 Mapping Student_City (Steps 1 – 5) Step 1 Step 2 Step 3 Step 4

36 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 35 Mapping Student_City Step (6)(1)

37 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 36 Mapping Student_City Step (6)(2) <!- -Phone[StudentIdId]  Student[StudentIdId], Phone  U - ->

38 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 37 Mapping Student_City (Steps 7&8)

39 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 38 Mapping Two NULL Referential Integrities Schema Student_City CREATE TABLE City (CityId char(7) PRIMARY KEY, City_Name varchar NOT NULL); CREATE TABLE Student (StudentId int PRIMARY KEY, Stud_Name varchar, CityId REFERENCES City); CREATE TABLE Phone (PhoneNo varchar PRIMARY KEY, Phone_Type varchar, StudentId REFERENCES Student);

40 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 39 Ref_Int Graph of the Student_City Student City Legend : NOT NULL foreign key nullabel foreign key Phone

41 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 40 Mapping Student_City (Steps 1 – 5) Step 1 Step 2 Step 3 Step 4

42 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 41 Mapping Student_City Step (6)(1)

43 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 42 Mapping Student_City Step (6)(2) <!- -Phone[StudentIdId]  Student[StudentIdId], nullable- -> <! ELEMENT Phone (PhoneNo, Phone_Type)

44 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 43 Mapping Student_City (Steps 7&8)

45 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 44 Mapping a Circular & Recursive Schema Schema Employee_Project CREATE TABLE Employee (EmployeeId smallint PRIMARY KEY, Emp_Name varchar NOT NULL, Manager smallint REFERENCES Employee (EmployeeId), ProjectId smallint NOT NULL REFERENCES Project); CREATE TABLE Project(ProjectId smallint PRIMARY KEY, Proj_Name varchar, Proj_Mgr NOT NULL UNIQUE REFERENCES Employee (EmployeeId);

46 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 45 Ref_Int Graph of the Employee_Project Employee Project Legend : NOT NULL foreign key nullabel foreign key Employee [Manager ]  Employee [EmployeeId ] Project [Proj_Mgr ]  Employee [EmployeeId ] Employee[ProjectId ]  Project [ProjectId ]

47 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 46 Mapping Employee_Project (Steps 1–5) Step 1 Step 2 Step 3 Step 4

48 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 47 Mapping Employee_Project (Step 6) <!- - Employee[ProjectId ]  Project [ProjectId ], Employee  U - ->

49 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 48 Mapping Employee_Project (Steps 7&8)

50 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 49 Some Remarks Semantics of and Employee [Manager ]  Employee [EmployeeId ] is “an emplyee has an optional manager, who is also an emplyee” Semantics of and Project [Proj_Mgr ]  Employee [EmployeeId ], UNIQUE, NOT NULL is “each project has exactly one manager who is an employee and an employee manages at most one project”

51 SWEN 432 Advanced Database Design and Implementation 2015 RelToXML Mapping 50 Summary Vast operational business data is stored in relational database systems To publish a database or part of it as an XML document, we need to infer the structure of the document first Relational to XML mapping maps: –Schema name to a root element –Tables to complex elements, –A referencing table either as a complex element in the content model of the referenced table or the referential integrity is represented as a pair of attributes of the ID/ IDREF types –Columns map to simple single elements, or attributes


Download ppt "VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation Mapping RDB Schema to."

Similar presentations


Ads by Google