I’ve published a new tip over on SearchSQLServer.com entitled “SQL Server encryption vs. hashing for data security“.
(It was published a couple of days ago, I just didn’t get notice until today.)
There is always a discussion going on somewhere over having NULL values in a database. Some people (such as myself) are fine with NULL values. Some people don’t like having NULL values in there database for some reason. I’ve heard all sorts of reasons; “The screw up performance”, “They are hard to deal with”, “I like using a default value better”, “Default values are easier to work with”.
The performance one is just bunk. NULL values are indexed just like any other value.
The rest of the reasons to not use NULL values ignore the fact that a NULL value is a perfectly legit value for some fields. Not all fields should allow a NULL value to be placed in them. If you are building a shopping cart and have a line items table, the Quantity and Price fields probably shouldn’t allow nulls as you should know the price and quantity when creating the invoice. However in the same shopping card application the users phone number may need to allow NULL values.
Something which is key to remember about NULL values is that NULL doesn’t mean nothing. It means unknown. There may be a value, there may not, we simply do not know what that value is. Putting in some default value will then put a value on the field when there shouldn’t be. A great example which I read recently involved a hypothetical database at a blood bank. A person comes in needing blood. They are entered into the system but the recipients blood type isn’t known. If you put in a default value of a blank value when you go to query the supply table, if there are pints of blood which have not been typed they will also have a blank value (as that is your standard default value). You’ll get a match when you run the query and possibly be giving the recipient blood which is of the wrong type. Now if you were using NULL values in the database the query would return no matching values as NULL <> NULL (under the default settings of Microsoft SQL Server). Granted we should be able to trust that the person give the recipient the blood would check the typing, but would you rather trust a person or the computer. People are overworked and usually underpaid, and nurses and doctors make simple mistakes just like the rest of us. Personally I’d rather know that the system wouldn’t find a match rather than have to worry that I’d be getting the wrong type of blood.
If you really want to display that default value when the data is returned that’s what the ISNULL system function is for. This way you don’t have to write long case statements around each field which allows NULL.
NULL values have there place, just like data does. NULL values don’t mean that we don’t care about the data, it means that we simply don’t know the value of the data as of yet. It shows that we care about the system and the people using it. How annoying is it to look at a form which is full of default values and having to read through all of them instead of simply looking at the ones which are blank.
There are some major differences between temp tables, table variables and common table expressions (CTEs). Some of the big differences are:
Temp Tables vs. Table Variables
- SQL Server does not place locks on table variables when the table variables are used.
- Temp tables allow for multiple indexes to be created
- Table variables allow a single index the Primary Key to be created when the table variable is declared only. There is an exception to this, that if you can create the index inline, for example by creating a unique constraint inline as shown in the comments. However these indexes (and the table variable in general) will always be assumed to have 1 row in them, no matter how much data is within the table variable.
- Temp tables can be created locally (#TableName) or globally (##TableName)
- Table variables are destroyed as the batch is completed.
- Temp tables can be used throughout multiple batches.
- Temp tables can be used to hold the output of a stored procedure (temp tables will get this functionality in SQL Server 2008).
Table variables and Temp Tables vs. CTEs
- CTEs are used after the command which creates them.
- CTEs can be recursive within a single command (be careful because they can cause an infinite loop).
- Table variables and Temp Tables can be used throughout the batch.
- The command before the CTE must end with a semi-colon (;).
- As Temp tables and table variables are tables you can insert, update and delete the data within the table.
- CTEs can not have any indexes created on them, source tables much have indexes created on them.
If you can think of anything that I’ve missed, feel free to post them in the comments.
Doing a code freeze at this time of year can be crucial to keeping your sanity this time of year. Often times the business unit likes to push out last minute changes before the year end. This often means pushing last minute code (which as everyone knows isn’t always perfect) which can lead to unpleasant little phone calls from the business or the NOC in the middle of holiday festivities.
Do your self and your family a favor, push for a code freeze until after the new year. The developers will thank you, the business with hate you (but really what else is new). I like about a three week code freeze (emergency bug fixes are except, but they have to actually be bug fixes, and they have to be an emergency) starting about December 15 ending about January 3 (depending on when the weekends fall).
It can be tough to get a code freeze implemented at first. But after the first year with the code freeze everyone will want to go it the next year.
We’ll I’ve finely gotten around to installing the SQL 2008 November CTP. While walking through the installer I have seen some excellent changes. Not only is the option to change the paths of the data files no longer hidden, there are more than just two options. There are at least 7 paths that you get to specify while installing. The first one is the Shared component directory. This appears to be the base path where all your binarys will be based off of.
Next you get the Instance root directory. This is where the system database files will be based off of, so make sure that you don’t point this to the C drive is you want the system databases on another drive.
A couple of screens later you get to set 6 install paths. The first is the data root directory. This changes the base path for all the others options. The others are the User database folder, user log database folder, tempdb data folder, tempdb log folder, and the backup directory.
This amount of flexability in the installer is a first, and it’s a welcome change. Look for more posts, tips and articles about SQL Server 2008 in the comming weeks and months up till the release.
If you have looked at pretty much any Microsoft provided T/SQL code you have probably seen an N in front of a string value when setting a variable much like this.
WHERE name = N'master'
What this N does is tell the SQL Server that the data which is being passed in is uni-code and not character data. When using only the Latin character set this is not really needed. However if using characters which are not part of the basic Latin character set then the N is needed so that SQL knows that the data being given it is uni-code data.
The path that Microsoft is going says yes. They are moving all there samples and defaults to using uni-code. In reality uni-code may not be needed in your environment. If you are storing only Latin characters (the US alphabet characters) then you probably don’t need to store uni-code characters. However if you are planning on moving your operations to support customers who do not speak English then you will want to setup your database and application to use the uni-code data types (nvarchar, nchar, ntext).
It doesn’t take much extra to use uni-code data types, however do keep in mind that it does require more disk space to use uni-code data types. For each character saved two bytes of disk space are used instead of one byte. While this may not seam like much space if you will have a large table with a large ntext field this can use a lot of space quickly.
I know that this is normally a technical blog, but with the US Thanksgiving day holiday coming tomorrow I wanted to be sure to point out that time needs to be made out of our busy work schedules to make sure that we spend time with our families. It’s very important that we find a good balance between work and family.
Microsoft announced at TechEd in Barcelona, Spain that the SQL Server 2008 November CTP would be made available this month. Read more here.
Microsoft has just posted the URL to download the new CTP. http://www.microsoft.com/downloads/details.aspx?FamilyId=3BF4C5CA-B905-4EBC-8901-1D4C1D1DA884&displaylang=en