Pular para o conteúdo principal

SQL: Primary Key constraint name convention


Demand

ALWAYS name forein keys constraints as "fk_{LocalTableName}_{LocalColumnName}_{DestinationTableName}_{DestinationFieldName}"


Description

We use this convention to easily identify the source of the failure, especially in schema updates.

When creating a CONSTRAINT we create its name starting with the string "fk", followed by the full name of the source table, also called local table or child table, followed by the name of the column used in the source table to store the value to be searched for later in the target table, the name of the target table and finally the name of the field in the target table. All separated by "_" (underline).

Examples

1:  CREATE TABLE customers (
2:      id INT NOT NULL,
3:      name VARCHAR(100) NOT NULL,
4:      user_id INT NULL COMMENT 'if the customer have a system login it will be refernced here',
5:     PRIMARY KEY (id),
6:     CONSTRAINT `fk_customers_user_id_users_user_id` 7:        FOREIGN KEY (`user_id`) 8:     
REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION 9: );


Examples Explanation

Sample of an error output using MariaDB

1: SQLSTATE[HY000]: General error: 1833 Cannot change column 'id': used in a foreign key constraint 'fk_customers_user_id_users_user_id' of table 'mydb.customers'


Sample of an error output using MS-SQLServer

1: The UPDATE statement conflicted with the REFERENCE constraint "fk_customers_user_id_users_user_id". The conflict occurred in database "mydb", table "mydb.customers", column 'user_id'.



Comentários

Postagens mais visitadas deste blog

Article: Preventing Database Gridlock: Recognizing and Resolving Deadlock Scenarios

Learn how deadlocks occur in database systems, understand their impact on performance, and discover practical techniques for identifying potential deadlock scenarios in your SQL code.

PHP: Always use an index to add a values to an array

  Demand Always identify the array index when manipulating the array directly Description Whenever you are adding a new item to the array you must add an index so that php does not need to recalculate the size of the array to identify in which position the new content will be added. Examples 1: # Bad code: 2: do { 3: $array[] = $newItem; 4: } while(!feof($fp)); 5: 6: # Good code 7: $array = []; 8: $index = 0; 9: do { 10: $array[$index++] = $newItem; 11: } while(!feof($fp)); Examples Explanation When instantiating a new item in an array in the "bad example" php will identify that you expect the item to be added to the end of the array, since array is a primitive type and has no attributes php always needs to call a calculation function to identify the current size of the array before trying to add a new item to the end of it. By monitoring the growth of the array in an external variable and automatically reference which position will...

NestJS Clustering: Advanced techniques to increase NestJS Apps response

In today’s fast-evolving digital landscape, performance and scalability have become the lifeblood of modern applications. As advanced JavaScript developers, we continuously seek strategies to optimize our code and leverage every ounce of our infrastructure. NestJS clustering promises not just incremental improvements but exponential enhancements by fully unlocking the potential of your virtual machines. This article delves into NodeJS clustering within the NestJS framework, exploring its theoretical underpinnings, offering practical, code-rich examples, and even highlighting an experimental twist that can set your application apart. Unlocking the Power of NodeJS Clustering At its core, NodeJS clustering is about taking full advantage of multi-core systems. Typical NodeJS apps run as single-threaded processes, which can be a bottleneck when you need to handle a massive number of concurrent operations. Clustering allows you to spin up multiple worker processes that share the same server ...