[Windows Azure] Managing SQL Database using SQL Server Management Studio
Managing Windows Azure SQL Database using SQL Server Management Studio
You can use Windows Azure SQL Database Management Portal or the SQL Server Management Studio (SSMS) client application to administer your SQL Database subscriptions and create and manage associated logical servers and databases. The guidance below describes how to use Management Studio to manage SQL Database logical servers and databases. For information on how to use SQL Database connections in application code, see How to use Windows Azure SQL Database.
You can use either SQL Server 2012 or the SQL Server 2008 R2 version of Management Studio. Earlier versions are not supported.
This task includes the following steps:
- Step 1: Get SQL Server Management Studio
- Step 2: Connect to SQL Database
- Step 3: Create and manage databases
- Step 4: Create and manage logins
- Step 5: Monitor SQL Database using Dynamic Management Views
Step 1: Get Management Studio
Management Studio is an integrated environment for managing SQL databases. When managing databases on Windows Azure, you can use the Management Studio application installed with SQL Server or download the free SQL Server 2012 Management Studio Express (SSMSE) version. The steps below describe how to install SSMSE.
On the Microsoft SQL Server 2012 Express page, select the x86 version of Management Studio if you are running a 32-bit operating system, or x64 if you are running a 64-bit operating system. Click Download, and when prompted, run Setup.
Click New SQL Server stand-alone installation or add features to an existing installation and click OK.
Accept the license terms and click OK.
Click Install to install files required by SQL Server Setup.
On the Feature Selection screen, Management Tools - Basic is preselected. This is because you are running the installer for Management Studio. If you are running Setup for all of SQL Server Express, choose the Management Tools - Basic option, and click Next.
On the Error Reporting screen, you can optionally choose to send error reports to Microsoft. This is not required to use SSMSE. Click Next to start the installation.
When the installation is complete, you will see the Complete page. Click Close.
Step 2: Connect to SQL Database
Connecting to SQL Database requires that you know the server name on Windows Azure. You might need to sign in to the portal to get this information.
Sign in to the Windows Azure Management Portal.
In the left pane, click on SQL Databases.
On the SQL Databases home page, click SERVERS at the top of the page to list all of the servers associated with your subscription. Find the name of the server to which you want to connect and copy it to the clipboard.
Next, configure your SQL Database firewall to allow connections from your local machine. You do this by adding your local machines IP address to the firewall exception list.
On SQL Databases home page, click SERVERS and then click the server to which you want to connect.
Click Configure at the top of the page.
Copy the IP address in CURRENT CLIENT IP ADDRESS.
In the Configure page, Allowed IP Addresses includes three boxes where you can specify a rule name and a range of IP addresses as starting and ending values. For a rule name, you might enter the name of your computer. For the start and end range, paste in the IP address of your computer into both boxes, and then click the checkbox that appears.
The rule name must be unique. If this is your development computer, you can enter the IP address in both the IP range start box and the IP range end box. Otherwise, you might need to enter a broader range of IP addresses to accommodate connections from additional computers in your organization.
Click SAVE at the bottom of the page.
Note: There can be up as much as a five-minute delay for changes to the firewall settings to take effect.
You are now ready to connect to SQL Database using Management Studio.
On the taskbar, click Start, point to All Programs, point to Microsoft SQL Server 2012, and then click SQL Server Management Studio.
In Connect to Server, specify the fully-qualified server name as serverName.database.windows.net. On Windows Azure, the server name is an autogenerated string composed of alphanumeric characters.
Select SQL Server Authentication.
In the Login box, enter the SQL Server administrator login that you specified in the portal when creating your server in the format login@yourServerName.
In the Password box, enter the password that you specified in the portal when creating your server.
Click Connect to establish the connection.
On Windows Azure, each SQL Database logical server is an abstraction that defines a grouping of databases. The physical location of each database might be on any computer in the data center.
In previous versions, you had to connect directly to master when setting up the connection in Management Studio. This step is no longer necessary. Connections will now succeed based on the server name, authentication type, and administrator credentials.
Many of the SSMS wizards you can use for tasks like creating and modifying logins and databases on a SQL Server database are not available for SQL databases on Windows Azure, so you'll need to utilize Transact-SQL statements to accomplish these tasks. The steps below provide examples of these statements. For more information about using Transact-SQL with SQL Database, including details about which commands are supported, see Transact-SQL Reference (SQL Database).
Step 3: Create and Manage Databases
While connected to the master database, you can create new databases on the server and modify or drop existing databases. The steps below describe how to accomplish several common database management tasks through Management Studio. To perform these tasks, make sure you are connected to the master database with the server-level principal login that you created when you set up your server.
To open a query window in Management Studio, open the Databases folder, right-click on master, and then click New Query.
Click Execute to run the query.
Use the CREATE DATABASE statement to create a new database. For more information, see CREATE DATABASE (SQL Database). The statement below creates a new database named myTestDB and specifies that it is a Web Edition database with a maximum size of 1 GB.
CREATE DATABASE myTestDB
(MAXSIZE=1GB,
EDITION='web');Use the ALTER DATABASE statement to modify an existing database, for example if you want to change the name, maximum size, or edition (business or web) of the database. For more information, see ALTER DATABASE (SQL Database). The statement below modifies the database you created in the previous step to change the maximum size to 5 GB.
ALTER DATABASE myTestDB
MODIFY
(MAXSIZE=5GB,
EDITION='web');Use the DROP DATABASE Statement to delete an existing database. For more information, see DROP DATABASE (SQL Database). The statement below deletes the myTestDB database, but don't drop it now because you will use it create logins in the next step.
DROP DATABASE myTestBase;
The master database has the sys.databases view that you can use to view details about all databases. To view all existing databases, execute the following statement:
SELECT * FROM sys.databases;
In SQL Database, the USE statement is not supported for switching between databases. Instead, you need to establish a connection directly to the target database.
Many of the Transact-SQL statements that create or modify a database must be run within their own batch and cannot be grouped with other Transact-SQL statements. For more information, see the statement specific information available from the links listed above.
Step 4: Create and Manage Logins
The master database keeps track of logins and which logins have permission to create databases or other logins. Manage logins by connecting to the master database with the server-level principal login that you created when you set up your server. You can use the CREATE LOGIN, ALTER LOGIN, or DROP LOGIN statements to execute queries against the master database that will manage logins across the entire server. For more information, see Managing Databases and Logins in SQL Database.
Use the CREATE LOGIN statement to create a new server-level login. For more information, see CREATE LOGIN (SQL Database). The statement below creates a new login called login1. Replace password1 with the password of your choice.
CREATE LOGIN login1 WITH password='password1';
Use the CREATE USER statement to grant database-level permissions. All logins must be created in the master database, but for a login to connect to a different database, you must grant it database-level permissions using the CREATE USER statement on that database. For more information, see CREATE USER (SQL Database).
To give login1 permissions to a database called myTestDB, complete the following steps:
Refresh Object Explorer to view the myTestDB database that you just created. It should appear below the System Databases folder that contains master.
If you closed the connection, you can reconnect by selecting Connect Object Explorer on the File menu. Repeat the instructions in Step 2: Connect to SQL Database to connect to the database.
Right-click myTestDB database and select New Query.
Execute the following statement against the myTestDB database to create a database user named login1User that corresponds to the server-level login login1.
CREATE USER login1User FROM LOGIN login1;
Use the sp_addrolemember stored procedure to give the user account the appropriate level of permissions on the database. For more information, see sp_addrolemember (Transact-SQL). The statement below gives login1User read-only permissions to the database by adding login1User to the db_datareader role.
exec sp_addrolemember 'db_datareader','login1User';
Use the ALTER LOGIN statement to modify an existing login, for example if you want to change the password for the login. For more information, see ALTER LOGIN (SQL Database). The ALTER LOGIN statement should be run against the master database. Switch back to the query window that is connected to that database.
The statement below modifies the login1 login to reset the password. Replace newPassword with the password of your choice, and oldPassword with the current password for the login.
ALTER LOGIN login1
WITH PASSWORD ='newPassword'
OLD_PASSWORD ='oldPassword';Use the DROP LOGIN statement to delete an existing login. Deleting a login at the server level also deletes any associated database user accounts. For more information, see DROP DATABASE (SQL Database). The DROP LOGIN statement should be run against the master database. The statement below deletes the login1 login.
DROP LOGIN login1;
The master database has the sys.sql_logins view that you can use to view logins. To view all existing logins, execute the following statement:
SELECT * FROM sys.sql_logins;
Step 5: Monitor SQL Database using Dynamic Management Views
SQL Database supports several dynamic management views that you can use to monitor an individual database. Below are a few examples of the type of monitor data you can retrieve through these views. For complete details and more usage examples, see Monitoring SQL Database using Dynamic Management Views.
Querying a dynamic management view requires VIEW DATABASE STATE permissions. To grant the VIEW DATABASE STATE permission to a specific database user, connect to the database you want to manage with your server-level principle login and execute the following statement against the database:
GRANT VIEW DATABASE STATE TO login1User;
Calculate database size using the sys.dm_db_partition_stats view. The sys.dm_db_partition_stats view returns page and row-count information for every partition in the database, which you can use to calculate the database size. The following query returns the size of your database in megabytes:
SELECT SUM(reserved_page_count)*8.0/1024
FROM sys.dm_db_partition_stats;Use the sys.dm_exec_connections and sys.dm_exec_sessions views to retrieve information about current user connections and internal tasks associated with the database. The following query returns information about the current connection:
SELECT
e.connection_id,
s.session_id,
s.login_name,
s.last_request_end_time,
s.cpu_time
FROM
sys.dm_exec_sessions s
INNER JOIN sys.dm_exec_connections e
ON s.session_id = e.session_id;Use the sys.dm_exec_query_stats view to retrieve aggregate performance statistics for cached query plans. The following query returns information about the top five queries ranked by average CPU time.
SELECT TOP 5 query_stats.query_hash AS "Query Hash",
SUM(query_stats.total_worker_time), SUM(query_stats.execution_count) AS "Avg CPU Time",
MIN(query_stats.statement_text) AS "Statement Text"
FROM
(SELECT QS.*,
SUBSTRING(ST.text,(QS.statement_start_offset/2)+1,((CASE statement_end_offset
WHEN -1 THEN DATALENGTH(ST.text)
ELSE QS.statement_end_offset END- QS.statement_start_offset)/2)+1) AS statement_text
FROM sys.dm_exec_query_stats AS QS
CROSS APPLY sys.dm_exec_sql_text(QS.sql_handle)as ST)as query_stats
GROUP BY query_stats.query_hash
ORDER BY 2 DESC;
Additional Resources
- Introducing SQL Database
- Managing Databases and Logins in SQL Database
- Monitoring SQL Database using Dynamic Management Views
- SQL Database Provisioning Model
- Adding Users to your SQL Database
- Transact-SQL Reference (SQL Database)
[Windows Azure] Managing SQL Database using SQL Server Management Studio的更多相关文章
- 在 Windows Azure 虚拟机中使用 Microsoft SQL Server 安全功能
编辑人员注释:本文章由 SQL Server 团队高级项目经理 Sung Hsueh 撰写. SQL Server 的最新用法之一是利用 Microsoft 的 Windows Azure 基础结构服 ...
- [Windows Azure] How to Scale a SQL Database Solution
How to Scale a SQL Database Solution On Windows Azure, database scalability is synonymous with scale ...
- 【数据库-Azure SQL Database】SQL Server 如何将数据库备份到 Azure Storage
打开本地的 SQL Server Management Studio.首先创建 Credentials.命令如下: IF NOT EXISTS (SELECT * FROM sys.credent ...
- 在英文Windows操作系统上使用SQL Server Management Studio(SSMS)导入Excel 97-2003文件时报错:Failure creating file
今天在公司服务器上使用SQL Server Management Studio(SSMS)导入Excel 97-2003文件(.xls)时报错: Failure creating file. (Mic ...
- (译)Windows Azure的7月更新:SQL数据库,流量管理,自动缩放,虚拟机
Windows Azure的7月更新:SQL数据库,流量管理,自动缩放,虚拟机 今早我们释出一些很棒的Windows Azure更新.这些新的提升包括:SQL数据库:支持SQL自动导出和一个新的高级层 ...
- Sql Server系列:Microsoft SQL Server Management Studio模板资源管理器
模板资源管理器是Microsoft SQL Server Management Studio的一个组件,可以用来SQL代码模板,使用模板提供的代码,省去每次都要输入基本代码的工作. 使用模板资源管理器 ...
- [转]删除SQL Server Management Studio中保存的帐户信息
http://www.2cto.com/database/201208/149850.html 删除SQL Server Management Studio中保存的帐户信息 SQL Serve ...
- 如何分离数据库 (SQL Server Management Studio)
在 SQL Server Management Studio 对象资源管理器中,连接到 SQL Server 数据库引擎的实例上,再展开该实例. 展开“数据库”,并选择要分离的用户数据库的名称. 分离 ...
- How To : Create SQL Server Management Studio Addin
原文 How To : Create SQL Server Management Studio Addin Read the full and original article from Jon Sa ...
随机推荐
- firefox插件卸载
1.根据插件名进行搜索,搜索到相关dll后删除,重启firefox. 2.about:config--plugin.expose_full_path:true,然后about:plugins去查看插件 ...
- 解决ubuntu13.04 有线网络 时常掉线的问题
不少朋友在升级或新装ubuntu13.04时遇到有线老掉线的问题:连上不到半分钟又掉了,把网线重新拔插一下又可以接着又掉..基本不能正常使用或工作,很恼人的问题. 网上这方面的资料很少现在我把解决方法 ...
- 修改cnblogs日历控件CSS
自定义cnblogs日期控件的样式时, 关于透明度 控件头部死活没法透明 在background属性值后边添加!important即把cnblogs系统自带的属性覆盖, 解决了该问题. !import ...
- Cordova笔记(一)
跨平台的PhoneGap被Adobe收购,改名为Cordova,现在是Apache下的一款开源软件.网上能找到的关于PhoneGap的教程有些方法已不适用,在学习使用最新版的Cordova时有些问题需 ...
- [转]shell awk sed tr grep 语法汇总
tr 基本语法 -c # 用字符串1中字符集的补集替换此字符集,要求字符集为ASCII -d # 删除字符串1中所有输入字符 -s # 删除所 ...
- C语言学习笔记 (003) - C/C++中的实参和形参(转)
今天突然看到一道关于形参和实参的题,我居然不求甚解.藐视过去在我的脑海里只有一个参数的概念,对于形参和实参的区别还真的不知道,作为学习了几年C++的人来说,真的深深感觉对不起自己对不起C++老师 T ...
- java string截取两个字符串之间的值
java string截取两个字符串之间的值 import java.util.regex.Matcher; import java.util.regex.Pattern; public class ...
- Jquery 数组操作(转)
在jquery中处理JSON数组的情况中遍历用到的比较多,但是用添加移除这些好像不是太多. 今天试过json[i].remove(),json.remove(i)之后都不行,看网页的DOM对象中好像J ...
- 【Spring】Spring之浅析Spring框架的搭建
Spring是什么 Spring是一个开源的容器框架,用于配置bean并且维护bean之间关系的.其主要的思想就是IOC(Inversion Of Control,控制反转)或者称作DI(Depend ...
- Windows10内置Linux子系统
WSL 前言 前段时间,机子上的win10又偷偷摸摸升级到了一周年正式版,比较无奈.不过之前听闻这个版本已经支持内置的linux子系统,于是就怀着好奇心试玩了一把.虽然期间遇到了很多问题,但总体来 ...