DUE TONIGHTfaculty.cascadia.edu/cduckett/bit275/lecture_05/BIT275... ·  · 2018-04-10• MID-TERM...

64
Instructor: Craig Duckett Lecture 05 : Tuesday, April 10 th , 2018 Transactions, Acid Test, DML, DDL 1 Assignment 1 DUE TONIGHT

Transcript of DUE TONIGHTfaculty.cascadia.edu/cduckett/bit275/lecture_05/BIT275... ·  · 2018-04-10• MID-TERM...

Instructor: Craig Duckett

Lecture 05: Tuesday, April 10th, 2018

Transactions, Acid Test, DML, DDL

1

Assignment 1DUE TONIGHT

2

Assignment 1 is due TONIGHT LECTURE 5, Tuesday, April 10th

in StudentTracker by MIDNIGHT

• MID-TERM EXAM is LECTURE 10, Tuesday, May 1st, 2018• Assignment 2 is due LECTURE 12, Tuesday, May 10th, 2018

in StudentTracker by MIDNIGHT

3

3 x 150 Points (450 points Total)

• Assignment 1 (Stage 1): DUE TONIGHT Tuesday, April 10th

• Assignment 2 (Stage 2): DUE LECTURE 12 Tuesday, May 8th

• Assignment 3 (Stage 3): DUE LECTURE 20 Tuesday, June 56th

• Database Presentation: DUE LECTURE 20 Tuesday, June 5th

4

Tuesday (LECTURE 5)

• Database Design for Mere Mortals: Chapter 4

Thursday (LECTURE 6)

• The Language of SQL: • Chapter 5: Sorting Data• Chapter 6: Column Based Logic

5

• Transactions• The Acid Test• Data Manipulation Language (DML)• Data Definition Language (DDL)• Normalization• Denormalization

6

Transactions and the ACID Test

Transactions and the ACID Test

When we're entering or changing data in our databases, we'll often need to work with

transactions. A transaction is an incredibly important thing in the world of databases. And tounderstand them, the best way is to think about what we think of as a transaction in the realworld. If you talk about making a transaction, you often mean something commercial. You handover $15 to a bookseller, the bookseller gives you the book. That's the transaction, and it'simportant that both of those things happen. If you hand over the money, you expect to get thebook. If they hand you the book, they expect to get the money. Either both of these thingshappen, or neither of them do.

Now in a computing system, a classic example of transactions is a banking system. Imagine thatyou log on to your bank's website, and you want to transfer $2,000 from one of your account toanother.

Transactions and the ACID Test

Now, that's going to require two updates to this data, one to subtract $2,000 from the savingsaccount, and the other to add $2,000 to the checking account. Now if the first part of this happenssuccessfully, subtracting $2,000 and then we try and add $2,000, but there's a problem with thesecond part, perhaps it's locked for editing. Well, we will need to reverse the first part of thistransaction.

Transactions and the ACID Test

We never want to be in a situation where $2,000 has been debited from the first account, but itdidn't get added to the second. So, a transaction is how you define a combined unit of work, eitherboth of these things happen or neither of them do, and the first change will be reversed instantlyby the database if any part of the transaction fails.

Transactions and the ACID Test

Now, there's a common acronym that you'll come across when working with transactions in adatabase, ACID. A transaction must be Atomic, Consistent, Isolated, and Durable.

Being Atomic, and this is the Greek word atom meaning an indivisible unit, it refers to the coreidea that this transaction must completely happen or not at all. So, whether there are two steps inthe transaction or 20 steps, they're all contained within the transaction. They either all completesuccessfully, or they all return to the original state.

There is no such thing as a transaction that halfway occurs.

Transactions and the ACID TestNow whether the reason for the transaction failing is that thedatabase had a power failure or ran out of space to hold anew data, or there was an application error, it doesn't matter.

Atomic is the all or nothing rule. Now Consistency meansthat any transaction must take the database from one validstate to another valid state based on the rules of thedatabase. So, even if a transaction is successfully atomic, itstill cannot result in a situation that violates any of theintegrity rules defined in a database.

Isolation refers to the data and the transaction being essentially locked for that moment in which thetransaction is occurring. So, while we're trying to change a balance on one of our account records, anotherpart of the system must not be allowed to access that data until the first transaction has finished. And

durability refers to the transaction being considered robust. If it happens and the database says thistransaction has happened successfully, then the transaction is guaranteed.

Say if you go to a travel website, purchase a flight on an aircraft, and you're guaranteed seat to A, then youshould be able to regard that transaction as being durable. Even if half a second later, the database suffersa power failure and shuts down, when it reboots, that transaction will have survived that failure and theyare not going to sell that seat to someone else simply because there was a glitch in the system half asecond after you made your purchase. Now, the great thing about working with most DatabaseManagement Systems is these capabilities are built into the system. You don't have to worry about how toprogram these. You just need to know when to tell the database, this is a transaction; these three things,these four things, these 10 things must be done together. That's what we'll see how to do a little later on.

12

Data Manipulation Language (DML)

Data Manipulation Language (DML)

A data manipulation language (DML) is a family of computer languages includingcommands permitting users to manipulate data in a database. This manipulation involvesinserting data into database tables, retrieving existing data, deleting data from existing tablesand modifying existing data. DML resembles simple English language and enhances efficient userinteraction with the system.

So far, we've been focused on using the SELECT statement to read information out of a database,but that begs the question, how did the data get in there in the first place? We have severaldifferent keywords in SQL for inserting, updating, and deleting information. Now, this goes backto the idea that any computer system, not just databases that deals with storing data needs toprovide four fundamental functions, the ability to Create, Read, Update, and Delete often using

the acronym CRUD.

Data Manipulation Language (DML)Data manipulation languages have theirfunctional capability organized by theinitial word in a statement, which is almostalways a verb. In the case of SQL, theseverbs are:

SELECT ... FROM ... WHERE ...INSERT INTO ... VALUES ...UPDATE ... SET ... WHERE ...DELETE FROM ... WHERE ...

Well, if you want to work with CRUD in SQL, we don't use these exact words here. There are acouple of them that are different.

We know already that when we want to read information out of the database, we use the SQLkeyword SELECT, and if we want to create, we're going to use the SQL keyword INSERT.

Now update and delete there's no conflict there. They are just UPDATE and DELETE in SQL.

Data Manipulation Language (DML)So, let's take a look at the ones we haven't covered yet. I'm going to talk about inserting first. So,the assumption is we're inserting a new row of data into a particular table and the generalformat is INSERT INTO, then your table name, then a list of columns and then the VALUES thatyou want to insert into those columns.

Data Manipulation Language (DML)So, if we're going to insert into this simple imaginary Employee table, I'll use the name of thetable, and then inside parenthesis, the name of the columns that I'm going to provide values for.

Data Manipulation Language (DML)So, if we're going to insert into this simple imaginary Employee table, I'll use the name of thetable, and then inside parenthesis, the name of the columns that I'm going to provide values for.

Now, you specifically name columns because you don't always provide data for every singlecolumn in a new row. Some of these columns might be null. Some columns might provide theirown default values and quite often you might have some columns that are automaticallygenerating primary keys, and you certainly don't want to try and insert your own values in thosesituations. So, you name the columns you're going to provide values for.

Data Manipulation Language (DML)And then after the keyword values, you provide those values. If there are strings they need to bein single quotes, if they are numeric key, you don't need the quotes.

The columns, and you values must match in number, in order, and type. You can't say you're going to provide five columns and then only provide values for four. And also the data types must match. If the column is defined as a number, you can try and insert a string into that column.

Data Manipulation Language (DML)

If I go ahead and execute this, what it's going to do is create a new row in that table. Now, I'mmaking the assumption here that the way the database has been defined, Employee ID will bean automatically-generating primary key. So, we didn't need to provide a value there. Hire Date?We probably could have provided one, but I'm imagining that it was set up to do a default valueof the today's date for this row to be created. And let's imagine that email was allowable to havenull values there. So, if we didn't provide one, it's just null.

Data Manipulation Language (DML)If I go ahead and execute this, what it's going to do is create a new row in that table. Now, I'mmaking the assumption here that the way the database has been defined, Employee ID will bean automatically-generating primary key. So, we didn't need to provide a value there. Hire Date?We probably could have provided one, but I'm imagining that it was set up to do a default valueof the today's date for this row to be created. And let's imagine that email was allowable to havenull values there. So, if we didn't provide one, it's just null.

We don't have to rank manual insert statements for every new row in our database. It's muchmore likely that this process will be done programmatically. The people will be using a morepleasant user interface to enter data, an application, or web-based portal that used PHP or ASPpages, but this is the kind of insert statement that's going on behind the scenes.

http://faculty.cascadia.edu/cduckett/bit275/documentation/ToyAuction.sql.txtEXAMPLE:

Data Manipulation Language (DML)We know we can read information out of the database, but what if we want to changesomething? It's quite often that that we'll change information in the database once it has beencreated, and we use the update statement for that. The format here is UPDATE, table name.You're going to use the keyword SET, and you're going to change a particular column. So, whatcolumn and what value do you want it to be and then almost always you'll be using a WHEREclause. This is exactly the same as doing where in the select statement that allows you to limitthe amount of rows that this update will apply to.

Data Manipulation Language (DML)So, an example would be, if we have that row that we just inserted, and I wanted to update it tochange the email column, I'd say UPDATE the Employee table. I'm going to set the column codeEmail equal to, provide the string value, and then the WHERE condition.

Data Manipulation Language (DML)In this case, the easiest way would be EmployeeID equals 734, and if we run that it will reachdirectly into that row, and change that one value.

Now technically, you don't have to have a WHERE clause, but just as leaving off a WHERE clausein the Select statement would return all the rows in that table leaving off a WHERE clause in anupdate statement would mean that you would set this column value for every single row in thattable. Now, occasionally, that is what you want in an update, but more often you will restrictyour updates to one row or a handful of rows. So, we use WHERE clauses for that and usingwhere in an update, it's exactly the same as using it in a select

Data Manipulation Language (DML)Finally, DELETE.

DELETE is actually the simplest because it's all the about the where. The format is DELETE FROM,particular table name, WHERE condition. We don't have to name any columns in this querybecause delete just works on rows. If you delete a row, you delete an entire row. You can'tdelete part of a row. So, there's no point in naming any of the columns here.

Data Manipulation Language (DML)If you just want to blank out some information in an existing row, well, that's an update. So, that WHERE clause and the DELETE just defines which rows and which table you're going to delete.

So, we'd say to DELETE FROM Employee, WHERE. In this case EmployeeID equals 734. This is about as specific as we could possibly get.

Data Manipulation Language (DML)Now, you can delete multiple rows at a time the same way that you could use a where clause toselect the multiple rows at the time, but for heaven sake take care with your delete statements,almost everyone who has worked with the databases for a while has a horror story about theDELETE statement that was written a little to casually. Because if you write something as simpleas this and execute it, the database management system is just going to say, "Well, you're theboss and go ahead and delete every single row in this employee table." It won't ask, are yousure? It won't ask you to confirm anything. It will just do it. DELETE is a very powerful keyword.

Data Manipulation Language (DML)So in this case, I'll do a select staff, an employee where employee ID equals 734 and just confirm that the result I would expect back, in this case one row, well, that's what would also affect if I was doing an update, and that's what I'd affect if I was doing a delete.

So, I'll create it as a select first and then change the select box to delete. Execute that, and we goto bit better faith that what we're actually affecting is just one row. So, to good practice to makesure the where clause is only selecting the rows you expect before you turn it into a delete or anupdate statement.

28

Data Definition Language (DDL)

Data Definition Language (DDL)

Now the majority of SQL that you'll writewill be things like SELECT statements,INSERT, UPDATE, and DELETE but that's notthe whole picture. These keywords arelumped together as the DML part of SQL.Data Manipulation Language is that part ofthe sequel language that deals withworking with and manipulating data in yourtable. So, selecting it, inserting it, updatingit, deleting it, but none of these wordsactually allow us to alter the structure ofthe database itself.

Data Definition Language (DDL)

So, how do we create a new table?How would we say what columnsexist? How would we define primarykeys? We can't use any of thesewords for that. But that can be donein SQL using a part of the languagethat is lumped together under thephrase DDL or Data DefinitionLanguage. The three key words thereare Create, Alter, and Drop. Thesewords let us change the structure ofthe database itself. Let's take a lookat a couple of them.

Data Definition Language (DDL)

If I want to write SQL to create a new table, this is the format that I'll use. The word CREATE,and then the table name you just make it up, it's whatever you want to call it and then inparentheses, the definition of your columns. This is after all what a table is it's simply namingthe columns and giving them data types in any rules they must comply to.

Data Definition Language (DDL)

So, if I wanted to create a new employee table, I would say CREATE Employee and then insideparenthesis, here's how I would provide column definitions. We start off with EmployeeID as anINTEGER which is PRIMARY KEY. And these are key words we're using to tell SQL theimportance of and the rules these columns must comply to. I'm not going to get into thespecific of these for reasons I'll cover in just a moment, but the general format should be atleast understandable about what this would do. It's going to create a new table with fivecolumns, an integer, three VARCHAR, and that means the variable length character field. I canrecognize even phrases like NOT NULL or NULL. So, it looks like we don't have to put anything inthe department column and then we're finishing off with salary as an integer. Now, obviously ina production database, these can be far more complex than this but this is the basics of how it'sdone.

Data Definition Language (DDL)

Now, if we move on a little bit and realize we need to change the definition of it, we can use thekey word, ALTER. Now, typically would alter a table. If you give it the table name and then yougive it a bit of information about what you're trying to change. In this case, I'm saying I'madding a new column. That column is called Email. It's going to be a variable length characterstring of 100 characters.

Data Definition Language (DDL)

If I have a problem with what I created earlier, I can use the last of the three keywords which isDROP. And we would simply say something like DROP TABLE Employee. We wouldn't need anyWHERE clauses, any column definitions. We're simply saying destroy the entire table, again,something to be very, very careful about working with.

Data Definition Language (DDL)

Now, I wanted to introduce these phrases, but I'm really not going to spend much more time on these wordshere for two reasons. First, many people who work on the actual design and structure of the database willuse a visual application like PHPMyAdmin (or MySQL Workbench) or SQL Server Management Studio,rather than hand coding create an ALTER statements.

It's true that if you're in a database administration role you all might then spend a lot of time with thesekinds of data definition statements, CREATE, ALTER, and DROP, but regular day to day developers working inthese databases typically don't. You shouldn't be spending most of your time changing the definition of thedatabase. You should be spending most of your time manipulating the actual data in it.

It's worth mentioning that there are a couple of other keywords in SQL that are typically lumped togetherunder the idea of data control keywords. This is where you can either grant or revoke permissions forpeople in the database. This is not an area we're going to cover in this course because that really gets intothe specifics of things like the operating system and the authentication schemes that you're using for yourparticular database. So, it's just not general content.

36

Normalization

http://www.engr.sjsu.edu/fayad/current.courses/cmpe138-Spring04/docs/CmpE226-DB-L10n3-Normalization-5-rules.doc

http://marcrettig.me/wp-content/uploads/2017/04/rettigNormalizationPoster.pdf

Normalization

Once we've started to plan out our tables, our columns, and relationships, we do somethingcalled Database Normalization. This is a process where you take your database design, and youapply a set of formal criteria of rules called Normal Forms. These were developed about 40years ago mainly by Edgar Codd, the father of relational databases. And we step through them1, 2, 3, first normal form, second normal form, and third normal form. There are others butthese are the important ones.

Normalization

Normalization should be carried out for every database you design. It's really not that hard,even though, yes, when you first start reading about database normalization, you'll run intophrases like:

But you don't have to get into all this language unless you are mathematically inclined. Theentire point of normalization is to make your database easier and more reliable to work with.You usually will end up creating a few new tables as part of the process. But the end result isyour database will contain a minimum of duplicate or redundant data. It will contain data that'seasy to get to, easier to edit, and maintain, and you can preform operations, even difficult oneson your database without creating garbage in it, without invalidating the state of it.

If you're a working database administrator or database designer, you can do normalization inyour sleep. It's a core competency of the job. It's important. And as you'll see, we've alreadybeen doing a little of it.

First Normal Form (1NF)

Before we apply the first set of criteria, what's called first normal form, often shortened to1NF, I'm taking as a given that we already have our columns and our primary keys specified.

First normal form says that each of your columns and each of your tables should contain onevalue, just one value, and there should be no repeating groups.

Okay, what does this actually mean?

First Normal Form (1NF)

Well, let's say I begin developing a database for my company and one of my tables is anEmployee table, very simple stuff, EmployeeID, LastName, FirstName, and so on. And weallocate every employee a computer.

I want to keep track of that, so we'll add a ComputerSerial column to keep track of who haswhat. Now, this is actually okay right now. This technically is in first normal form. Here's theproblem.

First Normal Form (1NF)Let's say I figured out that some of our employees need a Mac and a PC to do the testing. Othersneed a desktop and a laptop. So, several people have multiple computers, and I want to keep track ofall of them. There is a couple of ways that I could deal with this. I could just start stuffing extra datainto that one column. We could start putting commas or vertical bars or any other delimiter and putin multiple values in the one ComputerSerial column.

This is just something you just don't do in Relational Database Design. We're violating first normalform.

Understand the relational databases will happily deal with hundreds of tables. Each table could havehundreds of columns and millions of rows. But they do not want columns that have a variableamount of values. You would find it hard to search directly for a serial number. You'd find it hard tosort. You'd find it hard to maintain. So, it's not in first normal form if you do this because first normalform demands that every column, every field contains one and only one value.

First Normal Form (1NF)So, what we might do then is go back to the original way, and instead start adding new columns. So, ComputerSerial2, ComputerSerial3, this is what's called a repeating group, and there should be no repeating groups. The classic sign of a repeating group column is a column with the same name, and the number tacked onto the end of it just to make it unique, because usually this is a sign of an inflexible design.

Sure, if we could guarantee that there would only ever be two or three, that's fine. But what happens when we want to add the tablet and the smart phone? What happens when one employee manages testing and needs to be associated with six computers? We don't want to require a change to the database schema just because we buy a new computer. So, what do we do here?

First Normal Form (1NF)

Well, what we do is the same thing for a lot of these normalization steps. We'll take this dataout of the Employee table, and put it in its own table.

First Normal Form (1NF)

This then has relationships. We create a one-to-many relationship between employee, andthis new computer, or it could be called an asset table or whatever else makes sense. And ithas a foreign key back to the Employee table. I can take any EmployeeID like 551, follow it tothe Computer table, and find his two computers or 553, find his three computers, there are norepeating values, no repeating groups in either table. And this will get us into first normalform.

First Normal Form (1NF)

This then has relationships. We create a one-to-many relationship between employee, andthis new computer, or it could be called an asset table or whatever else makes sense. And ithas a foreign key back to the Employee table. I can take any EmployeeID like 551, follow it tothe Computer table, and find his two computers or 553, find his three computers, there are norepeating values, no repeating groups in either table. And this will get us into first normalform.

Now, it's very common that the solution to a normalization issue is to create a new table.Sometimes, it's a one-to-many relationship like this, other times it might even require a many-to-many with a linking table.

Second Normal Form (2NF)

Before you attempt to go into second normal form or 2NF, well first, you have to be in firstnormal form. You don't pick and choose between them. You go through this one, two, three.Now whereas first normal form is about the idea of repeating values in a particular column,second normal form, and third normal form are all about the relationship between yourcolumns that are your keys, and your other columns that aren't your keys. The second normalform has the rather puzzling official description that any non-key field should be dependenton the entire primary key. And that is about as simple as it can get phrased.

Second Normal Form (2NF)

Now, when I say the word field, it usually refers to the idea that the actual value in a particularcolumn position for a particular row. But what does this actually mean? Well, for most of whatwe've done in this course, this actually won't be an issue for us. Second normal form is onlyever a problem when we're using a Composite Primary Key. That is a primary key made of twoor more columns. So, let me show you a table that currently is in first normal form but not insecond normal form. Going back to the idea of a database for a training center, I have anEvents table here that has an ID of a Course, a Date, CourseTitle, Room, Capacity,AvailableSeats, and so on. Now, what's actually happening here is this table has been definedto use two columns as the primary key. It's a composite primary key.

Second Normal Form (2NF)Now, the issue with second normal form is that if you use a composite key, you need to lookclosely at the other columns in this table. So, going along to my non-key columns, I haveCourseTitle, SQL Fundamentals, Room 4A, Capacity is 12, there are 4 seats available. A lot ofthis information would be unique to this one entry, this one course on this particular date.That's fine. But second normal form asks that all of my non-key columns, everything that isn'tpart of the key, so Course Title, Room, Capacity, Available, they all have to be dependent onthe entire primary key. Now, that is the case for Room and Capacity and Available. These areunique values based on the fact that we're running this particular Date, this particular Course,and this particular room with a certain number of seats available. It will always be different.But CourseTitle, well, I could get that just from half of the key. I could get that just from thefirst part of the key. It has no connection to the Date whatsoever. SQL Fundamentals willalways be based on SQL101.

Second Normal Form (2NF)

It doesn't matter if it's being run in March or April or May. Now, this might sound a little bitivory tower. But here would be the impact.

What happens if somebody reached into this table, and they changed that Course ID, but theydidn't change the title? Now, we've got a conflict. We might have the wrong title for the wrongpiece of data. That's because my data now isn't in second normal form, and we're trying to fixthat conflict from ever happening. So, how do we fix it?

Second Normal Form (2NF)

Well, once again, we're going to rip out the CourseTitle. We're going to create a separateCourses table, where we want to again map the ID of the course into its own row. So, we'llalways have one specific title for one specific ID. And then we create a one-to-manyrelationship between Events and Course. And removing that from the Event table means thateverything in that table is now based on the entire key, particular course, at a particular datewhich may have a different room or different capacity, different number of available seats.

Third Normal Form (3NF)

Now, let's take a look at the third normal form or 3NF. Well, as plainly as this can be described,it's that no non-key field, meaning, a column that is not part of the primary key is dependenton another non-key field. It is in a way similar to second normal form. Second normal formasks can I figure out any of the values in this row from just part of the composite key? Whilethird normal form asks can I figure out any of the values in this row from any of the othervalues in this row? And I shouldn't be able to do that.

Third Normal Form (3NF)

Let's take a look at an example. I've got this updated version of the Events and Courses tablefrom the previous example. So, it's in both first normal form, it doesn't have any repeatingvalues or repeating groups, and it's in second normal form. Meaning, there's no part of thisthat's dependent on just on a piece of the key. What I need to do for third normal form is lookat my non-key fields, Room, Capacity, Availability. If I scan the entire row, let's take the firstrow, we've got SQL101 course occurring on the 1st of March. There is apparently 4 seatsavailable. It's in Room 4A with a capacity of 12. Now, this is at a first look at it perfectlyacceptable, because this course could be being scheduled in a different room every time with adifferent number of available seats as we start to sell different seats for a particular date.That's all okay.

Third Normal Form (3NF)

Here's the problem. It's between Room and Capacity. These are both non-key fields. Thesecolumns aren't part of the primary key. But if I look down the column for Room, I see 4A has12 seats capacity, 4A has 12 seats, 7B has 14 seats. So, if every time we're in Room 4A, wealways have 12 seats or every time we're in 7B, we always have 14 seats. I don't need to repeatthat information. I could figure out capacity from Room and Room alone. I have one non-keyfield that is based on another non-key field. So, we don't need these to be stored in the sametable.

What we need to do is, you guessed it, split some of this information out into its own table.

Third Normal Form (3NF)So, we need to pull out Capacity from the Event table, and just keep Room. And that's as longas Room will always tell us a fixed capacity, we'd create our own table for it, 4A always has 12,7B always has 14, and so on. Now, we're in third normal form, no non-key field is dependenton another non-key field.

Now, as you're seeing, it's all about the redundancy of the information. This is what we'retrying to do with normalization.

Third Normal Form (3NF)Now, another example of third normal form would be something like this, which is verycommon.

Let's say we've got an OrderItem table, which is calculating different parts of an invoice. So, ithas a ProductID with a Quantity, a UnitPrice, and a Total.

Third Normal Form (3NF)Now, you don't have to worry about how this might relate to different tables. All I'm interestedin looking at is this part. We've got Quantity for UnitPrice of $10, Total is $40. Here is the issue.We can see that Total is based purely as on Quantity times UnitPrice. Now, Quantity andUnitPrice are both non-key fields. So we're figuring out Total from these other two non-keyfields. We don't need to do this. We don't need to store this in the database. We don't want tostore information in your table that's easily ascertained by adding other non-key fieldstogether, or in this case multiplying them. One of the main reasons for this is to prevent anyconflicts.

If in this example I have a row that says we have a Quantity of 4 and the UnitPrice of 10, butthe Total says 50, well, where is the problem? There is a problem. How do we do it? Howwould we fix it?

Third Normal Form (3NF)Is the Total wrong or is the Quantity wrong? Your data doesn't make sense anymore. So, wewould remove that Total column form this table. We can figure it out when we need to figure itout. Now, third normal form will help you figure out these potential problems.

Now just a quick side bar, in cases like this where you might find a total useful in the table,many database systems offer you the option of defining a computed or calculated column. It'snot actually stored in the database, it is a convenient read-only fiction. Its value isautomatically calculated based on the other columns in the table, and you may find that usefulfrom time to time.

58

Denormalization

OPTIONAL SLIDES

Database DenormalizationSo, we should always take our database design through the first, second, and third normalforms. There are more criteria available. There are fourth, fifth, and sixth normal forms.There's something called Boyce-Codd normal form. But taking it to third normal form is theusual expectation in a business environment, and certainly all we need to cover in a course likethis one. Now, you will actually find a lot of tables out there intentionally break normalizationrules and some others seem like they do but they actually don't.

Denormalization

Here's one example. Let's say we've got an Employee table, and I'm storing an Email and a Phonenumber.

Technically, this can be described as breaking first normal form. It's a repeating group. But in practice,you may find it more convenient to just allow an Email and Email2 column or perhaps a HomePhoneand MobilePhone column rather than splitting everything out into multiple tables and having tofollow relationships every single time you read or write this data. This will be referred to as a de-normalization decision. You're consciously making the choice that something could be normalizedout into another table. You could follow the official rules. But for convenience and/or forperformance, you're not going to.

Denormalization

Normalizing a table like this, thinking that I've immediately spotted a non-key fielddependency, that would actually be taking it too far and making things more inconvenient.

And the question is you really want to understand your data before you can make all thesechoices whether to normalize or de-normalize. And you might de-normalize to make things abit more efficient, but do it knowingly instead of accidentally.

Denormalization

These really the three steps that we would go through, first normal form, second normalform, and third normal form.

First being about having no repeating values and no repeating groups, second normal form, novalues based on just part of say half of a composite key, and third normal form, none of yournon-key values should be based on or determined from another non-key value.

Taking your database design through these three central criteria will vastly improve the qualityof your data.

Denormalization

one example that can seem like a normalization and/or de-normalization issue but really isn'tany table that's full of address information. This situation can be a little deceptive. If I look at atable like this, and I can see I've got Zip code being stored as the last column here.Theoretically, I could figure out what the City, and the State are just from the Zip Code, if Iseparated them out into their own table. So technically, I have non-key fields, City and Statethat are dependent on another non-key field, Zip, that could be figured out from Zip alone.However, this kind of case is not the full story because while it might be true 99% of the timethat a Zip code maps to a particular City or Town, there are some cases where multiple townsor cities are allowed in the same zip code, some Zip codes even cross multiple states.

64

ICE 05: More World Database Queries