MySQL PI() function
In this context, we will learn how we can use the MySQL PI() function with proper syntax and good examples.
Introduction of MySQL PI() function
PI() function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally.
Syntax of the MySQL PI() function
The syntax of the MySQL PI() function is given as follows:
PI();
Parameters or arguments used in MySQL PI() function:
There is no parameter accepted by this function.
Returns:
It returns the Pi value, i.e., 3.141593.
Application used for PI() function:
The PI() function can be used in the given below MySQL versions.:
- MySQL 5.7
- MySQL 5.6
- MySQL 5.5
- MySQL 5.1
- MySQL 5.0
- MySQL 4.1
- MySQL 4.0
- MySQL 3.23
Examples of MySQL PI() function:
Now we will look into some MySQL PI() function examples and will explore how we can use the PI function in MySQL.
Example-1:
Here, we will derive the default value of Pi using the PI Function in MySQL.
SELECT PI() AS DefaultPiValue;
Output:
DefaultPiValue
3.141593
1 row in set (0.00 sec)
Example-2:
Here, we will derive the value of Pi up to 18 decimal places using the PI Function in MySQL.
SELECT PI()+0.000000000000000000
AS PiValue;
Output :
PiValue
3.141592653589793000
1 row in set (0.00 sec)
Example 3:
Now we will use the PI Function for the calculation of the Area and perimeter of all circles in a column. To demonstrate, let us create a table named Circle.
CREATE TABLE Circle(
Circle_id INT AUTO_INCREMENT,
Radius DECIMAL(10, 3) NOT NULL,
PRIMARY KEY(Circle_id )
);
Now, insert some data into the Circle table.
INSERT INTO Circle(Radius )
VALUES
(2 ),(3),(10 ),(12.5 ),(6.80),
(4.60 ),(6),(20),(25) ;
So, the Circle Table is as follows.
SELECT * FROM Circle;
Circle_id | Radius |
1 | 2.000 |
2 | 3.000 |
3 | 10.000 |
4 | 12.500 |
5 | 6.800 |
6 | 4.600 |
7 | 6.000 |
8 | 20.000 |
9 | 25.000 |
Now, we will evaluate the Area and perimeter of every Circle using the PI function.
SELECT Circle_id, Radius,
PI() * Radius * Radius
AS Area,
2 * PI() * Radius AS Perimeter
FROM Circle;
Output:
Circle_id | Radius | Area | Perimeter |
1 | 2.000 | 12.566371 | 12.566371 |
2 | 3.000 | 28.274334 | 18.849556 |
3 | 10.000 | 314.159265 | 62.831853 |
4 | 12.500 | 490.873852 | 78.539816 |
5 | 6.800 | 145.267244 | 42.725660 |
6 | 4.600 | 66.476101 | 28.902652 |
7 | 6.000 | 113.097336 | 37.699112 |
8 | 20.000 | 1256.637061 | 125.663706 |
9 | 25.000 | 1963.495408 | 157.0 |
Application of MySQL PI() function:
The PI() function will be used in the given below MySQL versions:
- MySQL 5.7
- MySQL 5.6
- MySQL 5.5
- MySQL 5.1
- MySQL 5.0
- MySQL 4.1
- MySQL 4.0
- MySQL 3.23
Summary:
In the above context, we have learned how to use the PI() function in MySQL to find the PI value in it.