Fix Error “AUTOINCREMENT not allowed on WITHOUT ROWID tables” in SQLite

If you’re getting an error that reads “AUTOINCREMENT not allowed on WITHOUT ROWID tables” in SQLite, it appears that you’re trying to define a column as an AUTOINCREMENT in a WITHOUT ROWID table.

SQLite doesn’t allow us to create AUTOINCREMENT columns on WITHOUT ROWID tables.

To address this issue, either remove the AUTOINCREMENT attribute or remove the WITHOUT ROWID from the table definition.

Continue reading

Fix Error: unknown datatype for (columnname): “DATE” in SQLite

If you’re getting an error that reads something like “unknown datatype for (columnname): “DATE”” in SQLite, it appears that you’re trying to define a column as a DATE type in a strict table.

SQLite doesn’t support the DATE type, however, this error should only occur on strict tables (i.e. a table defined as STRICT).

To fix this issue, either use a supported data type or make the table a regular (non-strict) table.

Continue reading

Why the Primary Key Might Not Appear in PRAGMA index_list() in SQLite

In most relational database management systems (RDBMSs) the PRIMARY KEY is used to define the unique row identifier for a table. But in SQLite, not all primary keys are handled the same way when it comes to indexing.

Depending on how the primary key is defined in a table, it may or may not show up in the list of indexes returned by the PRAGMA index_list() command. In particular, when the primary key is an INTEGER PRIMARY KEY, SQLite doesn’t explicitly create a separate index for it.

This article will explain why this happens and provide examples with different types of primary key definitions.

Continue reading

How to Effectively “Back Up” All Deleted Rows When Using DELETE in SQL Server

Deleting rows in a SQL database can sometimes be a nerve-racking experience. What if you’re deleting the wrong rows? Or what if the business later tells you they want their data back?

Fortunately SQL Server provides us with an easy way to essentially “back up” any rows affected by a DELETE operation to a table.

This article looks at using the OUTPUT ... INTO clause to save a copy of deleted rows to another table.

Continue reading

How to Use SERIAL Functionality on Integer Types Like INT in MySQL

Perhaps you’re familiar with MySQL’s SERIAL type, which is not actually a data type, but a shortcut for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.

One restriction of SERIAL is that it forces us to use the BIGINT data type, which is fine if you think you’ll need the extra space. But what if you want the same attributes for a smaller integer type, like INT or MEDIUMINT for example?

Enter SERIAL DEFAULT VALUE.

Continue reading

List All Prepared Statements in a PostgreSQL Session

Prepared statements are a precompiled SQL queries that we can execute multiple times with different arguments. They are limited to the current session, so we can only create them and run them from within the same session.

Sometimes we might want to review how many prepared statements we’ve created. Or we might need to deallocate one, but we’ve forgotten its name.

No problem. We can use the pg_prepared_statements view to see all of our prepared statements.

Continue reading

Create a DEFAULT Constraint in MySQL

In MySQL, a DEFAULT constraint is used to provide a default value for a column when no value is specified during an INSERT operation. This is particularly useful for ensuring that important columns have consistent, non-null values, even when omitted from inserts.

In this article, we’ll use MySQL to create a table with a couple of DEFAULT constraints, and we’ll also add a constraint to that table after it has been created.

Continue reading

Fix “Violation of PRIMARY KEY constraint” in SQL Server (Error 2627)

If you’re getting an error that reads something like “Violation of PRIMARY KEY constraint ‘PK_CatId’. Cannot insert duplicate key in object ‘dbo.Cats’. The duplicate key value is (1)” in SQL Server, it’s because you’re trying to insert a duplicate value into a primary key column.

A primary key cannot contain duplicate values.

To fix this issue, you’ll need to change the value you’re trying to insert into the primary key column.

Continue reading