Skip to content
Oleh Astappiev edited this page Oct 16, 2023 · 1 revision

SQL

Instead of REPLACE INTO use

INSERT INTO t1 (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE a=VALUES(a), b=VALUES(b), c=VALUES(c);

REPLACE INTO causes problems when using Triggers or Foreign key constraints because it triggers the delete event.

Name convention

  • use snake case for column names, like this_is_column_of_multiple_words;
  • entity ID field should name name like user_id, resource_id, NOT just id;
  • use TIMESTAMP for metadata fields (like created_at, updated_at) and DATETIME, DATE, TIME for user defined values (like date_of_birth);
  • always add a default value for TIMESTAMP fields, like DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP
Clone this wiki locally