trojek
trojek

Reputation: 3228

Unable to describe table, ERROR 1064 (42000): You have an error in your SQL syntax

I have database with numerous tables, one of them called group is created by code:

CREATE TABLE IF NOT EXISTS `sc`.`group` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NOT NULL,
  `description` TEXT NULL,
  `group_type_id` INT NOT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_group_group_type1`
    FOREIGN KEY (`group_type_id`)
    REFERENCES `sc`.`group_type` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE INDEX `fk_group_group_type1_idx` ON `sc`.`group` (`group_type_id` ASC);

When I use command show tables, I got:

+-------------------------------------+
| event_type                          |
| function                            |
| group                               |
| group_admin                         |
...
...
+-------------------------------------+

If I for example write: describe function;, the mysql returns:

+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(45) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+

But if I write describe group; I get an error: ERROR 1064 (42000): 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 'group' at line 1

I can't do any operation (e.g. select, insert) on group table.

What is wrong with it?

Upvotes: 5

Views: 3934

Answers (2)

amit rawat
amit rawat

Reputation: 783

Group is a reserved keyword in MySql. You need to use backticks in the describe statement.

Please try the following command:

describe `group`;

Please avoid using reserved keywords for table and column names. You can add prefix to your table name like tbl_group.

Upvotes: 0

etsa
etsa

Reputation: 5060

You create the table using quotes (I think because group is a reserved word)

Try

DESCRIBE `group`;

Upvotes: 9

Related Questions