Error
Error Code: 1355

MySQL Error 1355: View Missing Underlying Key

📦 MySQL
📋

Description

This error occurs when you attempt to update or delete rows through a SQL view, but the view's definition does not include all columns that constitute the primary or a unique key of its underlying base table. MySQL requires a complete key within the view to unambiguously identify rows for modification operations, preventing data integrity issues.
💬

Error Message

View being updated does not have complete key of underlying table in it
🔍

Known Causes

3 known causes
⚠️
Incomplete View Definition
The `CREATE VIEW` statement for the target view intentionally or unintentionally omitted one or more columns that form the primary or a unique key of the base table.
⚠️
Base Table Missing Key
The underlying table from which the view is derived lacks a defined primary key or any unique keys, making it impossible for the view to provide a complete key for updates.
⚠️
Complex View Logic
The view definition incorporates complex operations like joins, aggregations, or subqueries that obscure or prevent MySQL from identifying a complete and unique key from the base table for direct updates.
🛠️

Solutions

3 solutions available

1. Include All Primary Key Columns in the View easy

Ensure the view definition selects all columns that constitute the primary key of the underlying table.

1
Identify the primary key columns of the underlying table that the view is based on. You can do this by describing the table.
DESCRIBE your_underlying_table;
2
Modify the `CREATE VIEW` or `ALTER VIEW` statement to include all identified primary key columns in the `SELECT` list. If you are updating the view, this will involve rewriting the view definition.
CREATE OR REPLACE VIEW your_view_name AS
SELECT pk_col1, pk_col2, ..., other_columns
FROM your_underlying_table
WHERE ...;
3
If the view was created with an alias for a primary key column, ensure the alias is still present and corresponds to the original column name.
CREATE OR REPLACE VIEW your_view_name AS
SELECT pk_col1 AS alias_for_pk_col1, pk_col2 AS alias_for_pk_col2, ...
FROM your_underlying_table;

2. Rewrite the View Using Only Columns Necessary for Updates medium

If the view is intended for updates, simplify it to include only the columns needed for those updates, ensuring the primary key is fully represented.

1
Determine which columns are actively being updated through the view. Identify the primary key columns of the underlying table.
DESCRIBE your_underlying_table;
2
Create a new view or alter the existing one to select only the necessary updatable columns and all primary key columns. This often means the view will be a direct projection of the underlying table.
CREATE OR REPLACE VIEW your_view_name AS
SELECT pk_col1, pk_col2, ..., updatable_col1, updatable_col2
FROM your_underlying_table;
3
Avoid complex joins or aggregations in the view definition if you intend to perform `UPDATE` operations on it. Such operations can obscure the primary key.
CREATE OR REPLACE VIEW your_view_name AS
SELECT t1.pk_col1, t1.pk_col2, t2.some_data
FROM your_underlying_table t1
JOIN another_table t2 ON t1.fk_col = t2.id
WHERE ...; -- Potentially problematic for updates if PK is not fully represented.

3. Create a New View with a Complete Key Representation medium

If modifying the existing view is complex or risky, create a new view that correctly includes the full primary key.

1
Examine the `SHOW CREATE VIEW your_view_name;` output to understand the current definition.
SHOW CREATE VIEW your_view_name;
2
Identify the primary key of the underlying table using `DESCRIBE your_underlying_table;`.
DESCRIBE your_underlying_table;
3
Define a new view that explicitly selects all primary key columns from the underlying table, along with any other necessary columns for your intended operations.
CREATE VIEW new_view_name AS
SELECT pk_col1, pk_col2, ..., other_columns
FROM your_underlying_table
WHERE ...;
4
Once the new view is tested and verified, you can drop the old view and rename the new one if desired.
DROP VIEW your_view_name;
RENAME VIEW new_view_name TO your_view_name;
🔗

Related Errors

5 related errors