Table maintenance consists of reviewing or changing
its aspects. This includes reviewing the list of tables of a database,
renaming a table, or deleting it.
When making a change on a column, you are also said to
alter the table. To programmatically change a column, the SQL starts with the following
formula:
ALTER TABLE TableName
When using this statement, the ALTER TABLE
expression is required and it is followed by the name of the table.
Column maintenance consists of reviewing or changing
any of its aspects. This includes reviewing the structure of columns of a
table, renaming a column, deleting a column, changing the data type or the
nullity of a column, etc.
After a table has already been created, you can still
add a new column to it.
In SQL, the basic formula to add a new column to an
existing table is:
ALTER TABLE TableName ADD ColumnName Properties
The ColumnName factor is required. In fact, on
the right side of the ADD operator, define the column by its name and use
all the options we reviewed for columns.
Here is an example:
Imports System.Data.OleDb Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim strConnection As String Dim connection As OleDbConnection = New OleDbConnection strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("App_Data/exercise1.mdb") connection.ConnectionString = strConnection Dim CommandToExecute As OleDbCommand = _ New OleDbCommand("ALTER TABLE Customers " & _ "ADD EmailAddress nvarchar(50);", connection) connection.Open() CommandToExecute.Connection = connection CommandToExecute.ExecuteNonQuery() connection.Close() End Sub End Class
If you have an undesired column that you don't want
anymore in a table, you can remove it.
To programmatically delete a column, use the following formula:
ALTER TABLE TableName DROP COLUMN ColumnName
On the right side of the ALTER TABLE
expression, type the name of the table. On the right side of the DROP
COLUMN expression, enter the name of the undesired column. Here is an
example:
Imports System.Data.OleDb Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Dim strConnection As String Dim connection As OleDbConnection = New OleDbConnection strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("App_Data/exercise1.mdb") connection.ConnectionString = strConnection Dim CommandToExecute As OleDbCommand = _ New OleDbCommand("ALTER TABLE Customers " & _ "DROP COLUMN EmaillAddress;", connection) connection.Open() CommandToExecute.Connection = connection CommandToExecute.ExecuteNonQuery() connection.Close() End Sub End Class
When this code is executed, the interpreter
will look
for a column named CurrentResidence in a table named StaffMembers
of the current. If it finds that column, it will remove it from the
table.
If you have an undesired table in a database, you can
remove it. To delete a table using SQL, use the following
formula:
DROP TABLE TableName
The DROP TABLE expression is required and it is
followed by the name of the undesired table.
|
|
|||||||||||
|
The Tables of a Database
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment