Course
Literals
MySQL Tutorial
This SQL tutorial is structured for beginners to guide them from the foundational concepts to advanced data manipulation and querying techniques in SQL. By the end of this tutorial, you will have developed a robust understanding of SQL, equipping you with the knowledge to efficiently manage and analyze data across various database systems. This guide sets the stage for your growth into a skilled data professional, ready to tackle complex data challenges and contribute to the field of data analysis and database management.
Literals
In MySQL, literals are fixed values (constants) that can be used in SQL statements such as SELECT, INSERT, UPDATE, and DELETE. We can use a literal in SQL statements without needing to be represented by a variable or an expression.
Following are some common MySQL literals:
- Numeric Literals
- String Literals
- Boolean Literals
- Date and Time Literals
- NULL Literals
Numeric Literals
The MySQL numeric literals are numeric values that can represent positive or negative numbers, including both integers and floating-point values.
If we do not specify any sign (i.e. positive (+) or negative (-)) to a numeric value, then a positive value is assumed.
Let us see some examples by using various numeric literals in SQL queries.
Example
Following example displays an integer literal with no sign (by default positive sign will be considered)
SELECT 100 AS 'numeric literal';
Output
The output is obtained as follows
Example
Following example displays an integer literal with positive sign (+)
SELECT -100 AS 'numeric literal';
Output
The output is obtained as follows
Example
Following example displays an integer literal with negative sign (-)
SELECT +493 AS 'numeric literal';
Output
The output is obtained as follows
Example
Following example displays a floating point literal
SELECT 109e-06 AS 'numeric literal';
Output
The output is obtained as follows
Example
Following example displays a decimal literal
SELECT 793.200 AS 'numeric literal';
Output
The output is obtained as follows
String Literals
The MySQL string literals are character strings that are enclosed within the single quotes (') or double quotes (").
Let us see some examples where string literals in SQL queries are used in different ways.
Example
In this example, we are displaying a string literal enclosed in single quotes
SELECT 'tutorialspoint' AS 'string literal';
We can use double quotes to enclose a string literal as follows
SELECT "tutorialspoint" AS 'string literal';
Output
Following output is obtained in both cases
Example
In this example, we are displaying a string literal with spaces enclosed in single quotes
SELECT 'tutorials point india' AS 'string literal';
We can also enclose this string literal (spaces included) in double quotes
SELECT "tutorials point india" AS 'string literal';
Output
Following output is obtained with both queries
Boolean Literals
The MySQL Boolean literals are logical values that evaluate to either 1 or 0. Let us see some example for a better understanding.
Example
There are various ways a boolean value is evaluated to true in MySQL. Here, we use the integer 1 as a boolean literal
SELECT 1 AS 'boolean literal';
We can also use the keyword TRUE to evaluate the boolean literal to 1.
SELECT TRUE AS 'boolean literal';
We can also use the lowercase of the keyword TRUE, as true, to evaluate the boolean literal to 1.
SELECT true AS 'boolean literal';
Output
Following output is obtained
Example
Similarly, there are multiple ways a boolean value is evaluated to false in MySQL. Here, we use the integer 0 as a boolean literal
SELECT 0 AS 'boolean literal';
We can also use the keyword FALSE to evaluate the boolean literal to 0.
SELECT FALSE AS 'boolean literal';
We can also use the lowercase of the keyword FALSE, as false, to evaluate the boolean literal to 0.
SELECT false AS 'boolean literal';
Output
Following output is obtained
Date and Time Literals
The MySQL date and time literals represent date and time values. Let us see examples to understand how date and time values are represented in various ways in MySQL.
Example
In this example, we will display a date literal formatted as 'YYYY-MM-DD'
SELECT '2023-04-20' AS 'Date literal';
Output
Following output is obtained
Example
In this example, we will display a date literal formatted as 'YYYYMMDD'
SELECT '20230420' AS 'Date literal';
Output
Following output is obtained
Example
In this example, we will display a date literal formatted as YYYYMMDD
SELECT 20230420 AS 'Date literal';
Output
Following output is obtained
Example
In this example, we will display a date literal formatted as 'YY-MM-DD'
SELECT '23-04-20' AS 'Date literal';
Output
Following output is obtained
Example
In this example, we will display a date literal formatted as 'YYMMDD'
SELECT '230420' AS 'Date literal';
Output
Following output is obtained
Example
In this example, we will display a date literal formatted as YYMMDD
SELECT 230420 AS 'Date literal';
Output
Following output is obtained
Example
In this example, we are displaying a time literal formatted as 'HH:MM:SS'.
SELECT '10:45:50' AS 'Time literal';
Output
Following output is obtained
Example
In this example, we are displaying a time literal formatted as HHMMSS.
SELECT 104550 AS 'Time literal';
Output
Following output is obtained
Example
In this example, we are displaying a time literal formatted as 'HH:MM'.
SELECT '10:45' AS 'Time literal';
Output
Following output is obtained
Example
In this example, we are displaying a time literal formatted as 'MMSS'.
SELECT '4510' AS 'Time literal';
Output
Following output is obtained
Example
In this example, we are displaying a time literal formatted as 'SS'.
SELECT '10' AS 'Time literal';
Here, let us display a time literal formatted as SS.
SELECT 10 AS 'Time literal';
Output
Following output is obtained
Example
In this example, we are displaying a time literal formatted as 'D HH:MM:SS' where D can be a day value between 0 and 34.
SELECT '4 09:30:12' AS 'Time literal';
Output
Following output is obtained
Example
In this example, we are displaying a time literal formatted as 'D HH:MM' where D can be a day value between 0 and 34.
SELECT '4 09:30' AS 'Time literal';
Output
Following output is obtained
Example
In this example, we are displaying a time literal formatted as 'D HH' where D can be a day value between 0 and 34.
SELECT '4 09' AS 'Time literal';
Output
Following output is obtained
Example
In this example, we are displaying a Datetime literal formatted as 'YYYY-MM-DD HH:MM:SS'.
SELECT '2023-04-20 09:45:10' AS 'datetime literal';
Output
Following output is obtained
Example
In this example, we are displaying a Datetime literal formatted as 'YYYYMMDDHHMMSS'.
SELECT '20230420094510' AS 'datetime literal';
Now, we are displaying a Datetime literal formatted as YYYYMMDDHHMMSS.
SELECT 20230420094510 AS 'datetime literal';
Output
Both queries produce the same output as follows
Example
In this example, we are displaying a Datetime literal formatted as 'YY-MM-DD HH:MM:SS'.
SELECT '23-04-20 09:45:10' AS 'datetime literal';
Output
Following output is obtained
Example
In this example, we are displaying a Datetime literal formatted as 'YYMMDDHHMMSS'.
SELECT '230420094510' AS 'datetime literal';
Here, we are displaying a Datetime literal formatted as YYMMDDHHMMSS.
SELECT 230420094510 AS 'datetime literal';
Output
Both queries give the same following output
Null Literals
The MySQL Null literals represents the absence of a value. It is case in-sensitive.
Example
Following are some examples of valid NULL literals
SELECT NULL AS 'NULL literals';
In lowercase
SELECT null AS 'NULL literals';
Output
Following output is obtained
Client Program
We can also use Literals in a MySQL database using a Client Program.
Syntax
PHPNodeJSJavaPython
To perform literals through a PHP program, we need to execute the required query using the mysqli function query() as follows
$sql = "SELECT 100 AS 'Numerical_literal'";$mysqli->query($sql);
Example
Following are the programs
PHPNodeJSJavaPython
$dbhost = 'localhost';$dbuser = 'root';$dbpass = 'password';$db = 'TUTORIALS';$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $db);if ($mysqli->connect_errno) { printf("Connect failed: %s", $mysqli->connect_error); exit();}//printf('Connected successfully.');$sql = "SELECT 100 AS 'Numerical_literal'";If($result = $mysqli->query($sql)){ printf("Select query executed successfully...!\n"); while($row = mysqli_fetch_array($result)){ printf("Numerical literal: %d", $row["Numerical_literal"]); }printf("\n");}$sql = "SELECT 'Tutorialspoint' AS 'String_literal'";If($result = $mysqli->query($sql)){ printf("Select query executed successfully...!\n"); while($row = mysqli_fetch_array($result)){ printf("String Literal: %s", $row["String_literal"]); }}printf("\n");$sql = "SELECT 1 AS 'Boolean_literal'";If($result = $mysqli->query($sql)){ printf("Select query executed successfully...!\n"); while($row = mysqli_fetch_array($result)){ printf("Boolean literal: %s", $row["Boolean_literal"]); }}if($mysqli->error){ printf("Error message: ", $mysqli->error);}$mysqli->close();
Output
The output obtained is as shown below
Select query executed successfully...!Numerical literal: 100Select query executed successfully...!String Literal: TutorialspointSelect query executed successfully...!Boolean literal: 1