![]() |
Tables Maintenance |
|
Viewing the Properties of a Table |
|
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. Like every other object of a database or of the computer, a table possesses some characteristics that are proper to it. To view these characteristics in Microsoft SQL Server Management Studio, in the Object Explorer, you can right-click the table and click Properties. |
Most operations require that you open a table before using it. There are various ways a table displays, depending on how you want to examine it:
In Microsoft Visual Studio, to see the list of tables of a database, in the Server Explorer, expand the connection to the desired database and expand the Tables node. Here is an example:
Using Microsoft SQL Server Management Studio, to see the list of tables of a database using SQL, in a Query window, specify the database (using a USE statement), and execute sp_help (it is a stored procedure). Here is an example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Exercise
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}
private void Exercise_Load(object sender, EventArgs e)
{
using (SqlConnection connection =
new SqlConnection("Data Source=(local);" +
"Database='AltairRealtors1';" +
"Integrated Security=yes;"))
{
SqlCommand command =
new SqlCommand("sp_help", connection);
connection.Open();
SqlDataReader rdr = command.ExecuteReader();
while (rdr.Read())
{
lbxTables.Items.Add(rdr[0].ToString());
}
rdr.Close();
}
}
}
}
If you find out that the name of a table is not appropriate, you can change it. To change the name of a table in the SQL Server Management Studio, in the Object Explorer, right-click the table and click Rename. Type the desired name and press Enter. To change the name of a table with code, execute sp_rename, followed by the current name of the table, a comma, and the new desired name of the table. The formula to use is: sp_rename ExistingTableName, TableNewName; The names of tables should be included in single-quotes. Here is an example: sp_rename 'StaffMembers', 'Employees'; GO In this case, the interpreter would look for a table named StaffMembers in the current or selected database. If it finds it, it would rename it Employees. If the table does not exist, you would receive an error.
If you have an undesired table in a database, you can remove it. To delete a table
You will receive a warning giving you a chance to confirm your intentions. If you still want to remove the table, click OK. 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. When you execute the statement, you will not receive a warning before the table is deleted. You can also use sample code in Microsoft SQL Server Management Studio that can generate code for you. First display an empty query window. Also display the Templates Explorer and expand the Table node. Under Table, drag Drop Table and drop it in the empty query window. Sample code would be generated for you. You can then simply modify it and execute the statement.
In future lessons, we will write various expressions that involve the names of tables. In those expressions, you will need to specify a particular table you want to use. There are three main ways you can do this. To refer to, or to indicate, a table:
|
Published on Monday 28 December 2007
|
|
||
| Previous | Copyright © 2007 FunctionX, Inc. | Home |
|
|
||