Mastering SQL VIEWs: Syntax, Use Cases, and Best Practices
VIEWs are an undervalued and underused feature in SQL. They basically consist of a query that has been given a name, and a parameter list, so can be used like an inline macro. Technically, you’re...
View ArticleState Transition Constraints in SQL: Temporal Rules & Validation
State transition constraints enforce valid state changes in a database using DDL rather than application code. The technique uses an auxiliary StateChanges table that defines all legal transitions...
View ArticleData Encoding Schemes
You probably noticed that when you get a bill or bank statement, it uses the current modern Western version of Hindu-Arabic numerals. Yet during the early days of the Renaissance, the accounting firms...
View ArticleData Elements on a Nominal Scale
“On a scale from 1 to 10, what color is your favorite letter of the alphabet?” –Slogan on a T-shirt This is one of my favorite T-shirt gags because it teaches something about data types, scales and...
View ArticleThe CREATE DOMAIN Statement
One of the least known, least used features of SQL is the CREATE DOMAIN statement. It does not exist in SQL Server as of SQL Server 2022, but it has been a part of PostgreSQL since 7.3. Some...
View ArticleScales & Measurements
If you’re going to work with databases, you probably ought to know something about data. In particular, we don’t put data directly into a database; we have to encode it and represent it in a format...
View ArticleTemporary Tables
There is a feature of SQL I have not seen used much and probably with good reason. It’s the use of temporary tables. These are declared with the expected DDL. Since they were relatively easy to add to...
View ArticleVIEW Objects
A view is a virtual table, created when it’s invoked by the < view name>. The goal we had in the standards is to make it behave as much like a base table as possible. Creating a View The syntax...
View ArticleData Control Language (aka Security)
Like ancient Gaul, SQL is divided into three sub- languages. The DDL (Data Declaration Language) declares the data. This is where we find the data types, constraints, references and other structures...
View ArticleTransactions
By now, pretty much everyone has heard the stories about the first computers. They were huge, cost a fortune, required incredible amounts of air conditioning, and perhaps more importantly, ran batch...
View ArticleRounding Numbers
I would hope by now everybody has figured out when you see a price like $39.99 that it might as well be forty bucks. Dropping that penny from the price is done is to fool the consumer! Since we read...
View ArticleUniqueness, Keys, and Identity
If you’ve ever had a traditional logic course, you’ll have run into “The Law of Identity” as the founding principle of all Western thought. It says that: “To be is to be something in particular; to be...
View ArticleBad Data and Dirty Databases
Many years ago, my wife and I wrote an article for Datamation, a major trade publication at the time, under the title, “Don’t Warehouse Dirty Data!” It’s been referenced quite a few times over the...
View ArticleLIKE Constraints
In this article, I will cover a bit about the LIKE operator, including how it works, and a bit of history about why it is like it is. After establishing this, I will discuss a bit about how you can...
View ArticleThe CHECK() Constraint
One of the many ways a relational table differs from the file structures used by pre-relational storage systems is that the tables, rows and columns can have constraints on them. This allows you to...
View ArticleSQL Logic
In this article, I will discuss the history and thinking behind several types of logic that are typically associated with writing relational database code. Because of how relational databases implement...
View ArticleDepartment of Redundancy Department
Ever wonder all the reasons that we use databases instead of file systems? While we don’t think of it too much anymore, the first reason that databases came into existence was to remove redundancies....
View ArticleThe NTILE Function
The SQL NTILE() is a window function that allows you to break a table into a specified number of approximately equal groups, or <bucket count>. For each row in a grouping, the NTILE() function...
View ArticleFirst Normal Form Gets No Respect
Dr. Codd first described the relational model in a paper in Communications of the ACM (CACM 13 No 6; June 1970). Some more work followed up after that by other people, giving us normal forms and other...
View ArticleThe GROUP BY Clause
When you’re learning SQL DML, the most complicated clause is typically the GROUP BY. It’s a fairly simple grouping based on values from the FROM clause in a SELECT statement. It’s what a mathematician...
View Article