数据库练习发生的error—— check the manual that corresponds to your MySQL server version for the right syntax to use near ''id')

clina / 2024-03-14 / 原文

    记录一下发生的错误。

   check the manual that corresponds to your MySQL server version for the right syntax to use near ''id'),

    参考链接:完美解决 ERROR 1064 (42000): You have an error in your SQL syntax ... near …_response code:42000 1064 response message:java.sql-CSDN博客

    这是我写的代码,问题的关键是为了能够区分普通字符和关键字,普通字符应该加上反引号` `。不过要注意一下comment后面的字符串不需要要加上反引号。

   

    正确写法:

create table `t_article`(
`id` int(20) not null auto_increment comment '文章id',
`title` varchar(200) default null comment'文章标题',
`content` longtext comment'文章内容',
primary key(`id`)
)Engine=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET = utf8;

    当然还是推荐关键字大写。

    反引号打法

    

     补充:

1.在 MySQL 中,当主键定义为自增长后,这个主键的值就不再需要用户输入数据了,而由数据库系统根据定义自动赋值。每增加一条记录,主键会自动以相同的步长进行增长。

通过给字段添加 AUTO_INCREMENT 属性来实现主键自增长。语法格式如下:

2.在MySQL数据库中, 字段或列的注释是用属性comment来添加。创建新表的脚本中, 可在字段定义脚本中添加comment属性来添加注释。