Differences Between MySQL and Postgres

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.














Next Post Previous Post
No Comment
Add Comment
comment url