One of the reasons for Microsoft Access' tremendous success is the user interface. You can create tables, determine data types by example, and make changes to the table design even after it has been implemented. Actually, in many cases Microsoft Access doesn't really modify existing tables. Instead, it creates new tables according to your specifications, then moves data into it.
SQL Server makes changes to tables in the same way, but you must guide it through the steps. At first, you may regard this as a loss of functionality, but there is a reason for the difference in approach. Microsoft Access tables are typically smaller than SQL Server tables; data in a Microsoft Access table is usually measured in the tens of thousands or hundreds of thousands of records. In contrast, SQL Server tables often have millions of records. Many SQL Server databases are over 50 GB and some exceed 100 GB. Think twice before changing the structure of a table with 5,000,000 records. SQL Server will rewrite the table schema, copy the records to the new table, and rebuild all of the indexes for the table.
The SQL Server Browser Add-In included in the Upsizing Tools permits you to make some changes to your SQL Server data structure from within Microsoft Access, using a format similar to Microsoft Access design view. This tool has tabs for Tables, Views, Defaults, Rules, and Procs. This tool allows you to make changes equivalent to those that can be made with the ALTER TABLE syntax. More complex changes such as changing the data type of a field cannot be made here.
Microsoft has now released a new product Add-In called Visual Database Tools. This ships with the Enterprise Editions of Visual Studio™ 97 development software, Visual Basic 5, and Visual C++® 5. This Add-In is a more sophisticated interface than the SQL Server Browser. It looks much like Microsoft Access and writes the SQL statements necessary for more complicated changes such as changes to data types.
Adding fields is the easiest change to make. Using SQL Enterprise Manager, select your server in the Server Manager window. Click on the plus sign (+) to drill down through databases and select your database. On the Manage menu, select Tables. Select the desired table from the list. In this window you can view the design of the table, add new fields to the table, and change the names of the columns. You can't delete columns or change a column's data type.
You can also add a column using Transact-SQL language from SQL Enterprise Manager or from Microsoft Access with a SQL Pass-Through Query.
Type in the Transact-SQL statement in the following format:
ALTER TABLE SomeTable
ADD
Field1 INT NULL,
Field2 varchar(10) NULL
Because SQL Server rewrites tables to add the field(s), expect large table changes to take some time. SQL Server moves all of the data to new pages and rebuilds the indexes.
You can change the data type of a field in two ways:
In the Server Manager window double-click on the database name to open the Manage Database window. Click Options and place a check in the Select into/Bulk Copy box. Now you can make a copy of a table, for example the jobs table in the pubs database.
SELECT * INTO newjobs FROM jobs
You can modify this query slightly with the CONVERT function. Here is the query to make a copy of the jobs table, changing the max_lvl field from a data type of tinyint to a float.
SELECT job_id, job_desc, min_lvl,
CONVERT(float,max_lvl) as max_lvl
INTO changedjobs FROM JOBS
Open each table in the Manage Tables window to see that although the table definition has changed, no defaults or constraints have been applied to the new table. Read the descriptions in the old table and recreate them in the new table. More dangerously, the converted column will have been created with the default NULL behavior (NULL on SQL Server 6.5, NOT NULL on SQL Server 6.0). If this is the desired result, verify the data in the new version, drop the old version of the table, and rename the new version with the original name. If you have any foreign key constraints, drop them before dropping the old table. Then recreate them with an ALTER TABLE statement.
Changing a table by creating a new version from a SQL Script takes longer than using SELECT INTO, but it gives you more control over the process. You must make a new version of the table with the changed data type, move the data to the new table, rename (or delete) the old table, and rename the new table with the original table's name. Since you can't delete tables with relationships, you must first drop the relationships and recreate them. Recompile stored procedures that refer to the table to get a new object number for the new table. If the table you are changing is part of a complex structure of tables, you may find that it is quite complex to get all the details reconstructed properly. Document the structure well before you start making changes. Make a backup of databases before modifying them.
The following is the Transact-SQL syntax for changing a data type of a field. This example is intentionally complex to illustrate the level of detailed knowledge required to accurately make such a change. A field named ZipCode is changed from an integer to a five-character field (char(5)).
ALTER TABLE Orders
DROP CONSTRAINT UPKCLSuppliersConstr
GO
set quoted_identifier on
GO
if exists (select * from sysobjects where id =
object_id('dbo.Suppliers') and sysstat & 0xf = 3)
drop table "dbo"."Suppliers"
GO
CREATE TABLE "dbo"."Suppliers"
(
"Name" varchar (50) NOT NULL ,
"Address" varchar (50) NULL ,
"City" varchar (20) NULL CONSTRAINT "CityDefault"
DEFAULT ('Seattle'),
"State" char (2) NULL CONSTRAINT "StateDefault"
DEFAULT ('WA'),
"ZipCode" "int" NULL ,
CONSTRAINT "UniqueConstraint" UNIQUE NONCLUSTERED
(
"Name"
),
CONSTRAINT "StateConstraint" CHECK
(State = 'CA' or (State = 'OR' or (State = 'WA')))
)
GO
"ZipCode" int NULL ,
is changed to
"ZipCode" Char (5) NULL ,
set quoted_identifier on
GO
if exists (select * from sysobjects where id =
object_id('dbo.NewSuppliers') and sysstat & 0xf = 3)
drop table "dbo"."NewSuppliers"
GO
CREATE TABLE "dbo"."NewSuppliers"
(
"Name" varchar (50) NOT NULL ,
"Address" varchar (50) NULL ,
"City" varchar (20) NULL CONSTRAINT "NewCityDefault"
DEFAULT ('Seattle'),
"State" char (2) NULL CONSTRAINT "NewStateDefault"
DEFAULT ('WA'),
"ZipCode" char (5) NULL ,
CONSTRAINT "NewUniqueConstraint" UNIQUE
NONCLUSTERED
(
"Name"
),
CONSTRAINT "NewStateConstraint" CHECK
(State = 'CA' or (State = 'OR' or (State = 'WA')))
)
GO
INSERT INTO NewSuppliers
SELECT Name, Address, City, State, convert(char(5),ZipCode)
FROM Suppliers
You can't delete fields in a table with SQL Server, but you can use the process just discussed. Instead of specifying a change in data type, omit the field that you wish to drop from the table creation script. Omit the field in the INSERT INTO statement and transfer all of the data except the unwanted field.