1 Data Model

In this section it is explained how to develop web applications through an object creation, which can be transaction forms or lists.

It is used following data model:

First of all, it is needed to copy a SQL script of data base creation (BD):

Copy
-- ************************************************************************
-- ARTIST
-- ************************************************************************
CREATE TABLE artist (
    artistid SERIAL NOT NULL,
    name CHAR(120)
);
ALTER TABLE artist LOCK MODE (ROW);

CREATE UNIQUE INDEX p_artist ON artist(artistid);
ALTER TABLE artist
    ADD CONSTRAINT
    PRIMARY KEY (artistid)
    CONSTRAINT p_artist;


-- ************************************************************************
-- ALBUM
-- ************************************************************************
CREATE TABLE album (
    albumid serial not null,
    title char(160) not null,
    artistid integer not null,
    state char(1) default 'A' not null
);
ALTER TABLE album LOCK MODE (ROW);

CREATE UNIQUE INDEX p_album ON album(albumid);
ALTER TABLE album
    ADD CONSTRAINT
    PRIMARY KEY (albumid)
    CONSTRAINT p_album;

CREATE INDEX f_album1 ON album(artistid);
ALTER TABLE album
    ADD CONSTRAINT FOREIGN KEY (artistid)
    REFERENCES artist(artistid)
    CONSTRAINT f_album1;
    

-- **************************************************************************
-- ALBUM IMAGES
-- **************************************************************************
CREATE TABLE album_images (
    albumid integer not null,
    album_img blob not null,
    img_type varchar(40),
    img_size int
);
ALTER TABLE album_images LOCK MODE (ROW);

CREATE UNIQUE INDEX p_album_images ON album_images(albumid);
ALTER TABLE album_images
    ADD CONSTRAINT 
    PRIMARY KEY (albumid)
    CONSTRAINT p_album_images;

ALTER TABLE album_images
    ADD CONSTRAINT  FOREIGN KEY (albumid)
    REFERENCES album(albumid)
    CONSTRAINT f_album_images1;    
 
 
-- ************************************************************************
-- MEDIATYPE
-- ************************************************************************
CREATE TABLE mediatype (
    mediatypeid SERIAL NOT NULL,
    name CHAR(120)
);
ALTER TABLE mediatype LOCK MODE (ROW);

CREATE UNIQUE INDEX p_mediatype ON mediatype(mediatypeid);
ALTER TABLE mediatype
    ADD CONSTRAINT
    PRIMARY KEY (mediatypeid)
    CONSTRAINT p_mediatype;


-- ************************************************************************
-- GENRE
-- ************************************************************************
CREATE TABLE genre (
    genreid SERIAL NOT NULL,
    name VARCHAR(120)
);
ALTER TABLE genre LOCK MODE (ROW);

CREATE UNIQUE INDEX p_genre ON genre(genreid);
ALTER TABLE genre
    ADD CONSTRAINT
    PRIMARY KEY (genreid)
    CONSTRAINT p_genre;


-- ************************************************************************
-- EMPLOYEE
-- ************************************************************************
CREATE TABLE employee (
    employeeid SERIAL NOT NULL,
    lastname CHAR(20) NOT NULL,
    firstname CHAR(20) NOT NULL,
    title CHAR(30),
    reportsto integer,
    birthdate DATE,
    hiredate DATE,
    address CHAR(70),
    city CHAR(40),
    state CHAR(40),
    country CHAR(40),
    postalCode CHAR(10),
    phone CHAR(24),
    fax CHAR(24),
    email CHAR(60)
);
ALTER TABLE employee LOCK MODE (ROW);

CREATE UNIQUE INDEX p_employee ON employee(employeeid);
ALTER TABLE employee
    ADD CONSTRAINT
    PRIMARY KEY (employeeid)
    CONSTRAINT p_employee;

CREATE INDEX f_employee1 ON employee(reportsto);
ALTER TABLE employee
    ADD CONSTRAINT FOREIGN KEY (reportsto)
    REFERENCES employee(employeeid)
    CONSTRAINT f_employee1;


-- ************************************************************************
-- CUSTOMER
-- ************************************************************************
CREATE TABLE customer (
    customerid SERIAL NOT NULL,
    firstname CHAR(40) NOT NULL,
    lastname CHAR(20) NOT NULL,
    company CHAR(80),
    address CHAR(70),
    city CHAR(40),
    state CHAR(40),
    country CHAR(40),
    postalcode CHAR(10),
    phone CHAR(24),
    fax CHAR(24),
    email CHAR(60) NOT NULL,
    supportrepid INTEGER NOT NULL,
    gps_lon decimal(14,10),
    gps_lat decimal(14,10),
    status char(1) default 'A' not null
);
ALTER TABLE customer LOCK MODE (ROW);

CREATE UNIQUE INDEX p_customer ON customer(customerid);
ALTER TABLE customer
    ADD CONSTRAINT
    PRIMARY KEY (customerid)
    CONSTRAINT p_customer;

CREATE INDEX f_supportrep1 ON customer(supportrepid);
ALTER TABLE customer
    ADD CONSTRAINT FOREIGN KEY (supportrepid)
    REFERENCES employee(employeeid)
    CONSTRAINT f_supportrep1;


-- ************************************************************************
-- INVOICE
-- ************************************************************************
CREATE TABLE invoice (
    invoiceid SERIAL NOT NULL,
    customerid INTEGER NOT NULL,
    invoicedate DATE NOT NULL,
    billingaddress CHAR(70),
    billingcity CHAR(40),
    billingstate CHAR(40),
    billingcountry CHAR(40),
    billingpostalcode CHAR(10),
    total DECIMAL(10,2) NOT NULL
);
ALTER TABLE invoice LOCK MODE (ROW);

CREATE UNIQUE INDEX p_invoice ON invoice(invoiceid);
ALTER TABLE invoice
    ADD CONSTRAINT
    PRIMARY KEY (invoiceid)
    CONSTRAINT p_invoice;

CREATE INDEX f_invoice1 ON invoice(customerid);
ALTER TABLE invoice
    ADD CONSTRAINT FOREIGN KEY (customerid)
    REFERENCES customer(customerid)
    CONSTRAINT f_invoice1;


-- ************************************************************************
-- TRACK
-- ************************************************************************
CREATE TABLE track (
    trackid serial not null,
    name char(200) not null,
    codean char(15) not null,
    albumid integer,
    mediatypeid integer not null,
    genreid integer,
    composer char(220),
    miliseconds integer not null,
    bytes integer,
    unitprice decimal(10,2) not null,
    costprice decimal(10,2) default 0 not null
);
ALTER TABLE track LOCK MODE (ROW);

CREATE UNIQUE INDEX p_track ON track(trackid);
ALTER TABLE track
    ADD CONSTRAINT 
    PRIMARY KEY (trackid)
    CONSTRAINT p_track;

CREATE  INDEX f_track1 ON track(albumid);
ALTER TABLE track
    ADD CONSTRAINT  FOREIGN KEY (albumid)
    REFERENCES album(albumid)
    CONSTRAINT f_track1;

CREATE  INDEX f_track2 ON track(genreid);
ALTER TABLE track
    ADD CONSTRAINT  FOREIGN KEY (genreid)
    REFERENCES genre(genreid)
    CONSTRAINT f_track2;

CREATE  INDEX f_track3 ON track(mediatypeid);
ALTER TABLE track
    ADD CONSTRAINT  FOREIGN KEY (mediatypeid)
    REFERENCES mediatype(mediatypeid)
    CONSTRAINT f_track3;


-- ************************************************************************
-- INVOICELINE
-- ************************************************************************
CREATE TABLE invoiceline (
    invoicelineid SERIAL NOT NULL,
    invoiceid INTEGER NOT NULL,
    trackid INTEGER NOT NULL,
    unitprice DECIMAL(10,2) NOT NULL,
    quantity SMALLINT NOT NULL
);
ALTER TABLE invoiceline LOCK MODE (ROW);

CREATE UNIQUE INDEX p_invoiceline ON invoiceline(invoicelineid);
ALTER TABLE invoiceline
    ADD CONSTRAINT
    PRIMARY KEY (invoicelineid)
    CONSTRAINT p_invoiceline;

CREATE INDEX f_invoiceline1 ON invoiceline(invoiceid);
ALTER TABLE invoiceline
    ADD CONSTRAINT FOREIGN KEY (invoiceid)
    REFERENCES invoice(invoiceid)
    CONSTRAINT f_invoiceline1;

CREATE INDEX f_invoiceline2 ON invoiceline(trackid);
ALTER TABLE invoiceline
    ADD CONSTRAINT FOREIGN KEY (trackid)
    REFERENCES track(trackid)
    CONSTRAINT f_invoiceline2;


-- ************************************************************************
-- PLAYLIST
-- ************************************************************************
CREATE TABLE playlist (
    playlistid SERIAL NOT NULL,
    name CHAR(120)
);
ALTER TABLE playlist LOCK MODE (ROW);

CREATE UNIQUE INDEX p_playlist ON playlist(playlistid);
ALTER TABLE playlist
    ADD CONSTRAINT
    PRIMARY KEY (playlistid)
    CONSTRAINT p_playlist;


-- ************************************************************************
-- PLAYLISTTRACK
-- ************************************************************************
CREATE TABLE playlisttrack (
    playlistid INTEGER NOT NULL,
    trackid INTEGER NOT NULL
);
ALTER TABLE playlisttrack LOCK MODE (ROW);

CREATE UNIQUE INDEX p_playlisttrack ON playlisttrack(playlistid,trackid);
ALTER TABLE playlisttrack
    ADD CONSTRAINT
    PRIMARY KEY (playlistid,trackid)
    CONSTRAINT p_playlisttrack;

CREATE INDEX f_playlisttrack1 ON playlisttrack(playlistid);
ALTER TABLE playlisttrack
    ADD CONSTRAINT FOREIGN KEY (playlistid)
    REFERENCES playlist(playlistid)
    CONSTRAINT f_playlisttrack1;

CREATE INDEX f_playlisttrack2 ON playlisttrack(trackid);
ALTER TABLE playlisttrack
    ADD CONSTRAINT FOREIGN KEY (trackid)
    REFERENCES track(trackid)
    CONSTRAINT f_playlisttrack2;
Copy
/*******************************************************************************
   Chinook Database - Version 1.4
   Script: Chinook_PostgreSql.sql
   Description: Creates and populates the Chinook database.
   DB Server: PostgreSql
   Author: Luis Rocha
   License: http://www.codeplex.com/ChinookDatabase/license
********************************************************************************/


/*******************************************************************************
   Create Tables
********************************************************************************/
CREATE TABLE "Album"
(
    "AlbumId" INT NOT NULL,
    "Title" VARCHAR(160) NOT NULL,
    "ArtistId" INT NOT NULL,
    CONSTRAINT "PK_Album" PRIMARY KEY  ("AlbumId")
);

CREATE TABLE "Artist"
(
    "ArtistId" INT NOT NULL,
    "Name" VARCHAR(120),
    CONSTRAINT "PK_Artist" PRIMARY KEY  ("ArtistId")
);

CREATE TABLE "Customer"
(
    "CustomerId" INT NOT NULL,
    "FirstName" VARCHAR(40) NOT NULL,
    "LastName" VARCHAR(20) NOT NULL,
    "Company" VARCHAR(80),
    "Address" VARCHAR(70),
    "City" VARCHAR(40),
    "State" VARCHAR(40),
    "Country" VARCHAR(40),
    "PostalCode" VARCHAR(10),
    "Phone" VARCHAR(24),
    "Fax" VARCHAR(24),
    "Email" VARCHAR(60) NOT NULL,
    "SupportRepId" INT,
    CONSTRAINT "PK_Customer" PRIMARY KEY  ("CustomerId")
);

CREATE TABLE "Employee"
(
    "EmployeeId" INT NOT NULL,
    "LastName" VARCHAR(20) NOT NULL,
    "FirstName" VARCHAR(20) NOT NULL,
    "Title" VARCHAR(30),
    "ReportsTo" INT,
    "BirthDate" TIMESTAMP,
    "HireDate" TIMESTAMP,
    "Address" VARCHAR(70),
    "City" VARCHAR(40),
    "State" VARCHAR(40),
    "Country" VARCHAR(40),
    "PostalCode" VARCHAR(10),
    "Phone" VARCHAR(24),
    "Fax" VARCHAR(24),
    "Email" VARCHAR(60),
    CONSTRAINT "PK_Employee" PRIMARY KEY  ("EmployeeId")
);

CREATE TABLE "Genre"
(
    "GenreId" INT NOT NULL,
    "Name" VARCHAR(120),
    CONSTRAINT "PK_Genre" PRIMARY KEY  ("GenreId")
);

CREATE TABLE "Invoice"
(
    "InvoiceId" INT NOT NULL,
    "CustomerId" INT NOT NULL,
    "InvoiceDate" TIMESTAMP NOT NULL,
    "BillingAddress" VARCHAR(70),
    "BillingCity" VARCHAR(40),
    "BillingState" VARCHAR(40),
    "BillingCountry" VARCHAR(40),
    "BillingPostalCode" VARCHAR(10),
    "Total" NUMERIC(10,2) NOT NULL,
    CONSTRAINT "PK_Invoice" PRIMARY KEY  ("InvoiceId")
);

CREATE TABLE "InvoiceLine"
(
    "InvoiceLineId" INT NOT NULL,
    "InvoiceId" INT NOT NULL,
    "TrackId" INT NOT NULL,
    "UnitPrice" NUMERIC(10,2) NOT NULL,
    "Quantity" INT NOT NULL,
    CONSTRAINT "PK_InvoiceLine" PRIMARY KEY  ("InvoiceLineId")
);

CREATE TABLE "MediaType"
(
    "MediaTypeId" INT NOT NULL,
    "Name" VARCHAR(120),
    CONSTRAINT "PK_MediaType" PRIMARY KEY  ("MediaTypeId")
);

CREATE TABLE "Playlist"
(
    "PlaylistId" INT NOT NULL,
    "Name" VARCHAR(120),
    CONSTRAINT "PK_Playlist" PRIMARY KEY  ("PlaylistId")
);

CREATE TABLE "PlaylistTrack"
(
    "PlaylistId" INT NOT NULL,
    "TrackId" INT NOT NULL,
    CONSTRAINT "PK_PlaylistTrack" PRIMARY KEY  ("PlaylistId", "TrackId")
);

CREATE TABLE "Track"
(
    "TrackId" INT NOT NULL,
    "Name" VARCHAR(200) NOT NULL,
    "AlbumId" INT,
    "MediaTypeId" INT NOT NULL,
    "GenreId" INT,
    "Composer" VARCHAR(220),
    "Milliseconds" INT NOT NULL,
    "Bytes" INT,
    "UnitPrice" NUMERIC(10,2) NOT NULL,
    CONSTRAINT "PK_Track" PRIMARY KEY  ("TrackId")
);



/*******************************************************************************
   Create Primary Key Unique Indexes
********************************************************************************/

/*******************************************************************************
   Create Foreign Keys
********************************************************************************/
ALTER TABLE "Album" ADD CONSTRAINT "FK_AlbumArtistId"
    FOREIGN KEY ("ArtistId") REFERENCES "Artist" ("ArtistId") ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX "IFK_AlbumArtistId" ON "Album" ("ArtistId");

ALTER TABLE "Customer" ADD CONSTRAINT "FK_CustomerSupportRepId"
    FOREIGN KEY ("SupportRepId") REFERENCES "Employee" ("EmployeeId") ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX "IFK_CustomerSupportRepId" ON "Customer" ("SupportRepId");

ALTER TABLE "Employee" ADD CONSTRAINT "FK_EmployeeReportsTo"
    FOREIGN KEY ("ReportsTo") REFERENCES "Employee" ("EmployeeId") ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX "IFK_EmployeeReportsTo" ON "Employee" ("ReportsTo");

ALTER TABLE "Invoice" ADD CONSTRAINT "FK_InvoiceCustomerId"
    FOREIGN KEY ("CustomerId") REFERENCES "Customer" ("CustomerId") ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX "IFK_InvoiceCustomerId" ON "Invoice" ("CustomerId");

ALTER TABLE "InvoiceLine" ADD CONSTRAINT "FK_InvoiceLineInvoiceId"
    FOREIGN KEY ("InvoiceId") REFERENCES "Invoice" ("InvoiceId") ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX "IFK_InvoiceLineInvoiceId" ON "InvoiceLine" ("InvoiceId");

ALTER TABLE "InvoiceLine" ADD CONSTRAINT "FK_InvoiceLineTrackId"
    FOREIGN KEY ("TrackId") REFERENCES "Track" ("TrackId") ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX "IFK_InvoiceLineTrackId" ON "InvoiceLine" ("TrackId");

ALTER TABLE "PlaylistTrack" ADD CONSTRAINT "FK_PlaylistTrackPlaylistId"
    FOREIGN KEY ("PlaylistId") REFERENCES "Playlist" ("PlaylistId") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "PlaylistTrack" ADD CONSTRAINT "FK_PlaylistTrackTrackId"
    FOREIGN KEY ("TrackId") REFERENCES "Track" ("TrackId") ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX "IFK_PlaylistTrackTrackId" ON "PlaylistTrack" ("TrackId");

ALTER TABLE "Track" ADD CONSTRAINT "FK_TrackAlbumId"
    FOREIGN KEY ("AlbumId") REFERENCES "Album" ("AlbumId") ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX "IFK_TrackAlbumId" ON "Track" ("AlbumId");

ALTER TABLE "Track" ADD CONSTRAINT "FK_TrackGenreId"
    FOREIGN KEY ("GenreId") REFERENCES "Genre" ("GenreId") ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX "IFK_TrackGenreId" ON "Track" ("GenreId");

ALTER TABLE "Track" ADD CONSTRAINT "FK_TrackMediaTypeId"
    FOREIGN KEY ("MediaTypeId") REFERENCES "MediaType" ("MediaTypeId") ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE INDEX "IFK_TrackMediaTypeId" ON "Track" ("MediaTypeId");