The connection can be resumed by clicking the Change Connection button from SQL Management Studio if the connection has been interrupted Create a Data Table Using SQL Server Management
Trang 1248
TCP Provider, error: 0 - An established connection was aborted by the software in your host machine.)
Figure 8-7 The connection can be resumed by clicking the Change Connection button from SQL
Management Studio if the connection has been interrupted
Create a Data Table Using SQL Server Management Studio
A custom table can be created in the SQL Azure cloud database by using SQL Server Management Studio
in a way that is pretty similar to creating a database on a server running in an on-premises environment One difference, for example, is that the USE statement is not supported when using SQL Azure since we have used New Query to connect to the specific database already If you need to access a different database, you can start a new query by clicking the New Query button To find more guidelines and limitations to using SQL Azure, see http://msdn.microsoft.com/en-us/library/ee336245.aspx
Run the script in Listing 8-1 to create a data table called UserTable The results are shown in Figure 8-8
Listing 8-1 SQL Script Used to Create a Data Table in SQL Azure
CREATE TABLE [dbo].[UserTable](
[UserID] [int] IDENTITY(1,1)NOT NULL PRIMARY KEY CLUSTERED,
[Password] [nvarchar](100) NULL,
FirstName [nvarchar](100) NOT NULL,
LastName [nvarchar](100) NOT NULL,
[Timestamp] [timestamp] NOT NULL
)
GO
INSERT INTO UserTable
([Password],
FirstName,
LastName)
Values(
'password',
'Henry',
'Li'
)
GO
SELECT COUNT(*) FROM UserTable
SELECT * FROM UserTable
Trang 2249
Figure 8-8 Query results from Listing 8-1
Simple Benchmark Testing Results
Results of a simple benchmark test for SQL Azure access are shown in Table 8-1 (The scripts for the test
follow.) It may satisfy your curiosity as to the performance of the database in the cloud (I certainly was
curious) Figure 8-9 shows the Internet access bandwidth used to get the results of Table 8-1 (A free speed-testing tool Speakeasy (www.speakeasy.net/speedtest) can be used to determine your broadband speed.)
Table 8-1 Results of a Simple Benchmark Test for SQL Azure Database Access
INSERT 19 min, 46 sec 12 sec < 1 sec
SELECT 4 min, 39 sec 7 sec < 1sec
UPDATE 20 min, 11 sec 12 sec 1 sec
DELETE 20 min, 47 sec 13 sec < 1 sec
Figure 8-9 Broadband speed of the network used to get the benchmark testing results of Table 8-1
Trang 3250
To get benchmark testing results you need to create an index table using the script shown in Listing 8-2
Listing 8-2 SQL Script Used to Create an Index Table Using FirstName as Index Key
CREATE INDEX IX UserTable FirstName
ON [UserTable](FirstName)
Go
The SQL script used to do this benchmark testing is shown in Listing 8-3
Listing 8-3 SQL Scripts Used for Benchmark Testing to Get the Results of Table 8-1
CREATE PROCEDURE BatchInsert
@rows int
AS
DECLARE @index int
SELECT @index = 1
WHILE (@index < @rows)
BEGIN
INSERT INTO UserTable
([Password],
FirstName,
LastName)
VALUES
('password',
'Henry' + CAST(@index as nvarchar),
'Li')
SELECT @index = @index + 1
END
go
CREATE PROCEDURE BatchUpdate
@rows int
AS
DECLARE @index int
SELECT @index = 1
WHILE (@index < @rows)
BEGIN
UPDATE UserTable
SET
[Password] = 'passworduPDATE',
FirstName = 'Henry' + CAST(@index as nvarchar),
LastName = 'Li'
WHERE [UserID] = @index
SELECT @index = @index + 1
END
go
CREATE PROCEDURE BatchDelete
Trang 4251
@rows int
AS
DECLARE @index int
SELECT @index = 1
WHILE (@index < @rows)
BEGIN
DELETE UserTable
WHERE [UserID] = @index
SELECT @index = @index + 1
END
go
truncate table UserTable
DECLARE @NUMBER OF TEST ROWS INT
SET @NUMBER OF TEST ROWS = 100000
EXEC BatchInsert @NUMBER OF TEST ROWS
select * from UserTable where FirstName like 'Henry%'
EXEC BatchUpdate @NUMBER OF TEST ROWS
EXEC BatchDelete @NUMBER OF TEST ROWS
Verifying That SQL Azure Supports Relational Data Tables
Run the SQL script in Listing 8-4 against the SQL Azure cloud database described previously to create
two relational data tables The diagram of the table structure and relationships is shown in Figure 8-10
In the Address table the field UserID is a foreign key, which references UserID in UserTable The script to create the foreign key is shown in the boldface lines in Listing 8-4
Listing 8-4 SQL Script Used to Create Relational Data Tables
DROP TABLE [UserTable]
GO
CREATE TABLE [dbo].[UserTable](
[UserID] [int] IDENTITY(1,1)NOT NULL,
[Password] [nvarchar](100) NULL,
FirstName [nvarchar](100) NOT NULL,
LastName [nvarchar](100) NOT NULL,
[Timestamp] [timestamp] NOT NULL
)
GO
ALTER TABLE [UserTable]
ADD CONSTRAINT UserID PK PRIMARY KEY (UserID)
GO
CREATE TABLE [dbo].[Address](
AddressID [int] IDENTITY(1,1)NOT NULL,
[UserID] [int] NOT NULL,
Trang 5252
Address1 [nvarchar](100) NULL,
Address2 [nvarchar](100) NULL,
City [nvarchar](100) NULL,
State [nvarchar](100) NULL,
Zip [nvarchar](9) NULL,
County [nvarchar](50) NULL,
Email1 [nvarchar](100) NOT NULL,
Email2 [nvarchar](100) NULL
)
GO
ALTER TABLE [Address]
ADD CONSTRAINT AddressID PK PRIMARY KEY (AddressID)
GO
ALTER TABLE [dbo].[Address] WITH CHECK ADD CONSTRAINT [FK_Address_UserTable]
FOREIGN KEY([UserID])
REFERENCES [dbo].[UserTable] ([UserID])
GO
ALTER TABLE [dbo].[Address] CHECK CONSTRAINT [FK_Address_UserTable]
GO
Figure 8-10 Table structure and relationship between UserTable and Address tables
■ Note Since SQL Azure does not support object browsing from SQL Server Management Studio, this database
diagram is generated from a local SQL database The script used to create this database table is exported and executed against the SQL Azure server We are going to cover data table migration later in this chapter