Database Reference
In-Depth Information
Null means an undefined or unknown value, which means nothing is specified. For example,
consider that in the first name field someone filled in blank spaces; technically this is considered
information because a blank is a space, which is a character and therefore information.
Similarly, what if a person who didn't have an SSN entered 000-00-0000 as the SSN? This is also not
an undefined value because they entered zeros, which are characters and therefore information.
By default a created column allows Null, unless Not Null is specified at the time of column creation
explicitly.
SQL Server Data Types for Table Columns
To support business requirements, SQL Server provides various data types (see Table 3-3).
Table 3-3. SQL Server Data Types
Type of Data
SQL Server Data Type
Char, Varchar, Text
Character data
int, bigint, smallint, tinyint
Integer data
Datetime, Smalldatetime, Date, Time
Date and time
Creating a Table in SQL Server
To create a table in SQL Server, you need to establish a context to a database. In other words, you need
to get into a database where you want to create your tables. In this chapter, you created two databases:
MySqlDb and Sql2012Db. You will use the Sql2012Db database to create tables.
In Object Explorer, expand the Databases node, select the Sql2012Db database, and then click New
Query (either in the toolbar or from the context menu); you will see that the Sql2012Db database is
selected for this New Query pane you have opened.
In this query pane, type the following code:
CREATE TABLE MySqlTable
(
Name varchar(10) Not Null, --Name value is a must and can't be Null
Age int,
SSN varchar(11),
Date datetime,
Gender char(6)
)
Select the whole create statement, and click Execute or press F5. In Object Explorer, expand the
Sql2012Db database where you created this table and then expand the Tables node; you will see the
newly created table listed. This is how each database stores the tables inside it.
Also, to investigate the table you created, you can expand it. In the example case, you named it
MySqlTable, so expand that, and you will see the Columns node. Expand the Columns node as well, and
you will see all the columns you created in the previous create table statement. To help you recall, any
field or column that explicitly doesn't include Not Null with its declaration during table creation is Null
by default; therefore, except the Name column, all other fields or columns are made Null by SQL Server,
which you should be able to see in Figure 3-8.
 
Search WWH ::




Custom Search