Related Topic

Reserved Keywords

Reserved words are predefined keywords in computer programming language. These Reserved keywords can be any string of character which have any meaning or without any meaning.

Example :
Reserved keywords in PHP
  • Isset
  • Print
  • Abstract
  • Function
Click Here to find more reserved keywords on php.net

Reserved keywords in JAVA
  • Package
  • Volatile
  • Transient
  • Synchronized
Click Here to find more java reserved keywords

Reserved keywords in MYSQL
  • AVG
  • COMMIT
  • BIT
  • SELECT
Click Here to find more mysql reserved keywords

These reserved keywords should NOT be used as user-defined names to define an element, variable or identifier in code.

Reserved keywords require special treatment to use if applicable.

1. How to create a record if the mysql table column name matches with a reserved keyword?

Let's understand what would be issue if we use reserved keyword as column name.

Created a table with column name as limit (Where limit is reserved keywords used by mysql to return records as given Ex. Limit n,m where n&m can be any number)


CREATE TABLE `tbl_employee` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) NOT NULL,
 `email` varchar(255) NOT NULL,
 `limit` varchar(50) DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB

It will raise error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit)...... once we try to create record in following way


INSERT INTO `tbl_employee` (name, email, limit) VALUES ( 'test', 'test@testdemo.com', 4);

To overcome above syntax error we need to fix in following way

  • It is good practice that we Don’t use reserved keywords as table names or column names whenever possible.
  • Reserved words must be quoted before using as a column name or table in mysql.

Let's quote column name and rewrite above insert query again in following manner


INSERT INTO `tbl_employee` (`name`, `email`, `limit`) VALUES ( 'test', 'test@testdemo.com', 4)

//Output: 1 row inserted.

Note: It is good practice to quote column name during CRUD operation to avoid any such errors.

2. Can we use reserved words as class name or function name in php?

You can not use reserved keywords as class or function name.

You will receive Response Code: 500 Internal Server Error in most of the cases.

Sometimes it is hard to detect such error initially until we check server log to trace such errors.

Such error can be avoided if we using IDE like Netbeans, PhpStorms etc where we get warning while creating such class or functions.

Here is some example with error response

Use reserved keywords as class name


class final {
   public function isTest($param) {
    echo "hello final";
}
}

Where final is reserved keyword.

It will raise error PHP Parse error: syntax error, unexpected 'final' (T_FINAL), expecting identifier (T_STRING)

Use reserved keywords as function name


 function isset($param) {

     echo "hello isset";
}

Where isset is reserved keyword.

It will raise error PHP Parse error: syntax error, unexpected 'isset' (T_ISSET), expecting '('

3. Can Reserved keywords be used as a variable / method name in java?

You should NOT use Reserved keywords to declare variables/method name. It will be BUILD FAILED if using in such way.

Most IDEs will prompt warning while we use reserved keywords to declare variable.

Most common error message will be reported as below once declare param name or method name using Reserved keywords.

  • not a statement
  • illegal start of type
  • ';' expected

Example :
public class JavaApplication {


    public static void main(String[] args) {


       //Defined integer variable name as package where package is a reserve word

        int package =  10;
 
        
    } 
}

Above will not build and raise following errors

JavaApplication.java:13: error: not a statement int package = 10;

JavaApplication.java:13: error: ';' expected int package = 10;
2 errors