From the course: SQL Server Fundamentals: Master Basic Query Techniques

SQL data types and nullability - SQL Tutorial

From the course: SQL Server Fundamentals: Master Basic Query Techniques

Start my 1-month free trial

SQL data types and nullability

- [Instructor] The last thing that we need to cover before we can move on to write some code are the concepts of data types and nullability, which have to do with the rules around how data is stored in each column of a table. Data types define what kind of information is stored in a particular column, as well as how much precision is allowed in that data point. There are a number of different options available in SQL Server and we will only cover a handful here, but the basic ones are broken down into numbers, strings and characters, dates and times, and true/false values. For numbers, we can store whole numbers with the data type INT, and we can store decimals using either DECIMAL or NUMERIC, which are pretty much interchangeable, so you can decide whichever one you prefer. With the decimal values, you can specify how many places you want to allow after the decimal, using two numbers like you see here. The first number indicates the maximum number of digits you'll allow in the number, and the second tells you how many places will be allowed after the decimal. For strings and characters, we can store a fixed number of characters in a column using the CHAR data type. And you would use the number after to define how many characters would be stored. If the number of characters in a column will vary from row to row, you would use VARCHAR instead and define the max length of your string there. When you want to record dates and times, you have a few different options. You can store just the date using the DATE type, just the time using TIME, or you can store those two points together using DATETIME or DATETIME/2. DATETIME/2 just has a little more precision. And then, if you need to bring time zones into the mix, then you could use the DATETIMEOFFSET. Lastly, we've got true false values, which are called BITs, where the only permissible values are zero and one, where zero means false and one means true. So those are some of the different ways that we can store data in a table in SQL Server, but there's one more rule that we need to consider, which is whether or not the column is nullable. We deal with the concept of NULL in a couple of different ways in SQL, but in regard to its use at the table level, it answers the question of whether or not this is a required data point. The term NULL just means that there is nothing stored in that column. So if a column is defined as NULL that means that it is optional to have something saved there and logically NOT NULL means that you must provide input for this data point. And with that, we have made it through all of our database design concepts and we can get to actually writing some code.

Contents