Top Banner
Relation ALEX KHANG Design Rules and SQL Coding Conventions Guidelines for anybody who are learning, teaching and working in Software Engineering Professional Handbook Relational Database Database Design Rules and Regulations SQL Coding Standards and Conventions Best Practice SQL-Based Programming SQL Server . Oracle . SAP Anywhere . MySQL edX Books 4.0 Express
17

Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Jul 12, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Relation

ALEX KHANG

Design Rules and SQL Coding Conventions

Guidelines for anybody who are

learning, teaching and working in Software Engineering

Professional

Handbook

Relational Database

Database Design Rules and Regulations

SQL Coding Standards and Conventions

Best Practice SQL-Based Programming

SQL Server . Oracle . SAP Anywhere . MySQL

edX Books 4.0 Express

Page 2: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

About the Author

Alex Khang has hold potition of CEO,

Director of Software Engineering, Data

Engineering and Senior Software Product

Manager and led many software projects

using .NET, Java, Android, SQL Server,

Oracle, DB2, SAP Anywhere, Data

Warehouse, BigData, Data Science, AI/ML

Engineering, Data Migration, SharePoint

Server, Workflow, and Cloud for

companies of German, Singapore, UK,

America and Sweden in past years.

Alex Khang has over 25 years of working and more than 15 years of teaching the

coursework of Software Engineering, Data Engineering at Universities, Colleges,

Institues, Software Park Centers and ICT Agencies.

He is also an author of

48 best-seller books of specialized Software Engineering by Vietnamese since

2001 to 2012 at domestic market.

4 books of specialized Software Engineering by Vietnamese in the University.

Some books of specialized Software Engineering and Data Engineering by

English on the Global Market.

Moreover, Alex Khang has advised and consulted on the Software Engineering,

Software Solutions, Data Warehouse and Data Engineering for many foreign

companies, state companies and e-government projects.

Alex Khang is currently a Data Scientist, AI/ML Engineer, Database Expert,

Editor and Reviewer, and Senior Software Solution Architect and also holds the

role of "Senior Training Manager" at Global Software Development Company.

Page 3: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Acknowledgements

Build practical examples for writing this book has been an incredible effort and

marked the return of my writing work.

First of all, I would like to thank my daughter and son for encouraging me to write

this book.

Second, I would like to thank any body, who are developers, testers, business

analyst, database designer, trainers, software engineers who working in Software

Engineering or students, internship as well as teachers, instructors who are learning

and teaching in the faculty of Computer Science at University, so they always

putting up me the question is “Why you are famous author in past and working in

Software Engineering but you are not continuing to contribute your knowledge

and experiences to young people”.

Third, I would like to thank readers and students whom are used to read my

published books multiple times in past and give useful feedback.

Fourth, I would like to say thanks to my colleagues, for motivating me on going

back to write books and supporting me to join online market.

Finally, it is fantastic to serving practical books to all of you, and hopefully I can

inspire to you in next books.

Page 4: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Next Books

Software Engineering books will be published on next time:

1. Apply the modern Model of Application Lifecycle Management (ALM) to

Software Production and Services.

2. Mastering in Design and Implementation of Competency Modern Model

in Software Engineering Organization.

3. Mastering Oracle Database - Practical Architect Design and PL/SQL

Programming.

4. Mastering SQL Server Database - Practical Architect Design and T-SQL

Programming.

5. Mastering SAP Anywhere Database - Practical Architect Design and T-

SQL Programming.

6. Data Science – The Journey to become a Data Scientist in an Era of

Artificial Intelligence.

Page 5: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Whom This Handbook Is For?

Today, there are many books usually introduce knowledge and experience of

design and programming in specific relational database platform such as

Microsoft SQL Server database

Oracle database

MySQL database

PostgreSQL database

SAP Anywhere database

IBM DB2 database

But the most regulations and conventions in design rules, guidelines of SQL

coding standards as well as the most valuable in development of world-class

software products that you need to buy is an English book called "Relational

Database - Design Rules and SQL Coding Conventions".

This book teaches you the designing and SQL coding of relational database in

great details and professional but its written standards and conventions as well as

popular techniques; so the teachers, students, internship, developers, engineers

and even seniors can follow along easily.

If you want to get an in-depth practical designing and professional coding of

relational database platform, then this book is surly excellent choice for you.

You can find many books authored for sale on Amazon, pelase visit

http://amazon.com/author/mr.alexkhang and explore more of books.

Page 6: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Why You Should Read This Handbook

Guidelines of using the Design Rules and SQL Coding Conventions for

Relational Databases, will help you more professional in moving up and working

in the Global Software Engineering organization.

Don't wait any longer. Let read this Handbook as soon as possible.

You can enhance SQL and SQL-based programming and database design

skills, you also will get many esteemed programming styles and experiences

which can quick help you to design and manage database applications for

any business solution.

This Handbook will definitely help you to improve SQL knowledge,

coding techniques, programming skills, coding standards and convetions,

and tips to get you started on developing the modern database system.

It provides practice according to the syntax of the enclosed examples, you

always have been holding the following sentence in your mind: "Now, I

am Database Architect Design and Database Developer, and I am very

proud about myself."

Page 7: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Things You Will Learn

This first edition includes 06 sections,

The first section, PART 01-Design Rules and Regulations for popular

Relational Database Platform.

Next 04 sections of Relational Database - Design Rules and SQL

Coding Conventions are presented in PART 02 - PART 05

o T-SQL for Microsoft SQL Server database

o PL/SQL for Oracle database

o MSQL for MySQL database

o T-SQL for SAP Anywhere database

The last section, PART 6-Best Practices Programming for SQL and

SQL-Based languages.

Moreover, this edition includes examples with the latest data types that covers data

input of XML and JSON string for delivering the way to send and get back XML

and JSON via Web API or Front-End code.

I‘d Like to Hear from You

I have always open the (cloud) door to any readers who purchase books that I have

written on. If you run into problems, please e-mailing me at

[email protected].

In case I might not respond as quickly as I have busy on projects I will try to

feedback within 72 hours of receiving your email.

Page 8: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Table of Contents

About the Author ............................................................................................................ 2

Acknowledgements .......................................................................................................... 3

Next Books ....................................................................................................................... 4

Whom This Handbook Is For? ..................................................................................... 5

Why You Should Read This Handbook ....................................................................... 6

Things You Will Learn .................................................................................................... 7

I‘d Like to Hear from You ............................................................................................. 7

Table of Contents ............................................................................................................ 8

Introduction ....................................................................................................................16

PART 1 DESIGN RULES AND REGULATIONS OF RELATIONAL

DATABASE ...................................................................................................................18

1. Primary Objectives ................................................................................................19

2. Assumptions and Limitations ..............................................................................19

3. Development Teams and Parties ........................................................................20

4. Definitions of Design Actions.............................................................................20

5. Design Rules of Database Schema .....................................................................20

Design Rules of User-Defined Database ...........................................................20

Design Rules of Table ...........................................................................................21

Design Rules of Column ......................................................................................21

Design Rules of History Database ......................................................................21

Rules of Constraint between Two Tables ..........................................................22

6. Types of Data Storing in Table ...........................................................................22

Types of Character and String .............................................................................22

Types of Date, Time and Date Time ..................................................................22

Regulations of Coordinated Universal Time .....................................................22

7. Rules of Enterprise Database Design.................................................................24

Page 9: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Branch Code (optional) ........................................................................................25

Tenant Id (optional) ..............................................................................................25

Fiscal Year (optional) ............................................................................................26

Process Data (required) ........................................................................................26

Period of Application Data ..................................................................................29

ConcurrentControls and Management ...............................................................30

8. Regulations of Managing Transaction Data ......................................................31

Unique number (Optional)...................................................................................31

Identity or Auto Number number ......................................................................32

Regulation for getting an Auto number (Optional) ..........................................34

Regulations of Soft Delete ...................................................................................35

Rules of Handling the Action Queries ...............................................................35

Regulations of UTC Default for INSERT ........................................................37

Regulations of UTC Default for UPDATE ......................................................38

9. Regulations of linked Databases .........................................................................38

Microsoft SQL Server ...........................................................................................39

SAP Anywhere .......................................................................................................39

10. History of Changes ...............................................................................................40

11. Problems or Issues ................................................................................................40

12. Authority and Responsibility Sign-off ................................................................40

PART 2: SQL SERVER DATABASE T-SQL CODING STANDARDS AND

CONVENTIONS .........................................................................................................41

1. Primary Objectives ................................................................................................42

2. Assumptions and Limitations ..............................................................................42

3. Development Teams and Parties ........................................................................42

4. Definitions of Actions ..........................................................................................43

5. Definitions, Acronyms and Abbreviations ........................................................43

6. General Conventions ............................................................................................43

7. Commenting Conventions ...................................................................................44

8. TAB Conventions .................................................................................................46

Page 10: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

9. T-SQL Programming Language Guidelines ......................................................48

SELECT Command ..............................................................................................48

SQL Server Functions ...........................................................................................50

TOP Number .........................................................................................................50

CASE Operator .....................................................................................................52

SORT Clause ..........................................................................................................53

10. Rules of Auto Number .........................................................................................53

IDENTITY Global Variable ...............................................................................53

NewID () Function ...............................................................................................54

11. Naming Conventions ............................................................................................54

General Rules and Regulations ............................................................................54

Conventions of SQL Variables ............................................................................56

Conventions of SQL Parameters ........................................................................58

Conventions of Table Name ................................................................................61

Conventions of Column Name ...........................................................................62

Conventions of View Name ................................................................................63

Conventions of Stored Procedure Name ...........................................................64

Conventions of Function Name .........................................................................65

Conventions of Trigger Name .............................................................................66

12. Regulations of Exception Handling and Throwing .........................................70

13. Regulations of Concurrent Handling .................................................................73

14. Regulations of Deleting Data ..............................................................................75

Regulations of Soft Delete Mode ........................................................................75

Regulations of Hard Delete Mode ......................................................................76

15. Rules of Dynamic Query ......................................................................................78

16. Regulations of Bulk Data Query .........................................................................82

Regulations of XML Data Query ........................................................................82

Example of INPUT XML Data ..........................................................................84

Regulations of JSON Data Query .......................................................................85

Page 11: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Example of INPUT JSON Data .........................................................................86

17. History of Changes ...............................................................................................88

18. Known Issues.........................................................................................................88

19. Authority & Responsibility Sign-off ...................................................................88

PART 3: ORACLE DATABASE PL/SQL CODING STANDARDS AND

CONVENTIONS .........................................................................................................89

1. Primary Objectives ................................................................................................90

2. Assumptions and Limitations ..............................................................................90

3. Development Teams and Parties ........................................................................90

4. Definitions, Acronyms and Abbreviations ........................................................91

5. Definitions of Actions ..........................................................................................91

6. Standards and Conventions .................................................................................91

General Definitions ...............................................................................................91

Commenting Conventions ...................................................................................92

TAB Conventions ..................................................................................................95

7. PL/SQL Programming Language Guidelines ...................................................97

SELECT Command ..............................................................................................97

SQL Functions .......................................................................................................98

TOP Number of Records (Oracle 12c and later) .............................................99

CASE Operator .................................................................................................. 101

8. Naming Conventions ......................................................................................... 102

Conventions of SQL Variables ......................................................................... 102

Conventions of SQL Parameter ....................................................................... 103

Conventions of View Name ............................................................................. 104

Conventions of Stored Procedure Name ........................................................ 105

Conventions of Function Name ...................................................................... 107

Conventions of Trigger Name .......................................................................... 108

9. Rules of Auto Number ...................................................................................... 109

IDENTITY Global Variable ............................................................................ 109

Policies of Unique Identifier ............................................................................. 111

Page 12: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

10. Regulations of Deleting Data ........................................................................... 111

Regulations of Soft Delete Mode ..................................................................... 111

Regulations of Hard Delete Mode ................................................................... 113

11. Special Regulations ............................................................................................. 114

Regulations of Dynamic Query ........................................................................ 114

Regulations of Exception Handling and Throwing ....................................... 115

Regulations of Concurrent Handling ............................................................... 116

12. Regulations of Bulk Data Query ...................................................................... 118

Regulations of Xml Data Query ....................................................................... 118

Regulations of JSON Data Query .................................................................... 124

13. Record of Changes ............................................................................................. 131

14. Known Issues...................................................................................................... 131

15. Authority & Responsibility Sign-off ................................................................ 131

PART 4: MySQL DATABASE MSQL CODING STANDARDS AND

CONVENTIONS ...................................................................................................... 132

1. Primary Objectives ............................................................................................. 133

Assumptions and Limitations ........................................................................... 133

Development Teams and Parties ..................................................................... 134

Definitions, Acronyms and Abbreviations ..................................................... 134

2. Standards and Conventions .............................................................................. 134

Definitions of Actions ....................................................................................... 134

General Definitions ............................................................................................ 135

Commenting Conventions ................................................................................ 136

TAB Conventions ............................................................................................... 138

3. MSQL Programming Language Guidelines ................................................... 140

SELECT Command ........................................................................................... 140

SQL Functions .................................................................................................... 142

TOP Number of Records (MySQL 8.x and later) ......................................... 143

CASE Operator .................................................................................................. 144

4. Naming Conventions ......................................................................................... 145

Page 13: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Convention of SQL Variables .......................................................................... 145

Convention of SQL Parameter ......................................................................... 147

Convention of View Name ............................................................................... 148

Convention of Stored Procedure Name ......................................................... 149

Convention of Function Name ........................................................................ 152

Convention of Trigger Name ........................................................................... 153

5. Rules of Auto Number ...................................................................................... 154

AUTO NUMBER number ............................................................................... 154

Unique Identifier Policies .................................................................................. 156

6. Special Regulations ............................................................................................. 156

Regulations of Dynamic Query ........................................................................ 156

Regulations of Exception Handling and Throwing ....................................... 158

Regulations of Concurrent Handling ............................................................... 159

7. Regulations of Deleting Data ........................................................................... 161

Regulations of Soft Delete Mode ..................................................................... 161

Regulations of Hard Delete Mode ................................................................... 162

8. History of Changes ............................................................................................ 164

9. Known issues ...................................................................................................... 164

10. Authority & Responsibility sign-off ................................................................ 164

PART 5 SAP ANYWHERE DATABASE T-SQL CODING STANDARDS

AND CONVENTIONS ........................................................................................... 165

1. Primary Objectives ............................................................................................. 166

2. Assumptions and Limitations ........................................................................... 166

3. Development Teams and Parties ..................................................................... 166

4. Standards and Conventions .............................................................................. 167

Definitions of Actions ....................................................................................... 167

Definitions, Acronyms and Abbreviations ..................................................... 167

General Conventions ......................................................................................... 167

Commenting Conventions ................................................................................ 168

TAB Conventions ............................................................................................... 170

Page 14: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

5. SAP T-SQL Programming Language Guidelines .......................................... 172

SELECT Command ........................................................................................... 172

SQL Functions .................................................................................................... 174

TOP Number ...................................................................................................... 174

CASE Operator .................................................................................................. 175

SORT Clause ....................................................................................................... 177

IDENTITY Global Variable ............................................................................ 177

6. Naming Conventions ......................................................................................... 178

General Definitions ............................................................................................ 178

Conventions of SQL Variables ......................................................................... 179

Conventions of Table Name ............................................................................. 181

Conventions of SQL Parameters ..................................................................... 182

Conventions of Column Name ........................................................................ 186

Conventions of View Name ............................................................................. 187

Conventions of Stored Procedure Name ........................................................ 187

Conventions of Function Name ...................................................................... 189

Conventions of Trigger Name .......................................................................... 190

7. Regulations of Exception Handling and Throwing ...................................... 193

8. Regulations of Concurrent Handling .............................................................. 196

9. Regulations of Deleting Data ........................................................................... 198

Regulations of Soft Delete Mode ..................................................................... 198

Regulations of Hard Delete............................................................................... 200

10. Rule of Auto Number ........................................................................................ 201

NewID () Function ............................................................................................ 201

11. Regulations of Dynamic Query ........................................................................ 201

12. Watcom-SQL Syntax ......................................................................................... 206

13. Regulations of Bulk Data Query ...................................................................... 206

Regulations of XML Data Query ..................................................................... 206

Example of INPUT XML Data ....................................................................... 208

Page 15: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Regulations of JSON Data Query .................................................................... 209

Example of INPUT JSON Data ...................................................................... 211

14. History of Changes ............................................................................................ 212

15. Known Issues...................................................................................................... 212

16. Authority & Responsibility Sign-off ................................................................ 212

PART 6 BEST PRACTICE PROGRAMMING WITH SQL AND SQL-

BASED COMMANDS ............................................................................................. 213

1. Introduction ........................................................................................................ 214

2. Coding Conventions .......................................................................................... 214

3. Out of Scope ....................................................................................................... 214

4. Definitions of Actions ....................................................................................... 214

5. SQL-Based Commands ..................................................................................... 215

6. DO PREFER ...................................................................................................... 215

7. AVOID ................................................................................................................ 229

References of Material ................................................................................................ 236

Page 16: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Introduction

Welcome to Handbook of “Relational Database - Design Rules and SQL

Coding Conventions”!

Handbook is a set of major guidelines of the Design Rules and SQL Coding

Conventions, and continues to improve new conventions and modern techniques

in the other relational databases.

PART 1 is a solid introduction of popular Design Rules and Regulations for

Relational Database Platform, which includes

Design rules that supports designing the structure of Application and

History Databases.

Design rules for database objects such as tables, columns, constraints.

Types of Data Storing in Table and Regulations of Coordinated Universal

Time.

Design rules of Enterprise Database Design and Regulations of Managing

Transaction Data.

Design rules of Regulations of linked Databases such as COMMON

databases, MASTER databases and Transaction databases, and more;

PART 2-5 is coding standards and conventions along with specific examples for

guiding how to apply

T-SQL coding conventions for SQL Server database.

PL/SQL coding conventions for Oracle database.

MSQL coding conventions for MySQL database.

T-SQL coding conventions for SAP Anywhere database.

Which includes

Guidelines and Naming Conventions of SQL-Based Programming

Language.

Page 17: Professional Handbook Relational Database Design Rules and ...edxbooks.com/pdfs/RelationalDatabase-DesignRules-CodingConventi… · Mastering Oracle Database - Practical Architect

Database Design Rules and SQL Coding Conventions Professional Handbook

Prof. Software Engineering, Database Expert, Data Scientist, AI/ML Engineer

Regulations of Exception Throwing and ConcurrentHandling.

Rules of Deleting Data and Dynamic Query.

Regulations of Soft Delete and Hard Delete Modes.

Rule of XML and JSON Data Query.

PART 6, you are recommended to choose an action of "DO USE”, “PREFER

USE”, “CAN USE” or “DO NOT USE”, “ALLOW USE” or “AVOID USE"

corresponding to the syntax of SQL-Based Commands depend on the specific

context represents the best of the business logic world.

If you are working in Software Development or Software Engineering field and

want to create database system that take full advantages of database architect

design, SQL-Based coding standards and conventions, you need to learn this

Professional Handbook. Because this new edition is fully updated for applying on

popular relational database platform and embedded databases so far.

With this Professional Handbook, the highlight of guidelines is support for

T-SQL of Microsoft SQL Server database.

PL/SQL of Oracle database.

MSQL of MySQL database.

And specifically T-SQL of SAP Anywhere database.

You will be able to set up above database platforms on OS Windows’s machine

then enjoy all the benefits that examples that I have to offer.

If you interest in the further details of each section, please take a look part of

content on the next pages.