Database
 sql >> डेटाबेस >  >> RDS >> Database

सिंथेटिक डेटा जनरेशन

परिचय

जल्दी या बाद में, किसी भी सूचना प्रणाली को एक डेटाबेस मिलता है, अक्सर - एक से अधिक। समय के साथ, वह डेटाबेस कई जीबी से लेकर दर्जनों टीबी तक बहुत अधिक डेटा एकत्र करता है। यह समझने के लिए कि डेटा वॉल्यूम बढ़ने के साथ फंक्शनल कैसे प्रदर्शन करेंगे, हमें उस डेटाबेस को भरने के लिए डेटा जेनरेट करना होगा।

प्रस्तुत और कार्यान्वित सभी स्क्रिप्ट JobEmplDB . पर निष्पादित होंगी एक भर्ती सेवा का डेटाबेस। डेटाबेस प्राप्ति यहां उपलब्ध है।

डेटाबेस में परीक्षण और विकास के लिए डेटा भरने के दृष्टिकोण

डेटाबेस विकास और परीक्षण में डेटा भरने के लिए दो प्राथमिक दृष्टिकोण शामिल हैं:

  1. उत्पादन परिवेश से संपूर्ण डेटाबेस को कॉपी करने के लिए व्यक्तिगत और अन्य संवेदनशील डेटा को बदल दिया गया है। इस तरह, आप डेटा सुनिश्चित करते हैं और गोपनीय डेटा मिटा देते हैं।
  2. सिंथेटिक डेटा जेनरेट करने के लिए। इसका अर्थ है लुक, प्रॉपर्टी और इंटरकनेक्शन में वास्तविक डेटा के समान टेस्ट डेटा जेनरेट करना।

दृष्टिकोण 1 का लाभ यह है कि यह उत्पादन डेटाबेस के लिए विभिन्न मानदंडों द्वारा डेटा और उनके वितरण का अनुमान लगाता है। यह हमें हर चीज का सटीक विश्लेषण करने की अनुमति देता है और इसलिए, निष्कर्ष निकालने और उसके अनुसार पूर्वानुमान लगाने की अनुमति देता है।

हालाँकि, यह दृष्टिकोण आपको डेटाबेस को स्वयं कई बार बढ़ाने की अनुमति नहीं देता है। भविष्य में संपूर्ण सूचना प्रणाली की कार्यक्षमता में परिवर्तन की भविष्यवाणी करना समस्याग्रस्त हो जाता है।

दूसरी तरफ, आप उत्पादन डेटाबेस से लिए गए अवैयक्तिक स्वच्छता डेटा का विश्लेषण कर सकते हैं। उनके आधार पर, आप परिभाषित कर सकते हैं कि परीक्षण डेटा कैसे उत्पन्न किया जाए जो उनके रूप, गुणों और अंतर्संबंधों द्वारा वास्तविक डेटा की तरह होगा। इस तरह, दृष्टिकोण 1, दृष्टिकोण 2 उत्पन्न करता है।

अब, परीक्षण और विकास के लिए डेटाबेस में डेटा भरने के लिए दोनों दृष्टिकोणों की विस्तार से समीक्षा करें।

उत्पादन डेटाबेस में डेटा की प्रतिलिपि बनाना और उसमें फेरबदल करना

सबसे पहले, आइए उत्पादन परिवेश से डेटा को कॉपी करने और बदलने के सामान्य एल्गोरिथम को परिभाषित करें।

सामान्य एल्गोरिथम

सामान्य एल्गोरिथम इस प्रकार है:

  1. नया खाली डेटाबेस बनाएं।
  2. उस नए बनाए गए डेटाबेस में एक योजना बनाएं - वही सिस्टम जो उत्पादन डेटाबेस से है।
  3. उत्पादन डेटाबेस से आवश्यक डेटा को नए बनाए गए डेटाबेस में कॉपी करें।
  4. नए डेटाबेस में गुप्त डेटा को साफ़ करें और बदलें।
  5. नए बनाए गए डेटाबेस का बैकअप बनाएं।
  6. आवश्यक वातावरण में बैकअप वितरित करें और पुनर्स्थापित करें।

हालांकि, चरण 5 के बाद एल्गोरिदम अधिक जटिल हो जाता है। उदाहरण के लिए, चरण 6 को प्रारंभिक परीक्षण के लिए एक विशिष्ट, संरक्षित वातावरण की आवश्यकता होती है। उस चरण को यह सुनिश्चित करना चाहिए कि सभी डेटा अवैयक्तिक हैं, और गुप्त डेटा बदल गए हैं।

उस चरण के बाद, आप सुरक्षित गैर-उत्पादन वातावरण में परीक्षण किए गए डेटाबेस के लिए चरण 5 पर फिर से लौट सकते हैं। फिर, आप परीक्षण किए गए बैकअप को इसे पुनर्स्थापित करने और विकास और परीक्षण के लिए उपयोग करने के लिए आवश्यक वातावरण में अग्रेषित करते हैं।

हमने उत्पादन डेटाबेस के डेटा की प्रतिलिपि बनाने और बदलने का सामान्य एल्गोरिथम प्रस्तुत किया है। आइए वर्णन करें कि इसे कैसे लागू किया जाए।

सामान्य एल्गोरिथम की प्राप्ति

एक नया खाली डेटाबेस निर्माण

आप यहाँ की तरह CREATE DATABASE निर्माण की मदद से एक खाली डेटाबेस बना सकते हैं।

डेटाबेस का नाम है JobEmplDB_Test . इसके तीन फ़ाइल समूह हैं:

  • प्राथमिक - यह डिफ़ॉल्ट रूप से प्राथमिक फ़ाइल समूह है। यह दो फाइलों को परिभाषित करता है:JobEmplDB_Test1(path D:\DBData\JobEmplDB_Test1.mdf) , और JobEmplDB_Test2 (पथ D:\DBData\JobEmplDB_Test2.ndf) . प्रत्येक फ़ाइल का प्रारंभिक आकार 64 Mb है, और प्रत्येक फ़ाइल के लिए वृद्धि चरण 8 Mb है।
  • DBTableGroup - एक कस्टम फ़ाइल समूह जो दो फ़ाइलें निर्धारित करता है:JobEmplDB_TestTableGroup1 (पथ D:\DBData\JobEmplDB_TestTableGroup1.ndf) और JobEmplDB_TestTableGroup2 (पथ D:\DBData\JobEmplDB_TestTableGroup2.ndf) . प्रत्येक फ़ाइल का प्रारंभिक आकार 8 Gb है, और प्रत्येक फ़ाइल के लिए वृद्धि चरण 1 Gb है।
  • DBIndexGroup - एक कस्टम फ़ाइल समूह जो दो फ़ाइलों को निर्धारित करता है:JobEmplDB_TestIndexGroup1 (पथ D:\DBData\JobEmplDB_TestIndexGroup1.ndf) , और JobEmplDB_TestIndexGroup2 (पथ D:\DBData\JobEmplDB_TestIndexGroup2.ndf) . प्रत्येक फ़ाइल के लिए प्रारंभिक आकार 16 Gb है, और प्रत्येक फ़ाइल के लिए वृद्धि चरण 1 Gb है।

साथ ही, इस डेटाबेस में लेन-देन का एक जर्नल शामिल है:JobEmplDB_Testlog , पथ ई:\DBLog\JobEmplDB_Testlog.ldf . फ़ाइल का प्रारंभिक आकार 8 Gb है, और वृद्धि चरण 1 Gb है।

उत्पादन डेटाबेस से योजना और आवश्यक डेटा को नए बनाए गए डेटाबेस में कॉपी करना

उत्पादन डेटाबेस से योजना और आवश्यक डेटा को नए में कॉपी करने के लिए, आप कई टूल का उपयोग कर सकते हैं। सबसे पहले, यह विजुअल स्टूडियो (एसएसडीटी) है। या, आप तृतीय-पक्ष उपयोगिताओं का उपयोग कर सकते हैं जैसे:

  • डीबीफोर्ज स्कीमा तुलना और डीबीफोर्ज डेटा तुलना
  • एपेक्सएसक्यूएल डिफ और एपेक्स डेटा डिफ
  • SQL टूल और SQL डेटा की तुलना टूल की तुलना करें

डेटा परिवर्तन के लिए स्क्रिप्ट बनाना

डेटा परिवर्तन की स्क्रिप्ट के लिए आवश्यक आवश्यकताएं

1. उस स्क्रिप्ट का उपयोग करके वास्तविक डेटा को पुनर्स्थापित करना असंभव होना चाहिए।
उदाहरण के लिए, लाइनों का उलटा उपयुक्त नहीं होगा, क्योंकि यह हमें वास्तविक डेटा को पुनर्स्थापित करने की अनुमति देता है। आमतौर पर, विधि प्रत्येक वर्ण या बाइट को छद्म यादृच्छिक वर्ण या बाइट द्वारा प्रतिस्थापित करना है। वही तारीख और समय पर लागू होता है।

2. डेटा बदलने से उनके मूल्यों की चयनात्मकता में परिवर्तन नहीं होना चाहिए।
यह तालिका के क्षेत्र में NULL असाइन करने के लिए काम नहीं करेगा। इसके बजाय, आपको यह सुनिश्चित करना होगा कि वास्तविक डेटा में समान मान परिवर्तित डेटा में समान रहेगा। उदाहरण के लिए, वास्तविक डेटा में, आपके पास तालिका में 12 बार पाया गया 103785 का मान है। जब आप इस मान को परिवर्तित डेटा में बदलते हैं, तो नया मान तालिका के समान फ़ील्ड में 12 बार रहना चाहिए।

3. परिवर्तित डेटा में मानों का आकार और लंबाई महत्वपूर्ण रूप से भिन्न नहीं होनी चाहिए। उदाहरण के लिए, आप प्रत्येक बाइट या वर्ण को छद्म यादृच्छिक बाइट या वर्ण से बदलते हैं। प्रारंभिक स्ट्रिंग आकार और लंबाई में समान रहती है।

4. परिवर्तनों के बाद डेटा में अंतर्संबंधों को नहीं तोड़ा जाना चाहिए। यह बाहरी कुंजियों और अन्य सभी मामलों से संबंधित है जहां आप परिवर्तित डेटा को संदर्भित करते हैं। परिवर्तित डेटा उसी संबंध में रहना चाहिए जैसा वास्तविक डेटा था।

डेटा स्क्रिप्ट कार्यान्वयन को बदलता है

अब, गुप्त जानकारी को प्रतिरूपित करने और छिपाने के लिए डेटा बदलने के विशेष मामले की समीक्षा करें। नमूना भर्ती डेटाबेस है।

नमूना डेटाबेस में निम्नलिखित व्यक्तिगत डेटा शामिल होता है जिसे आपको प्रतिरूपित करने की आवश्यकता होती है:

  1. अंतिम और प्रथम नाम;
  2. जन्म तिथि;
  3. आईडी-कार्ड जारी करने की तारीख;
  4. बाइट अनुक्रम के रूप में रिमोट एक्सेस प्रमाणपत्र;
  5. फिर से शुरू प्रचार के लिए सेवा शुल्क।

सबसे पहले, हम प्रत्येक प्रकार के परिवर्तित डेटा के लिए सरल उदाहरणों की जाँच करेंगे:

  1. तिथि और समय परिवर्तन;
  2. संख्यात्मक मान परिवर्तन;
  3. बाइट अनुक्रम बदलना;
  4. चरित्र डेटा परिवर्तन।

तारीख और समय में बदलाव

आप निम्न स्क्रिप्ट का उपयोग करके एक यादृच्छिक दिनांक और समय प्राप्त कर सकते हैं:

DECLARE @dt DATETIME;

SET @dt = CAST(CAST(@StartDate AS FLOAT) + (CAST(@FinishDate AS FLOAT) - CAST(@StartDate AS FLOAT)) * RAND(CHECKSUM(NEWID())) AS DATETIME);

यहां, @StartDate और @FinishDate श्रेणी के प्रारंभ और अंत मान हैं। वे छद्म यादृच्छिक तिथि और समय पीढ़ी के लिए क्रमशः सहसंबंधित हैं।

इन डेटा को उत्पन्न करने के लिए, आप सिस्टम फ़ंक्शन RAND, CHECKSUM, और NEWID का उपयोग करते हैं।

UPDATE [dbo].[Employee]
SET [DocDate] = CAST(CAST(CAST(CAST([BirthDate] AS DATETIME) AS FLOAT) + (CAST(GETDATE() AS FLOAT) - CAST(CAST([BirthDate] AS DATETIME) AS FLOAT)) * RAND(CHECKSUM(NEWID())) AS DATETIME) AS DATE);

[DocDate] फ़ील्ड दस्तावेज़ जारी करने की तारीख के लिए है। हम तारीखों की सीमाओं और उनकी सीमाओं को ध्यान में रखते हुए इसे एक छद्म यादृच्छिक तारीख से बदल देते हैं।

"नीचे" सीमा उम्मीदवार की जन्म तिथि है। "ऊपरी" किनारा वर्तमान तिथि है। हमें यहां समय की आवश्यकता नहीं है, इसलिए समय और दिनांक प्रारूप को आवश्यक तिथि में बदलना अंत में आता है। आप उसी तरह दिनांक और समय के किसी भी भाग के लिए छद्म यादृच्छिक मान प्राप्त कर सकते हैं।

संख्यात्मक मान परिवर्तन

आप निम्न स्क्रिप्ट की सहायता से एक यादृच्छिक पूर्णांक प्राप्त कर सकते हैं:

DECLARE @Int INT;

SET @Int = CAST(((@MaxVal + 1) - @MinVal) *
    RAND(CHECKSUM(NEWID())) + @MinVal AS INT);

@MinVal और @MaxVal छद्म यादृच्छिक संख्या पीढ़ी के लिए प्रारंभ और समाप्ति श्रेणी के मान हैं। हम इसे सिस्टम फ़ंक्शंस RAND, CHECKSUM, और NEWID का उपयोग करके उत्पन्न करते हैं।

UPDATE [dbo].[Employee]
SET [CountRequest] = CAST(((@MaxVal + 1) - @MinVal) *
    RAND(CHECKSUM(NEWID())) + @MinVal AS INT);

[CountRequest] फ़ील्ड उन अनुरोधों की संख्या को दर्शाता है जो कंपनियां इस उम्मीदवार के फिर से शुरू करने के लिए करती हैं।

इसी तरह, आप किसी भी संख्यात्मक मान के लिए छद्म यादृच्छिक मान प्राप्त कर सकते हैं। उदाहरण के लिए, दशमलव प्रकार (18,2) पीढ़ी की यादृच्छिक संख्या पर एक नज़र डालें:

DECLARE @Dec DECIMAL(18,2);

SET @Dec=CAST(((@MaxVal + 1) - @MinVal) *
    RAND(CHECKSUM(NEWID())) + @MinVal AS DECIMAL(18,2));

इस प्रकार, आप निम्न तरीके से फिर से शुरू प्रचार सेवा शुल्क को अपडेट कर सकते हैं:

UPDATE [dbo].[Employee]
SET [PaymentAmount] = CAST(((@MaxVal + 1) - @MinVal) *
    RAND(CHECKSUM(NEWID())) + @MinVal AS DECIMAL(18,2));

बाइट क्रम बदलना

आप निम्न स्क्रिप्ट का उपयोग करके एक यादृच्छिक बाइट अनुक्रम प्राप्त कर सकते हैं:

DECLARE @res VARBINARY(MAX);

SET @res = CRYPT_GEN_RANDOM(@Length, CAST(NEWID() AS VARBINARY(16)));

@लंबाई अनुक्रम की लंबाई के लिए खड़ा है। यह लौटाए गए बाइट्स की संख्या को परिभाषित करता है। यहां, @Length 16 से बड़ी नहीं होनी चाहिए।

सिस्टम फंक्शन CRYPT_GEN_RANDOM और NEWID की मदद से जनरेशन किया जाता है।

उदाहरण के लिए, आप प्रत्येक उम्मीदवार के लिए रिमोट एक्सेस प्रमाणपत्र को निम्न तरीके से अपडेट कर सकते हैं:

UPDATE [dbo].[Employee]
SET [RemoteAccessCertificate] = CRYPT_GEN_RANDOM(CAST(LEN([RemoteAccessCertificate]) AS INT), CAST(NEWID() AS VARBINARY(16)));

हम परिवर्तन के समय [RemoteAccessCertificate] फ़ील्ड में मौजूद समान लंबाई का एक छद्म यादृच्छिक बाइट अनुक्रम उत्पन्न करते हैं। हम मानते हैं कि बाइट अनुक्रम की लंबाई 16 से अधिक नहीं है।

इसी तरह, हम अपना फ़ंक्शन बना सकते हैं जो किसी भी लम्बाई के छद्म यादृच्छिक बाइट अनुक्रम लौटाएगा। यह सिस्टम फ़ंक्शन CRYPT_GEN_RANDOM के परिणामों को सरल "+" अतिरिक्त ऑपरेटर का उपयोग करके एक साथ काम करेगा। लेकिन आमतौर पर 16 बाइट व्यवहार में पर्याप्त होते हैं।

आइए एक निश्चित लंबाई के छद्म यादृच्छिक बाइट अनुक्रम को लौटाते हुए एक नमूना फ़ंक्शन बनाएं, जहां 16 बाइट्स से अधिक की लंबाई निर्धारित करना संभव होगा। इसके लिए निम्नलिखित प्रस्तुतीकरण करें:

CREATE VIEW [test].[GetNewID]
AS
SELECT NEWID() AS [NewID];
GO

हमें फ़ंक्शन के भीतर NEWID का उपयोग करने से मना करने वाली सीमा से बचने के लिए इसकी आवश्यकता है।

इसी तरह, इसी उद्देश्य के लिए अगली प्रस्तुति बनाएं:

CREATE VIEW [test].[GetRand]
AS
SELECT RAND(CHECKSUM((SELECT TOP(1) [NewID] FROM [test].[GetNewID]))) AS [Value];
GO

एक और प्रस्तुति बनाएं:

CREATE VIEW [test].[GetRandVarbinary16]
AS
SELECT CRYPT_GEN_RANDOM(16, CAST((SELECT TOP(1) [NewID] FROM [test].[GetNewID]) AS VARBINARY(16))) AS [Value];
GO

तीनों कार्यों की परिभाषाएँ यहाँ हैं। और यहां फ़ंक्शन का कार्यान्वयन है जो निश्चित लंबाई के छद्म यादृच्छिक बाइट अनुक्रम देता है।

सबसे पहले, हम परिभाषित करते हैं कि आवश्यक कार्य मौजूद है या नहीं। यदि नहीं - हम पहले एक स्टड बनाते हैं। किसी भी मामले में, कोड में फ़ंक्शन की परिभाषा को उचित रूप से बदलना शामिल है। अंत में, हम विस्तारित गुणों के माध्यम से फ़ंक्शन का विवरण जोड़ते हैं। डेटाबेस के दस्तावेज़ीकरण पर अधिक विवरण इस आलेख में हैं।

प्रत्येक उम्मीदवार के लिए रिमोट एक्सेस प्रमाणपत्र को अपडेट करने के लिए, आप निम्न प्रकार से कर सकते हैं:

UPDATE [dbo].[Employee]
SET [RemoteAccessCertificate] = [test].[GetRandVarbinary](CAST(LEN([RemoteAccessCertificate]) AS INT));

जैसा कि आप देख सकते हैं, यहां बाइट अनुक्रम लंबाई की कोई सीमा नहीं है।

डेटा बदलना - वर्ण डेटा परिवर्तन

यहां, हम अंग्रेजी और रूसी वर्णमाला के लिए एक उदाहरण लेते हैं, लेकिन आप इसे किसी अन्य वर्णमाला के लिए कर सकते हैं। केवल शर्त यह है कि इसके पात्र एनसीएचएआर प्रकारों में मौजूद हों।

हमें एक ऐसा फ़ंक्शन बनाने की ज़रूरत है जो लाइन को स्वीकार करे, प्रत्येक वर्ण को एक छद्म यादृच्छिक वर्ण से बदल दे, और फिर परिणाम को एक साथ रखकर उसे वापस कर दे।

हालाँकि, हमें पहले यह समझने की आवश्यकता है कि हमें किन पात्रों की आवश्यकता है। उसके लिए, हम निम्नलिखित स्क्रिप्ट निष्पादित कर सकते हैं:

DECLARE @tbl TABLE ([ValueInt] INT, [ValueNChar] NCHAR(1), [ValueChar] CHAR(1));

DECLARE @ind int=0;
DECLARE @count INT=65535;

WHILE(@count>=0)
BEGIN
	INSERT INTO @tbl ([ValueInt], [ValueNChar], [ValueChar])
	SELECT @ind, NCHAR(@ind), CHAR(@ind)
	
	SET @ind+=1;
	SET @count-=1;
END

SELECT *
INTO [test].[TblCharactersCode]
FROM @tbl;

हम [टेस्ट]।[TblCharacterCode] तालिका बना रहे हैं जिसमें निम्नलिखित फ़ील्ड शामिल हैं:

  • ValueInt - वर्ण का संख्यात्मक मान;
  • ValueNChar - NCHAR-प्रकार का वर्ण;
  • ValueChar - CHAR-प्रकार का वर्ण।

आइए इस तालिका की सामग्री की समीक्षा करें। हमें निम्नलिखित अनुरोध की आवश्यकता है:

SELECT [ValueInt]
      ,[ValueNChar]
      ,[ValueChar]
  FROM [test].[TblCharactersCode];

संख्याएँ 48 से 57 के बीच हैं:

अपरकेस में लैटिन वर्ण 65 से 90 के बीच हैं:

लोअर केयर में लैटिन वर्ण 97 से 122 के बीच हैं:

अपरकेस में रूसी वर्ण 1040 से 1071 की सीमा में हैं:

निचले मामले में रूसी वर्ण 1072 से 1103 की सीमा में हैं:

और, 58 से 64 के बीच के वर्ण:

हम आवश्यक वर्णों का चयन करते हैं और उन्हें [परीक्षण] में डालते हैं। [SelectCharactersCode] तालिका इस प्रकार है:

SELECT
	[ValueInt]
   ,[ValueNChar]
   ,[ValueChar]
   ,CASE
		WHEN ([ValueInt] BETWEEN 48 AND 57) THEN 1
		ELSE 0
	END AS [IsNumeral]
   ,CASE
		WHEN (([ValueInt] BETWEEN 65 AND 90) OR
			([ValueInt] BETWEEN 1040 AND 1071)) THEN 1
		ELSE 0
	END AS [IsUpperCase]
   ,CASE
		WHEN (([ValueInt] BETWEEN 65 AND 90) OR
			([ValueInt] BETWEEN 97 AND 122)) THEN 1
		ELSE 0
	END AS [IsLatin]
   ,CASE
		WHEN (([ValueInt] BETWEEN 1040 AND 1071) OR
			([ValueInt] BETWEEN 1072 AND 1103)) THEN 1
		ELSE 0
	END AS [IsRus]
   ,CASE
		WHEN (([ValueInt] BETWEEN 33 AND 47) OR
			([ValueInt] BETWEEN 58 AND 64)) THEN 1
		ELSE 0
	END AS [IsExtra]
INTO [test].[SelectCharactersCode]
FROM [test].[TblCharactersCode]
WHERE ([ValueInt] BETWEEN 48 AND 57)
OR ([ValueInt] BETWEEN 65 AND 90)
OR ([ValueInt] BETWEEN 97 AND 122)
OR ([ValueInt] BETWEEN 1040 AND 1071)
OR ([ValueInt] BETWEEN 1072 AND 1103)
OR ([ValueInt] BETWEEN 33 AND 47)
OR ([ValueInt] BETWEEN 58 AND 64);

अब, निम्न स्क्रिप्ट का उपयोग करके इस तालिका की सामग्री की जांच करें:

SELECT [ValueInt]
      ,[ValueNChar]
      ,[ValueChar]
      ,[IsNumeral]
      ,[IsUpperCase]
      ,[IsLatin]
      ,[IsRus]
      ,[IsExtra]
  FROM [test].[SelectCharactersCode];

हमें निम्नलिखित परिणाम प्राप्त होते हैं:

इस तरह, हमारे पास [परीक्षण] है।[SelectCharactersCode] टेबल, जहां:

  • ValueInt - चरित्र का संख्यात्मक मान
  • ValueNChar - एनसीएचएआर-टाइप कैरेक्टर
  • ValueChar - CHAR-प्रकार का वर्ण
  • संख्यात्मक है - किसी वर्ण के अंक होने की कसौटी
  • IsUpperCase - अपरकेस में वर्ण की कसौटी
  • इस्लाटीन - किसी वर्ण के लैटिन वर्ण होने की कसौटी;
  • इसरस - एक चरित्र के रूसी चरित्र होने की कसौटी
  • IsExtra - एक पात्र के अतिरिक्त चरित्र होने की कसौटी

अब, हम आवश्यक वर्णों के सम्मिलन के लिए कोड प्राप्त कर सकते हैं। उदाहरण के लिए, लोअर केस में लैटिन अक्षरों के लिए इसे इस प्रकार करना है:

SELECT 'SELECT '+CAST([ValueInt] AS NVARCHAR(255))+' AS [ValueInt], '+''''+[ValueNChar]+''''+' AS [ValueNChar], '+''''+[ValueChar]+''''+' AS [ValueChar] UNION ALL'
  FROM [test].[SelectCharactersCode]
  WHERE [IsUpperCase]=0
    AND [IsLatin]=1;

हमें निम्नलिखित परिणाम प्राप्त होते हैं:

यह निचले मामले में रूसी पात्रों के लिए समान है:

SELECT 'SELECT '+CAST([ValueInt] AS NVARCHAR(255))+' AS [ValueInt], '+''''+[ValueNChar]+''''+' AS [ValueNChar], '+COALESCE(''''+[ValueChar]+'''', 'NULL')+' AS [ValueChar] UNION ALL'
  FROM [test].[SelectCharactersCode]
  WHERE [IsUpperCase]=0
    AND [IsRus]=1;

हमें निम्नलिखित परिणाम मिलते हैं:

यह पात्रों के लिए समान है:

SELECT 'SELECT '+CAST([ValueInt] AS NVARCHAR(255))+' AS [ValueInt], '+''''+[ValueNChar]+''''+' AS [ValueNChar], '+''''+[ValueChar]+''''+' AS [ValueChar] UNION ALL'
  FROM [test].[SelectCharactersCode]
  WHERE [IsNumeral]=1;

परिणाम इस प्रकार है:

इसलिए, हमारे पास निम्नलिखित डेटा को अलग से सम्मिलित करने के लिए कोड हैं:

  • लोअर केस में लैटिन वर्ण।
  • लोअर केस में रूसी पात्र।
  • अंक।

यह NCHAR और CHAR दोनों प्रकार के लिए काम करता है।

इसी तरह, हम पात्रों के किसी भी सेट के लिए एक इंसर्शन स्क्रिप्ट तैयार कर सकते हैं। इसके अलावा, प्रत्येक सेट का अपना सारणीकरण कार्य होगा।

सरल होने के लिए, हम सामान्य सारणीकरण फ़ंक्शन को लागू करते हैं जो पहले से चयनित डेटा के लिए आवश्यक डेटासेट निम्नलिखित तरीके से लौटाएगा:

SELECT
	'SELECT ' + CAST([ValueInt] AS NVARCHAR(255)) + ' AS [ValueInt], '
	+ '''' + [ValueNChar] + '''' + ' AS [ValueNChar], '
	+ COALESCE('''' + [ValueChar] + '''', ‘NULL’) + ' AS [ValueChar], '
	+ CAST([IsNumeral] AS NCHAR(1)) + ' AS [IsNumeral], ' +
	+CAST([IsUpperCase] AS NCHAR(1)) + ' AS [IsUpperCase], ' +
	+CAST([IsLatin] AS NCHAR(1)) + ' AS [IsLatin], ' +
	+CAST([IsRus] AS NCHAR(1)) + ' AS [IsRus], ' +
	+CAST([IsExtra] AS NCHAR(1)) + ' AS [IsExtra]' +
	+' UNION ALL'
FROM [test].[SelectCharactersCode];

अंतिम परिणाम इस प्रकार है:

तैयार स्क्रिप्ट को सारणीकरण फ़ंक्शन [परीक्षण] में लपेटा गया है। [GetSelectCharacters]।

उत्पन्न स्क्रिप्ट के अंत में एक अतिरिक्त UNION ALL को हटाना महत्वपूर्ण है, और [ValueInt]=39 में, हमें "' से "":

को बदलने की आवश्यकता है।
SELECT 39 AS [ValueInt], '''' AS [ValueNChar], '''' AS [ValueChar], 0 AS [IsNumeral], 0 AS [IsUpperCase], 0 AS [IsLatin], 0 AS [IsRus], 1 AS [IsExtra] UNION ALL

यह सारणीकरण फ़ंक्शन फ़ील्ड के निम्नलिखित सेट को लौटाता है:

  • संख्या - डेटा के दिए गए सेट में लाइन नंबर;
  • ValueInt - चरित्र का संख्यात्मक मान;
  • ValueNChar - एनसीएचएआर-टाइप कैरेक्टर;
  • ValueChar - CHAR-प्रकार का वर्ण;
  • संख्यात्मक है - वर्ण के अंक होने की कसौटी;
  • IsUpperCase - यह परिभाषित करने वाला मानदंड कि वर्ण अपरकेस में है;
  • इस्लाटीन - यह परिभाषित करने वाली कसौटी कि वर्ण एक लैटिन वर्ण है;
  • इसरस - मानदंड परिभाषित करता है कि चरित्र एक रूसी चरित्र है;
  • IsExtra - मानदंड यह परिभाषित करता है कि चरित्र एक अतिरिक्त है।

इनपुट के लिए, आपके पास निम्नलिखित पैरामीटर हैं:

  1. @IsNumral - अगर इसे नंबर वापस करना चाहिए;
  2. @IsUpperCase :
    • 0 - इसे अक्षरों के लिए केवल निचला केस वापस करना होगा;
    • 1 - इसे केवल बड़े अक्षरों को वापस करना होगा;
    • NULL - इसे सभी मामलों में पत्र वापस करना होगा।
  3. @IsLatin - इसे लैटिन वर्ण अवश्य लौटाना चाहिए
  4. @IsRus - इसे रूसी पात्रों को वापस करना होगा
  5. @IsExtra - इसे अतिरिक्त वर्ण वापस करने होंगे।

सभी झंडे तार्किक OR के अनुसार उपयोग किए जाते हैं। उदाहरण के लिए, यदि आपको लोअर केस में अंकों और लैटिन वर्णों को वापस करने की आवश्यकता है, तो आप सारणीकरण फ़ंक्शन को निम्न तरीके से कॉल करते हैं:

हमें निम्नलिखित परिणाम मिलते हैं:

declare
@IsNumeral BIT=1
   ,@IsUpperCase BIT=0
   ,@IsLatin BIT=1
   ,@IsRus BIT=0
   ,@IsExtra BIT=0;

SELECT *
FROM [test].[GetSelectCharacters](@IsNumeral, @IsUpperCase, @IsLatin, @IsRus, @IsExtra);

हमें निम्नलिखित परिणाम मिलते हैं:

हम [टेस्ट] को लागू करते हैं। [GetRandString] फ़ंक्शन जो प्रारंभिक स्ट्रिंग लंबाई को ध्यान में रखते हुए, छद्म यादृच्छिक वर्णों के साथ लाइन को प्रतिस्थापित करेगा। इस फ़ंक्शन में केवल उन वर्णों के संचालन की संभावना शामिल होनी चाहिए जो अंक हैं। उदाहरण के लिए, जब आप आईडी-कार्ड की श्रृंखला और संख्या बदलते हैं तो यह उपयोगी हो सकता है।

जब हम [टेस्ट]। [GetRandString] फ़ंक्शन को लागू करते हैं, तो हमें सबसे पहले इनपुट पैरामीटर @Length में निर्दिष्ट लंबाई की छद्म यादृच्छिक रेखा उत्पन्न करने के लिए आवश्यक वर्णों का सेट मिलता है। बाकी पैरामीटर ऊपर बताए अनुसार काम करते हैं।

फिर, हम डेटा के प्राप्त सेट को सारणीकरण चर @tbl . में डालते हैं . यह तालिका फ़ील्ड [आईडी] - वर्णों की परिणामी तालिका में क्रम संख्या, और [मान] - एनसीएचएआर प्रकार में चरित्र की प्रस्तुति को सहेजती है।

उसके बाद, एक चक्र में, यह पहले प्राप्त @tbl वर्णों की कार्डिनैलिटी के लिए 1 की सीमा में एक छद्म यादृच्छिक संख्या उत्पन्न करता है। हम इस नंबर को खोज के लिए सारणीकरण चर @tbl के [ID] में डालते हैं। जब खोज लाइन लौटाती है, तो हम [वैल्यू] कैरेक्टर लेते हैं और परिणामी लाइन @res पर इसे "गोंद" करते हैं।

जब साइकिल का काम खत्म हो जाता है, तो प्राप्त लाइन @res वेरिएबल के माध्यम से वापस आ जाती है।

आप उम्मीदवार के प्रथम और अंतिम दोनों नामों को निम्न तरीके से बदल सकते हैं:

UPDATE [dbo].[Employee]
SET [FirstName] = [test].[GetRandString](LEN([FirstName])),
        [LastName] = [test].[GetRandString](LEN([LastName]));

इस प्रकार, हमने NCHAR और NVARCHAR प्रकारों के लिए फ़ंक्शन के कार्यान्वयन और इसके उपयोग की जांच की है। हम इसे CHAR और VARCHAR प्रकारों के लिए आसानी से कर सकते हैं।

कभी-कभी, हालांकि, हमें वर्ण सेट के अनुसार एक पंक्ति उत्पन्न करने की आवश्यकता होती है, न कि वर्णानुक्रमिक वर्णों या संख्याओं के अनुसार। इस तरह, हमें सबसे पहले निम्नलिखित मल्टी-ऑपरेटर फ़ंक्शन [परीक्षण] का उपयोग करने की आवश्यकता है। [GetListCharacters]।

[टेस्ट]। [GetListCharacters] फ़ंक्शन को इनपुट के लिए निम्नलिखित दो पैरामीटर मिलते हैं:

  1. @str - वर्णों की पंक्ति ही;
  2. @IsGroupUnique - यह परिभाषित करता है कि क्या उसे पंक्ति में अद्वितीय वर्णों को समूहबद्ध करने की आवश्यकता है।

रिकर्सिव सीटीई के साथ, इनपुट लाइन @str वर्णों की तालिका में बदल जाती है - @ListCharacters। उस तालिका में निम्नलिखित फ़ील्ड हैं:

  1. आईडी - वर्णों की परिणामी तालिका में पंक्ति की क्रम संख्या;
  2. चरित्र - एनसीएचएआर में चरित्र की प्रस्तुति(1)
  3. गणना - पंक्ति में चरित्र के दोहराव की संख्या (यह हमेशा 1 होता है यदि पैरामीटर @IsGroupUnique=0)

आइए इसके कार्य को बेहतर ढंग से समझने के लिए इस फ़ंक्शन के उपयोग के दो उदाहरण लेते हैं:

  • अद्वितीय वर्णों की सूची में रेखा का रूपांतरण:
SELECT *
FROM [test].[GetListCharacters]('123456888  789 0000', 0);

हमें परिणाम मिलता है:

इस उदाहरण से पता चलता है कि रेखा वर्णों की विशिष्टता के आधार पर "जैसी है" वर्णों की सूची में बदल जाती है ([गणना] फ़ील्ड में हमेशा 1 होता है।

  • अद्वितीय वर्णों की सूची में रेखा का परिवर्तन
SELECT *
FROM [test].[GetListCharacters]('123456888  789 0000', 1);

परिणाम इस प्रकार है:

इस उदाहरण से पता चलता है कि रेखा को उनकी विशिष्टता के आधार पर समूहीकृत वर्णों की सूची में बदल दिया गया है। [गणना] फ़ील्ड इनपुट लाइन में प्रत्येक वर्ण के निष्कर्षों की संख्या प्रदर्शित करता है।

मल्टी-ऑपरेटर फ़ंक्शन [टेस्ट] के आधार पर। [GetListCharacters], हम एक स्केलर फ़ंक्शन [टेस्ट] बनाते हैं। [GetRandString2]।

नए स्केलर फ़ंक्शन की परिभाषा [टेस्ट] के लिए इसकी समानता दिखाती है। [GetRandString] स्केलर फ़ंक्शन। फर्क सिर्फ इतना है कि यह [टेस्ट] का उपयोग करता है। [गेटलिस्ट कैरेक्टर्स] मल्टी-ऑपरेटर फ़ंक्शन के बजाय [टेस्ट]। [गेटसेलेक्ट कैरेक्टर्स] टेबुलेशन फ़ंक्शन।

यहां, आइए कार्यान्वित स्केलर फ़ंक्शन उपयोग के दो उदाहरणों की समीक्षा करें :

अद्वितीयता के आधार पर समूहीकृत नहीं किए गए वर्णों की इनपुट लाइन से हम 12 वर्णों की एक छद्म यादृच्छिक रेखा उत्पन्न करते हैं:

SELECT [test].[GetRandString2](12, '123456789!!!!!!!!0!!!', DEFAULT);

नतीजा यह है:

64017!!5!!!7

कीवर्ड डिफ़ॉल्ट है। यह बताता है कि डिफ़ॉल्ट मान पैरामीटर सेट करता है। यहाँ, यह शून्य (0) है।

या

हम विशिष्टता द्वारा समूहीकृत वर्णों की इनपुट लाइन से 12 वर्णों की लंबाई में एक छद्म यादृच्छिक रेखा उत्पन्न करते हैं:

SELECT [test].[GetRandString2](12, '123456789!!!!!!!!0!!!', 1);

नतीजा यह है:

35792!428273

डेटा स्वच्छता और गुप्त डेटा परिवर्तन के लिए सामान्य स्क्रिप्ट का कार्यान्वयन

हमने प्रत्येक प्रकार के परिवर्तित डेटा के लिए सरल उदाहरणों की जांच की है:

  1. तारीख और समय बदलना;
  2. संख्यात्मक मान बदलना;
  3. बाइट अनुक्रम बदलना;
  4. पात्रों का डेटा बदलना।

हालांकि, ये उदाहरण डेटा बदलने वाली स्क्रिप्ट के मानदंड 2 और 3 को पूरा नहीं करते हैं:

  • मानदंड 2 :परिवर्तित डेटा में मूल्यों की चयनात्मकता महत्वपूर्ण रूप से नहीं बदलेगी। आप तालिका के क्षेत्र के लिए NULL का उपयोग नहीं कर सकते। इसके बजाय, आपको यह सुनिश्चित करना होगा कि वही वास्तविक डेटा मान बदले गए डेटा में समान रहें। उदाहरण के लिए, यदि वास्तविक डेटा में परिवर्तन के अधीन तालिका के फ़ील्ड में 103785 मान 12 बार होता है, तो संशोधित डेटा में तालिका के एक ही फ़ील्ड में 12 बार पाया गया एक अलग (बदला हुआ) मान शामिल होना चाहिए।
  • मानदंड 3 :परिवर्तित डेटा में मानों की लंबाई और आकार को महत्वपूर्ण रूप से नहीं बदला जाना चाहिए। उदा., आप प्रत्येक वर्ण/बाइट को एक छद्म यादृच्छिक वर्ण/बाइट से बदलते हैं।

इस प्रकार, हमें तालिका के क्षेत्रों में मूल्यों की चयनात्मकता को ध्यान में रखते हुए एक स्क्रिप्ट बनाने की आवश्यकता है।

आइए भर्ती सेवा के लिए हमारे डेटाबेस पर एक नज़र डालें। जैसा कि हम देखते हैं, व्यक्तिगत डेटा केवल उम्मीदवारों की तालिका में मौजूद हैं [dbo]।[कर्मचारी]।

मान लें कि तालिका में निम्नलिखित फ़ील्ड शामिल हैं:

विवरण:

  • प्रथम नाम - नाम, लाइन NVARCHAR(255)
  • अंतिम नाम – उपनाम, लाइन NVARCHAR(255)
  • जन्मतिथि - जन्म तिथि, दिनांक
  • डॉक नंबर - पासपोर्ट श्रृंखला के लिए शुरुआत में दो अंकों वाला आईडी-कार्ड नंबर, और अगले सात अंक दस्तावेज़ की संख्या हैं। उनके बीच, हमारे पास NCHAR(10) लाइन के रूप में एक हाइफ़न है।
  • डॉकडेट - आईडी-कार्ड जारी करने की तिथि, दिनांक
  • अनुरोध करें - रिज्यूमे की खोज के दौरान उस उम्मीदवार के लिए अनुरोधों की संख्या, पूर्णांक INT
  • भुगतान राशि - रिज्यूम प्रमोशन सेवा शुल्क प्राप्त हुआ, दशमलव संख्या (18,2)
  • RemoteAccessCertificate - रिमोट एक्सेस सर्टिफिकेट, बाइट सीक्वेंस VARBINARY
  • पता - आवासीय पता या पंजीकरण पता, लाइन NVARCHAR(MAX)

फिर, प्रारंभिक चयनात्मकता बनाए रखने के लिए, हमें निम्नलिखित एल्गोरिथम को लागू करने की आवश्यकता है:

  1. प्रत्येक फ़ील्ड के लिए सभी अद्वितीय मान निकालें और परिणामों को अस्थायी तालिकाओं या सारणीकरण चर में रखें;
  2. प्रत्येक अद्वितीय मान के लिए एक छद्म यादृच्छिक मान उत्पन्न करें। यह छद्म यादृच्छिक मान मूल मान से लंबाई और आकार में महत्वपूर्ण रूप से भिन्न नहीं होना चाहिए। रिजल्ट को उसी जगह सेव करें जहां हमने पॉइंट 1 रिजल्ट सेव किया था। प्रत्येक नए उत्पन्न मूल्य में एक अद्वितीय वर्तमान मूल्य सहसंबद्ध होना चाहिए।
  3. तालिका में सभी मानों को बिंदु 2 से नए मानों से बदलें।

शुरुआत में, हम उम्मीदवारों के पहले और अंतिम नामों का प्रतिरूपण करते हैं। हम मानते हैं कि अंतिम और प्रथम नाम हमेशा मौजूद होते हैं, और वे प्रत्येक क्षेत्र में कम से कम दो वर्ण लंबे होते हैं।

सबसे पहले, हम अद्वितीय नामों का चयन करते हैं। फिर, यह प्रत्येक नाम के लिए एक छद्म यादृच्छिक रेखा उत्पन्न करता है। नाम की लंबाई वही रहती है; पहला अक्षर अपरकेस में है, और अन्य वर्ण निचले मामले में हैं। हम पहले बनाए गए [परीक्षण] का उपयोग करते हैं। [GetRandString] स्केलर फ़ंक्शन परिभाषित वर्णों के मानदंडों के अनुसार विशिष्ट लंबाई की छद्म यादृच्छिक रेखा उत्पन्न करने के लिए।

फिर, हम उम्मीदवारों की तालिका में उनके अद्वितीय मूल्यों के अनुसार नामों को अपडेट करते हैं। यह अंतिम नामों के लिए समान है।

हम DocNumber फ़ील्ड को प्रतिरूपित करते हैं। यह आईडी-कार्ड (पासपोर्ट) नंबर है। पहले दो अक्षर दस्तावेज़ की श्रृंखला के लिए खड़े हैं, और अंतिम सात अंक दस्तावेज़ की संख्या हैं। उनके बीच में हाइफ़न है। फिर हम सैनिटाइजेशन ऑपरेशन करते हैं।

हम सभी अद्वितीय दस्तावेज़ों की संख्याएँ एकत्र करते हैं और प्रत्येक के लिए एक छद्म यादृच्छिक रेखा उत्पन्न करते हैं। लाइन का प्रारूप 'XX-XXXXXXX' है, जहां एक्स 0 से 9 की सीमा में अंक है। यहां, हम पहले से बनाए गए [टेस्ट] का उपयोग करते हैं। [GetRandString] स्केलर फ़ंक्शन के अनुसार निर्दिष्ट लंबाई की छद्म यादृच्छिक रेखा उत्पन्न करने के लिए पात्रों के पैरामीटर सेट।

उसके बाद, [DocNumber] फ़ील्ड को उम्मीदवारों की तालिका [dbo] में अपडेट किया जाता है। [कर्मचारी]।

हम DocDate फ़ील्ड (आईडी-कार्ड जारी करने की तारीख) और जन्मतिथि फ़ील्ड (उम्मीदवार की जन्मतिथि) को प्रतिरूपित करते हैं।

सबसे पहले, हम "जन्म तिथि और आईडी-कार्ड जारी करने की तारीख" से बने सभी अद्वितीय जोड़े का चयन करते हैं। For each such pair, we create a pseudorandom date for the date of birth. The pseudorandom date of the ID-card issue is made according to that “date of birth” – the date of the document’s issue must not be earlier than the date of birth.

After that, these data are updated in the respective fields of the candidates’ table [dbo].[Employee].

And, we update the remaining fields of the table.

The CountRequest value stands for the number of requests made for that candidate by companies during the resume search.

The PaymentAmount is the final amount of the resume promotion service fee paid. We calculate these numbers similarly to the previous fields.

Note that it generates a pseudorandom integer for the first case and a pseudorandom decimal for the second case. In both cases, the pseudorandom number generation occurs in the range of “two times less than original” to “two times more than original.” The selectivity of values in the fields is not changed too much.

After that, it writes the values into the fields of the candidates’ table [dbo].[Employee].

Further, we collect unique values of the RemoteAccessCertificate field for the remote access certificate. We generate a pseudorandom byte sequence for each such value. The length of the sequence must be the same as the original. Here, we use the previously created [test].[GetRandVarbinary] scalar function to generate the pseudorandom byte sequence of the specified length.

Then recording into the respective field [RemoteAccessCertificate] of the [dbo].[Employee] candidates’ table takes place.

The last step is the collection of the unique addresses from the [Address] field. For each value, we generate a pseudorandom line of the same length as the original. Note that if it was NULL originally, it must be NULL in the generated field. It allows you to keep NULL and don’t replace it with an empty line. It minimizes the selectivity values’ mismatch in this field between the production database and the altered data.

We use the previously created [test].[GetRandString] scalar function to generate the pseudorandom line of the specified length according to the characters’ parameters defined.

It then records the data into the respective [Address] field of the candidates’ table [dbo].[Employee].

This way, we get the full script for depersonalization and altering of the confidential data.

Finally, we get the database with altered personal and confidential data. The algorithms we used make it impossible to restore the original data from the altered data. Also, the values’ selectivity, length, and size aren’t changed significantly. So, the three criteria for the personal and secret data altering scripts are ensured.

We won’t review the criterion 4 separately here. Our database contains all the data subject to change in one candidates’ table [dbo].[Employee]. The data conformity is needed within this table only. Thus, criterion 4 is also here. However, we need to remember this criterion 4 claiming that all interrelations must remain the same in the altered data.

We often see other conditions for personal and confidential data altering algorithms, but we won’t review them here. Besides, the four criteria described above are always present. In many cases, it is enough to estimate the functionality of the algorithm suitable to use it.

Now, we need to make a backup of the created database, check it by restoring on another instance, and transfer that copy into the necessary environment for development and testing. For this, we examine the full database backup creation.

Full database backup creation

We can make a database backup with construction BACKUP DATABASE as in our example.

Make a compressed full backup of the database JobEmplDB_Test. The checksum calculation takes place in the file E:\Backup\JobEmplDB_Test.bak. Further, we check the backup created.

Then, we check the backup created by restoring the database for it. Let’s examine the database restoring.

Restoring the database

You can restore the database with the help of RESTORE DATABASE construction in the following way:

USE [master]
RESTORE DATABASE [JobEmplDB_Test]
FROM  DISK = N'E:\Backup\JobEmplDB_Test.bak' WITH  FILE = 1,
MOVE N'JobEmplDB' TO N'E:\DBData\JobEmplDB.mdf',
MOVE N'JobEmplDB_log' TO N'F:\DBLog\JobEmplDB_log.ldf',
NOUNLOAD,
REPLACE,
STATS = 5;
GO

We restore the JobEmplDB_Test database of the E:\Backup\JobEmplDB_Test.bak backup. The files will be overwritten, and the data file will be transferred into the file E:\DBData\JobEmplDB.mdf , while the transactions log file will be moved into F:\DBLog\JobEmplDB_log.ldf

After we successfully check how the database is restored from the backup, we forward the backup to the development and testing environments. It will be restored again with the same method as described above.

We’ve examined the first approach to the data populating into the database for testing and development. This approach implies copying and altering the data from the production database. Now, we’ll examine the second approach – the synthetic data generation.

Synthetic data generation

The General algorithm for the synthetic data generation is following:

  1. Make a new empty database or clear a previously created database by purging all data.
  2. Create or renew a scheme in the newly created database – the same as that of the production databases.
  3. Copy of renew guidelines and regulations from the production database and transfer them into the new database.
  4. Generate synthetic data into the necessary tables of the new database.
  5. Make a backup of a new database.
  6. Deliver and restore the new backup in the necessary environment.

We already have the JobEmplDB_Test database to practice, and we have reviewed the means of creating a schema in the new database. Let’s focus on the tasks that are specific to this approach.

Clean up the database with the data purge

To clear the database off all its data, we need to do the following:

  1. Keep the definitions of all external keys.
  2. Disable all limitations and triggers.
  3. Delete all external keys.
  4. Clear the tables using the TRUNCATE construction.
  5. Restore all the external keys deleted in point 3.
  6. Enable all the limitations disabled in point 2.

You can save the definitions of all external keys with the following script:

1. The external keys’ definitions are saved in the tabulation variable @tbl_create_FK

2. You can disable the limitations and triggers with the following script:

EXEC sp_MSforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT ALL";
EXEC sp_MSforeachtable "ALTER TABLE ? DISABLE TRIGGER ALL";

To enable the limitations and triggers, you can use the following script:

EXEC sp_MSforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL";
EXEC sp_MSforeachtable "ALTER TABLE ? ENABLE TRIGGER ALL";

Here, we use the saved procedure sp_MSforeachtable that applies the construction to all the database’s tables.

3. To delete external keys, use the special script. Here, we receive the information about the external keys through the INFORMATION_SCHEMA.TABLE_CONSTRAINTS system presentation. We delete external keys through the cursor, one by one, using the formed dynamic script T-SQL, transferring the request into the system saved procedure sp_executesql

4. To clear the tables with the TRUNCATE construction, use the dedicated script. The script works in the same way as above, but it receives the data for tables, and then it clears the tables one by one through the cursor, using the TRUNCATE construction.

5. Restoring the external keys is possible with the below script (earlier, we saved the external keys’ definitions in the tabulation variable @tbl_create_FK):

DECLARE @tsql NVARCHAR(4000);

DECLARE FK_Create CURSOR LOCAL FOR SELECT
	[Script]
FROM @tbl_create_FK;

OPEN FK_Create;

FETCH NEXT FROM FK_Create INTO @tsql;

WHILE (@@fetch_status = 0)
BEGIN
	EXEC sp_executesql @tsql = @tsql;
	FETCH NEXT FROM FK_Create INTO @tsql;
END

CLOSE FK_Create;
DEALLOCATE FK_Create;

The script works in the same way as the two other scripts we mentioned above. But it restores the external keys’ definitions through the cursor, one for each iteration.

A particular case of data purging in the database is the current script. To get this output, we need the below construction in the scripts:

EXEC sp_executesql @tsql = @tsql;

Before this construction, or instead of it, we need to write the generated construction output. It is necessary to call it manually or via the dynamic T-SQL query. We do it via the system saved procedure sp_executesql.

Instead of the below code fragment in all cases:

WHILE (@@fetch_status = 0)
BEGIN
	EXEC sp_executesql @tsql = @tsql;
...

We write:

WHILE (@@fetch_status = 0)
BEGIN
	PRINT @tsql;

	EXEC sp_executesql @tsql = @tsql;
...

Or:

WHILE (@@fetch_status = 0)
BEGIN
	PRINT @tsql;
...

The first case implies both the output of constructions and their execution. The second case if for the output only – it is helpful for the scripts’ debugging.

Thus, we get the general database cleaning script.

Copy guidelines and references from the production database to the new one

Here you can use the T-SQL scripts. Our example database of the recruitment service includes 5 guidelines:

  • [dbo].[Company] – companies
  • [dbo].[Skill] – skills
  • [dbo].[Position] – positions (occupation)
  • [dbo].[Project] – projects
  • [dbo].[ProjectSkill] – project and skills’ correlations

The “skills” table [dbo].[Skill] serves to show how to make a script for the data insertion from the production database into the test database.

We form the following script:

SELECT 'SELECT '+CAST([SkillID] AS NVARCHAR(255))+' AS [SkillID], '+''''+[SkillName]+''''+' AS [SkillName] UNION ALL'
  FROM [dbo].[Skill]
ORDER BY [SkillID];

We execute it in a copy of the production database that is usually available in read-only mode. It is a replica of the production database.

नतीजा यह है:

Now, wrap the result up into the script for the data adding as here. We have a script for the skills’ guideline compilation. The scripts for other guidelines are made in the same way.

However, it is much easier to copy the guidelines’ data through the data export and import in SSMS. Or, you can use the data import and export wizard.

Generate synthetic data

We’ve determined the pseudorandom values’ generation for lines, numbers, and byte sequences. It took place when we examined the implementation of the data sanitization and the confidential data altering algorithms for approach 1. Those implemented functions and scripts are also used for the synthetic data generation.

The recruitment service database requires us to fill the synthetic data in two tables only:

  • [dbo].[Employee] – candidates
  • [dbo].[JobHistory] – a candidate’s work history (experience), the resume itself

We can fill the candidates’ table [dbo].[Employee] with synthetic data using this script.

At the beginning of the script, we set the following parameters:

  • @count – the number of lines to be generated
  • @LengthFirstName – the name’s length
  • @LengthLastName – the last name’s length
  • @StartBirthDate – the lower limit of the date for the date of birth
  • @FinishBirthDate – the upper limit of the date for the date of birth
  • @StartCountRequest – the lower limit for the field [CountRequest]
  • @FinishCountRequest – the upper limit for the field [CountRequest]
  • @StartPaymentAmount – the lower limit for the field [PaymentAmount]
  • @FinishPaymentAmount – the upper limit for the field [PaymentAmount]
  • @LengthRemoteAccessCertificate – the byte sequence’s length for the certificate
  • @LengthAddress – the length for the field [Address]
  • @count_get_unique_DocNumber – the number of attempts to generate the unique document’s number [DocNumber]

The script complies with the uniqueness of the [DocNumber] field’s value.

Now, let’s fill the [dbo].[JobHistory] table with synthetic data as follows.

The start date of work [StartDate] is later than the issuing date of the candidate’s document [DocDate]. The end date of work [FinishDate] is later than the start date of work [StartDate].

It is important to note that the current script is simplified, as it does not deal with parameters of the generated data selectivity configuration.

Make a full database backup

We can make a database backup with the construction BACKUP DATABASE, using our script.

We create a full compressed backup of the database JobEmplDB_Test. The checksum is calculated into the file E:\Backup\JobEmplDB_Test.bak. It also ensures further testing of the backup.

Let’s check the backup by restoring the database from it. We need to examine the database restoring then.

Restore the database

You can restore the database with the help of the RESTORE DATABASE construction, as shown below:

USE [master]
RESTORE DATABASE [JobEmplDB_Test]
FROM  DISK = N'E:\Backup\JobEmplDB_Test.bak' WITH  FILE = 1,
MOVE N'JobEmplDB' TO N'E:\DBData\JobEmplDB.mdf',
MOVE N'JobEmplDB_log' TO N'F:\DBLog\JobEmplDB_log.ldf',
NOUNLOAD,
REPLACE,
STATS = 5;
GO

We restore the database JobEmplDB_Test from the backup E:\Backup\JobEmplDB_Test.bak. The files are overwritten, and the data file is transferred to the file E:\DBData\JobEmplDB.mdf. The transaction log file is transferred to file F:\DBLog\JobEmplDB_log.ldf.

After checking the database restoring from the backup successfully, we transfer the backup to the necessary environments. It will be used for testing and development, and further deployment through the database restoring, as described above.

This way, we’ve examined the second approach to filling the database in with data for testing and development purposes. It is the synthetic data generation approach.

Data generation tools (for external resources)

When we have a job to fill in the database with data for testing and development purposes, it can be much faster and easier with the help of specialized tools. Let’s review the most popular and powerful data generation tools and explore their practical usage.

Full list of tools

DATPROF

IRI RowGen

Data Generator for SQL Server

Redgate SQL Data Generator

DTM Data Generator

Datanamic Data Generator MultiDB

Now, let’s examine one of these tools more precisely.

An overview of the employees’ generation by the Data Generator for SQL Server

The Data Generator for SQL Server utility is embedded in SSMS, and also it is a part of dbForge Studio. We reviewed this utility here. Let’s now examine how it works for synthetic data generation. As examples, we use the [dbo].[Employee] and the [dbo].[JobHistory] tables.

This generator can quickly generate first and last names of candidates for the [FirstName] and [LastName] fields respectively:

Note that FirstName requires choosing the “First Name” value in the “Generator” section. For LastName, you need to select the “Last Name” value from the “Generator” section.

It is important to note that the generator automatically determines which generation type it needs to apply to every field. The settings above were set by the generator itself, without manual correction.

You can configure distribution of values for the date of birth [BirthDate]:

Set the distribution for the document’s date of issue [DocDate] through the Phyton generator using the below script:

import random
from System import DateTime
# receive the value from the Birthday field 
bd = DateTime.Parse(str(BirthDate))
# receive the current date 
current = DateTime.Now
# calculate the age in years 
timeSpan = current - bd
age = (int)(timeSpan.TotalDays / 365);
# passport’s date of issue 
releaseDate = 0
if age >= 45:
releaseDate = bd.AddDays(45 * 365 + random.randint(1, 30)) 
# randomize the issue during the month
elif age >= 20:
  releaseDate  = bd.AddDays(20 * 365 + random.randint(1, 30))
 # randomize the issue during the month
else:
  releaseDate  = bd.AddDays(14 * 365 + random.randint(1, 30)) 
# randomize the issue during the month
releaseDate

This way, the [DocDate] configuration will look as follows:

For the document’s number [DocNumber], we can select the necessary type of unique data generation, and edit the generated data format, if needed:

E.g., instead of the format

(0[1-6]|1[0-6]|2[0-7]|3[0-9]|4[0-68]|5[0-9]|6[0-8]|7[1-7]|8[0-8]|9[0-589])-(\d{7})

We can set the following format:

(\d{2})-(\d{7})

This format means that the line will be generated in format XX-XXXXXXX (X – is a digit in the range of 0 to 9).

We set up the generator for [CountRequest] and [PaymentAmount] fields in the same way, according to the generated data type:

In the first case, we set the values’ range of 0 to 2048 for [CountRequest]. In the second case, it is the range of 0 to 100000 for [PaymentAmount].

We configure generation for [RemoteAccessCertificate] and [Address] fields in the same way:

In the first case, we limit the byte sequence [RemoteAccessCertificate] with the range of lengths of 16 to 32. In the second case, we select values for [Address] as real addresses. It makes the generated values looking like the real ones.

This way, we’ve configured the synthetic data generation settings for the candidates’ table [dbo].[Employee]. Let’s now set up the synthetic data generation for the [dbo].[JobHistory] table.

We set it to take the data for the [EmployeeID] field from the candidates’ table [dbo].[Employee] in the following way:

We select the generator’s type from the table or presentation. We then define the sample of MS SQL Server, the database, and the table to take the data from. We can also configure filters in the “WHERE filter” section, and select the [EmployeeID] field.

Here we suppose that we generate the “employees” first, and then we generate the data for the [dbo].[JobHistory] table, basing on the filled [dbo].[Employee] reference.

However, if we need to generate the data for both [dbo].[Employee] and [dbo].[JobHistory] at the same time, we need to select “Foreign Key (manually assigned) – references a column from the parent table,” referring to the [dbo].[Employee].[EmployeeID] column:

Similarly, we set up the data generation for the following fields.

[CompanyID] – from [dbo].[Company], the “companies” table:

[PositionID] – from the table of positions [dbo].[Position]:

[ProjectID] – from the table of projects [dbo].[Project]:

The tool cannot link the columns from different tables and shift them in some way. However, the generator can shift the date within one table – the “date” generator – fill with date values with Range – Offset from the column. Also, it can use data from a different table, but without any transformation (Table or View, SQL query, Foreign key generators).

That’s why we resolve the dates’ problem (BirthDate

E.g., we limit the BirthDate with the 40-50 years’ interval. Then, we restrict the DocDate with 20-40 years’ interval. The StartDate is, respectively, limited with 25-35 years’ interval, and we set up the FinishDate with the offset from StartDate.

We set up the date of birth:

Set up the date of the document’s issue

Then, the StartDate will match the age from 35 to 45:

The simple offset generator sets FinishDate:

The result is, a person has worked for three months till the current date.

Also, to configure the date of the working end, we can use a small Python script:

import random
from System import DateTime
  
bd = DateTime.Parse(str(StartDate))

releaseDate = bd.AddDays(random.randint(1, 30))
 
releaseDate

This way, we receive the below configuration for the dates of work end [FinishDate] data generation:

Similarly, we fill in the rest of fields. We set the generator type – string, and set the range for generated lines’ lengths:

Also, you can save the data generation project as dgen-file consisting of:

  1. Connection;
  2. The database name;
  3. All settings for bases, tables, and columns;
  4. All settings of generators by columns, etc.

We can save all these settings:it is enough to keep the project’s file and work with the database further, using that file:

There is also the possibility to both save the new generators from scratch and save the custom settings in a new generator:

Thus, we’ve configured the synthetic data generation settings used for the jobs’ history table [dbo].[JobHistory].

निष्कर्ष

We have examined two approaches to filling the data in the database for testing and development:

  1. Copying and changing the data from the production database
  2. Synthetic data generation

We’ve defied the objects for each approach and each script implementation. These objects are here. We’ve also provided scripts for changing the data from the production database and synthetic data generation. An example is the database of recruitment services. In the end, we’ve examined popular data generation tools and explored one of these tools in detail.

References

SQL SERVER – How to Disable and Enable All Constraint for Table and Database
Microsoft TechNet Wiki
Top 10 Best Test Data Generation Tools In 2020
SQL Server Documentation


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. एक रियल एस्टेट एजेंसी डेटा मॉडल

  2. Windows Azure VM प्रदर्शन की तुलना करना, भाग 2

  3. Azure SQL डेटाबेस प्रदर्शन ट्यूनिंग विकल्प

  4. एसक्यूएल चयन और ऑपरेटर

  5. शीर्ष प्रतीक्षा आँकड़ों के बारे में क्या करें (या न करें)