Skip to content

PlusMagi's Blog By Pitt Phunsanit

Plus emotional magic to the knowledge of logic.

  • About’s Pitt
Close Button
PlusMagi's Blog By Pitt Phunsanit architecture,automation,Backend,Business Analysis,computer science,Database,Programming สร้าง Data Dictionary แบบด่วน ๆ

สร้าง Data Dictionary แบบด่วน ๆ

2024-09-172024-09-17| phunsanitphunsanit| 0 Comment | 10:00
Categories:
  • architecture
  • automation
  • Backend
  • Business Analysis
  • computer science
  • Database
  • Programming
Update อ่าน สร้าง Data Dictionary แค่คลิก แทนครับ

ต้องเขียน Data Dictionary ให้โครงการที่ทำอยู่ แต่มันมีหลายตารางมาก และที่สำคัญคือ แต่ละตารางจะมี relation ยุบยับเต็มไปหมด copy โครงสร้างมาแต่ละตารางมาวางในเอ็กส์เซล์ที่ละตัวก็ตาลาย พลาดได้ง่าย ๆ เลย

วิธีที่คิดออกคือ มันต้องมีโปรแกรมที่ช่วยงานนี้ได้ซิ เราไม่ใช่คนแรกที่ต้องเขียน Data Dictionary ซะหน่อย มีจริง ๆ แต่แพง ไม่ก็ดูแปลก ๆ เปลี่ยนไปใช้อีกวิธีคือ ทำไมไม่ให้ SQL ทำให้ละ search เจอ SQL Server Data Dictionary แต่มันก็ดูยาก เลยต้องเอามาเขียนใหม่ซะเอง

query ที่เขียนขึ้นมาใหม่คือ

SET ANSI_DEFAULTS OFF;
GO
SET NOCOUNT ON;
GO
SET TEXTSIZE 2147483647; GO PRINT &apos;<!doctype html>&apos;
PRINT &apos;<html>&apos;
PRINT &apos;<head>&apos;
PRINT &apos;<meta charset="utf-8">&apos;
PRINT &apos;<title>SQL Server Data Dictionary by Pitt Phunsanit</title>&apos;
PRINT &apos;<style> body { font-family: "Calibri", "Segoe UI", sans-serif; padding: 30px; line-height: 1.6; background-color: #f5f7fa; } h1 { color: #2f5496; border-bottom: 2px solid #2f5496; padding-bottom: 10px; } h3 { color: #555; } table { background: #fff; border-collapse: collapse; width: 100%; margin-bottom: 40px; box-shadow: 0 2px 5px rgba (0,0,0,0.1) ; page-break-inside: avoid; } table caption { text-align: left; padding: 15px; font-size: 16pt; color: #2f5496; font-weight: bold; background: #e9eff7; border: 1px solid #7ba0cd; border-bottom: none; } table thead tr { background-color: #4e80bc; color: white; text-align: center; } table th, table td { border: 1px solid #7ba0cd; padding: 10px; font-size: 10pt; } table tbody tr:nth-of-type (odd) { background: #f2f6fb; } table tbody tr:hover { background: #e2ebf5; } .pk { color: #d9534f; font-weight: bold; text-align: center; } .fk { color: #5bc0de; font-size: 9pt; } .footer { font-size: 10pt; color: #777; margin-top: 50px; border-top: 1px solid #ccc; padding-top: 20px; text-align: center; } a { color: #4e80bc; text-decoration: none; } a:hover { text-decoration: underline; }
</style>&apos;
PRINT &apos;</head>&apos;
PRINT &apos;<body>&apos;
PRINT &apos;<h1>Database: &apos; + DB_NAME () + &apos;</h1>&apos;
PRINT &apos;<h3>Generated Date: &apos; + CONVERT (VARCHAR (10) , GETDATE () , 111) + &apos;</h3>&apos; SELECT html + html1 + html2 AS [GeneratedHTML]
FROM (-- 1. Table Header & Caption (คง REPLACE ไว้เพราะ Description ตารางอาจมีอักขระพิเศษ) SELECT s.[name] AS [schema], t.[name] AS [table], 0 AS column_id, CAST (&apos;<table><caption>Table: &apos; + s.[name] + &apos;.&apos; + t.[name] + ISNULL (&apos; <small style="font-weight:normal; color:#666;"> (&apos; + REPLACE (REPLACE (CAST (ep.[value] AS NVARCHAR (MAX)) , &apos;<&apos;, &apos;&lt;&apos;) , &apos;>&apos;, &apos;&gt;&apos;) + &apos;) </small>&apos;, &apos;&apos;) + &apos;</caption>&apos; AS NVARCHAR (MAX)) AS html, CAST (&apos;<thead><tr><th style="width:40px;">PK</th><th style="width:160px;">FK (Referenced Table) </th><th style="width:220px;">Column Name</th><th>Description</th>&apos; AS NVARCHAR (MAX)) AS html1, CAST (&apos;<th style="width:140px;">Data Type</th><th style="width:50px;">Null</th><th style="width:50px;">ID</th><th style="width:180px;">Default Value</th></tr></thead><tbody>&apos; AS NVARCHAR (MAX)) AS html2 FROM sys.tables t INNER JOIN sys.schemas s ON s.[schema_id] = t.[schema_id] LEFT OUTER JOIN sys.extended_properties ep ON ep.major_id = t.[object_id] AND ep.minor_id = 0 AND ep.name = &apos;MS_Description&apos; WHERE t.is_ms_shipped = 0 UNION ALL -- 2. Table Rows (Columns) SELECT s.[name], t.[name], c.column_id, &apos;&apos;, &apos;<tr><td class="pk">&apos; + CASE WHEN pk.column_id IS NOT NULL THEN &apos;PK&apos; ELSE &apos;&apos; END + &apos;</td>&apos; + &apos;<td class="fk">&apos; + ISNULL (fk.primary_table + &apos;.&apos; + fk.primary_column, &apos;&apos;) + &apos;</td>&apos; + &apos;<td><b>&apos; + c.[name] + &apos;</b></td>&apos; + &apos;<td>&apos; + ISNULL (REPLACE (REPLACE (CAST (ep.[value] AS NVARCHAR (MAX)) , &apos;<&apos;, &apos;&lt;&apos;) , &apos;>&apos;, &apos;&gt;&apos;) , &apos;&apos;) + &apos;</td>&apos; AS html1, &apos;<td>&apos; + sty.[name] + CASE WHEN sty.[name] IN (&apos;char&apos;, &apos;nchar&apos;, &apos;varchar&apos;, &apos;nvarchar&apos;, &apos;binary&apos;, &apos;varbinary&apos;) THEN &apos; (&apos; + CASE WHEN c.max_length = -1 THEN &apos;max&apos; ELSE CAST (CASE WHEN sty.[name] IN (&apos;nchar&apos;, &apos;nvarchar&apos;) THEN c.max_length/2 ELSE c.max_length END AS VARCHAR (10)) END + &apos;) &apos; WHEN sty.[name] IN (&apos;numeric&apos;, &apos;decimal&apos;) THEN &apos; (&apos; + CAST (c.precision AS VARCHAR (5)) + &apos;,&apos; + CAST (c.scale AS VARCHAR (5)) + &apos;) &apos; ELSE &apos;&apos; END + &apos;</td>&apos; + &apos;<td style="text-align:center;">&apos; + CASE WHEN c.is_nullable = 1 THEN &apos;Y&apos; ELSE &apos;&apos; END + &apos;</td>&apos; + &apos;<td style="text-align:center;">&apos; + CASE WHEN c.is_identity = 1 THEN &apos;Y&apos; ELSE &apos;&apos; END + &apos;</td>&apos; + &apos;<td><small>&apos; + ISNULL (dc.[definition], &apos;&apos;) + &apos;</small></td></tr>&apos; AS html2 -- เอา REPLACE ออกที่นี่ FROM sys.columns c INNER JOIN sys.tables t ON t.[object_id] = c.[object_id] INNER JOIN sys.schemas s ON s.[schema_id] = t.[schema_id] LEFT OUTER JOIN sys.types sty ON sty.user_type_id = c.user_type_id LEFT OUTER JOIN sys.extended_properties ep ON ep.major_id = t.[object_id] AND ep.minor_id = c.column_id AND ep.name = &apos;MS_Description&apos; LEFT OUTER JOIN sys.default_constraints dc ON dc.parent_object_id = t.[object_id] AND dc.parent_column_id = c.column_id LEFT OUTER JOIN (SELECT ic.column_id, i.[object_id] FROM sys.indexes i INNER JOIN sys.index_columns ic ON ic.index_id = i.index_id AND ic.[object_id] = i.[object_id] WHERE i.is_primary_key = 1) pk ON pk.column_id = c.column_id AND pk.[object_id] = t.[object_id] LEFT OUTER JOIN (SELECT fkc.parent_object_id, fkc.parent_column_id, OBJECT_SCHEMA_NAME (fk.referenced_object_id) AS primary_schema, OBJECT_NAME (fk.referenced_object_id) AS primary_table, COL_NAME (fkc.referenced_object_id, fkc.referenced_column_id) AS primary_column FROM sys.foreign_keys fk INNER JOIN sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.[object_id]) fk ON fk.parent_object_id = t.[object_id] AND fk.parent_column_id = c.column_id WHERE t.is_ms_shipped = 0 UNION ALL -- 3. Table Footer SELECT s.[name], t.[name], 999999, &apos;&apos;, &apos;&apos;, &apos;</tbody></table>&apos; FROM sys.tables t INNER JOIN sys.schemas s ON s.[schema_id] = t.[schema_id] WHERE t.is_ms_shipped = 0) tmp
ORDER BY tmp.[schema], tmp.[table], tmp.column_id; PRINT &apos;<div class="footer">&apos;
PRINT &apos;Developed by <a href="https://pitt.plusmagi.com/" target="_blank">PlusMagi.com by Pitt Phunsanit</a><br>&apos;
PRINT &apos;Contact: [email protected] | Source: SQL Server Metadata&apos;
PRINT &apos;</div>&apos;
PRINT &apos;</body></html>&apos;

query ตัวนี้มีอะไรพิเศษมากกว่าตัวอื่น ๆ หลายอย่าง คือ

  1. การเอาไปใช้ แทนที่จะดูผลลัพธ์ในรูปแบบ grid ต้องใช้มันในแบบ Results to Text โดยกด (Ctrl+T) แล้วเอาผลที่ได้ (เป็น code html) ให้เอาไป copy เป็นไฟล์ .html แล้วจะ copy ไปลง excel / word อีกต่อหนึ่งก็ได้ ถ้าใช้ grid ก็ได้ code เหมือนกันแต่จะไม่สวย เพราะพวกคำสั่ง print มันจะไม่ทำงาน
  2. ผลลัพท์ต้องแบ่งเป็น column html, html1, html2 เพราะว่าถ้าผลลัพธ์ที่ออกมากมันยาว จะโดนตัดทิ้งไปเฉย ๆ พยามแก้อยู่นาน นานกว่าเขียน code ของมันซะอีก โดยลองใช้ SET TEXTSIZE 8192; และ cast ร่วมกับตัวเลือก Maximum number of characters display in each column ก็ไม่ได้ผล เลยต้องซอย column ออกมาแทน
  3. การเอา code ที่เอาไปใช้ ถ้ามีการจัด code ใหม่ต้องระวังเงื่อนไข อย่าง ‘CHAR’ ถ้าโดนจัดเป็น ‘ CHAR ‘ (มี space) มันจะไม่ทำงาน
  4. การใช้งานถ้าจะให้ข้อมูลออกมาครบต้องทำ table relation และใส่ descriptions ให้ครบ ถึงจะออกมาสวย ๆ ครบ ๆ เหมือนกัน
  5. sub query ด้านล่าง ๆ จริง ๆ คิดว่าถ้าให้มีประสิทธิภาพน่าจะเขียนวิธีอื่น ๆ ได้ แต่ตอนนี้ขอใช้ quick and easy (dirty) ไว้ก่อน
  6. ยังมีจุดที่จะแก้ในครั้งต่อไปอย่าง มันยังมี result header อยู่ทำให้มีเส้น ———————- กับ html html1 html2 เกินมาอยู่ แต่ลบเองไม่กี่วิก็ใช้ได้ละ เพราะงั้นปล่อย ๆ มันไปก่อน
  7. ใครแก้จุดไหนได้ รบกวนบอกผมด้วยละกันครับ

ตัวอย่างผลลัพธ์ที่ได้


DATABASE NAME


create date : 2016/05/30

areas_cities : อำเภอPrimary KeyForeign keyColumn NameDescriptionData TypeAllow NullsIdentityDefault Value

PK city_id int   
  status tinyintY  
  geo_idภูมิภาคint   (‘0’)
  province_id int   (‘0’)
  amphur_code varchar (4) Y  

areas_cities_lang : อำเภอ (แปล) Primary KeyForeign keyColumn NameDescriptionData TypeAllow NullsIdentityDefault Value

PKareas_cities.city_idcity_id int   
PK language char (2)    
  name nvarchar (600)    

จริง ๆ มันมีสีสันด้วยนะ สวยจนเอาไปใช้ได้เลยไม่ต้องแต่งอะไรอีก


อ่านเพิ่มเติม

  • HTML: สลับตารางแนวนอนและตารางแนวตั้ง
  • ตรวจฟอร์มด้วย HTML 5 / jQuery Validation Plugin
  • Docker: SQL Server Edition
Tags: Automation, Data dictionary, Data Modeling, Database, database schema, Metadata, Schema, Software Development, SQL, Table, ฐานข้อมูล, ตาราง, ระบบ, สาระความรู้

แนะแนวเรื่อง

PREVIOUS Previous post: ผลกระทบทางกฎหมายหากเจ้าของร่วมโต้แย้งว่าไม่ได้รับหนังสือเชิญประชุมตามขั้นตอนที่กฎหมายกำหนด
NEXT Next post: สถานะทางกฎหมายของถนนภายในโครงการ (หมู่บ้านจัดสรรหรือคอนโดมิเนียมที่เป็นพื้นที่ส่วนบุคคลและมีการควบคุมการเข้า-ออก): ไม่อยู่ภายใต้การบังคับโดยตรงของพระราชบัญญัติจราจรทางบก พ.ศ. 2522 (เจ้าหน้าที่ตำรวจจราจรไม่สามารถเข้ามาจับปรับตามกฎหมายจราจรได้ทันที เว้นแต่จะเป็นถนนที่ยกให้เป็นทางสาธารณะ)

Projects

  • Statement Columns Mapping Helper
  • PlusMagi Blocks
  • PlusMagi Site Search
  • PlusMagi Tags Reindex
  • jQuery Plus Repeater

Recent Posts

  • อธิบายการใช้ Range Types และ GiST Index ใน PostgreSQL เพื่อแก้ปัญหา Range Query สองคอลัมน์
  • เทียบ Database GUI (DBeaver, DBX, DataGrip, Navicat)
  • ทำความรู้จักกับ dbx (getdbx.com): Database Client ยุคใหม่ที่ออกแบบมาเพื่อ Developer
  • Vorsan: แอปสารพัดประโยชน์ที่คนใช้ Mac ควรมีติดเครื่อง
  • Apple แต่งตั้ง จอห์น เทอร์นัส (John Ternus) ขึ้นเป็น CEO โดยไม่ใช่ โจนี ไอฟ์ (Jony Ive)

Archives

Categories

  • .net core (44)
  • Accessibility (5)
  • Accounting (61)
  • Action Genre (1)
  • agriculture (2)
  • applied mathematics (62)
  • Archaeology (3)
  • art (93)
  • Artificial Intelligence (87)
  • Astronomy (3)
  • Automotive (9)
  • Automotive History (4)
  • Bioengineering (5)
  • Botany (1)
  • business (1,176)
    • Business Analysis (517)
    • Finance (42)
    • Real Estate (27)
  • cd (139)
  • chemistry (12)
  • ci (142)
  • Civil Engineering (26)
  • CMS (48)
  • collaboration (120)
  • Comics (1)
  • Command Line Interface (26)
  • Communication (157)
  • Communication Studies (1)
  • Community Governance (1)
  • Community Management (1)
  • compliance (1)
  • Computer Engineering (4)
  • Computer Hardware (63)
  • computer science (2,085)
    • AI (395)
    • Biology (114)
      • Environment (34)
    • Cloud Computing (460)
    • Data Visualization (65)
    • Mathematics (46)
  • construction (1)
  • Consumer Electronics (1)
  • container (63)
  • Content Creation (1)
  • Corporate Governance (3)
  • Corporate Law (1)
  • Cosmology (2)
  • Culture (151)
  • data engineering (5)
  • data management (5)
  • Data Modeling (1)
  • Data Privacy (80)
  • data science (7)
  • Data Transformation (171)
  • Database Management (6)
  • Database Systems (3)
  • Design (333)
    • UX/UI (45)
  • development (1)
  • devops (19)
  • DevOps Engineering (1)
  • Digital Electronics (1)
  • digital marketing (2)
  • digital technology (1)
  • disaster management (1)
  • e-learning (1)
  • Earth Science (1)
  • Ecology (1)
  • Education (4)
  • Electrical Engineering (4)
  • Electronics (4)
  • Electronics Engineering (3)
  • Embedded Systems (1)
  • engineering (1,154)
    • architecture (1,024)
    • Building Engineering (112)
  • Entertainment (1)
  • Entrepreneurship (1)
  • environmental (1)
  • Environmental Science (3)
  • Environmentalism (1)
  • Facility Management (1)
  • Fantasy Literature (1)
  • Film (2)
  • film studies (2)
  • Forensic Science (1)
  • front-end development (2)
  • Frontend Development (3)
  • Geographical Information System (1)
  • Geology (1)
  • Governance (6)
  • Grammar (1)
  • graph theory (1)
  • Hardware (5)
  • Health (159)
    • Safety (127)
  • health and safety (1)
  • health and wellness (1)
  • Health Science (2)
  • Home Appliances (1)
  • Human Resources (1)
  • Human-Computer Interaction (1)
  • Humanities (506)
    • Academia (288)
    • history (113)
    • Linguistics (16)
    • Literature (98)
  • Information Architecture (1)
  • Information Management (1)
  • information retrieval (2)
  • Information Science (14)
  • information security (1)
  • Information Technology (3)
  • Insurance (1)
  • International Relations (1)
  • Internet of Things (2)
  • IT Operations (1)
  • landscape architecture (1)
  • Language Arts (1)
  • Law (302)
  • Law and Regulation (1)
  • Legal (1)
  • Legal Advice (1)
  • legal affairs (1)
  • Legal Compliance (3)
  • Legal Framework (1)
  • legal studies (2)
  • legal_affairs (1)
  • library science (2)
  • Life (1,586)
    • Cartoon (41)
    • D.I.Y (62)
    • Mindset (311)
    • Movies (58)
    • Philosophy (229)
    • Psychology (1,195)
      • Behavioral Science (589)
      • Cognitive Science (501)
    • Sci-Fi (65)
    • Tips and Tricks (32)
    • พุทธ (7)
  • Lifestyle (1)
  • logic (1)
  • logistics (1)
  • Machine Learning (2)
  • management (890)
    • knowledge management (295)
      • Documentation (98)
    • productivity (5)
    • Project Management (16)
    • strategy (4)
  • manufacturing technology (1)
  • Marketing (1)
  • Markup Languages (1)
  • Mechanical Engineering (3)
  • Media (1)
  • media studies (3)
  • medicine (1)
  • military history (2)
  • Military Technology (1)
  • Mystery (1)
  • Mythology (1)
  • Network (388)
    • IOT (24)
    • Nginx (26)
  • network security (1)
  • networking (56)
  • Neuroscience (1)
  • Object-Relational Mapping (1)
  • operating system (18)
  • Operating Systems (582)
    • Linux (208)
    • macOS (142)
      • Homebrew (25)
    • Shell Script (44)
      • Oh My ZSH (5)
    • Windows (99)
      • PowerShell (26)
  • Operations Management (2)
  • physics (3)
  • Plumbing Systems (1)
  • Pop Culture (1)
  • popular culture (1)
  • Process Improvement (1)
  • Process Management (1)
  • Process Modeling (1)
  • Product Design (1)
  • Programming (2,229)
    • .NET (117)
      • .NET Core EF (9)
      • C# (81)
    • API (535)
      • REST (8)
    • Database (701)
      • MariaDB (38)
      • MySql (69)
      • Oracle Database (24)
      • PostgreSQL (16)
      • RDBMS (55)
      • SQL Server (76)
        • T-SQL (26)
    • Programming Languages (368)
      • Java (95)
      • PHP (216)
      • Python (13)
      • Rust (31)
    • Software Architecture (7)
    • Software Engineering (66)
    • System Analyst (SA) (56)
    • Testing (126)
      • Automated Testing (112)
    • Web (1,537)
      • Apache HTTP Server (38)
      • Backend (1,259)
        • Laravel (54)
        • Spring Boot (15)
      • Frontend (309)
        • Angular (11)
        • CSS (67)
        • JavaScript (188)
        • jQuery (57)
        • Tabulator (24)
        • Tailwind CSS (4)
        • Vue.js (6)
      • WordPress (35)
  • Programming Language (5)
  • Property Law (2)
  • Property Management (3)
  • Public Health (2)
  • Public Policy (1)
  • quality assurance (1)
  • Religion (4)
  • risk management (2)
  • Safety and Security (1)
  • Safety Engineering (3)
  • Science (1)
  • Science Fiction (9)
  • SecDevOps (829)
    • automation (301)
    • CI/CD (137)
    • Docker (89)
    • GIT (58)
    • SVN (6)
    • system (232)
      • System Administration (22)
  • Security (494)
    • Authentication (125)
    • Cryptography (43)
    • Cybersecurity (304)
  • Sensory Technology (1)
  • SEO (1)
  • Server Infrastructure (1)
  • social science (4)
  • Sociology (2)
  • Software Design (1)
  • Software Development (6)
  • software development lifecycle (1)
  • Software Development Practices (1)
  • Software Licensing (3)
  • Software Testing (2)
  • Space Exploration (1)
  • Spirituality (2)
  • sports (1)
  • SQL (1)
  • statistics (2)
  • storage (1)
  • Storage Systems (1)
  • Storytelling (1)
  • structural engineering (1)
  • supply chain (1)
  • Supply Chain Management (1)
  • system architecture (2)
  • system design (6)
  • System Modeling (3)
  • Systems Modeling (1)
  • Systems Programming (2)
  • Technical Support (1)
  • technology (5)
  • Technology History (1)
  • transportation (1)
  • UI (3)
  • Uncategorized (671)
  • urban planning (7)
  • user experience (5)
  • User Experience Design (3)
  • User Interface Design (1)
  • UX Design (3)
  • Version Control (3)
  • Virtualization (1)
  • visualization (1)
  • Web Design (2)
  • web development (67)
  • Web Standards (2)
  • web technology (2)
  • wellness (2)
  • กฎหมาย (3)
  • การขนส่ง (1)
  • การดูแลรถ (1)
  • การตลาด (61)
  • การบำรุงรักษา (1)
  • การพัฒนาอสังหาริมทรัพย์ (1)
  • การเงิน (61)
  • ความปลอดภัยทางไซเบอร์ (30)
  • จราจร (1)
  • ธุรกิจ (174)
  • นวนิยายสืบสวน (1)
  • ผังเมือง (1)
  • วรรณกรรม (1)
  • สถาปัตยกรรม (1)
  • อสังหาริมทรัพย์ (2)
  • เทคโนโลยี (6)
  • เรื่องเล่า (1)

Sirat WordPress Theme By VWThemes

Scroll Up