In MySQL we sometimes often encounter backticks (``). At first I didn’t know what they meant. What is their function?
Select * from `member` order by posts desc limit 0,10;
It is to distinguish MYSQL’s reserved words from ordinary ones Symbols introduced by characters.
For example: SELECT `select` FROM `test` WHERE select='Field value'
In the test table , there is a select field. If no backticks are used, MYSQL will regard select as a reserved word and cause an error. Therefore, if has a MYSQL reserved word as a field, backticks must be added to distinguish .
Quotation marks are generally used in field values. If the field value is a character or string, Then add quotation marks , such as: select='field value'
Tables built without backticks cannot contain MYSQL Reserved words, otherwise an error will occur
Backtick `, the symbol to the left of the number 1.
Reserved words cannot be used in table names , such as desc. In this case, backticks need to be added to distinguish them, but backticks can be ignored when using table names.
create table desc error
create table `desc` successful
create table `test `Success
drop table test success
Reserved words cannot be used for field names, such as desc, at this time Backticks need to be added, and backticks must also be added when using insert, etc.
create table `test`(`desc` varchar(255)) succeeded
insert into test(desc) values('fxf' ) failed
insert into test(`desc`) values('fxf')success
Mysql common reserved words
##Be sure to pay attention when using mysql , do not use its reserved words as table names or column names, otherwise inexplicable errors will occur.
Yesterday I created a table with a column named interval (time interval). As a result, the data could not be inserted. Finally, I discovered that interval is a reserved word of MySQL.
If similar errors occur in the future, first think about whether it is caused by table name or column name conflicts.
I found a MySQL reserved word list from the Internet for reference only.
ADD | ALL | ALTER |
ANALYZE | AND | AS |
ASC | ASENSITIVE | BEFORE |
BETWEEN | BIGINT | BINARY |
BLOB | BOTH | BY |
CALL | CASCADE | CASE |
CHANGE | CHAR | CHARACTER |
CHECK | COLLATE | COLUMN |
CONDITION | CONNECTION | CONSTRAINT |
CONTINUE | CONVERT | CREATE |
CROSS | CURRENT_DATE | CURRENT_TIME |
CURRENT_TIMESTAMP | CURRENT_USER | CURSOR |
DATABASE | DATABASES | DAY_HOUR |
DAY_MICROSECOND | DAY_MINUTE | DAY_SECOND |
DEC | DECIMAL | DECLARE |
DEFAULT | DELAYED | DELETE |
DESC | DESCRIBE | DETERMINISTIC |
DISTINCT | DISTINCTROW | p |
DOUBLE | DROP | DUAL |
EACH | ELSE | ELSEIF |
ENCLOSED | ESCAPED | EXISTS |
EXIT | EXPLAIN | FALSE |
FETCH | FLOAT | FLOAT4 |
FLOAT8 | FOR | FORCE |
FOREIGN | FROM | FULLTEXT |
GOTO | GRANT | GROUP |
HAVING | HIGH_PRIORITY | HOUR_MICROSECOND |
HOUR_MINUTE | HOUR_SECOND | IF |
IGNORE | IN | INDEX |
INFILE | INNER | INOUT |
INSENSITIVE | INSERT | INT |
INT1 | INT2 | INT3 |
INT4 | INT8 | INTEGER |
INTERVAL | INTO | IS |
ITERATE | JOIN | KEY |
KEYS | KILL | LABEL |
LEADING | LEAVE | LEFT |
LIKE | LIMIT | LINEAR |
LINES | LOAD | LOCALTIME |
LOCALTIMESTAMP | LOCK | LONG |
LONGBLOB | LONGTEXT | LOOP |
LOW_PRIORITY | MATCH | MEDIUMBLOB |
MEDIUMINT | MEDIUMTEXT | MIDDLEINT |
MINUTE_MICROSECOND | MINUTE_SECOND | MOD |
MODIFIES | NATURAL | NOT |
NO_WRITE_TO_BINLOG | NULL | NUMERIC |
ON | OPTIMIZE | OPTION |
OPTIONALLY | OR | ORDER |
OUT | OUTER | OUTFILE |
PRECISION | PRIMARY | PROCEDURE |
PURGE | RAID0 | RANGE |
READ | READS | REAL |
REFERENCES | REGEXP | RELEASE |
RENAME | REPEAT | REPLACE |
REQUIRE | RESTRICT | RETURN |
REVOKE | RIGHT | RLIKE |
SCHEMA | SCHEMAS | SECOND_MICROSECOND |
SELECT | SENSITIVE | SEPARATOR |
SET | SHOW | SMALLINT |
SPATIAL | SPECIFIC | SQL |
SQLEXCEPTION | SQLSTATE | SQLWARNING |
SQL_BIG_RESULT | SQL_CALC_FOUND_ROWS | SQL_SMALL_RESULT |
SSL | STARTING | STRAIGHT_JOIN |
TERMINATED | THEN | |
TINYINT | TINYTEXT | |
TRAILING | TRIGGER | |
UNDO | UNION | |
UNLOCK | UNSIGNED | |
USAGE | USE | |
UTC_DATE | UTC_TIME | |
VALUES | VARBINARY | |
VARCHARACTER | VARYING | ##WHEN |
WHILE | WITH | |
X509 | ##XOR | |
ZEROFILL |
safety
For the sake of convenience, you can add `` to both the table name and the field name.
The above is the detailed content of Why do we need to add backticks in mysql statements?. For more information, please follow other related articles on the PHP Chinese website!