A UNIQUE
constraint is a rule we apply to one or more columns in a database table to ensure that the values in those columns are unique across all rows. This means that no two rows in the table can have the same value(s) in the column(s) where we’ve applied the UNIQUE
constraint. It’s an essential tool for maintaining data integrity and preventing duplicate entries in our database.
Tag: constraints
Fix Error 2788 “Synonyms are invalid in a schemabound object or a constraint expression” in SQL Server
If you’re getting SQL Server error 2788 that reads “Synonyms are invalid in a schemabound object or a constraint expression” it seems that you’re trying (whether intentionally or not) to create a schemabound object that includes a synonym in its definition, or a constraint with a synonym in its expression.
Continue readingHow to DROP Tables with Foreign Key Relationships in PostgreSQL
Nothing pulls me out of the “zone” quicker than attempting to drop a table, only to be told “Nope, that table has a dependency!”.
Granted, this usually only happens in my development environments, as I rarely have any need to drop tables in a production environment.
And that’s probably why it “pulls me out of the zone”. I suddenly have to stop and start thinking about what tables contain foreign keys to the one I’m trying to drop. In dev environments, we’re often trying things out and so it’s not out of the question that we might inadvertently try to drop a table without realising it has dependent objects.
Fortunately, when we’re working with PostgreSQL, we have a quick and easy method to overcome this barrier.
Continue readingHow to “Unhide” a GIPK in MySQL
In MySQL, GIPKs are invisible by definition. GIPK stands for generated invisible primary key, and it’s basically an invisible column automatically created by MySQL with a primary key constraint.
However, just because GIPKs are automatically created invisible, it doesn’t mean that we can’t “unhide” them – or make them visible.
We can make a GIPK visible just as we would make any other invisible column visible – use the ALTER TABLE
statement to set it to VISIBLE
.
Create a Generated Invisible Primary Key (GIPK) in MySQL
MySQL 8.0.30 introduced generated invisible primary keys (GIPKs), which are primary keys that are automatically created whenever we create a table without explicitly defining a primary key.
GIPKs only work with the InnoDB
storage engine, and they only work when we have GIPKs enabled.
In this article, I check whether or not GIPKs are enabled on my system, I then enable GIPKs, and finally I create a table with a GIPK.
Continue readingWhat is a Generated Invisible Primary Key (GIPK) in MySQL?
While primary keys are generally considered a necessity when it comes to relational databases, they’re usually included in the SQL code that creates the database table.
MySQL 8.0.30 introduced generated invisible primary keys which provide an alternative to explicitly specifying a primary key for a table.
A generated invisible primary key (GIPK) is a primary key that’s created implicitly by the MySQL server. If we create a table without an explicit primary key, the MySQL server automatically creates a generated invisible primary key for us (assuming it’s an InnoDB table and that GIPKs are enabled).
Continue readingAdd a Primary Key to an Existing Column in MySQL
MySQL allows us to easily add a primary key constraint to an existing column. We can use the ALTER TABLE
statement to achieve this outcome.
We can also use the ADD PRIMARY KEY
clause, which can be useful for adding composite primary keys.
What is a Composite Primary Key?
Primary keys are a crucial part of relational database management systems (RDBMSs). Most of the time we create a primary key constraint against a single column that is the unique identifier for the table.
But we can also create primary key constraints against more than one column. When we do this, we call it a composite primary key.
Composite keys can be handy when we don’t have a single column that contains unique values, but multiple columns can be combined to create a unique value.
Continue readingCreate a Composite Primary Key in MySQL
A composite primary key is a primary key that consists of two or more columns. Together they will always provide a unique value within the table. In other words, the combination of both values will be unique across all rows – no two rows will share the same combined value.
In MySQL we can create a composite primary key with the PRIMARY KEY
clause of the CREATE TABLE
statement. We can also use the ADD PRIMARY KEY
statement to add a composite primary key to an existing table that doesn’t already have a primary key.
How to Skip Rows that Fail a CHECK Constraint in MySQL
Normally if we try to insert data that violates a CHECK
constraint in MySQL, we get an error, and the whole INSERT
operation fails (including any conforming rows).
But it’s possible to change this, so that any conforming rows are inserted, and only the non-conforming rows are skipped.
We can do this by using the IGNORE
clause.