Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When you're designing databases, it's a good idea to apply as many rules or constraints to your data as possible. And one of the classic ways to do this is by setting up default values for your columns. The idea of this of course is simple. We have a table such as an OrderItem table made of product IDs and quantities and unit prices. Whereas a lot of this information is very different from row to row, Quantity for example might typically be the number 1. Occasionally it might be 2, but nine times out of ten or nineteen times out of twenty or ninety-nine out of a hundred it will be the number 1.
So we will make this the default value for that column, so it doesn't always have to be entered, either by the application or by the user. I'm going to go into my OrderItem table in SQL Server Management Studio and select Design. Applying a default value is very simple. Find the column that you're looking for, in this case Quantity, and then down here in the Column Property section, you'll find a section that says Default Value. We hit the number 1. That's counted as a change to the database definition.
So you'll see that we have the asterisk up here. I am going to hit Save to save those changes to the table. Close that down and now let's test it. I am going to right-click and say Edit Top 200 Rows and I am going to enter in a new order item. I am, of course, just going to do this manually. Now, right now I have OrderItemID added as an identity column for a primary key, which means it will generate that too. Hopefully, the number 19 or the number 20. I am going to put in ProductID-123. I'm going to Tab to skip over the Quantity and I am going to say the UnitPrice is 54.98.
Then I am going to tab off on to next row. Now you will quite often see this message where the row itself kind of shows up in a pale color and the exclamation mark, if I tooltip over it, it will say, "This row was successfully committed to the database. However, there was an issue with retrieving the data back after the commit, because the displayed data is read-only." But it almost certainly worked. So I'm going to just come out of this and go back in and just select the Top 1000 Rows again, just to double check that.
I can see that yes, at position 19 we have ProductID 123 with a default value of 1 at 54.98. Now bear in mind though that the only real point of working with the default value is it will have an effect when a row is being added to the table. If I was editing rows and I just decided, for example, to leave a blank value, it's not going to dump in a new default value for a row that already exists. So I will hit Escape there.
The only place where default values can have an effect after the fact is when we're adding a new column. Let's say going back into OrderItem. I am going top hit Design and I am going to add a new column here that I will call DateCreated. I want this to always have a date or indeed a datetime. I will use datetime2 and uncheck Allow Nulls. I always want a value in this column of when that row was created. When I make that change, if I'm trying to save my changes back, I'm going to get this Post-Save Notification pop-up.
It was unable to modify the table. It gives me a long-winded explanation that really comes down to you can't create a column and say it's not allowed to have null values and then add it to lots of existing rows with null values. Well, one of the ways we can use the default value here is I'm going to say the default value is getdate. This is the default value of DateCreated. If I now save this, what will happen is it's going to use that rule for any new row.
Because we've also used it for pushing a change to this table, it's going to use that value for the old rows as well. Just to close down my existing information, I will then right-click and reopen it up. And I can actually see that the DateCreated has been filled out for all those old rows. From this point forward if I add a new row, ProductID 456, I will skip over Quantity because that should give me a default value. I will do a UnitPrice of 98 and I will skip over the DateCreated.
If we then close that down and just reopen it, I'm going to see that I have both the default value of 1 and the DateCreated of just a moment ago.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 91091 Viewers
80 Video lessons · 138152 Viewers
59 Video lessons · 56964 Viewers
52 Video lessons · 70598 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.