Tables Fundamentals |
|
|
|
Table Creation |
To create a table, you start a statement with the CREATE TABLE expression followed by the desired name of the table as follows: CREATE TABLE TableName When creating a table, you must provide a name for it. The name of a table can be made of digits, letters, underscores, or a combination of those. In our lessons, we will use the following rules:
Every table must have at least one column. The list of columns of a table starts with an opening parenthesis “(“ and ends with a closing parenthesis “)”. If the table will be made of more than one column, you can separate them with a comma. The syntax used would be: CREATE TABLE TableName(Column1, Column2, Column_n); To create a column, you specify its name, followed by its data type, and some possible options. Therefore, the syntax of creating a column is: ColumnName DataType Options Notice that there is only space that separates the sections of the syntax. For this reason, the name of a column should be in one word because, if you put space after the first word, the SQL engine would treat the next word as a data type. For example, the following statement would produce an error: CREATE TABLE Test(Marital Status) If you want to use space in a column name, include it between an opening square bracket “[“ and a closing square bracket “]”. The above statement would be changed to: CREATE TABLE Test([Marital Status]) After (or on the right side of) the column name, you must specify the type of information, also called the data type) that will be stored in the cells under that column. Here are the data types you can use:
CREATE TABLE Persons ( FirstName VARCHAR, [Last Name] varchar, Gender int, [Date Of Birth] DateTime, MaritalStatus Integer, IsMarried Bit, PersonalIncome Money, HouseholdIncome Currency ); A field size can be used to specify the size of value used on a field. Based on this, when creating a column whose data type is char, text or varchar, you can optionally specify the desired number of characters that the field should allow. To specify the maximum number of characters of a text-based field, include it in parentheses just to the left of the data type. In ADO, to create a table, you include its whole statement between double-quotes, as a string. To actually perform the assignment, you can call the Execute() method of the ADO reference you are using, passing your string as argument. |
Practical Learning: Creating a Table |
|
Table Deletion |
If you have a table in a database but don't need that table anymore, you can get rid of it. To delete a table, write the DROP TABLE expression followed by the name of the table. |
Practical Learning: Deleting a Table |
|
|
||
Previous | Copyright © 2001-2005 FunctionX, Inc. | Next |
|