C# Language

The Tables of a Database

We have defined a database as one or more lists. A list in a database is called a table. The idea is that a table is an arrangement of the categories of…

2 min read403 words5 code samples

Table Creation#

Introduction#

We have previously described a database as being comprised of one or more lists. Each of these lists is known as a table, which is essentially an object designed to store data in a structured way.

Last NameFirst NameMain SubjectAlternate Subject
PastoreAlbertMathPhysics
AndongGertrudeChemistryStatic
MissianoHelenaPhysical Ed
JonesCelestineComp SciencesMath

Notice that the first names are grouped in a common category, so are the last names and so on. This makes it easy to locate a category and possibly a value.

Table Creation#

To create a table, you start an expression with CREATE TABLE followed by the name of the table:

C#
CREATE TABLE Name;

The CREATE and TABLE keywords must be used to create a table. The Name factor specifies the name of the new table.

The Name of a Table#

Following the CREATE TABLE command, you are required to provide a name for your new table. Table naming is quite flexible, allowing you to choose a name that fits your data structure.

  • The name of a table will start with a letter. In most cases, the name will start in uppercase
  • Because we believe that a table represents a list of items, its name will be in plural. Examples are Students, Employees, Products
  • When a name is a combination of words, each part will start in uppercase. Examples are Student Names or Sport Activities
  • In most cases, we will avoid including space in a name; but if we do, the name of a table will be included between [ and ]

Here is an example:

C#
using System;
using ADODB;

public class Program
{
    static int Main()
    {
        string strStatement = "CREATE TABLE Persons...";
        
        return 0;
    }
}

After formulating the expression that creates the table, you can pass it to the Execute() method of a Connection variable. This would be done as follows:

C#
using System;
using ADODB;

public class Program
{
    static int Main()
    {
        ConnectionClass conDatabase = new ConnectionClass();

        try
        {
            object objAffected;
            string strStatement = "CREATE TABLE Persons...";
            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                   "Data Source='C:\\Programs\\Exercise1.mdb';";

            conDatabase.Open(strConnection, "", "", 0);
            conDatabase.Execute(strStatement, out objAffected, 0);
        }
        finally
        {
            conDatabase.Close();
        }
        return 0;
    }
}

Besides the CREATE TABLE expression followed by a name, there are other issues related to creating a table. We will review more details in future lessons.

Table Maintenance#

The Tables Collection#

In an ADO-based database, the individual tables are managed within a collection that you can access via the Tables property of the Connection class.

Deleting a Table#

To remove a table from a database, create a DROP TABLE expression followed by the name of the table. The formula to use is:

C#
DROP TABLE TableName;

Replace the TableName factor of our formula with the name of the table you want to delete. Here is an example:

C#
using System;
using ADODB;

public class Program
{
    static int Main()
    {
        ConnectionClass conDatabase = new ConnectionClass();

        try
        {
            object objAffected;
            string strStatement = "DROP TABLE Persons";
            string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                                   "Data Source='C:\\Programs\\Exercise1.mdb';";

            conDatabase.Open(strConnection, "", "", 0);
            conDatabase.Execute(strStatement, out objAffected, 0);
        }
        finally
        {
            conDatabase.Close();
        }
        return 0;
    }
}

Previous Copyright © 2008 C# Key Next