简体   繁体   English

初学者MySQL语法错误

[英]Beginner MySQL Syntax Error

I'm trying to create a table in an already created database. 我正在尝试在已创建的数据库中创建表。 I've already switched into the database using USE church_fpip . 我已经使用USE church_fpip切换到数据库。 Here's my code for creating the new table but for some reason it seems ZEROFILL is creating a syntax error. 这是我用于创建新表的代码,但是由于某些原因,ZEROFILL似乎正在创建语法错误。

mysql> CREATE TABLE sermons (
    ->   id INT NOT NULL AUTO_INCREMENT,
    ->   year INT(4) NOT NULL,
    ->   month INT(2) NOT NULL ZEROFILL,
    ->   day INT(2) NOT NULL ZEROFILL,
    ->   title VARCHAR(50) NOT NULL,
    ->   preacher VARCHAR(30) NOT NULL,
    ->   length INT(3) NOT NULL,
    ->   visible TINYINT(1) NOT NULL,
    ->   PRIMARY KEY (id)
    -> );
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 'ZEROFILL,
  day INT(2) NOT NULL ZEROFILL,
  title VARCHAR(50) NOT NULL,
  preach' at line 4

I think ZEROFILL needs to be before NOT NULL such as 我认为ZEROFILL必须在NOT NULL之前,例如

 month INT(2) ZEROFILL NOT NULL,
 day INT(2) ZEROFILL NOT NULL,

However I would recommend the use of DATETIME for what you are trying to do, instead of three columns representing day/month/year. 但是,我建议您将DATETIME用于您要执行的操作,而不要使用三列来表示日/月/年。

I would recommend to use date field instead of seperate year, month and day. 我建议使用日期字段,而不要使用单独的年,月和日。 You can parse date as per your necessity. 您可以根据需要解析日期。 This way it would help to keep your database clean and normalized. 这样,将有助于保持数据库的清洁和规范化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM