Commit b382032f authored by Dr. Martin Goik's avatar Dr. Martin Goik

Foreign key constraints, SQL syntax, CHAR --> INTEGER

parent d97c7dba
......@@ -7,6 +7,10 @@
<xs:element ref="product" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:key name="uniqueProductId">
<xs:selector xpath="product"/>
<xs:field xpath="@id"/>
</xs:key>
</xs:element>
<xs:element name="product">
......@@ -16,7 +20,7 @@
<xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="age" type="xs:int" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="id" type="xs:int" use="required"/>
</xs:complexType>
</xs:element>
......
......@@ -2,7 +2,7 @@ DROP TABLE IF EXISTS Description;
DROP TABLE IF EXISTS Product;
CREATE TABLE Product (
id CHAR(20) NOT NULL PRIMARY KEY
id INTEGER NOT NULL PRIMARY KEY
,name VARCHAR(255) NOT NULL
,age SMALLINT
);
......
......@@ -2,12 +2,12 @@
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Schema/catalog.xsd">
<product id="mpt">
<product id="17">
<name>Monkey Picked Tea</name>
<description>Rare wild Chinese tea</description>
<description>Picked only by specially trained monkeys</description>
</product>
<product id="instantTent">
<product id="42">
<name>4-Person Instant Tent</name>
<description>4-person, 1-room tent</description>
<description>Pre-attached tent poles</description>
......
DROP TABLE IF EXISTS Description;
DROP TABLE IF EXISTS Product;
CREATE TABLE Product (
id INTEGER NOT NULL PRIMARY KEY
,name VARCHAR(255) NOT NULL
,age SMALLINT
) ENGINE=InnoDB;
CREATE TABLE Description (
product INTEGER NOT NULL
,orderIndex int NOT NULL -- preserving the order of descriptions belonging to a given product
,text VARCHAR(255) NOT NULL
,UNIQUE(product, orderIndex)
,FOREIGN KEY(product) REFERENCES Product(id)
) ENGINE=InnoDB;
-- example data corresponding to products.xml --
-- Product lacking age property --
INSERT INTO Product (id, name) VALUES ('mpt', 'Monkey Picked Tea');
INSERT INTO Description VALUES('mpt', 0, 'Picked only by specially trained monkeys');
INSERT INTO Description VALUES('mpt', 1, 'Rare wild Chinese tea');
INSERT INTO Product VALUES ('instantTent', '4-Person Instant Tent', 15);
INSERT INTO Description VALUES('instantTent', 0, 'Exclusive WeatherTec system.');
INSERT INTO Description VALUES('instantTent', 1, '4-person, 1-room tent');
INSERT INTO Description VALUES('instantTent', 2, 'Pre-attached tent poles');
......@@ -29,7 +29,7 @@ public class DataInsert {
* @param productName See {@link #insertproduct(String, String, int)}
*/
public void insertproduct(final String productId, final String productName) {
final String sqlInsertStmt = "INSERT INTO Product (id, name) VALUES ('" + productId + "', '" + productName + "');";
final String sqlInsertStmt = "INSERT INTO Product (id, name) VALUES (" + productId + ", '" + productName + "');";
try {
stmt.executeUpdate(sqlInsertStmt);
} catch (SQLException e) {
......@@ -48,7 +48,7 @@ public class DataInsert {
// A PreparedStatement is preferable but not yet introduced in lecture
//
final String sqlInsertStmt = "INSERT INTO Product VALUES ('" + productId + "', '" + productName + "', " + age + ");";
final String sqlInsertStmt = "INSERT INTO Product VALUES (" + productId + ", '" + productName + "', " + age + ");";
try {
stmt.executeUpdate(sqlInsertStmt);
} catch (SQLException e) {
......
......@@ -7,6 +7,10 @@
<xs:element ref="product" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:key name="uniqueProductId">
<xs:selector xpath="product"/>
<xs:field xpath="@id"/>
</xs:key>
</xs:element>
<xs:element name="product">
......@@ -16,7 +20,7 @@
<xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="age" type="xs:int" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="id" type="xs:int" use="required"/>
</xs:complexType>
</xs:element>
......
......@@ -2,17 +2,18 @@ DROP TABLE IF EXISTS Description;
DROP TABLE IF EXISTS Product;
CREATE TABLE Product (
id CHAR(20) NOT NULL PRIMARY KEY
id INTEGER NOT NULL PRIMARY KEY
,name VARCHAR(255) NOT NULL
,age SMALLINT
);
) ENGINE=InnoDB;
CREATE TABLE Description (
product CHAR(20) NOT NULL REFERENCES Product
product INTEGER NOT NULL
,orderIndex int NOT NULL -- preserving the order of descriptions belonging to a given product
,text VARCHAR(255) NOT NULL
,UNIQUE(product, orderIndex)
);
,FOREIGN KEY(product) REFERENCES Product(id)
) ENGINE=InnoDB;
-- example data corresponding to products.xml --
......
......@@ -2,12 +2,12 @@
<catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Schema/catalog.xsd">
<product id="mpt">
<product id="17">
<name>Monkey Picked Tea</name>
<description>Rare wild Chinese tea</description>
<description>Picked only by specially trained monkeys</description>
</product>
<product id="instantTent">
<product id="42">
<name>4-Person Instant Tent</name>
<description>4-person, 1-room tent</description>
<description>Pre-attached tent poles</description>
......
......@@ -29,10 +29,10 @@ public class RdbmsAccess {
final ResultSet products = selectProduct.executeQuery();
while (products.next()) {
final String productId = products.getString("id");
final int productId = products.getInt("id");
final Element productElement = new Element("product");
root.addContent(productElement);
productElement.setAttribute("id", productId);
productElement.setAttribute("id", "" + productId);
final Element nameElement = new Element("name");
productElement.addContent(nameElement);
......@@ -50,8 +50,8 @@ public class RdbmsAccess {
products.close();
}
private void addDescriptions(final String productId, final Element productElement) throws SQLException {
selectDescription.setString(1, productId);
private void addDescriptions(final int productId, final Element productElement) throws SQLException {
selectDescription.setInt(1, productId);
final ResultSet descriptions = selectDescription.executeQuery();
while (descriptions.next()) {
final Element descriptionElement = new Element("description");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment