Differences Between MySQL and Postgres
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.