CMSI 486: Welcome to Week 04

This Week's Class Agenda

'Tables', Keys, and Normalization

database table?

OK, so officially, the entities in a Relational Database are called just that: entities, and the things that they have that are their parts [so to speak] are called attributes. However, in the vernacular [or the parlance of our times] we frequently refer to these things as tables, with each table having columns and rows much like a spreadsheet or a matrix.

Within that context, there will be [or should be] one column of the table which is used to uniquely identify or differentiate any row from any other row in that table. This column [attribute] is called a key. There are several different types of keys as we saw on week 3.

In order to organize the database components to make things more effective, we go through a process known as normalization. This process helps to reduce data repetition within the database, and also helps improve the integrity of the data.

There are several objectives of normalization, as specified by Edgar Codd when he invented the idea of relational databases in the early 1970's:

the full key?

Notice the word dependencies in the last item. This is one of the main concepts that we need to consider when working to normalize. A dependency is a relationship between two attributes within an entity, such that one of them can determine the other. For example, you might have a table that contains student information, with fields for student ID and student name. This would be a dependency, because each student has a unique student ID, while there may be multiple named Bartholemew Leznorsmorphels. The student name, then, depends on the student ID. However, the reverse is NOT true.

There are several types of dependencies:

Why do we care?

Well, that depends...

Dependencies are important because they are what we need to deal with to normalize the database to make it more efficient. The process is to break the data apart, reorganizing it based on the logical relationships between the entities/attributes, to minimize the duplication of the data within the schema. Why? Because duplicated data can waste storage space, for one thing, but [perhaps more importantly] any duplicate data can lead to inconsistencies in the data. [Think about the reasons we eschew magic numbers in our code.] The idea, though, is to be discriminating about the process – we try to strike a balance between a logical, well-normalized schema, and having queries that will execute quickly and that are easy to understand and maintain.

There are several types or levels of normal forms, as they are called, which are [usually] reached by getting rid of the dependencies in the schema [which we've identified above]. Normal forms use numbers for their levels [mostly], and each successively higher number is built on the previous levels. For example, if a database is in first normal form, there will be no redundant data in any row. This means two conditions are met: 1) every row has only one value in each column; and 2) there are no columns that duplicate information. An example of something that is NOT in first normal form would be a table with the columns name, first_name, last_name, address, and house_number because there is duplicate information in the columns of each row, and multiple values in the name and address columns. To normalize this table, you would remove the name column and change the name of the address column to be street_name. Frequently this can be done within the table.

 1st 

 2nd 

Next, a database is in second normal form if there is no repeating or redundant data across any rows in the same table. For example, consider a table that keeps track of people buying books, that has the columns userID, firstname, lastname, title, author, and pagecount. When Mabel Smith buys two books, now we have duplicate information in the table, because Mabel's name is in every row that corresponds to a book she bought. Further, if she buys multiple books by the same author, THAT information will also be duplicated. Reaching second normal form frequently means we need to split up the tables so that the information doesn't get repeated. In this case, we would make a table for users that would contain Mabel's information, a second table for books that can track what user bought what book, a third table for authors which contains author information, and even a fourth table for books that links the book title and number of pages to the author[s] that wrote the book.


Next up, a database is in third normal form if there is no data in any table that is *NOT* dependent on that table's primary key. If you have normalized to second normal form, there may actually be no need to do anything to get to third normal form, because every table will already be there. However, it is a good idea to check, just for insurance. For instance, assume you have started with a table that contains addresses. If you think of the addresses as NOT related to the user but as being stand-alone things, then you can separate the addresses from the user records in the database, and provide an index in the user table to match them up. Further, you caneven break the address components out into separate tables, because when you think about it, the street address depends upon the zip code, the zip code relies on the city, and the city depends on the state. A database in third normal form will actually have separate tables for each of these entities.

 3rd 

There are several more normal forms: BCNF comes after third, and fourth comes after that. There is even a fifth normal form. However in practice, these last three are not used too often, partly because of the law of diminishing returns

. You may find out that in trying to achieve BCNF, you spend a large amount of time re-arranging things, only to find that you really haven't improved the processing or structure by all that much. So, part of what you will learn from experience, is when you should *STOP* normalizing and go with what you have.

ACID and CRUD

These are a couple of acronyms that you should remember. Remeber them because of the importance of the database concepts they represent, NOT because they are cute acronnyms. They reflect key elements that are part of every database management system [DBMS].

ACID stands for:


CRUD stands for the four basic operations that can be performed in a database:

Constraints and Stored Procedures

Constraints

Constraints in database parlance are just that: things that must be taken into account when performing any operation on the data, which may limit or even prohibit the operation to take place. In some database engines, constrains also are used [required] to designate the primary key for a table. There are several types of constraints. Here are a few that are noteworthy:

Stored Procedures

Stored Procedures are a way of programming repetitive or frequently-used database operations so that they can be done by a much simpler query that if the query was being written and executed by the underlying application. For example, instead of having the query…

      SELECT AMT_AssetTable.AssetID
            ,AMT_AssetTable.AsBuiltRevision
            ,AMT_AssetTable.AssetType
            ,AMT_AssetTable.PreviousRecord
            ,AMT_AssetTable.SerialNumber
            ,AMT_AssetTable.LastCalDate
            ,AMT_AssetTable.CalCycleDurationDays
            ,AMT_AssetTable.LastPMDate
            ,AMT_AssetTable.PMCycleDurationDays
            ,AMT_AssetTable.LocationBldgRm
            ,AMT_AssetTable.AssetDescription
            ,AMT_StatusValuesTable.StatusValue
            ,AMT_HistoryLogTable.HistoryLogID
            ,AMT_HistoryLogTable.BEMSID
            ,AMT_HistoryLogTable.EntryDate
            ,AMT_HistoryLogTable.EntryType
            ,AMT_HistoryLogTable.Title
      FROM AMT_AssetTable
      INNER JOIN AMT_StatusValuesTable
      ON AMT_AssetTable.StatusIndexID = AMT_StatusValuesTable.StatusIndexID
      INNER JOIN AMT_HistoryLogTable
      ON AMT_AssetTable.AssetID = AMT_HistoryLogTable.AssetID
      WHERE AMT_AssetTable.AssetID LIKE 'G123461';
            

Code from Microsoft SQL Server 2014


…you could have the following query…

        EXEC AMT_QueryAssetInfoWithStatusAndHistory 'G123456';
            

Not only does this make your application code much cleaner and easier to read/maintain, it also improves the security of your database, because you can grant stored procedure permissions to specific users, and keep everyone else from executing them.

Stored procedures are not available in all database engines. Some, like older versions of Microsoft Access, have no such facility. Others provide the ability, but may call it by another name. But no matter what it's called or how it's implemented, it provides you with the ability to write functions, include if/then statements and loops, and much more.

The MySQL Database Engine

MySQL is an open source database management system. It is highly popular because of its high reliability, ease of use and high performance. MySQL is used for many latest applications that are built on Apache, Linux, Perl/PHP etc. Many popular organizations such as Google, Alcatel Lucent, Facebook, Zappos and Adobe rely on this database management system.

MySQL can run on more than twenty platforms that include MAC OS, Windows, Linux, IBM AIX, HP-UX and provides much flexibility. A wide variety of database tools, services, training and support is provided by MySQL database system. MySQL comes in several different editions:

The SQL Server Database Engine

SQL Server is a RDBMS (Relational Database Management System) developed by Microsoft. This system works on Transact-SQL which is a set of programming extensions from Microsoft and Sybase. T-SQL adds other features that include error and exception handling, transaction control, declared variables and row processing. However, Sybase developed the original SQL Server back in 1980s. The final version was called SQL Server 4.2 that was developed in collaboration with Ashton-Tate, Sybase and Microsoft for OS/2.

SQL Server 2005 was launched in the month of November 2005. This version provided enhanced reliability, flexibility, security and scalability to database applications. The features provided by SQL Server are:

For Businesses, SQL server also provides Integration Services, Reporting Services, Data mining, key performance indicators, clustering support, proactive caching and repot building. It also provides integration with Microsoft Office tools [of course].

The PostGreSQL [or Postgres] Database Engine

PostgreSQL is an ORDBMS or Object Relational Database Management. It was developed at the Berkeley Computer Science Department of University of California. It is also an open source database management system and comes from the original Berkeley code. A large part of SQL standard is supported by PostgreSQL and it offers many features such as transactional integrity, triggers, foreign keys, multiversion concurrency control, complex queries and views. The user can extend the PostgreSQL by adding new index methods, procedural languages, functions, operators, data types and aggregate functions. As it is open source it can be modified, distributed or used by everyone free of cost for academic, commercial or provide use.

The differences between PostGres and MySQL are as follows:

Coming up…