Difference between revisions of "Difference MySQL PostGreSQL"

From IMSMA Wiki
Jump to: navigation, search
Line 98: Line 98:
 
CREATE VIEW `my_view` AS
 
CREATE VIEW `my_view` AS
 
SELECT
 
SELECT
sum(if((`nominefound_view_device`.`Device Type` = _utf8'SAA'),`nominefound_view_device`.`Quantity`,0)) AS`SAA`,
+
sum(if((`my_view2`.`Device Type` = _utf8'SAA'),`my_view2`.`Quantity`,0)) AS`SAA`,
sum(if((`nominefound_view_device`.`Device Type` = _utf8'AP'),`nominefound_view_device`.`Quantity`,0)) AS `AP`,
+
sum(if((`my_view2`.`Device Type` = _utf8'AP'),`my_view2`.`Quantity`,0)) AS `AP`,
sum(if((`nominefound_view_device`.`Device Type` = _utf8'AIED'),`nominefound_view_device`.`Quantity`,0)) AS `AIED`,
+
sum(if((`my_view2`.`Device Type` = _utf8'AIED'),`my_view2`.`Quantity`,0)) AS `AIED`,
sum(if((`nominefound_view_device`.`Device Type` = _utf8'UXO'),`nominefound_view_device`.`Quantity`,0)) AS `UXO`,
+
sum(if((`my_view2`.`Device Type` = _utf8'UXO'),`my_view2`.`Quantity`,0)) AS `UXO`,
sum(if((`nominefound_view_device`.`Device Type` = _utf8'AT'),`nominefound_view_device`.`Quantity`,0)) AS `AT`,
+
sum(if((`my_view2`.`Device Type` = _utf8'AT'),`my_view2`.`Quantity`,0)) AS `AT`,
sum(if((`nominefound_view_device`.`Device Type` = _utf8'Fragments'),`nominefound_view_device`.`Quantity`,0)) AS `Fragments`
+
sum(if((`my_view2`.`Device Type` = _utf8'Fragments'),`my_view2`.`Quantity`,0)) AS `Fragments`
 
FROM
 
FROM
 
`my_view2`;
 
`my_view2`;
Line 113: Line 113:
 
CREATE VIEW my_view AS
 
CREATE VIEW my_view AS
 
SELECT
 
SELECT
sum(case when nominefound_view_device.Device Type = 'SAA') then nominefound_view_device.Quantity else 0 end) AS "SAA",
+
sum(case when my_view2.Device Type = 'SAA') then my_view2.Quantity else 0 end) AS "SAA",
sum(case when nominefound_view_device.Device Type = 'AP') then nominefound_view_device.Quantity else 0 end) AS "AP",
+
sum(case when my_view2.Device Type = 'AP') then my_view2.Quantity else 0 end) AS "AP",
sum(case when nominefound_view_device.Device Type = 'AIED') then nominefound_view_device.Quantity else 0 end) AS "AIED",
+
sum(case when my_view2.Device Type = 'AIED') then my_view2.Quantity else 0 end) AS "AIED",
sum(case when nominefound_view_device.Device Type = 'UXO') then nominefound_view_device.Quantity else 0 end) AS "UXO",
+
sum(case when my_view2.Device Type = 'UXO') then my_view2.Quantity else 0 end) AS "UXO",
sum(case when nominefound_view_device.Device Type = 'AT') then nominefound_view_device.Quantity else 0 end) AS "AT",
+
sum(case when my_view2.Device Type = 'AT') then my_view2.Quantity else 0 end) AS "AT",
sum(case when nominefound_view_device.Device Type = 'Fragments') then nominefound_view_device.Quantity else 0 end) AS "Fragments"
+
sum(case when my_view2.Device Type = 'Fragments') then my_view2.Quantity else 0 end) AS "Fragments"
 
FROM
 
FROM
 
my_view2;
 
my_view2;
Line 128: Line 128:
 
END
 
END
 
</pre>
 
</pre>
 +
 
=== Date manipulations ===__NOEDITSECTION__
 
=== Date manipulations ===__NOEDITSECTION__
 +
There are differences between MySQL and PostgreSQL when it comes to manipulating fields of type DATE, such as for example extracting the year or the month of a date. In MySQL, the related functions are year() and month(). In PostgreSQL, there is an extract() function that does the job. See the below example for reference. The following create view statement:<br />
 +
'''MySQL'''
 +
<pre>
 +
CREATE VIEW `my_view` AS
 +
SELECT
 +
(year(`my_view2`.`DateofAccident`) - year(`my_view2`.`DateofBirth`)) AS `Age`,
 +
year(`my_view2`.`DateofAccident`) AS `Year`,
 +
month(`my_view2`.`DateofAccident`) AS `Month`
 +
FROM `my_view2`;
 +
</pre>
 +
 +
needs to be changed to:<br />
 +
'''PostgreSQL'''
 +
<pre>
 +
CREATE VIEW `my_view` AS
 +
SELECT
 +
extract(year from my_view2."DateofAcciden") - extract(year from my_view2."DateofBirth") AS "Age",
 +
extract(year from my_view2."DateofAccident") AS "Year",
 +
extract(month from my_view2."DateofAccident") AS "Month"
 +
FROM my_view2;
 +
</pre>
  
 
=== The GROUP BY clause ===__NOEDITSECTION__
 
=== The GROUP BY clause ===__NOEDITSECTION__

Revision as of 08:44, 19 June 2014

Ambox warning blue construction.png This page is under construction

Introduction

The biggest differences are:

  • PostGreSQL is case sensitive
  • Command/function
  • Dates are stored without format in PostGreSQL
  • Round off
  • Customised functions

Changes in database views from MySQL to PostreSQL

When upgrading from IMSMANG 5.08.04 to 6.0, the database views need to be adapted, i.e. changed from MySQL syntax to PostgreSQL syntax. The country focal point is taking care of this. For reference, this section describes the main changes encountered. This can also be used as a tutorial for creating views in PostgreSQL, for IMSMANG administrators who were used to creating them in MySQL.

Case sensitivity and column names

PostgreSQL, as opposed to MySQL, is case-sensitive. This means that strings need to be provided in exactly the way they are stored in the database. For example, in the table IMSMAENUM, one out of many values in the column ENUMVALUE is Progress Report, with capital P and capital R. In MySQL it was possible to write:

SELECT *
FROM imsmaenum
WHERE enumvalue = 'progress report'

and still get one row as result. In PostgreSQL, the same query will return no result.

Therefore, a create view statement like this:
MySQL

CREATE OR REPLACE VIEW my_view AS
SELECT ...
FROM imsmaenum, ...
WHERE imsmaenum.enumvalue = 'progress report';

needs to be changed to:
PostgreSQL

CREATE OR REPLACE VIEW my_view AS
SELECT ...
FROM imsmaenum, ...
WHERE imsmaenum.enumvalue = 'Progress Report';

Similarly, in MySQL it is possible to define column names with a mix of upper and lower case as well as blanks and other special characters without any trouble. In PostgreSQL, this is also possible, but in this case the column name must be enclosed in double quotes. For example, the following create view statement:
MySQL

CREATE VIEW my_view AS
SELECT hazreducdeviceinfo.hazreduc_guid AS hazreduc_guid,
ordnance.model AS Device Type
FROM hazreducdeviceinfo;

needs to be changed to:
PostgreSQL

CREATE VIEW my_view AS
SELECT hazreducdeviceinfo.hazreduc_guid AS hazreduc_guid,
ordnance.model AS "Device Type"
FROM hazreducdeviceinfo;
Note.jpg This also applies to querying the data. For select rows where the device type is equal to 'xy', the syntax is the following:
 ... WHERE "Device Type" = 'xy'
Note.jpg If an column name in PostgreSQL is written with upper case but not enclosed into double quotes, it will be created in lower case in the database.

CAST AS CHAR CHARSET utf8 and _utf8 (mySQL)

In some (older) MySQL versions, UTF-8 was not the default character set and strings thus needed to be explicitly converted to UTF-8. This is not the case in PostgreSQL and UTF-8 related syntax as used in MySQL does not work in PostgreSQL. Below are two examples encountered in IMSMANG views.

MySQL

AND (`customdefinedfield`.`label` = _utf8'CDF-Area Reduction MDU')

PostgreSQL

AND (customdefinedfield.label = 'CDF-Area Reduction MDU')

MySQL

cast(`geopoint`.`latitude` as char charset utf8) AS `lat`

PostgreSQL

cast(geopoint.latitude as char) AS lat

IF and IFNULL (mySQL) / CASE (PostgreSQL) statements (conditional expressions)

The statements IF() and IFNULL() do not exist in PostgreSQL. In PostgreSQL, the CASE statement is used for conditional expressions. The syntax of the CASE statement is as follows:

CASE WHEN condition THEN result
     [WHEN ...]
     [ELSE result]
END

Therefore, for example the following create view statement:
MySQL

CREATE VIEW `my_view` AS
SELECT
sum(if((`my_view2`.`Device Type` = _utf8'SAA'),`my_view2`.`Quantity`,0)) AS`SAA`,
sum(if((`my_view2`.`Device Type` = _utf8'AP'),`my_view2`.`Quantity`,0)) AS `AP`,
sum(if((`my_view2`.`Device Type` = _utf8'AIED'),`my_view2`.`Quantity`,0)) AS `AIED`,
sum(if((`my_view2`.`Device Type` = _utf8'UXO'),`my_view2`.`Quantity`,0)) AS `UXO`,
sum(if((`my_view2`.`Device Type` = _utf8'AT'),`my_view2`.`Quantity`,0)) AS `AT`,
sum(if((`my_view2`.`Device Type` = _utf8'Fragments'),`my_view2`.`Quantity`,0)) AS `Fragments`
FROM
`my_view2`;

needs to be changed to:
PostgreSQL

CREATE VIEW my_view AS
SELECT
sum(case when my_view2.Device Type = 'SAA') then my_view2.Quantity else 0 end) AS "SAA",
sum(case when my_view2.Device Type = 'AP') then my_view2.Quantity else 0 end) AS "AP",
sum(case when my_view2.Device Type = 'AIED') then my_view2.Quantity else 0 end) AS "AIED",
sum(case when my_view2.Device Type = 'UXO') then my_view2.Quantity else 0 end) AS "UXO",
sum(case when my_view2.Device Type = 'AT') then my_view2.Quantity else 0 end) AS "AT",
sum(case when my_view2.Device Type = 'Fragments') then my_view2.Quantity else 0 end) AS "Fragments"
FROM
my_view2;

The ISNULL() function can also be translated into a CASE statement:

CASE WHEN field IS NULL THEN result
END

Date manipulations

There are differences between MySQL and PostgreSQL when it comes to manipulating fields of type DATE, such as for example extracting the year or the month of a date. In MySQL, the related functions are year() and month(). In PostgreSQL, there is an extract() function that does the job. See the below example for reference. The following create view statement:
MySQL

CREATE VIEW `my_view` AS
SELECT
(year(`my_view2`.`DateofAccident`) - year(`my_view2`.`DateofBirth`)) AS `Age`,
year(`my_view2`.`DateofAccident`) AS `Year`,
month(`my_view2`.`DateofAccident`) AS `Month`
FROM `my_view2`;

needs to be changed to:
PostgreSQL

CREATE VIEW `my_view` AS
SELECT
extract(year from my_view2."DateofAcciden") - extract(year from my_view2."DateofBirth") AS "Age",
extract(year from my_view2."DateofAccident") AS "Year",
extract(month from my_view2."DateofAccident") AS "Month"
FROM my_view2;

The GROUP BY clause