|
Table maintenance consists of reviewing or changing some
of its aspects. This includes reviewing the list of tables of a database,
renaming a table, or deleting it.
|
Viewing the Properties of a Table
|
|
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 the Object Explorer, 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:
- To view the structure of a table, perhaps to change its design, in
the Object Explorer, expand your database and its Tables node.
Right-click the table and click Design. The table would open in design
view, the same view you use to visually create a table
 |
In our lessons, if you right-click a table in
the Object Explorer and click Design, the window that displays will
be referred to as the Design View of a table.
|
- If you want to view the SQL code of a table, in the Object Explorer,
right-click the table, position the mouse on Script Table AS, CREATE To,
and click New Query Editor Window
|
Modifying the
Design of a Table
|
|
Probably the most routine operation you will perform on
a table is to change its design. This is usually easily done after you have
opened the table in Design view. You can then change what you want. After
making the change(s), you must save the table. Depending on how your
database is configured, you may receive an error as "Saving changes is not
permitted...":

To avoid this error and make it possible to modify
tables, on the main menu of SQL Server Management Studio, click Tools ->
Options... In the left tree list of the Options dialog box, click Designers.
In the Table Options, clear the Prevent Saving Changes That Require Table
Re-Creation check box.
|
Practical
Learning: Allowing Changes on Tables
|
|
- On the main menu of SQL Server Management Studio, click Tools ->
Options...
- In the left tree list of the Options dialog box, click Designers
- In the Table Options, clear the Prevent Saving Changes That Require
Table Re-Creation check box

- Click OK
To see the list of tables of a database in the Object
Explorer, you can click the Tables node:
To see the list of tables of a database using SQL, in a
Query Editor, specify the database (using a USE statement).
On the next line, type sp_help and execute it. Here is an example:

|
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, type sp_rename,
followed by the current name of the table, a comma, and the new
desired name of the table. Then execute the code. 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 N'StaffMembers', N'Employees';
GO
In this case, the database engine 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 doesn't exist,
you would receive an error
|
 |
If you have an undesired table in a database, you can
remove it:
- To delete a table in the SQL Server Management Studio, in the Object
Explorer, right-click the table under its database node and click
Delete. 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 that Microsoft SQL Server
can generate for you. First display an empty Query Editor. Also display the
Templates Explorer and expand the Table node. Under Table, drag Drop Table
and drop it in the empty Query Editor. Sample code would be generated for
you. You can then simply modify it and execute the statement.
In your database development assignments, you will write
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:
- You can simply type its name. An example would be Students
- You can type the schema name, followed by the period operator,
followed by the name of the table. An example would be
Registration.Students. In reality, if a table belongs to a schema
other than dbo, you should always precede its name with its
schema
- You can type the name of the database to which the table belongs,
followed by the period operator, followed by the schema, followed by the
period operator, and followed by the name of the table. An example would
be RedOakHighSchool.Registration.Students
|
Practical
Learning: Ending the Lesson
|
|
- Close Microsoft SQL Server.
If asked whether you want to save
something, click No
|
|