MySQL and Postgres are two of the most popular SQL databases. They have some key differences in syntax and features which can make jumping between the two a bit tricky.
Here are 5 differences that can have impact in your queries, that I faced personally.
1. MySQL is case insensitive, Postgres is case sensitive
- ππππππ πΈπ³, π½π°πΌπ΄ ππππ πππππ π ππππ ππππ = 'πΉπππ'
- ππππππ ππ, ππππ ππππ πππππ π ππππ ππππ = 'ππππ'
- In Postgres, searching for "john" vs "John" will yield different results, whereas in MySQL it will return the same result set
- Also, selecting "ID, NAME" will throw an error in Postgres if the columns are lowercase (which they should be for Postgres)
2. MySQL allows camelCase for identifiers, Postgres does not
- select dob as πππππΎππ±ππππ from users where id = 1
- This query will return the dataset with the πππππΎππ±ππππ column for MySQL, but πππππππππππ for Postgres. Be careful when mapping raw query results to objects in code.
3. MySQL allows ON UPDATE to auto-update column values, but Postgres does not
- For columns like updated_at, in MySQL we can simply declare the table definition as “ππππππ πππππππ_ππ ππππππππ ππ ππππππ πππππππ_πππππππππ”
- In postgres, you have to create a separate trigger on the table to achieve the same thing
4. MySQL allows the use of boolean column values interchangeably between 1/0 and TRUE/FALSE, while Postgres only uses TRUE/FALSE values.
- For columns like updated_at, in MySQL we can simply declare the table definition as “column updated_at datetime on update current_timestamp”
- In Postgres, you have to create a separate trigger on the table to achieve the same thing
- select * from users where is_active = 1
- let’s say is_active is a boolean column. this query will work in mysql but not in postgres
5. MySQL allows bulk-updating multiple rows for a partial set of columns, while for Postgres you must pass the whole row’s values
- In MySQL you can do something like this, where you can update columns for multiple records by the primary ID (or unique constraint), while keeping other columns intact.
πΈπ½ππ΄ππ πΈπ½ππΎ ππ· (ππ,ππππ) π π°π»ππ΄π (π·,'πΉπππ'),(πΈ,'πΉπππ') π°π πππ
πΎπ½ π³ππΏπ»πΈπ²π°ππ΄ πΊπ΄π ππΏπ³π°ππ΄ ππππ = πππ .ππππ;
- For Postgres there is a similar "πΎπ½ π²πΎπ½π΅π»πΈπ²π (ππ) π³πΎ ππΏπ³π°ππ΄” statement, but for that you cannot just have “id,name”, you will need to pass all the columns of the user table for the bulk-update to work.
1. MySQL is case insensitive, Postgres is not
2. MySQL allows camelCase identifiers, Postgres does not
3. MySQL allows "ON UPDATE" for columns, Postgres needs a trigger for the same
4. MySQL allows 1/0 and TRUE/FALSE for Boolean, while Postgres only allows 1/0
5. MySQL allows bulk-updating a partial set of columns for multiple records, Postgres does not
6. Postgres has no autoincrement. Mysql allows for an autoincrement on PRIMARY KEY.
MySQL also has a 'heartbeat' for easy replication/synchronization. Postgres does not.

No comments:
Post a Comment