2011-08-23 60 views
4

我運行以下腳本爲我創建的其中一個數據庫創建主密鑰,但我無法看到它們在節點中輸入密鑰(請參見快照);有誰知道爲什麼?我期待聽到您的回覆,謝謝。SQL Server數據庫主密鑰

USE AdventureWorks 
GO 

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '23987hxJ#KL95234nl0zBe' 
GO 

enter image description here

回答

7

你不能看到SSMS GUI這個信息,但是你可以看到,如果你運行下面的數據庫是否有一個主鍵:

SELECT d.is_master_key_encrypted_by_server 
FROM sys.databases AS d 
WHERE d.name = 'AdventureWorks'; 
+0

這裏有用的文檔:http://msdn.microsoft.com/en-us/庫/ ms179331.aspx –

1

這裏有一對夫婦測試DMK存在的方法。

請注意,sys.databases中的[is_master_key_encrypted_by_server]列可以顯示0,但DMK存在並且已從SMK加密中刪除。

我希望這會有所幫助。

======================

-- Test for existence of a DMK. If it does not exist, then create it. 

-- Method 1: 
IF (SELECT COUNT(*) FROM sys.symmetric_keys WHERE name LIKE '%DatabaseMasterKey%') = 0 
BEGIN 
    CREATE MASTER KEY ENCRYPTION BY PASSWORD = '[email protected]' 
END 

-- Method 2: 
IF NOT EXISTS (SELECT * FROM sys.symmetric_keys WHERE name LIKE '%DatabaseMasterKey%') 
BEGIN 
    SELECT 'DMK does not exist' 
    CREATE MASTER KEY ENCRYPTION BY PASSWORD = '[email protected]' 
END 
ELSE 
BEGIN 
    SELECT 'DMK exists' 
END 



-- Demo showing that is_master_key_encrypted_by_server in sys.databases does not show whether the DMK exists or not. 
DROP MASTER KEY 
GO 

CREATE MASTER KEY ENCRYPTION BY PASSWORD = '[email protected]' 
GO 

SELECT is_master_key_encrypted_by_server, name 
FROM sys.databases 
WHERE name = 'GalaxianTest1' 
--> is_master_key_encrypted_by_server name 
--> 1 GalaxianTest1 

USE GalaxianTest1 
GO 

-- This command causes the DMK to not be encrypted by the SMK. 
ALTER MASTER KEY DROP ENCRYPTION BY SERVICE MASTER KEY 

-- This command now shows 0, although the DMK still exists. 
SELECT is_master_key_encrypted_by_server, name 
FROM sys.databases 
WHERE name = 'GalaxianTest1' 
--> is_master_key_encrypted_by_server name 
--> 0 GalaxianTest1 

-- Try creating a new DMK. This will error because the DMK still exists. 
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '[email protected]' 
GO 
--> Error: There is already a master key in the database. Please drop it before performing this statement. 

DROP MASTER KEY 
GO 
--> Command(s) completed successfully. 

SELECT is_master_key_encrypted_by_server, name 
FROM sys.databases 
WHERE name = 'GalaxianTest1' 
--> is_master_key_encrypted_by_server name 
--> 0 GalaxianTest1 
-- Note: this is the same message as above when the DMK existed, but had been dropped from encryption by service master key.