The Columns of a Table



In our study of data sets and in our introduction to tables, we saw that a list could be organized in categories called columns. Here is an example:

Name Age Gender Relationship
Judie 18 Female Sister
Ernest 24 Male Cousin
Bill 52 Unknown Uncle
David 36 Male Brother
Hermine 12 Unknown Niece


As you can see from this arrangement, a column is used to particularly classify one type of data. For example, one column can be used to list some names. Another column can be used to list numbers. Yet another column can be used for a select list of items that keep repeating.

To organize the information that a column holds, a table needs a series of details about each column. Two aspects are particularly important: a name and the type of data that a column should/must/can hold.

The Name of a Column

To be able to recognize the categories of information that a column holds, the column must have a name. In the Microsoft SQL Server Management Studio, the name of a column displays in the top, the header part, of the column. The name of a column allows the database as a file to identify the column. The name of a column also will help you, the database developer, to identify that column. There are rules and suggestions you must or should follow when naming the columns of a table.

The name of a column:

  • Can start with a letter, a digit, or an underscore
  • Can include letters, digits, and spaces in any combination

After respecting these rules, you can add your own rules. In our lessons, here are the rules we will use to name our columns:

  • A name will start with a letter. Examples are n, act, or Second
  • After the first character as an underscore or a letter, the name will have combinations of underscores, letters, and digits. Examples are n24 or col_52_t
  • Unless specified otherwise, a name will not include special characters such as !, @, #, $, %, ^, &, or *
  • If the name is a combination of words, each word will start in uppercase. Examples are Date Hired, LastName, Drivers License Number, or EmailAddress

Practical LearningPractical Learning: Setting Columns Names

  • Click the empty box under Column Name (the caret should be blinking in it already)and type ReceiptNumber

The Types of Data

After deciding on the name of a column, the database needs to know what kind of information the column would hold. Since there are various kinds of information a database can deal with, we saw in Lesson 20 the types of data that Microsoft SQL Server supported. Therefore, you must specify the data type that is necessary for a particular column.

Practical LearningPractical Learning: Setting Data Types

  1. Press Tab and press Alt + down arrow key to display the list of data types
  2. Scroll up and select int from the list
    Setting Data Types
  3. Click the first empty field under ReceiptNumber
  4. Create the following columns:
    Column Name Data Type
    ReceiptNumber int
    OrderDate datetime
    OrderTime datetime
  5. To save the table, on the Standard toolbar, click the Save button
  6. In the Choose Name dialog box, type RepairOrders and click OK

The Length of Data

A database deals with various types of data, appropriate or not for certain fields. This means that you should take care of jobs behind the scenes as much as you can. One way you can do this is by controlling the amount of information that can be stored in a particular field. As various columns can hold different types of data, so can the same data type control its own mechanism of internal data entry. The length of data means different things to different fields. Columns that carry the same data type can have different lengths.

Bit Fields: We saw already that a bit column type is meant for one of two answers. The user is supposed to simply let the database know that the answer is yes or no, true or false, on or off, 1 or 0. Therefore, the only length of this field is 1.

Integers: The length of an integer is the number of bytes its field can hold. For an int type, that would be 4 bytes.

Decimal and Floating-Point Numbers: The Length specifies how many bytes the field can store.

Strings: The length of a character or string column specifies the maximum number of characters that the field can hold.

In some circumstances, you will need to change or specify the length as it applies to a particular field. For example, since you should use the varchar data type for a string field whose content will change from one record to another, not all varchar columns need to have the same length. Although a First Name and a Book Title columns should use the varchar type, both columns would not have the same length of entries. As it happens, people hardly have a first name that is beyond 20 characters and many book titles go beyond 32 characters. In this case, both fields would use the same data type but different lengths. On the other hand, for columns of datetime and money data types, you should accept the default length suggested by the database.

There are two ways you can change the length of a string-based column:

  • In the top section of the window, to change the length of the field, in the parentheses of the data type, enter the desired value
  • In the top section of the window, click the name of the column. In the bottom section, click the Length field and type the desired value

Practical LearningPractical Learning: Setting Data Types

  1. Click the empty box under OrderTime, type CustomerName and press Enter
  2. Click the arrow of the data type combo box and select varchar(50)
  3. In the bottom section of the table, click Length and on the right side, replace 50 with 80
  4. Complete the table with following columns:
    Column Name Data Type
    ReceiptNumber int
    OrderDate datetime
    OrderTime datetime
    CustomerName varchar(80)
    CarMake varchar(50)
    CarModel varchar(50)
    CarYear smallint
    ProblemDescription text
    TotalParts money
    TotalLabor money
    TaxRate decimal(6,2)
    TaxAmount money
    TotalOrder money
    Recommendations text
  5. Save the table

Column Creation with SQL

We saw that the primary formula to create a table was:


After specifying the name of the table, you must list the columns of the table. The list of columns starts with an opening parenthesis "(". The list ends with a closing parenthesis ")". Each column must be separated from the next with a comma, except for the last column. You can include all columns on the same line if possible as follows:

CREATE TABLE Country(Column1, Column2, Column3)

There are two primary pieces of information you must specify for each column: its name and its type. Therefore, the syntax of creating a column is:

ColumnName DataType Options

The name of the column follows the rules and suggestions we listed. After typing the name of the column, type the desired or appropriate data type for the column. For this example, use one of the (appropriate) data types we reviewed.

Remember that some of the data types need to have a length. This is certainly true for all string or text-based columns (char, text, varchar, etc). In the case of text-based columns, when using SQL to create your columns, because it is less visual than the table design, you cannot rely on the default length of strings suggested by SQL (in fact, in MySQL, you must specify a length for varchar). As it happens, the table design specifies different default values for text-based columns. Therefore, when using SQL to create your columns, you should (strongly) specify your own default length for text-based columns.

To create a table in your Windows application, you can pass the CREATE TABLE expression to a command object. Here is an example:

private void btnDatabase_Click(object sender, EventArgs e)
    using (SqlConnection connection =
	new SqlConnection("Data Source=(local);" +
			  "Database='Exercise1';" +
			  "Integrated Security=yes;"))
	SqlCommand command =
		 new SqlCommand("CREATE TABLE Customers (" +
				"DrvLicNbr VarChar(50), " +
				"DateIssued DateTime," +
				"DateExpired DateTime," +
				"FullName varchar(120)," +
				"Address VARCHAR(120)," +
				"City varchar(50)," +
				"State varchar(100)," +
				"PostalCode varchar(20)," +
				"HomePhone varchar(20)," +
				"OrganDonor bit);",

		"A new table named \"Customers\" has been created.");

We also saw that you could use sample code to create a table. This allows you to have more control over the various columns you want the table to have. To do this, open an empty query window and display the Templates Explorer. Expand the Table node. Under Table, you can drag Create Table, Add Column, or Drop Column, and drop it in the query window. If you used Add Column or Drop Column, you can delete the undesired sections of the code and isolate only the part that handles table creation. Here is an example:

-- Add column template
-- This template creates a table, then it adds a new column to the table.
USE <database, sysname, AdventureWorks>

CREATE TABLE <schema_name, sysname, dbo>.<table_name, sysname, sample_table>
	column1 int, 
	column2 char(10)

Referring to a Column



We will write many expressions that include the names of columns. In such expressions, you will need to indicate the particular column you are referring to. There are various ways you can do this. To refer to, or to indicate, a table:

  • You must type the name of the table to which the column belongs, followed by the period operator, followed by the name of the column. An example would be Employees.LastName
  • You can type dbo, followed by the period operator, followed by the name of the table to which the column belongs, followed by the period operator, followed by the name of the column. An example would be dbo.Employees.LastName
  • You can type the name of the database that owns the table's column, followed by the period operator, followed by dbo, followed by the period operator, followed by the name of the table to which the column belongs, followed by the period operator, followed by the name of the column. An example would be RedOakHighSchool.dbo.Employees.LastName

Using the Alias Name of a Table

You can create an alias name of a table to use in an expression that involves a column. To do this, type a letter or a word that will represent the table to which the column belongs. The letter or the word is followed by a period operator, and followed by the name of the column. An example would be empl.LastName. At the end of the statement, you must type the name of the table, followed by space, and followed by the letter or the word. An example would be Employee empl.


Published on Monday 28 December 2007


Home Copyright © 2007 FunctionX, Inc. Next