From d9ec9187002c26275550a849325589e339c4e849 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Thu, 30 Oct 2014 17:21:45 +0100 Subject: [PATCH] Complete relational/Xsd airline schema --- Sda1/P/Relational/.project | 17 ++ Sda1/P/Relational/airline.xsd | 143 +++++++++++++ Sda1/P/Relational/flightSchedule.xml | 27 +++ Sda1/P/Relational/schema.sql | 43 ++-- Sda1/sda1.xml | 307 +++++++++++++++++++++++++-- 5 files changed, 501 insertions(+), 36 deletions(-) create mode 100644 Sda1/P/Relational/.project create mode 100644 Sda1/P/Relational/airline.xsd create mode 100644 Sda1/P/Relational/flightSchedule.xml diff --git a/Sda1/P/Relational/.project b/Sda1/P/Relational/.project new file mode 100644 index 000000000..df811b3b0 --- /dev/null +++ b/Sda1/P/Relational/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>Relational</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>com.oxygenxml.editor.xmlbuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>com.oxygenxml.editor.xmlnature</nature> + </natures> +</projectDescription> diff --git a/Sda1/P/Relational/airline.xsd b/Sda1/P/Relational/airline.xsd new file mode 100644 index 000000000..736524790 --- /dev/null +++ b/Sda1/P/Relational/airline.xsd @@ -0,0 +1,143 @@ +<?xml version="1.0" encoding="UTF-8"?> +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" + xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" + vc:minVersion="1.1"> + + <xs:simpleType name="ICAOAirportCode"> + <xs:restriction base="xs:string"> + <xs:length value="4" /> + <xs:pattern value="[A-Z09]+"></xs:pattern> + </xs:restriction> + </xs:simpleType> + + <xs:simpleType name="ICAOAirlineCode"> + <xs:restriction base="xs:string"> + <xs:length value="3"/> + <xs:pattern value="[A-Z]+"></xs:pattern> + </xs:restriction> + </xs:simpleType> + + <xs:element name="top"> + <xs:complexType> + <xs:sequence> + <xs:element ref="airlines"/> + <xs:element ref="destinations"/> + <xs:element ref="flights"/> + </xs:sequence> + </xs:complexType> + + <xs:keyref name="_FK_Flight_airline" refer="_PK_Airline_id"> + <xs:selector xpath="flights/flight"/> + <xs:field xpath="@airline"/> + </xs:keyref> + + <xs:keyref name="_FK_Flight_origin" refer="_PK_Destination_id"> + <xs:selector xpath="flights/flight"/> + <xs:field xpath="@origin"/> + </xs:keyref> + + <xs:keyref name="_FK_Flight_destination" refer="_PK_Destination_id"> + <xs:selector xpath="flights/flight"/> + <xs:field xpath="@destination"/> + </xs:keyref> + + </xs:element> + + <xs:element name="airlines"> + <xs:complexType> + <xs:sequence> + <xs:element ref="airline" minOccurs="0" maxOccurs="unbounded"/> + </xs:sequence> + </xs:complexType> + + <xs:key name="_PK_Airline_id"> + <xs:selector xpath="airline"/> + <xs:field xpath="@id"/> + </xs:key> + + <xs:key name="_UN_Airline_name"> + <xs:selector xpath="airline"/> + <xs:field xpath="name"/> + </xs:key> + + <xs:key name="_UN_Airline_airlineCode"> + <xs:selector xpath="airline"/> + <xs:field xpath="@airlineCode"/> + </xs:key> + </xs:element> + + <xs:element name="airline"> + <xs:complexType> + <xs:sequence> + <xs:element name="name" type="xs:string"/> + </xs:sequence> + <xs:attribute name="id" type="xs:int" use="required"/> + <xs:attribute name="airlineCode" type="ICAOAirlineCode" use="required"/> + </xs:complexType> + </xs:element> + + <xs:element name="destinations"> + <xs:complexType> + <xs:sequence> + <xs:element ref="destination" minOccurs="0" maxOccurs="unbounded"/> + </xs:sequence> + </xs:complexType> + + <xs:key name="_PK_Destination_id"> + <xs:selector xpath="destination"/> + <xs:field xpath="@id"/> + </xs:key> + + <xs:key name="_UN_Destination_airportCode"> + <xs:selector xpath="destination"/> + <xs:field xpath="@airportCode"/> + </xs:key> + </xs:element> + + <xs:element name="destination"> + <xs:complexType> + <xs:sequence> + <xs:element name="fullName"/> + </xs:sequence> + <xs:attribute name="id" type="xs:int"/> + <xs:attribute name="airportCode" type="ICAOAirportCode"/> + </xs:complexType> + </xs:element> + + <xs:element name="flights"> + <xs:complexType> + <xs:sequence> + <xs:element ref="flight" minOccurs="0" maxOccurs="unbounded"/> + </xs:sequence> + </xs:complexType> + + <xs:key name="_PK_Flight_id"> + <xs:selector xpath="flight"/> + <xs:field xpath="@id"/> + </xs:key> + + <xs:key name="_UN_Flight_flightNumber"> + <xs:selector xpath="flight"/> + <xs:field xpath="flightNumber"/> + </xs:key> + + </xs:element> + + <xs:element name="flight"> + <xs:complexType> + <xs:sequence> + <xs:element name="flightNumber" type="xs:string"/> + </xs:sequence> + <xs:attribute name="id" type="xs:int" use="required"/> + <xs:attribute name="airline" type="xs:int" use="required"/> + <xs:attribute name="origin" type="xs:int"/> + <xs:attribute name="destination" type="xs:int"/> + <xs:assert test="not(@origin = @destination)"> + <xs:annotation> + <xs:documentation>CHECK constraint _CK_Flight_origin_destination</xs:documentation> + </xs:annotation> + </xs:assert> + </xs:complexType> + </xs:element> + +</xs:schema> \ No newline at end of file diff --git a/Sda1/P/Relational/flightSchedule.xml b/Sda1/P/Relational/flightSchedule.xml new file mode 100644 index 000000000..7476b3965 --- /dev/null +++ b/Sda1/P/Relational/flightSchedule.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="UTF-8"?> +<top xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="airline.xsd"> + <airlines> + <airline airlineCode="DLH" id="1"> + <name>Lufthansa</name> + </airline> + <airline airlineCode="AFR" id="2"> + <name>Air France</name> + </airline> + </airlines> + <destinations> + <destination id="1" airportCode="EDDF"> + <fullName>Frankfurt International Airport – Frankfurt am Main</fullName> + </destination> + + <destination id="3" airportCode="EBCI"> + <fullName>Brussels South Charleroi Airport – Charleroi</fullName> + </destination> + </destinations> + + <flights> + <flight id="1" airline="2" origin="1" destination="3"> + <flightNumber>LH 4234</flightNumber> + </flight> + </flights> +</top> \ No newline at end of file diff --git a/Sda1/P/Relational/schema.sql b/Sda1/P/Relational/schema.sql index 953a940ef..36d4a5fb6 100644 --- a/Sda1/P/Relational/schema.sql +++ b/Sda1/P/Relational/schema.sql @@ -3,28 +3,43 @@ DROP TABLE IF EXISTS Flight; DROP TABLE IF EXISTS Destination; DROP TABLE IF EXISTS Airline; - CREATE Table Airline ( - id INT NOT NULL PRIMARY KEY - ,name CHAR(20) NOT NULL UNIQUE - ,airlineCode CHAR(5)NOT NULL UNIQUE + id INT NOT NULL + ,name CHAR(20) NOT NULL + ,airlineCode CHAR(5) NOT NULL + + ,CONSTRAINT _PK_Airline_id PRIMARY KEY(id) + ,CONSTRAINT _UN_Airline_name UNIQUE(name) + ,CONSTRAINT _UN_Airline_airlineCode UNIQUE(airlineCode) ); CREATE TABLE Destination ( - id INT NOT NULL PRIMARY KEY - ,fullName CHAR(20) NOT NULL - ,airportCode CHAR(20) NOT NULL UNIQUE + id INT NOT NULL + ,fullName CHAR(20) NOT NULL + ,airportCode CHAR(5) + + ,CONSTRAINT _PK_Destination_id PRIMARY KEY(id) + ,CONSTRAINT _UN_Destination_airportCode UNIQUE(airportCode) ); - CREATE TABLE Flight ( - id INT NOT NULL PRIMARY KEY - ,flightNumber CHAR(10) NOT NULL UNIQUE + id INT NOT NULL + ,flightNumber CHAR(10) NOT NULL ,airline INT NOT NULL REFERENCES Airline ,origin int NOT NULL REFERENCES Destination ,destination int NOT NULL REFERENCES Destination - ,CHECK(NOT(origin = destination)) -); - - + -- For yet unknown reasons the following alternative MySQL 5.1 syntax compatible + -- statements fail with message 'Cannot add foreign key constraint": + -- ,CONSTRAINT _FK_Flight_airline FOREIGN KEY(airline) REFERENCES Airline + -- ,CONSTRAINT _FK_Flight_origin FOREIGN KEY(origin) REFERENCES Destination + -- ,CONSTRAINT _FK_Flight_destination FOREIGN KEY(destination) REFERENCES Destination + + ,CONSTRAINT _PK_Flight_id UNIQUE(id) + ,CONSTRAINT _UN_Flight_flightNumber UNIQUE(flightNumber) + ,CONSTRAINT _CK_Flight_origin_destination CHECK(NOT(origin = destination)) +); + + + + \ No newline at end of file diff --git a/Sda1/sda1.xml b/Sda1/sda1.xml index fa0b632b2..305b7c179 100644 --- a/Sda1/sda1.xml +++ b/Sda1/sda1.xml @@ -652,45 +652,59 @@ drwxr-xr-x 4 goik fb1prof 4096 Nov 8 22:04 .. <listitem> <para>Constraint: origin and destination must differ. - Hint: Mysql provides a syntactical means to implement - this constraint. It will however not be enforced at - runtime. Database vendors like Oracle, IBM/DB2 support - this type of runtime integrity constraint - enforcement.</para> + Hint: <productname>Mysql</productname> provides a + syntactical means to implement this constraint. It will + however not be enforced at runtime. Database vendors + like Oracle, IBM/DB2, <productname>Sybase</productname>, + <productname>Informix</productname> + <abbrev>etc.</abbrev> support this type of runtime + integrity constraint enforcement.</para> </listitem> </itemizedlist> </listitem> </itemizedlist> - <para>Provide surrogate keys for all entities.</para> + <para>Provide surrogate keys for all entities and provide names + for all constraints (<abbrev>e.g.</abbrev> defining + <code>CONSTRAINT _PK_XYZ PRIMARY KEY(...)</code> etc. ).</para> </question> <answer> - <programlisting language="sql">DROP TABLE IF EXISTS Flight; -DROP TABLE IF EXISTS Destination; -DROP TABLE IF EXISTS Airline; - - -CREATE Table Airline ( - id INT NOT NULL PRIMARY KEY - ,name CHAR(20) NOT NULL UNIQUE - ,airlineCode CHAR(5)NOT NULL UNIQUE + <programlisting language="sql">CREATE Table Airline ( + id INT NOT NULL + ,name CHAR(20) NOT NULL + ,airlineCode CHAR(5) NOT NULL + + ,CONSTRAINT _PK_Airline_id PRIMARY KEY(id) + ,CONSTRAINT _UN_Airline_name UNIQUE(name) + ,CONSTRAINT _UN_Airline_airlineCode UNIQUE(airlineCode) ); CREATE TABLE Destination ( - id INT NOT NULL PRIMARY KEY - ,fullName CHAR(20) NOT NULL - ,airportCode CHAR(20) NOT NULL UNIQUE + id INT NOT NULL + ,fullName CHAR(20) NOT NULL + ,airportCode CHAR(5) + + ,CONSTRAINT _PK_Destination_id PRIMARY KEY(id) + ,CONSTRAINT _UN_Destination_airportCode UNIQUE(airportCode) ); - CREATE TABLE Flight ( - id INT NOT NULL PRIMARY KEY - ,flightNumber CHAR(10) NOT NULL UNIQUE + id INT NOT NULL + ,flightNumber CHAR(10) NOT NULL ,airline INT NOT NULL REFERENCES Airline ,origin int NOT NULL REFERENCES Destination ,destination int NOT NULL REFERENCES Destination - ,CHECK(NOT(origin = destination)) + + -- For yet unknown reasons the following alternative MySQL 5.1 syntax compatible + -- statements fail with message 'Cannot add foreign key constraint": + -- ,CONSTRAINT _FK_Flight_airline FOREIGN KEY(airline) REFERENCES Airline + -- ,CONSTRAINT _FK_Flight_origin FOREIGN KEY(origin) REFERENCES Destination + -- ,CONSTRAINT _FK_Flight_destination FOREIGN KEY(destination) REFERENCES Destination + + ,CONSTRAINT _PK_Flight_id UNIQUE(id) + ,CONSTRAINT _UN_Flight_flightNumber UNIQUE(flightNumber) + ,CONSTRAINT _CK_Flight_origin_destination CHECK(NOT(origin = destination)) );</programlisting> </answer> </qandaentry> @@ -2123,6 +2137,255 @@ public class Memo { </qandaset> </section> + <section xml:id="airlineXsd"> + <title>The airline example revisited</title> + + <qandaset defaultlabel="qanda" xml:id="qandaAirlineXsd"> + <title>Airline meta information by XML schema</title> + + <qandadiv> + <qandaentry> + <question> + <para>Transform the relational schema from <xref + linkend="airlineRelationalSchema"/> into an XML schema and + supply some test data. In particular consider the following + constraints:</para> + + <itemizedlist> + <listitem> + <para>Data types</para> + + <itemizedlist> + <listitem> + <para><link + xlink:href="http://en.wikipedia.org/wiki/List_of_airline_codes">ICAO + airline designator</link></para> + </listitem> + + <listitem> + <para><link + xlink:href="http://en.wikipedia.org/wiki/International_Civil_Aviation_Organization_airport_code">ICAO + airport code</link></para> + </listitem> + </itemizedlist> + </listitem> + + <listitem> + <para>Primary / Unique key definitions</para> + </listitem> + + <listitem> + <para>Foreign key definitions</para> + </listitem> + + <listitem> + <para>CHECK constraint: Your XML schema will require <tag + class="starttag">xs:assert test="..." </tag> and thus XML + schema version 1.1. You may want to read about + co-occurrence constraints as being described in <link + xlink:href="http://www.ibm.com/developerworks/library/x-xml11pt2">Listing + 6. Assertion on complex type - @height < + @width</link>.</para> + </listitem> + </itemizedlist> + + <para>The following XML example instance may guide you towards + an <filename>airline.xsd</filename> schema:</para> + + <programlisting language="none"><top xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="airline.xsd"> + <airlines> + <airline airlineCode="DLH" id="1"> + <name>Lufthansa</name> + </airline> + <airline airlineCode="AFR" id="2"> + <name>Air France</name> + </airline> + </airlines> + <destinations> + <destination id="1" airportCode="EDDF"> + <fullName>Frankfurt International Airport – Frankfurt am Main</fullName> + </destination> + + <destination id="3" airportCode="EBCI"> + <fullName>Brussels South Charleroi Airport – Charleroi</fullName> + </destination> + </destinations> + + <flights> + <flight id="1" airline="2" origin="1" destination="3"> + <flightNumber>LH 4234</flightNumber> + </flight> + </flights> +</top></programlisting> + + <para>Hints:</para> + + <itemizedlist> + <listitem> + <para>Identify all relational schema constraints from + solution of <xref linkend="airlineRelationalSchema"/> and + model them accordingly. </para> + </listitem> + + <listitem> + <para>The above example does not contain any constraint + violations. In order to test your schema for completeness + tinkering with primary key, unique and referencing + attribute values may be helpful.</para> + </listitem> + </itemizedlist> + </question> + + <answer> + <programlisting language="none"><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" + xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" + vc:minVersion="1.1"> + + <xs:simpleType name="ICAOAirportCode"> + <xs:restriction base="xs:string"> + <xs:length value="4" /> + <xs:pattern value="[A-Z09]+"></xs:pattern> + </xs:restriction> + </xs:simpleType> + + <xs:simpleType name="ICAOAirlineCode"> + <xs:restriction base="xs:string"> + <xs:length value="3"/> + <xs:pattern value="[A-Z]+"></xs:pattern> + </xs:restriction> + </xs:simpleType> + + <xs:element name="top"> + <xs:complexType> + <xs:sequence> + <xs:element ref="airlines"/> + <xs:element ref="destinations"/> + <xs:element ref="flights"/> + </xs:sequence> + </xs:complexType> + + <xs:keyref name="_FK_Flight_airline" refer="_PK_Airline_id"> + <xs:selector xpath="flights/flight"/> + <xs:field xpath="@airline"/> + </xs:keyref> + + <xs:keyref name="_FK_Flight_origin" refer="_PK_Destination_id"> + <xs:selector xpath="flights/flight"/> + <xs:field xpath="@origin"/> + </xs:keyref> + + <xs:keyref name="_FK_Flight_destination" refer="_PK_Destination_id"> + <xs:selector xpath="flights/flight"/> + <xs:field xpath="@destination"/> + </xs:keyref> + + </xs:element> + + <xs:element name="airlines"> + <xs:complexType> + <xs:sequence> + <xs:element ref="airline" minOccurs="0" maxOccurs="unbounded"/> + </xs:sequence> + </xs:complexType> + + <xs:key name="_PK_Airline_id"> + <xs:selector xpath="airline"/> + <xs:field xpath="@id"/> + </xs:key> + + <xs:key name="_UN_Airline_name"> + <xs:selector xpath="airline"/> + <xs:field xpath="name"/> + </xs:key> + + <xs:key name="_UN_Airline_airlineCode"> + <xs:selector xpath="airline"/> + <xs:field xpath="@airlineCode"/> + </xs:key> + </xs:element> + + <xs:element name="airline"> + <xs:complexType> + <xs:sequence> + <xs:element name="name" type="xs:string"/> + </xs:sequence> + <xs:attribute name="id" type="xs:int" use="required"/> + <xs:attribute name="airlineCode" type="ICAOAirlineCode" use="required"/> + </xs:complexType> + </xs:element> + + <xs:element name="destinations"> + <xs:complexType> + <xs:sequence> + <xs:element ref="destination" minOccurs="0" maxOccurs="unbounded"/> + </xs:sequence> + </xs:complexType> + + <xs:key name="_PK_Destination_id"> + <xs:selector xpath="destination"/> + <xs:field xpath="@id"/> + </xs:key> + + <xs:key name="_UN_Destination_airportCode"> + <xs:selector xpath="destination"/> + <xs:field xpath="@airportCode"/> + </xs:key> + </xs:element> + + <xs:element name="destination"> + <xs:complexType> + <xs:sequence> + <xs:element name="fullName"/> + </xs:sequence> + <xs:attribute name="id" type="xs:int"/> + <xs:attribute name="airportCode" type="ICAOAirportCode"/> + </xs:complexType> + </xs:element> + + <xs:element name="flights"> + <xs:complexType> + <xs:sequence> + <xs:element ref="flight" minOccurs="0" maxOccurs="unbounded"/> + </xs:sequence> + </xs:complexType> + + <xs:key name="_PK_Flight_id"> + <xs:selector xpath="flight"/> + <xs:field xpath="@id"/> + </xs:key> + + <xs:key name="_UN_Flight_flightNumber"> + <xs:selector xpath="flight"/> + <xs:field xpath="flightNumber"/> + </xs:key> + + </xs:element> + + <xs:element name="flight"> + <xs:complexType> + <xs:sequence> + <xs:element name="flightNumber" type="xs:string"/> + </xs:sequence> + <xs:attribute name="id" type="xs:int" use="required"/> + <xs:attribute name="airline" type="xs:int" use="required"/> + <xs:attribute name="origin" type="xs:int"/> + <xs:attribute name="destination" type="xs:int"/> + <xs:assert test="not(@origin = @destination)"> + <xs:annotation> + <xs:documentation>CHECK constraint _CK_Flight_origin_destination</xs:documentation> + </xs:annotation> + </xs:assert> + </xs:complexType> + </xs:element> + +</xs:schema></programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> + </section> + <section xml:id="xmlAndJava"> <title>Relating <abbrev xlink:href="http://en.wikipedia.org/wiki/Document_Type_Declaration">schema</abbrev>'s -- GitLab