How to DROP a Primary Key in SQL Server

Dropping a primary key in any RDBMS is a critical operation that requires careful consideration, especially when there are related foreign keys in other tables.

The decision to drop a primary key is not to be taken lightly.

That said, this article walks through the process of how to drop a primary key in SQL Server, while highlighting important considerations.

Continue reading

Fix Error “No column name was specified for column…” in SQL Server (Error 8155)

If you’re getting an error that reads something like “No column name was specified for column 2 of ‘SalaryTotals’” in SQL Server, it could be that you’re running a subquery or CTE, but you’re not providing a column name for an aggregate function, or for the XML or JSON result of the CTE.

To fix this issue, use an alias to define a column name for all aggregate functions and XML/JSON outputs in the subquery or CTE. Or if it’s a CTE, you can specify the column name using the CTE syntax.

Continue reading

Prepared Statements in MySQL: A Complete Guide with Examples

In MySQL, prepared statements are a feature that allows us to execute the same SQL query multiple times with different parameters. They can improve performance, make our code cleaner, and help defend against SQL injection attacks.

In this article, we’ll explore prepared statements in MySQL, with examples and practical tips on how to use them effectively. We’ll also cover the importance of deallocating statements to free up resources.

Continue reading

How to Use the HANDLER Statement in MySQL for Faster Data Access

The HANDLER statement in MySQL provides a low-level mechanism to directly access storage engine-level functionality, bypassing some of the overhead associated with regular SQL queries. It can be especially useful for traversing a database in high-performance scenarios.

HANDLER is available for MyISAM and InnoDB tables, and can be used as a faster alternative to the SELECT statement.

Continue reading

Fix “Invalid column name ‘$action'” When Using the OUTPUT Clause in SQL Server (Error 207)

If you’re getting an error that reads “Invalid column name ‘$action’” in SQL Server, it could be that you’re using the $action argument of the OUTPUT clause for an operation that doesn’t support this argument.

Or it could be that the operation does support the argument, but you’re referencing it with the wrong syntax.

The $action argument can only be used with MERGE operations, and it must be done correctly.

To fix this issue, be sure to only use the $action argument in MERGE operations and to reference it with the correct syntax.

Continue reading

SQL Server MERGE Statement: Beginner’s Guide with Examples

The MERGE statement in SQL Server allows us to perform INSERT, UPDATE, and DELETE operations in a single query. This makes it an efficient way to synchronize two tables, typically between a source and a target, based on a defined condition. Rather than running separate queries to handle insertions, updates, and deletions, we can combine all of these operations into one statement; the MERGE statement.

Continue reading

SQL Joins with the USING Clause: How It Compares to the ON Clause

Perhaps one of the lesser-known clauses when it comes to SQL joins is the USING clause. While the more widely-used ON clause allows us to explicitly specify join conditions, the USING clause simplifies the syntax when the join is based on columns with the same name in both tables.

In this article, we’ll dive into the USING clause, compare it to the ON clause, and look at examples that illustrate the difference.

Continue reading

Fix “Column, parameter, or variable #1: Cannot find data type” in SQL Server (Error 2715)

If you’re getting SQL Server error 2715, which reads something like “Column, parameter, or variable #1: Cannot find data type SERIAL“, it appears that you’re trying to define a column to have a data type that’s not supported in SQL Server.

To fix this issue, be sure to use a supported data type for all columns.

Continue reading

A Deep Dive into PostgreSQL’s TRUNCATE Statement

Most relational database management systems support the TRUNCATE statement, and PostgreSQL is no exception.

That said, PostgreSQL has a few differences in the way its TRUNCATE statement works when compared to many other RDBMSs.

In this article, we’ll explore the various features of PostgreSQL’s implementation of the TRUNCATE statement, along with examples to demonstrate.

Continue reading

A Comparison of 6 SQL Rank Functions

SQL rank functions can be handy things to have when analysing data. Most major RDBMSs implement a similar bunch of ranking functions, usually with the same names. These rank functions allow us to assign rankings to rows based on specific criteria.

In this article, we’ll look at six commonly used SQL ranking functions, and observe how they differ. We’ll throw them all together into a single query and see their results side by side.

The rank functions in question are: ROW_NUMBER(), RANK(), DENSE_RANK(), NTILE(), PERCENT_RANK(), and CUME_DIST().

Continue reading