MArme ALane Phase5
MArme ALane Phase5
MArme ALane Phase5
DATABASE
CS342 Database Project – Fall 2015
1
3.1.3 Relation Normalization ........................................................................................................ 63
3.2 SQL *Plus ............................................................................................................................ 64
3.2.1 Main Purpose....................................................................................................................... 64
3.2.2 Oracle Schema Objects ........................................................................................................ 64
3.3 Relation Schema and Data .................................................................................................... 67
3.3.1 Contract ............................................................................................................................... 67
3.3.2 Artist .................................................................................................................................... 67
3.3.3 Composed_Of ...................................................................................................................... 68
3.3.4 Member ............................................................................................................................... 69
3.3.5 Album .................................................................................................................................. 70
3.3.6 Sold_Through....................................................................................................................... 71
3.3.7 Transaction .......................................................................................................................... 72
3.3.8 Buyer.................................................................................................................................... 72
3.3.9 Song ..................................................................................................................................... 73
3.3.10 Studio .................................................................................................................................. 75
3.3.11 Writer .................................................................................................................................. 76
3.4 SQL Queries ........................................................................................................................ 77
Phase 4: Stored Procedures, Packages, and Triggers ............................................................................ 83
4.1 Oracle PL/SQL ..................................................................................................................... 83
4.1.1 What is PL/SQL? ................................................................................................................... 83
4.1.2 PL/SQL Syntax ...................................................................................................................... 84
4.2 MS SQL Server and MySQL Stored Procedures ........................................................................ 87
4.2.1 Microsoft SQL Server and T-SQL .......................................................................................... 87
4.2.2 MySQL Server Routines........................................................................................................ 89
4.3 PL/SQL Subprogram Implementations.................................................................................... 90
4.3.1 Procedures ........................................................................................................................... 90
4.3.2 Triggers ................................................................................................................................ 94
Phase 5: Graphical User Interface ...................................................................................................... 97
5.1 User Groups ........................................................................................................................ 97
5.1.1 Executive Assistants ............................................................................................................. 97
5.1.2 Artists................................................................................................................................... 98
5.2 GUI Design and Development in Java ..................................................................................... 99
5.3 Major Features ................................................................................................................... 101
2
5.3.1 Connecting to the Oracle Database ................................................................................... 101
5.3.2 Inserting into Database ...................................................................................................... 102
5.3.3 Row Filtering For Searching ............................................................................................... 103
5.3.4 Assigning Studios to Albums .............................................................................................. 104
5.3.5 Assigning Writers to Songs ................................................................................................ 106
5.3.6 Generated Reports ............................................................................................................ 107
5.4 Outcome ........................................................................................................................... 109
3
Phase 1: Fact-Finding, Information Gathering, and
Conceptual Database Design
The imaginary company our database will be created for is for a record company called
Armenlane Records. Record companies, also known as “Record Labels”, are companies that hire
musicians and artists by contract to record albums. These contracts have certain parameters,
such as having a contract that is for a specific number of albums. After the albums are recorded,
Armenlane Records releases the albums by selling them to major retailer distribution centers,
such as Wal-Mart, Target, or Kmart. The main type of music our company focuses on is rock
music and other similar types, or genres, of music. This company rents out a small number of
independently-owned recording studios for each album to be recorded at. Each song also has a
songwriter.
For fact-finding, we used the internet and popular search websites in order for us to find
and use information for our project. To find more specific information on record companies, we
researched a few modern record companies, as well as legal information on contracts and the
We were able to get into contact with Asthmatic Kitty Records, which is a small
independent record company based in the United States. We emailed them for information on
4
data that is stored for their company. They weren’t that specific in the information they
provided, but they gave us the general idea of what data they store in their databases, such as
We were also able to get into contact with a musician who has recorded music at actual
studios and he was able to give us more information on what information is stored, such as
From all of our research, we discovered that the music business is a very complex
business. Every company’s database may be different but they are probably very complex due to
how complex the music business is, so for the purpose of this project, we will keep things simple.
Contracts can be very complicated, especially when it comes to financial information and
income from music sales. But upon researching, we found that contracts often involve terms
where an artist will have only a certain number of albums they can record under their current
contract. We also discovered that record companies don’t actually own recording studios
We also used the basic, and common structure of artists creating albums, which all have
at least one song, with each song having a writer. Entities were easy to discover once we laid out
The reports that would need to be generated are numerous, but we came to identify a
few important ones. For example, general artist and album information reports for all artists
hired by the company would need to be generated, as well as financial reports and studio usage
reports would be generated. To generate a report, the user will specify what items they would
5
like to see. For example, they could see all of the songs from a specific album, or view all the
Record companies can be very complicated, especially when dealing with financial
information. So for this software, we decided to focus more on music part of the record
company. Rather than covering the entire business, we will only focus on the following: The
artist and their contract information, the album and their songs, studios and songwriters, as well
Keeping the main focus on artists and their music allows us to keep the project simple.
For example, if we wanted to view information for an artist who is hired by contract by the
company, we would include information based only on their role in music-making. But by
including some financial information, we are able to make a more complete database.
Our time in lecture helped us develop a more solid approach to how an entity should be
created. During the lab period, we watched a few groups present their ideas for their proposed
database. During each presentation, we discussed how each idea could possibly be improved, or
if there was in issue with the current model. Using this experience, we tried to improve our own
following entities: Contracts, Artists, Members, Albums, Songs, Studios, Writers, Transactions,
and Buyers.
6
The Contract entity represent the conditions by which an Artist is hired by the record
company. The Artist entity represents the group or individual that is hired by the record
company under a contract with a certain number of albums as their terms. Each artist will have
For example, if a band, called The Beatles, is hired by the company, and the band has four
members, John, Paul, George, and Ringo, the Artist entity would contain an entry for The
Beatles, but the Member entity would contain four entries, one for each member of The Beatles.
Another example is if a single artist, called Bob Dylan, is hired by the company, then the Artist
entity would contain a record for Bob Dylan and the Member entity would also contain just one
Each Artist will have albums that they have recorded. Each of these Albums will have a
Studio where it was recorded. Each Album will be sold to major retailer distribution centers. Each
of these transactions will kept track of in the Transaction entity. Each album will also have a set
All of these entities combine to create a database of hired artists without getting too
complex for this quarter’s project. It is a very general database, containing general artist and
7
1.2 Conceptual Database Design
having their own terms and a date when the contract started and
Attributes:
contract_ID: Internal ID for organizing artist contracts
o String; no nulls; unique; single-valued and simple.
8
Artist – Strong Entity
o string with any value; nulls allowed; not unique; single-valued and simple.
9
Member – Strong Entity
Artists may have more than one member as part of their group, or
they may just be a single person as the only member of the artist. For this
Attributes:
ssn: - Social Security Number unique to each person
o String with length of 9 characters; no nulls; unique.
o string with length of 10 chars; nulls allowed; not unique; single-valued and simple.
10
o string; nulls not allowed; not unique; single-valued and simple.
o Date type; null not allowed; not unique; single-valued and simple.
o Date type that is after starting date; nulls allowed; not unique. Single-valued and
simple.
11
Album – Strong Entity
Attributes:
12
Song - Strong Entity
Attributes:
13
Studio – Strong Entity
studios for artists to use. In order to keep track of where each song
was recorded and how much money was spent in recording this a
Attributes:
14
state: Specific state where studio is located
o date that takes place after open_date; nulls allowed; not unique; single-valued
and simple.
15
Writer – Strong Entity
compose lyrics or tracks for the artists to sing or use. A writer can
Attributes:
16
city: City where writer lives
o date that takes place after start_date nulls allowed; not unique; single-valued and
simple.
17
Transaction – Strong Entity
18
Buyer – Strong Entity
required.
Attributes:
19
1.2.2 Relationship Set Description
This relationship links each artist to the contract by which they were hired. Each artist
has one contract, and each contract has one artist, so it is a One to One relationship. This
This relationship links each artist to the members to make up that artist. Single artists
will have only member in the member entity, and bands will have more than one member in the
member entity.
Using the same example from earlier, if the band The Beatles were hired, there would
be a single entry in the Artist entity for “The Beatles”, but there would be four entries for the
band members John, Paul, George, and Ringo, in the Member entity.
However, this relationship is a Many to Many relationship with any number of artists
having any number of members and any number of members have any number artists. So a
member can be a part of more than one group or single artist. This relationship utilizes the Artist
This relationship links each artist to all the albums they have recorded. It is a One to
Many relationship with one artist having any number of albums and any number of albums
20
having one artist. This relationship utilizes the Artist and Album entities.
This relationship links each album recorded to all of the songs that are on that album. It
is a One to Many relationship with one album have any number of songs and any number of
songs having one album. This relationship utilizes the Album and Song entities.
This relationship links each album to the transactions the recording company makes in
order to sell an album to a major retailer. It is a Many to Many relationship with any number of
albums having any number of transactions, and any number of transactions having any number
This relationship links each transaction to the buyer who purchased the batch of
albums. It is a Many to One relationship, with any number of transactions being purchased by a
single buyer, and one buyer having any number of transactions. This relationship utilizes the
This relationship links each album to the recording studio it was recorded at. It is a
Many to One relationship with any number of albums being recorded at one studio and one
21
studio having any number of albums. This relationship utilizes the Album and Studio entities.
This relationship links each song to the writer who wrote the song. It is a Many to One
relationship with any number of songs written by one writer and one writer writing any number
Generalization is when entities are grouped together to show a more general view. With
generalization, we bring together multiple entities into one entity to create a larger entity based
entities are divided into sub classes based on their traits. You can take an entity such as artist,
and split it into subclasses like Band or Solo. These are both very similar to the entity Artist.
We thought about possibly keeping Bands and Solo artists separate with two distinct
entities, but they have very similar attributes. So we decided to generalize them into a single
entity called Artist. What will eventually separate the two separate ideas is the number of
members this entity is related to in the Member entity. An artist with one entry in the member
entity will be a solo artist, and an artist with more than one entry in the member entity will be
22
1.2.4 E-R Diagram
An E-R model, or entity-relationship model, is a visualization of entities and how they are
related to other entities. This includes the relationships between entities and their cardinalities.
Referring back to all previous information and development, we created the following E-R
Model for this database. It is a very basic and high level understanding of what our company
would require for this database. This is tentative, as this design may change as we progress with
this project. But, we can now use this model to create a relational database.
23
Phase 2: From E-R Model to Relational Model
E-R Model
The Entity-Relationship model was created by Peter Chen, a Computer Science and
Applied Mathematics graduate from Harvard. Chen created the model to create a formal
The E-R model has a few major features that assist in the development and visualization
process of creating a database. E-R modeling allows the developer to create a graphical
representation of how their database will look. Creating Entities which hold the place of real-life
conceptual or physical objects with independent existence, a data model can be created by
adding attributes to the Entity in order to fully describe it, and then by linking Entities together
through relationships with real-world meaning. For example, if entities EMPLOYEE and PROJECT
are created, they may be linked together by EMPLOYEE WORKS_ON PROJECT. In this way, a data
Relational Model
The Relational Model, created by IBM researcher Ted Codd in 1970, is a data model
rooted in set theory and first-order predicate logic that is widely used in developing modern
database systems. Organized into tables, or more formally called “relations”, the two main
methods of representation are through the tuple (row) and by attribute (column). Through the
24
use of unique primary keys, data requests, or queries, can be used to for extracting data from
the database. Through the use of what are called foreign keys, relations can be linked in a similar
fashion to the way the E-R model links entities together, allowing for a simplistic means of
storing data, and yet a powerful means of organizing data for retrieval.
The E-R and Relational Model both help to visualize and conceptualize what the actual
database design will eventually be. Developers use the E-R model to visualize the main
conceptual or physical entities of their database, as well as the attributes for each entity, and
each relationship between other entities. The Relational Model is used to assist the developer to
finalize their proposed database layout, as well as to create relations from proposed entities and
link them together through foreign keys or relation tables, but the relational model could be
difficult to create without some form of visual aid. So the E-R model helps with the initial design,
but the two models are so similar that the E-R model can be easily converted to the Relational
An advantage of using the E-R model over the relational model is the visualization aspect.
In the E-R model, entities, attributes, and relationships can be easily visualized in a diagram using
shapes that are linked together. In the relational model, relations are represented as tables,
which aren’t as visually appealing and are harder to work with when designing a database.
25
Another advantage the E-R Model has over the Relational Model is the fact that the E-R
Model fully supports multi-valued and composite attributes while the Relational Model only
supports single-valued and simple attributes. This can be problematic when real-world objects
work better with multi-valued and composite attributes, such as when a car has more than one
color. Conversion techniques can take care of this, but it is certainly an advantage over the
Relational Model.
The main disadvantage of the E-R Model is that there does not exists a query language
for that model. The Relational Model has the advantage of the SQL language, used for retrieving
When converting strong entity types to relations in the Relational Model, you must
create a Relation that contains all of the simple attributes that are a part of the entity being
converted. The relational model only supports simple attributes, and not composite or multi-
valued attributes. One of the key attributes from the entity must also be selected as the primary
When converting weak entity types to relations in the Relational Model, a relation can be
created using all of the simple attributes of the weak entity being converted. But because this is
26
a weak entity, which does not have a key of its own, a foreign key must be created from the
primary key of the parent entity, so that the relations can be properly mapped for the relational
model.
Only simple attributes are supported by the Relational Model. So for simple attributes in
the E-R Model, simply include all simple attributes as simple attributes of their respective
relation.
Composite attributes are not supported by the Relational Model. So for converting E-R
Model composite attributes, you will break the composite attribute into a set of simple
attributes that can easily be used in their respective relation. You can also create a separate
relation for composite attributes instead of splitting them into simple attributes.
Only single-valued attributes are supported by the Relational Model. So for single-valued
attributes in the E-R Model, include them as simple, single-valued attributes in their respective
relation.
Multi-valued attributes are not supported by the Relational Model. So for each multi-
valued attribute, you must create a new relation that will hold the values for that multi-valued
attribute. This new relation will have a foreign key that will relate this table to its parent relation
27
2.2.2 Converting Relationship Types to Relations
There are three options available for converting binary One to One relationship types.
1. The first option is to include a foreign key in of the relations that references the other
relation. Total participation of one entity in the other is very helpful in this case.
2. The second option is actually merge the two entities into one relation. The total
3. The last option for binary One to One relationships is to create a cross-reference relation.
Given entities T and S that are in a One to One relationship, create a relation from the
relationship between T and S. The primary key of this relation R will be a combination of
the primary keys of T and S. Any simple attributes from the relationship will be included
For converting binary One to Many Relationship types there are two options available.
1. If S is on the N-side of the relationship, include the primary key of T in the S relation as a
foreign key. All simple attributes of the relationship are included as attributes of the S
relation.
28
2. The last option for binary One to Many relationships is to create a cross-reference
relation. Given entities T and S that are in a One to Many relationship, create a relation
from the relationship between T and S. The primary key of this relation R will be a
combination of the primary keys of T and S. Any simple attributes from the relationship
For converting binary Many to Many relationship types, there is only one option available.
1. The last option for binary Many to Many relationships is to create a cross-reference
relation. Create a relation from the relationship between T and S. The primary key of this
relation R will be a combination of the primary keys of T and S. Any simple attributes
set of subclasses from a certain superclass entity type. The resulting set of subclasses that forms
a specialization is defined on the basis of some defining characteristic of the entities within the
superclass.
This is the opposite of the abstraction process which we disregard the differences among
several entity types and then identify their like features. Then, we can generalize them into a
single superclass. This results in the original entity types being special subclasses. The term
29
generalization is used to refer to the process of defining a generalized entity type from the given
entity types.
1. Create relations for all superclasses and subclasses. Include a foreign key for each subclass
relations to link them to their respective superclass. This works for any specialization.
2. Create relations only for the subclasses. This only works for specialization, and when
3. Create a single relation for all super and subclasses, but the relation will contain a
discrimination attribute. This attribute will indicate what subclass each tuple belongs to,
4. Create a single relation with a set of type attributes that will indicate which subclass
each tuple belongs to. This works for overlapping subclasses in specialization.
Recursive Relationship
relationships to the Relational Model, we can create a foreign key that will reference the primary
key of its own relation. This way it can be in a relationship with itself.
For example, say in a company, there are Employees and Managers, but in the database
there is only an Employee relation that contains both lower-level employees and upper-
management employees. We can look at an employee tuple, and if the employee is a manager,
30
his primary key will show up as the foreign key in another employee tuple who is a lower-level
N-ary Relationships
In an n-ary relation, you have more than 2 entities participating in a relationship. So for
converting these relationships to the Relational Model, you must create a cross-reference table,
which will contain an n number of foreign keys to relate each of the relations together. You
would also include any and all simple and single-valued attributes.
In the E-R Model, union type relationships, or categories, are when you create a subclass
from the union of two or more entity types. These subclasses are otherwise known as a union
type or a category.
For converting these relationships to the Relational Model, you must create a new
relation which will only hold a surrogate key. A surrogate key is a new unique attribute created
to uniquely identify members of the union relationship. This new key will then be added as a
Databases must obey certain constraints that are either inherent in the relational schema
itself, or are from business rules. Constraints are directly related to the database schema, or the
method of organization. Constraints are related to how the data will be handled when deleting,
inserting, or updating, and to ensure the integrity of the data in the database.
31
Domain Constraints
Domain constraints specify that all of the values within a tuple must be within the
specified domain for each attribute, such as their datatype or enumerated datatype. For
example, if the attribute’s domain is an integer, than that attribute’s value for all tuples must be
an integer. Or if the domain is an enumerated datatype, the attribute’s value for all tuples must
exist within that enumerated set. When updating or inserting, the DBMS will not allow you to
add a value to an attribute that does not exist in the domain of the attribute.
Key Constraints
Because the Relational Model is based on set theory, which by definition states that all
elements in a set must be unique, there must be a way to distinguish tuples from each other for
data retrieval. Through the use of primary and candidate keys, we can use these unique values to
identify unique records in a database. However, a relation can have multiple unique keys, or
candidate keys. One must be chosen as the primary key for this purpose. When updating or
inserting, the DBMS will not allow you to create a key that is not unique.
NULL Constraints
Another constraint is when an attribute is not allowed to be null, such as for a first name
or last name of a student. For updating or inserting, if the attribute is specified to be non-null,
32
Entity Integrity Constraints
The entity integrity constraint is the constraint where no primary key can be null. A
primary key is used to identify unique records in a database, and each table is only allotted one
primary key, so constraining the primary key to always being unique and not NULL is important.
With joins, primary keys can also be used to relate tables together. For inserting or updating, the
reference an existing tuple from the relation it is referencing at any given time.
This constraint is important for database operations. For example, the delete operation
can only be done if certain requirements are met. In our database, we could not delete an artist
from the Artist relation without also deleting any and all albums and songs because this would
violate the referential integrity constraint. For inserting or updating data in the database, you
can’t make foreign keys that reference tuples in other relations that do not currently exist. This
Check Constraint
Check constraints are constraints that are defined upon creating a database table using
SQL. Check constraints are used to ensure the integrity of data that is being updated or inserted
by making sure that a specific condition is met. If the value being inserted is not null, then the
33
check constraint will evaluate to TRUE or FALSE. If the value being inserted is null, then the check
constraint will evaluate to UNKNOWN, but it will not violate the constraint.
An example of a check constraint would be to say that the price of an item is greater than
$0.00. So that when data is entered or updated, no item in the database can have a price of less
than or equal to $0.00. So, when inserting or updating data, if the data evaluates the check
constraint to false, the DBMS will not allow you to insert that data.
Business Rules
Business rules are policies that businesses follow that must also apply to the data in their
databases. For example, if a company’s policy states that an accountant can’t have a relative
who works as a cashier, this rule also needs to be enforced in the database through either
These rules essentially maintain proper relationships between entities as well as ensure
the integrity of the data. For example, for our database we have an entity called Artist. Every
Artist has Albums, but no Album can exist without an Artist. Below are several more examples
1. No Member can exist in the database without being a part of at least one Artist.
2. No Artist can exist in the database without having a Contract in the database.
3. No Song can exist in the database without having an Album in the database.
4. No Album can exist in the database without having an Artist in the database.
34
5. No Transaction can exist without a Buyer, and no Transaction can exist without having at
Using all previously mentioned conversion techniques from E-R Model to Relational
Database, we are able to convert our entities to relations. Each of our Entities in our E-R Model
we created were strong entities, so we created relations for each of them with all simple
attributes.
All of the relationships we created between entities were either 1:N, N:1, or N:M. So for
all of our 1:N or N:1 relationships, we used foreign keys for the conversion to the relational
model. For the two N:M relationships we have, we used the method of creating a relation, where
The following tables are representations of our relation schemas for our relational
database design.
35
Keys PRIMARY FOREIGN
Contract
For converting the strong entity Contract, we created a relation that contains all of the
simple attributes from the Contract entity. The primary key is contract_ID.
Artist
For converting the strong entity Artist, we created a relation that contains all of the
simple attributes from the Artist entity. For the Contract Hires Artist relationship, which is 1:1,
we’ve added a foreign key, contract_ID, to the Contract relation. The primary key is artist_ID.
Composed_Of
Composed_Of was a relationship between the two strong entities Artist and Member. It
is a N:N relationship, so for converting, we’ve created a relation where each tuple is an instance
of that relationship. The primary key of this relation is the combination of the primary keys from
36
Attribute Domain Description
artist_ID String Unique artist ID
Ssn String Social Security Number of
artist member
Member
For converting the strong entity Member, we created a relation that contains all of the
simple attributes from the Member entity. The primary key is ssn.
Album
For converting the strong entity Album, we created a relation that contains all of the
simple attributes from the Album entity. For the Artist Records Album relationship, which is 1:N,
we’ve added a foreign key, artist_ID, to the Album relation. For the Album Recorded At Studio
relationship, which is N:1, we’ve added a foreign key, studio_ID, to the Album relation. The
37
Attribute Domain Description
Album_ID String Unique album ID
Album_name String Album name
Date_released Date Date album released
Unit_price Float Selling price per album
artist_ID String Unique artist ID
Studio_ID String Unique studio ID
Start_date Date Date work on album began
End_date Date Date work on album ended
Hrs_worked Int Number of hours worked on
album
Sold_Through
Sold_Through was a relationship between the two strong entities Album and Transaction.
It is a N:N relationship, so for converting, we’ve created a relation where each tuple is an
instance of that relationship. The primary key of this relation is the combination of the primary
Transaction
For converting the strong entity Transaction, we created a relation that contains all of the
simple attributes from the Transaction entity. For the Transaction Purchased By Buyer
relationship, which is N:1, we’ve added a foreign key, buyer_ID, to the Buyer relation. The
38
Attribute Domain Description
Transaction_ID String Unique transaction ID
Buyer_ID String Unique buyer ID
Date Date Date of purchase
Buyer
For converting the strong entity Buyer, we created a relation that contains all of the
simple attributes from the Buyer entity. The primary key is buyer_id.
Song
For converting the strong entity Song, we created a relation that contains all of the
simple attributes from the Song entity. For the Album Composed Of Song relationship, which is
1:N, we’ve added a foreign key, album_ID, to the Contract relation to link songs to their album.
For the Song Written By Writer relationship, which is N:1, we’ve added a foreign key, writer_ID,
to the Contract relation to link songs to their Writer. The primary key is artist_ID.
39
Studio
For converting the strong entity Studio, we created a relation that contains all of the
simple attributes from the Studio entity. The primary key is studio_ID.
Writer
For converting the strong entity Writer, we created a relation that contains all of the
simple attributes from the Writer entity. The primary key is writer_ID.
40
Relational Model
41
2.3.2 Sample Data of Relation
Contract
Artist
42
Composed Of
Artist_ID SSN
A17813 115-80-7312
A17813 898-63-6127
A17813 095-12-8618
A17813 718-40-2914
A17813 094-10-3269
A17813 390-79-6801
A17813 799-51-6609
A44086 466-69-1455
A44086 913-28-7463
A44086 316-63-2012
A44086 706-38-8832
A11328 230-40-5066
A11328 237-09-5808
A11328 030-52-9568
A4752 350-82-6131
A4752 966-72-8262
A4752 174-63-5988
A4752 555-69-8350
A4752 522-64-6239
A4752 033-39-0750
A4752 752-64-0200
A6789 060-96-8225
A6789 704-11-3574
A6789 380-38-4098
A6789 936-50-7878
A6789 080-69-9386
A55390 445-08-9490
A55390 694-77-5842
A55390 359-68-4742
A55390 022-62-8780
A55390 727-70-6860
A85862 297-66-5184
43
A85862 727-43-1600
A85862 835-47-1116
A34728 681-99-1076
A34728 326-33-7324
A34728 794-07-1975
A34728 065-29-1852
A93471 238-44-2093
A93471 647-75-9371
A50643 493-54-0743
A50643 315-18-5134
A19760 934-66-9708
A53622 103-18-0494
A53622 689-61-9138
A53622 519-05-5209
A53622 130-87-1020
A53622 648-37-7849
A42579 018-38-7601
A42579 647-90-6445
A42579 737-34-7478
A96710 043-98-9774
A96710 276-39-2123
A67044 800-96-0314
A67044 055-92-3612
A67044 372-27-3921
A67044 703-99-8621
A67044 815-09-6438
A67044 249-64-8859
A67044 539-63-8320
A67044 985-14-8004
A67044 355-42-2240
A67044 196-45-7896
44
45
Sold Through
46
Transaction
47
Song
48
SO8531 7 Mislead Beggar 9:81 AL2151 W5894
SO3251 8 Analyze Secretary 6:07 AL2151 W5894
SO3942 9 Exercise View 0:25 AL2151 W5894
SO1421 10 Intend Approval 3:16 AL2151 W5894
SO9412 11 That’s too much 6:62 AL7741 W5894
SO4618 12 Talented Turkey 1:33 AL7741 W5894
SO0985 13 Arrogant Apples 5:38 AL7741 W6310
SO6835 14 Young Yard 4:93 AL7741 W6310
SO8417 15 Simple Scent 9:76 AL7741 W6310
SO9423 16 Ten Town 7:74 AL7741 W6310
SO4161 17 Comfortable Cactus 6:49 AL7741 W6310
SO3935 1 Cooing Crayon 6:69 AL7741 W6310
SO8108 2 Pricey Party 8:73 AL7741 W6310
SO0478 3 Poised Plastic 9:63 AL7741 W6310
SO5138 4 Silly Scale 9:45 AL7741 W4552
SO3275 5 Ketchup and A1 3:11 AL7741 W4552
SO2116 6 Wax War 6:14 AL7741 W4552
SO3114 7 Art Ants 8:46 AL7741 W4552
SO6665 8 Mind Minister 7:05 AL7741 W4552
SO6383 9 Mother Mice 0:63 AL7741 W4552
SO4064 1 Texture Tramp 7:55 AL7741 W4552
SO0519 2 Bread Bean 1:86 AL2381 W4552
SO6505 1 Ship Space 5:97 AL2381 W4552
SO8475 2 Prose Produce 6:98 AL2381 W4552
SO8551 1 Plough Pump 2:11 AL2381 W4552
SO1457 1 Skate Spot 1:04 AL5377 W1384
SO6451 2 Don’t touch me 4:69 AL5377 W1384
SO9401 3 Ascertain Army 2:36 AL5377 W1384
SO0623 4 Jail Join 7:62 AL5377 W1384
SO0892 5 Branch Bath 4:44 AL5377 W1384
SO3588 6 Sense Slope 7:99 AL5377 W1384
SO6478 7 Search Stop 2:39 AL1803 W1384
49
SO4088 8 Smile Stew 0:28 AL1803 W1384
SO4779 9 Bump Beam 6:63 AL1803 W1384
SO0859 10 Receive Reward 1:77 AL1803 W1384
SO3772 11 Sell Slope 1:37 AL1803 W1384
SO7864 12 Misunderstand Meal 1:89 AL1803 W1384
SO7597 13 Cough Range 5:51 AL1803 W1099
SO0073 14 Zipper Camp 8:12 AL1803 W1099
SO5223 1 Heat Vegetable 2:59 AL1803 W1099
SO1528 2 Clouds Disgust 1:34 AL5392 W1099
SO6312 3 Touch me 3:83 AL5392 W1099
SO7996 4 Attack Uncle 9:81 AL5392 W1099
SO3087 5 Determine Dress 8:50 AL5392 W1099
Recommend
SO6947 6 Direction 6:30 AL5392 W1099
SO3981 7 Scream Birthday 7:59 AL5392 W1099
SO9372 8 Fax Secretary 5:41 AL5392 W1099
SO2585 9 Process Road 0:58 AL5392 W1099
SO3545 1 Squash Pickle 2:07 AL5392 W1099
SO7466 2 Diagnose Bomb 8:03 AL5392 W1017
SO7763 3 Experiment Authority 4:58 AL7282 W1017
SO8381 4 Finalize Plantation 0:24 AL7282 W1017
SO3969 5 Help me 9:58 AL7282 W1017
SO1657 6 Elite Edge 5:19 AL7282 W2383
SO5914 7 Pushy Pollution 8:23 AL7282 W2383
SO9667 8 Disillusioned Donkey 8:62 AL7282 W2383
SO1319 9 Uppity Underwear 6:27 AL7282 W2383
SO2713 1 Ad Aunt 8:39 AL725 W2383
SO2932 1 Tacit Tooth 7:23 AL725 W2383
SO2873 2 Married Morning 0:14 AL8817 W3370
SO3144 3 Fallacious Fuel 0:86 AL8817 W3370
SO7803 4 Massive Mass 1:25 AL9098 W3370
SO1986 5 Careless Chair 3:54 AL9098 W3370
SO7525 6 It was the dog 9:44 AL9098 W3370
50
SO6298 7 Smoke Shame 1:51 AL3881 W3370
SO1895 8 Crate Chance 3:48 AL3881 W3370
SO9648 9 Square Shop 2:47 AL3881 W4099
SO7926 10 Juice Jewel 9:38 AL4688 W6369
SO4249 11 Blow Baseball 5:85 AL4688 W3386
SO1891 12 Business Beam 6:68 AL4688 W126
SO4994 13 Stomach Shirt 5:92 AL4688 W1262
51
2.4 Sample Queries for Database
Once a database has been developed, data must be retrieved using formal queries. We
will show a few simple SQL queries of our database before getting into more complex queries.
This query would result in all the columns from the ARTIST table being extracted. The
SELECT command allows for the extraction of information directly from a table and the *
operator allows for the retrieval of all columns. FROM specifies where the data will be pulled
from, with ARTIST being the relation we’re querying in this example. The query, in English, reads
We can also be much more specific in the data we wish to retrieve from our database.
Using a Select operation, we are able to narrow down the results to only those Albums
with the ID being equivalent to ‘123456’, but using a Project operation we only retrieve two
SELECT artist_id, artist_name FROM ARTIST WHERE genre = ‘Rock’ ORDER BY DESCENDING;
Using SELECT, we specify which attributes we want retrieved, which are artist_ID and
artist_name. Then, using the FROM operator, we specify we want data FROM the ARTIST table,
but only WHERE the genre of the artist is ‘Rock’. This will output all Rock Artists. In SQL, the
52
result set can also be ordered to be more appealing or for a specific purpose, as shown in the
In the case where a count is required, the following query is an example of this.
This query would return the amount of albums within our ALBUM entity. This information
The entire purpose of this section was to display how powerful queries can be. Practically
anything and everything can be retrieved from the database with the correct queries. But these
were very simple examples. In the following sections, examples queries are shown that are far
The following list are ten non-trivial sample queries that are designed specific to our
database. These are great examples to show the expressiveness of data retrieval through queries
and will be later written using three formal languages: Relational Algebra, Tuple Relational
53
8. Find all artists who have recorded at all studios.
10. Find the most worked on album between Jan 10th 2011 and October 3rd, 2013.
defining queries to retrieve data stored in a Relational Database. A sequence of these operations
is a relational algebra expression, whose output is a relation that contains data queried from a
database. Below are ten query examples from our database written using Relational Algebra.
A1 ← (Artist * Album)
A2 ← (Artist * Album)
πA1.artist_Name ( σ A1.artistID = A2.artistID ∧ A1.albumID ≠ A2.albumID (A1 × A2) )
s1 ← (Song * Writer)
s2 ← (Song * Writer)
π s1.fname, s1.lname (s1 − π s1.* ( σ s1.writerID = s2.writerID (s1 × s2)))
S1 ← Song
S2 ← Song
NotSingles ← πs1.* (σ s1.song_ID ≠ s2.song_ID ∧ s1.album_ID = s2.album_ID (s1 × s2))
54
πAlbum_Name (Album * (π album_ID (Song - NotSingles )))
S1←Song
S2←Song
π s1.song_name, s1.song_length ( σ s1.song_length > s2.song_length (S1 × S2) )
S1←Studio
S2←Studio
Expensive ← ( π s1.* ( σ s1.hourlyCost > s2.hourlyCost (S1 x S2) ) )
π studio_name, hourly_Cost ( Studio - Expensive )
10. Find the most worked on album between Jan 10th 2011 and October 3rd, 2013.
55
2.4.3 Tuple Relational Calculus Expressions
to retrieve from a database, but not how to retrieve it. Tuple Relational Calculus expressions
utilize variables that range over tuples from relations in a Relational Database. These expressions
are based on predicate logic, which make use of the Existential (∃) and Universal (∀) quantifiers.
Below are ten example queries written using Tuple Relational Calculus.
56
6. Find the longest song.
10. Find the most worked on album between Jan 10th 2011 and October 3rd, 2013.
{ a | Album(a) ∧ a.start_date < 10/3/2013 ∧ a.end_date > 1/10/2011 ∧ ¬(∃ a2) (a2.hrs_worked >
a.hrs_worked ∧ a2.start_date < 10/3/2013 ∧ a2.end_date > 1/10/2011) }
Domain Relational Calculus is very similar to Tuple Relational Calculus. It is also a non-
procedural language used to define what information to retrieve from a database, but not how
57
to retrieve it. These expressions are also based on predicate logic, which make use of the
Existential (∃ ) and Universal (∀ ) quantifiers. The main difference is that Domain Relational
Calculus and Tuple Relational Calculus is that Domain Relational Calculus uses variables that
range over domains of attributes, not tuples. Below are ten example queries written using Tuple
Relational Calculus.
58
8. Find all artists who have recorded at all studios.
{ < aID, n > | Artist( aID, _, n, _) ∧ (∀s) (Studio(s, _, _, _, _, _, _, _, _, _) → (∃a) Album(_, _, _, _,
aID, s, _,_, _))}
10. Find the most worked on album between Jan 10th 2011 and October 3rd, 2013.
{ < a, aID, hw > | Album( aID, n, _, _, _, _, < 10/3/2013, > 1/10/2011, hw) ∧ ¬(∃ hw2) (Album( aID,
n, _, _, _, _, < 10/3/2013, > 1/10/2011, hw2) ∧ hw2 > hw ) }
59
Phase 3: Implementation of Relational Database
3.1.1 Anomalies
For relations that have yet to be normalized, it is possible to encounter a number of issues.
Insertion Anomalies
For poorly defined relation schema, insertion anomalies are common. For example, if
our Album table contained all attributes for the Studio where each album was recorded at, and
we were going to insert a new Album in the Album table, we would need to make sure that the
Studio attributes were consistent between Album tuples. This would also make it difficult to
insert a new Studio into the database since there are no Albums that were recorded there yet.
The only workaround for this issue is to insert NULL values in the attributes for the Album, which
could lead to integrity constraint issues, or to normalize the relation, which will be discussed
later.
Deletion Anomalies
The main issue with deletion anomalies is connected to the second insertion anomaly. If
we were to delete an Album from the Album table that could possibly be the only Album that
was recorded at a particular Studio, the information that once existed for that Studio would be
completely lost.
60
Modification Anomalies
Update Anomalies are when, in a poorly designed schema, updating a value in one tuple
can cause inconsistencies across many other tuples in a relation. For example, if somebody were
to change the value of an attribute of a Studio, all Album tuples that contain data for this Studio
would need to be updated to reflect this new information. If this is not done correctly, massive
3.1.2 Normalization
conform to certain degree of normal, aimed at reducing redundant data and data anomalies. The
normalization procedure consists of a series of tests done against relations, looking for
opportunities to decompose the relations when necessary to minimize potential data issues.
The goal of normalization is done to minimize the amount of data redundancy that
occurs, as well as to minimize insertion, deletion, and update anomalies, as was previously
described. But there are varying degrees of normalization. The main types of normalization are
as follows: First Normal Form, Second Normal Form, Third Normal Form, and Boyce-Codd
Normal Form.
First Normal Form, or 1NF, states that the domain of a particular attribute must only be
atomic values, and that the value of any attribute must be a single value from the domain of that
attribute. This does not allow for having a set of values, tuple of values, or a combination of the
two. This also does not allow for nested relations. 1NF is now considered to be a part of the
61
definition of a relation in the relational model, were multi-valued attributes, and composite
Second Normal Form, or 2NF, utilizes the concept of full functional dependency. The
discarding any attribute C from A means that the dependency does not hold. So for relations that
have primary keys containing more than one attribute, any non-key attribute should not be
functionally dependent on a part of the primary key. 2NF must also satisfy 1NF.
For example, if we were to have a member tuple where {SSN, address} → fname is a
functional dependency. If we removed address from the dependency and it still held as true,
Third Normal Form, or 3NF, utilizes the concept of transitive dependency. The functional
attributes C in T that is neither a candidate key or a subset of any key of T, as well as both A → C
and C→B. In other words, no relation should have any non-key attributes functionally dependent
on another single non-key attribute, or set of non-key attributes. 3NF must also satisfy 1NF and
2NF.
62
Boyce-Codd Normal Form
Boyce-Codd Normal Form, or BCNF, is similar to 3NF, but is much more strict and
stronger. For relational schema R to be in BCNF it must be the case such that, if whenever a
BCNF only differs slightly from 3NF since the condition of 3NF which would allow A to be prime is
not within the definition of BCNF. The absence of this condition makes BCNF a stronger normal
Upon designing this relational database, we followed the 1NF rule that multi-valued
attributes and composite values are not allowed. Our initial E-R Model design did not contain any
multi-valued attributes or composite values to begin with, so upon converting the E-R model to
Relational Model, the adherence to 1NF came naturally from our design.
as for the Buyer relation. In early design stages, Buyer information was stored in each
Transaction tuple, which created a potential problem with inconsistent data. But by
decomposing the Transaction relation, we were able to create a Buyer relation that would
centralize all Buyer information as and eliminate any redundant and inconsistent data.
The only potential modification anomaly that is currently present in our database is how
our songs are stored in the Song relation. Each song tuple holds is track number, which signifies
its place in the track listing of its respective album. This information must be stored in this
63
manner, so to minimize any problems, either a procedure will be created to correct any potential
SQL *Plus is a powerful, yet simple, command-line program that allows the user to
execute commands that allow for the creation of Oracle database components, as well as the
ability to maintain and update an Oracle database. SQL *Plus also allows for the execution of
SQL, and PL/SQL statements for querying, updating, or deleting data, as well as to create
Schema objects are logical structures of data in a database. Oracle allows many
different types of schema objects for their relational databases. Some examples of schema
objects are dimensions, sequences, synonyms, and clusters. The main objects that we are using
Tables
Tables are the basic structures used by databases to store data. They are composed of
Syntax:
64
…
CONSTRAINT constraintName1 PRIMARY KEY (columnName, columnName,…),
CONSTRAINT constraintName2 FOREIGN KEY (columnName) REFERENCES tableName
(columnName),
…
CONSTRAINT constraintName3 CHECK(condition)
}
Views
A view is a logical, or virtual, table that is based on a stored query that gets data from
already existing base tables. Views are useful in the case of building GUI’s, as the application can
Syntax:
Stored procedures
Stored procedures are a set of SQL statements that, together, perform a specific task
on the database. They can take parameters, similar to functions, and can be used to encapsulate
a set of operations.
Syntax:
65
Triggers
Triggers are procedures that are implicitly executed whenever an insert, delete, or
Syntax:
Indexes
An index is an object that contains an entry for each value that every indexed column
contains. Indexes allow for direct and fast access to rows in the database, which makes queries
Syntax:
66
3.3 Relation Schema and Data
3.3.1 Contract
3.3.2 Artist
67
3.3.3 Composed_Of
68
3.3.4 Member
69
3.3.5 Album
70
3.3.6 Sold_Through
71
3.3.7 Transaction
3.3.8 Buyer
72
3.3.9 Song
73
74
3.3.10 Studio
75
3.3.11 Writer
76
3.4 SQL Queries
The following queries are translated from the Phase 2 relational algebra and relational
calculus queries.
Output:
Artist_name
Shaman
Queen
Output:
Artist_ID Artist_Name
A1010 Led Zeppelin
A67044 Shaman
A00001 Queen
77
3. Find all writers who have only written one song.
Output:
Writer_ID Fname Lname
W127 Rhea Amaya
W1264 Rhea Amaya
Output:
Album_ID Album_Name
AL1313 Coda
AL2020 I Am the Greatest Ever
AL725 Laser
AL0004 A Night at the Opera
78
5. Find albums that have at most three songs
Output:
Album_ID Album_Name
AL7741 System
AL5377 Fork
AL7282 Log
AL1803 Surgeon
AL3881 Throne
AL3627 Microscope
AL4688 Hazard
AL4621 Midget
AL2381 Observation
AL2151 Credit Card
AL5392 Wisdom
Output:
Song_ID Album_Name
S0010 Death on Two Legs
79
7. Find the least expensive studio.
Output:
Studio_ID Studio_Name
S8241 Pede Malesuada Industries
Output:
Artist_id Artist_name
A00001 Queen
Output:
Buyer_id Buyer_name
B00002 Kmart
80
10. Find the most worked on album between Jan 10th 2011 and October 3rd, 2013.
Output:
Album ID Album Name Hrs_Worked
AL56 Physical Graffiti 8000
11. For all transactions, find the total number of album units purchased, as well as the total cost
for each transaction.
Output:
Transaction_ID Buyer_name Total_units Total_price Tdate
T53622 FYE 1500 14985 21-JAN-14
T67044 FYE 100000 1699000 05-OCT-15
T00001 Kmart 3000 31326 01-NOV-15
T11328 Wal-Mart 150000 598500 25-JUL-14
T4752 Costco 10000 99900 09-SEP-14
81
T85862 Costco 1000 2990 21-NOV-15
T44086 Target 250000 3247500 26-JUN-14
T6789 Target 500000 3995000 23-SEP-15
T34728 Target 100 199 18-SEP-14
T50643 Target 230000 2067700 05-AUG-14
T19760 Target 100000 999000 01-APR-15
T55390 Amoeba Records 25000 624750 21-MAR-15
T93471 Amoeba Records 55000 439450 10-APR-15
T96710 Amoeba Records 250 3747.5 19-SEP-15
82
Phase 4: Stored Procedures, Packages, and Triggers
PL/SQL, which stands for Procedural Language extensions to SQL, is a language used by
Oracle in order to add more programming ability to the SQL language for creating more complex
objects and operations. PL/SQL has procedural language attributes such as conditions and loops
and allows declaration of constants and variables, as well as functions and error handling.
Benefits of PL/SQL
PL/SQL allows for the creation of stored procedures, functions, and triggers. These
1. They allow for database automation. Instead of having to worry about complicated
updates and deletes, procedures can be created that will handle all of this for you
automatically, instead of having to execute the individual statements in the client, which
can be less secure.
2. If a database is used by many applications written in different languages, it can then be
stored in the database and then called by any of those different applications.
3. There is less latency, or quicker response time, when stored procedures are used.
4. A procedure can assist in creating a view by allowing for a more complex type of derived
data to become available to the user. They can also be used to check for more advanced
constraints.
5. Error handling can be stored as part of the procedure.
Control Statements
PL/SQL has a number of control statements that are very useful for creating procedures.
83
1. Conditional Selection Statement: Runs different statements for different data value.
These statements are IF and CASE.
2. Loop Statements: Run the same statements with a series of different data values. The
loop statements are the LOOP, FOR LOOP, and WHILE LOOP.
3. Sequential Control Statements: Are not necessary to PL/SQL, but can be used. The
sequential control statements are GOTO and NULL.
Stored Procedure
Stored procedures are similar to functions, in that they are a set of stored PL/SQL
statements that can be executed any number of times. These procedures can also take in
parameters. Stored procedures are helpful in that instead of explicitly running the same series of
PL/SQL statements over and over, you can just store it once and call it as many times as
Syntax:
Stored Functions
A stored function is a set of PL/SQL statements that can be called by a function name.
They are almost the same as a stored procedures, except a function returns a value where it is
84
Syntax:
Packages
Packages are schema objects that are used to logically group related procedures and
functions, as well as variables. Upon grouping these items into a package, an application that is
using these subprograms only needs to know the name of the subprogram and the parameters
needed for these subprograms, and does not need to know any of the specific implementation
Syntax:
Triggers
Triggers are programs or procedures that are stored in the database and implicitly
executed before, after, or instead of an update, insert, or deletion. This is a method of specifying
85
certain rules, and will help to enforce these rules whenever data has been modified within a
Syntax:
Cursor
A Cursor is a temporary work space created in the system memory when SQL
statements are executed. Cursors allows you to give a select statement a name, so that you can
then access the information retrieved in that select statement in some kind of procedure. There
are two types of cursors, which are explicit and implicit. Both have the same functionality but are
Syntax:
86
4.2 MS SQL Server and MySQL Stored Procedures
Microsoft SQL Server uses an extension of SQL called T-SQL, short for Transaction-SQL,
which, similar to PL/SQL, has a number of features that are not available with SQL, including
procedural programming and variables, which allow for the creation of stored procedures.
A major feature that T-SQL has, that differs from Oracle, is that the DELETE and
UPDATE commands both allow for the inclusion of the FROM clause which allows that usage of
JOINS, making filtering records much easier, and the deletion of records far easier than in
PL/SQL.
T-SQL Procedure
The major difference between T-SQL and PL/SQL is parameter passing. PL/SQL uses IN,
OUT, and INOUT to differentiate between different types of parameters. T-SQL uses OUT,
OUTPUT, and READONLY for this purpose, which is the same idea as what Oracle implements,
but just with different syntax. Another key difference is the use of the @ to signify the use of a
variable. T-SQL also allows the use of a SELECT statement without having to use the keyword
INTO, which PL/SQL requires when assigning SELECT results into a variable. Despite these
Syntax:
87
[ FOR REPLICATION ]
AS
BEGIN
[declaration_section]
executable_section
END;
T-SQL Function
T-SQL Functions are very similar to T-SQL procedures, in terms of syntax and semantics.
Functions in T-SQL also make use of OUT, OUTPUT, and READONLY variable types, and their
overall structure is very similar to that of PL/SQL. The symbol @ is also used throughout for
Syntax:
88
4.2.2 MySQL Server Routines
MySQL uses what are called Routines, which are equivalent to Procedures in PL/SQL, and
are very similar to Oracle Procedures in terms of syntax and semantics, as they have the ability to
use cursors, as well as all control statements Oracle has, such as IF, ELSE, case statements, and
loops. Similar to Oracle, MySQL also allows the use of IN, OUT, and INOUT parameter passing.
And MySQL also requires that you use the keyword INTO when using SELECT in a procedure,
Syntax:
CREATE
[DEFINER = { user | CURRENT_USER }]
PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...]
BEGIN
routine_body
END
MySQL Function
MySQL Functions are very similar to Oracle functions, but in MySQL you cannot use OUT
or INOUT parameters. By default, all parameters are IN and this cannot be changed. This is a
major difference between Oracle, which allows the use of all three types of parameters in
functions and procedures. MySQL functions are also only allowed to return a single value.
Syntax:
CREATE
[DEFINER = { user | CURRENT_USER }]
FUNCTION sp_name ([func_parameter[,...]])
RETURNS type
89
[characteristic ...]
BEGIN
routine_body
End
4.3.1 Procedures
The following procedures were created for our database. They serve various functions
The following procedure is used to delete an Album and all of its Songs from their
The following procedure is used to delete an Artist from the database. In order to fully
delete an Artist from the database, all albums, songs, contract information, and transaction
90
CREATE OR REPLACE PROCEDURE maal_delete_artist_proc(aid IN maal_artist.artist_id%type)
IS
BEGIN
DELETE FROM maal_song
WHERE album_id IN
(
SELECT album_id FROM maal_song
NATURAL JOIN maal_album
WHERE artist_id = maal_delete_artist_proc.aid
);
DELETE FROM maal_composed_of
WHERE maal_composed_of.artist_id = maal_delete_artist_proc.aid;
DELETE FROM maal_member
WHERE ssn IN
(
SELECT ssn FROM maal_member
NATURAL JOIN maal_composed_of
WHERE maal_composed_of.artist_id = maal_delete_artist_proc.aid
);
DELETE FROM maal_sold_through
WHERE album_id IN
(
SELECT album_id FROM maal_sold_through
NATURAL JOIN maal_album
WHERE maal_album.artist_id = maal_delete_artist_proc.aid
);
DELETE FROM maal_album
WHERE maal_album.artist_id = maal_delete_artist_proc.aid;
DELETE FROM maal_contract
WHERE CONTRACT_ID IN
(
SELECT contract_id FROM maal_contract
NATURAL JOIN maal_artist
Where maal_artist.artist_id = maal_delete_artist_proc.aid
);
DELETE FROM maal_artist
WHERE artist_id = maal_delete_artist_proc.aid;
COMMIT;
END;
91
Album Sales and Average Revenue Procedure
The following procedure is used to get the total amount of revenue generated from an
album transaction purchased by a major retailer distributor. This procedure also retrieves the
--DECLARE
revenue number(20);
total number(20) :=0 ;
counts number(10);
average number(20,4);
Cursor cur1 is
select transaction_id, album_id, batch_units
from maal_sold_through;
v_ST_transid maal_SOLD_THROUGH.transaction_id%type;
v_ST_albumid maal_SOLD_THROUGH.album_id%type;
v_ST_batch maal_SOLD_THROUGH.batch_units%type;
v_a_unit maal_ALBUM.unit_price%type;
Cursor cur2 is
select album_id, unit_price
from maal_album;
v_T_album maal_album.album_id%type;
v_T_unit_price maal_album.unit_price%type;
BEGIN
select count(transaction_id) into counts from MAAL_sold_through;
dbms_output.put_line( RPAD('TransID',15,' ') || RPAD('albumID',15,' ') || RPAD('Batch',15,' ')
|| RPAD('Revenue',15,' '));
dbms_output.put_line('--------------------------------------------------------------------');
OPEN cur1;
LOOP
fetch cur1 into v_ST_transid, v_ST_albumid, v_ST_batch;
exit when cur1%NOTFOUND;
OPEN cur2;
LOOP
92
fetch cur2 into v_T_album, v_T_unit_price;
exit when cur2%NOTFOUND;
if v_ST_albumid = v_T_album then
revenue := (v_T_unit_price * v_ST_batch);
end if;
End LOOP;
CLOSE cur2;
dbms_output.put_line( RPAD(v_ST_transid,15, ' ') || RPAD(v_ST_albumid,15, ' ') ||
RPAD(v_ST_batch,15,' ') || RPAD(revenue,15,' '));
total := (total + revenue);
--dbms_output.put_line( RPAD('Total Revenue', 15, ' ') || RPAD(total,15,' '));
END LOOP;
CLOSE cur1;
dbms_output.put_line('--------------------------------------------------------------------');
average := (total / counts);
dbms_output.put_line( RPAD('Average Revenue',20, ' ') || RPAD(average,15,' '));
end;
The following procedure is used to insert a new Contract into the Contract relation.
93
4.3.2 Triggers
The following trigger is execute when you attempt to update the primary key
Album_ID. The new value will be updated everywhere else it is referenced as a foreign key.
The following trigger is used when you attempt to update data on the Album_Info view
in the database. Instead of updating the view, this trigger instead uses the new values to insert
into the base tables that the view uses to draw data from.
94
Writer ID Update Trigger
This trigger is used when you attempt to update a writer_id, which is the primary key of
the Writer relation. This trigger will update this value everywhere it is referenced as a foreign key
in the database.
The following triggers are used together in order to do three things. First, upon
attempting to delete a Transaction record, all Sold_Through records associated with that specific
Transaction will also be deleted. After this has occurred, the second two triggers will be
executed, which will back up this deleted data into two log tables created specifically for this
operation.
95
END;
96
Phase 5: Graphical User Interface
Executive assistants group will use this database at any location in order to gather
information on new artists that have been contracted out, as well as the amount of terms they
have been assigned to. They gather this information for higher-up executives as well as for
financial purposes.
Daily Activities:
Search for information needed for executives, such as financial information, and overall
View
ARTIST_CONTRACT INFO
CURRENT_CONTRACT REPORT
MEMBER_GEN_INFO
97
5.1.2 Artists
The artist group will use this database in order to keep informed with their current
albums and track listings, as well as their current studio that they will be working at. The group
Daily Activities:
View current albums that they have released or are currently working on
The number of albums left under their current contract with the company.
Views
ARTIST_CONTRACT_INFO
ALBUM_SONG
98
5.2 GUI Design and Development in Java
Both of us have had a little experience with Java prior to us starting the database project.
This allowed us to ease into the developer tool called NetBeans, which is an IDE that can be used
for application development. We began by looking into internet resources as well as online
tutorials for help on how to develop a simple user interface. Once we were able to get a few
examples down, we began to work out a layout that we would use for the database GUI. The
1. We brainstormed ideas on what the user interface should allow the user to do. We then
drew several diagrams on paper and did quick implementations in NetBeans to see the
practicality of those designs and ideas. We thought of a few ideas on how to display the
2. Once we found a layout that we both liked, we moved forward and began to implement
3. We designed two reports for the GUI. One report is designed to print Album information,
We learned that even though we had an IDE to help us navigate our GUI, it would not always
turn out the way we had hoped. There were many times where a GUI design would look fine
during development, but when we would test the application, text fields and labels would start
Pulling data from a table and display it through a GUI was a main goal, and then display
information in different areas based off of their relations. We learned that if our prepared
99
statements did not currently match our data model, our queries would fail and leave us with no
information.
There are many components that can be added to the database, but for the purpose of this
project, we wanted to limit the amount information for now. This can later be fixed by adding
more.
100
5.3 Major Features
The below code is used to connect to our database, and add values to the current
selected table. We use the prepared statements to insert or update values as they are passed to
This code allows us to establish a connection to the CSUB Delphi database. We first
create a Connection class and specify the credentials needed to connect. An instance of this class
is used in the main Menu class and the function getDBConnection() is called to connect to the
101
5.3.2 Inserting into Database
The following code is an example of an insertion into the Contract and Artist tables.
This code is executed when the Save button is clicked on the Contract Information panel. This
code pulls text from all the appropriate text fields and uses a Prepared Statement to set up a SQL
Insert statement to insert a new Contract and Artist into the database.
102
5.3.3 Row Filtering For Searching
The below code is used to filter table results in the table. When a user wants to search
for a specific attribute, they can use the search text field to narrow down information. The data
is filtered by each letter entered. For example, the letter "A" will filter fields that contain the
letter "A". By typing "Ab", the search will filter fields that contain the combination "Ab" and so
on.
103
5.3.4 Assigning Studios to Albums
A major feature of our GUI is the ability to assign a Studio to an Album. Using a JDialog
form, when a user is entering in Album information, you have the ability to click on
“Change/Assign Studio” which will open the JDialog form. In this JDialog form, you can add a new
Studio if the Album was recorded at a Studio that does not currently exist in the database, or
search for the already existing Studio and click “Choose Studio” to assign that Studio to the
104
Through the use of static variables, whenever a JDialog is created, the JDialog is able to
access and change data in the Menu frame, which allows for simple communication between the
JDialog and the Menu frame, which allows for assigning Studios to Albums.
When focus is gained on the Menu frame, that means that the JDialog has completed
its task, and a Studio has been chosen. So it will automatically pull the string data from the static
variable that has been assigned a value in the JDialog, and it will use it to fill the proper
JTextField.
105
5.3.5 Assigning Writers to Songs
A major feature of our GUI is the ability to assign a Songwriter to a Song. Using a JDialog
form, when a user is entering in Song information, you have the ability to click on
“Change/Assign Writer” which will open the JDialog form. In this JDialog form, you can add a new
Writer if the Song was written by a Songwriter that does not currently exist in the database, or
search for the already existing Songwriter and click “Choose Writer” to assign that writer to the
This is implemented in the exact same way as when an Album is assigned a Studio. By
using static varibles, the JDialog and the Menu frame are able to communicate and pass data
106
5.3.6 Generated Reports
A major feature of our GUI is the ability to generate reports. For a generated report, the
user has the option of either selecting a single artist and displaying the contract information
related to that artist, or they can print all available contracts under the company. This
information is displayed in a separate window for readability. Our reports are generated using a
plugin for NetBeans called Jasper reports. The image below is a contract report generated for a
107
Jasper Report utilizes HashMaps in order to take in parameters to fill in the XML
reports based on a specified query. So for the Contract report, the Contract ID of the selected
artist is passed in to the report when it is generated. This allows the report to be dynamic and
108
5.4 Outcome
In conclusion, we learned that if we had an issue that could not immediately be solved,
outside sources or communication with each other is needed. We also learned that
collaborating with a partner allows for a better design. Communication with each other is key
From a technical aspect, we learned how databases are designed and implemented in
great detail. We were able to explore databases from initial design, to implementation and GUI
development. This covered several large topics, and we were able to get experience in all of
them.
Members' Outcome
Outcome
Answers btw 1 & 10
109