Skip to main content

Task

Instead of creating the database tables via the database editor (see chapter Database Connection), try to create them via Liquibase.

tip

You can find helpful information in chapter Database Tables in the generic part of the documentation.


Solution

In your project's folder "alias", navigate to folder "Data_alias" and create a (sub-)folder named "carpool", via Data_alias' context menu ("New" > "New Folder...).

Place the following 3 XML files ("changesets") in the new folder, and reference them in you top-level changelog.xml.

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">
<changeSet author="j.smith" id="23533445-0d3d-499c-aa98-cf37ca4798c1">
<createTable tableName="CAR">
<column name="CARID" type="CHAR(36)">
<constraints primaryKey="true" primaryKeyName="PK_CAR_CARID"/>
</column>
<column name="COLOR" type="VARCHAR(36)"/>
<column name="LICENSEPLATENUMBER" type="NVARCHAR(20)"/>
<column name="MANUFACTUREDATE" type="DATE"/>
<column name="MANUFACTURER" type="VARCHAR(36)"/>
<column name="PICTURE" type="LONGBLOB"/>
<column name="PRICE" type="DECIMAL(10,2)"/>
<column name="CURRENCY" type="VARCHAR(36)"/>
<column name="TYPE" type="NVARCHAR(30)"/>
</createTable>
</changeSet>
</databaseChangeLog>
----

create_car.xml

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">
<changeSet author="j.smith" id="64fd2d43-8c77-42d4-b349-4ebcd3a45037">
<createTable tableName="CARDRIVER">
<column name="CARDRIVERID" type="CHAR(36)">
<constraints primaryKey="true" primaryKeyName="PK_CARDRIVER_CARDRIVERID"/>
</column>
<column name="CONTACT_ID" type="CHAR(36)"/>
<column name="DRIVINGLICENSENUMBER" type="NVARCHAR(30)"/>
<column name="DRIVINGLICENSEISSUEDATE" type="DATE"/>
</createTable>
</changeSet>
</databaseChangeLog>
----

create_cardriver.xml

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">
<changeSet author="j.smith" id="45e21347-53cd-48e1-9667-591f3506e9e5">
<createTable tableName="CARRESERVATION">
<column name="CARRESERVATIONID" type="CHAR(36)">
<constraints primaryKey="true" primaryKeyName="PK_CARRESERVATION_CARRESERVATIONID"/>
</column>
<column name="CAR_ID" type="CHAR(36)"/>
<column name="CARDRIVER_ID" type="CHAR(36)"/>
<column name="STARTDATE" type="DATETIME"/>
<column name="ENDDATE" type="DATETIME"/>
<column name="MILEAGERETURN" type="INT"/>
<column name="PARKINGTICKETFINE" type="DECIMAL(7,2)"/>
<column name="SPEEDINGFINE" type="DECIMAL(7,2)"/>
<column name="CURRENCY" type="VARCHAR(36)"/>
<column name="DAMAGE" type="NVARCHAR(300)"/>
</createTable>

<!--Index for speeding up searches / join for CAR_ID, e.g. when searching data by a specific car -->
<createIndex indexName="IDX_CAR_ID" tableName="CARRESERVATION">
<column name="CAR_ID"/>
</createIndex>
<!--Index for speeding up searches / join for CARDRIVER_ID, e.g. when searching for a spcific driver -->
<createIndex indexName="IDX_CARDRIVER_ID" tableName="CARRESERVATION">
<column name="CARDRIVER_ID"/>
</createIndex>
<!--Compound index for speeding up joins over CAR_ID and CARDRIVER_ID -->
<createIndex indexName="IDX_CAR_ID_CARDRIVER_ID" tableName="CARRESERVATION">
<column name="CAR_ID"/>
<column name="CARDRIVER_ID"/>
</createIndex>
</changeSet>
</databaseChangeLog>
----

create_carreservation.xml

note

Please note that the latter Liquibase file also includes 3 indices: One for each foreign key (speeding up searches for the respective UID), as well as a compound index, which speeds up SQL JOINs over CAR_ID and CARDRIVER_ID.

Liquibase update

As soon as you have created and placed the above 3 XML files and linked them in your changelog.xml you can execute a Liquibase update: Data_alias > RMB: Liquibase > Update...

note

If you, at a later time, execute "Drop All & Update", make sure that, in the update dialog, checkbox "example" is checked, because, e.g., demo data of Context "Contact" (PERSON) are required for being referenced in Context "CarDriver".

You can now check, if all tables have been created correctly, using ADITO's database editor.

The car pool related database tables and columns should appear as follows:

Carpool Database Tables

The carpool-related database tables and their columns

important

Make sure that you named database tables and columns exactly as shown above. Otherwise, some code snippets of this documentation might not work.