|
Columns, also called fields, are used to organize the
data of a table.
|
Creating Columns of a Table
|
|
There are various ways you can create a add a column
to a table:
- To work in the Datasheet View,
start the table
in Datasheet View (on the Ribbon, click Create and, in the Tables
section, click Table
).
To create a column, click Click to Add. A list of columns types would
display:
Click the type of value the column will have. A
default name will be given to the column, such as Field1. You can accept
it or, to change it, simply type the desired name. If the name was given
already, to change it, double-click the column header, type the desired
name, and press Enter.
- To add a column in the Design View, after displaying the table in
that view, click under Field Name and type the desired name of the
column. Select the type of value in the corresponding Data Type field:
- To create a column using code, start a query window (on the
Ribbon, click Create; in the Queries section, click Query Design; in
the Show Tables dialog box, close Close; right-click the window and
click SQL View). The basic formula to create a table and its column(s)
is:
CREATE TABLE TableName
(
ColumnName_1 DataType Options,
ColumnName_2 DataType Options,
ColumnName_n DataType Options
);
You start with the CREATE TABLE
expression followed by the name of the table. The name of a table or that
of a column can be in one or more words. If the name is in one word, just
type it. If it is in more than one word, include it in square brackets.
After the name of the table, you must open parentheses and define each
column in it.
A column, also called a field, is a category or type
of value you will use in your table. After the name of the column, you
must specify the type of value, also called the data type, that the values
of the field must follow. The most common data types are:
- BIT: This is used for Boolean values that can be
TRUE or FALSE
- TEXT and VARCHAR: This is for a
group of characters or symbols of any kind. The data type should be
followed by parentheses that contain the maximum number of characters
- LONGTEXT and MEMO: This is a
long text made of a character, a string, a line of text, a paragraph,
many paragraphs, many pages, or a whole book
- COUNTER: This is used for automatically generated
natural numbers added to the field
- BYTE: This is for small natural numbers
- SHORT: This is for natural numbers, relatively
large
- INT, INTEGER, and LONG:
This is for (very large) natural numbers
- SINGLE and REAL: This is for
decimal numbers that do not need much precision
- DOUBLE and NUMERIC: This is for
decimal numbers that need precision
- CURRENCY: This used for monetary values
- DATE, TIME, and DATETIME:
This is for date, time, or a combination of date and time values
Here is an example that creates a table named Persons:
CREATE TABLE Employees
(
EmployeeNumber char,
[Date Hired] DateTime,
FirstName VARCHAR,
[Last Name] Text,
Gender char,
MaritalStatus Integer,
HourlySalary Money,
[Employee Picture] Image,
[Employee Review] Memo
);
Of course, you must execute the SQL code to finalize
the creation. To do this, in the Design table of the Ribbon, in the
Results section, you can click the Run button
There are various ways you can add a column to an
existing table:
- To visually add a column in the Datasheet View, right-click an
existing column and click Insert Field
- To visually add a column in the Design View, right-click an
existing column and click Insert Rows
- To a column using code, start with ALTER TABLE
followed by the name of the table that has the column, followed by
ADD and the definition of the column. Here is an
example that adds a column named Address to a Customers table:
ALTER TABLE Customers
ADD Address VARCHAR(50);
There are various ways you can remove a column from an
existing table:
- In the Datasheet View, right-click the column header of the
undesired column and click Delete Field
- In the Design View, right-click the undesired column and click
Delete Rows
- To remove a column using code, start with ALTER TABLE
followed by the name of the table that has the column, followed by
DROP COLUMN and the name of the column. Here is an
example that deletes a column named PhoneNumber from a Customers
table:
ALTER TABLE Customers
DROP COLUMN PhoneNumber;