MySQL创建表语法错误

时间:2013-11-10 01:38:41

标签: mysql

我有一个家庭作业,我正试图找到我的错误。我正在MySQL中创建一个表,我是这个的初学者。请给出建议!!

Create table computer_inventory 
( 
   assetnumber int(10) not null default “0”,
   manufacturer varchar(15) not null default ‘ ‘, 
   originalcost decimal(12,2) not null default “0”, 
   currentvalue decimal(12,2) not null default ‘0’, 
   boughtfrom varchar(20) not null default ‘ ‘, 
   instock tinyint(1) not null default ‘ ‘, 
   currentuser varchar(20) not null default ‘ ‘, 
   userphonenum varchar(13) not null default ‘ ‘, 
   boughtdate datatime not null default ‘ ‘ 
);

同样,我是新人,所以可能会有很多错误。

**EDIT:** 

Create table computer_inventory ( 
      assetnumber int(10) not null default 0,
     manufacturer varchar(15) not null default ‘ ‘, 
     originalcost decimal(12,2) not null default 0, 
     currentvalue decimal(12,2) not null default 0, 
     boughtfrom varchar(20) not null default ‘ ‘, 
     instock tinyint(1) not null default 0, 
     currentuser varchar(20) not null default ‘ ‘,
     userphonenum varchar(13) not null default ‘ ‘,
     boughtdate datetime not null default ‘0000-00=00’ 
 );

我正在使用MySQL 5.6。错误显示“错误1064(42000):您的SQL语法中有错误;请检查与您的MySQL服务器版本对应的手册,以便在'''附近使用正确的语法,originalcost decimal(12,2)not null default 0,当前值十进制(第2行为1'

1 个答案:

答案 0 :(得分:1)

有一些错误...... datetime拼写错误。某些默认值也是列的错误数据类型。

Create table computer_inventory 
( 
   assetnumber int(10) not null default 0,
   manufacturer varchar(15) not null default '', 
   originalcost decimal(12,2) not null default 0, 
   currentvalue decimal(12,2) not null default 0, 
   boughtfrom varchar(20) not null default '', 
   instock tinyint(1) not null default 0, 
   currentuser varchar(20) not null default '', 
   userphonenum varchar(13) not null default '', 
   boughtdate datetime not null  default '0000-00-00'
);

下次出现这种问题时,您可以执行以下操作:首先尝试使用一列或两列来创建表,如果这些行没有错误,请删除表并使用几列。如果出现错误,这将更容易准确地隔离哪个部分不正确。当我测试这个时,我得到错误的默认值和类似的东西......如instock tinyint(1) not null default ' ',这不起作用,因为你试图将空字符串设置为整数列。它不起作用。对于日期时间默认值,我不得不在谷歌上查找它以查看适当的空日期值。其中一些也是反复试验,比如删除反引号并用正常引号替换它们。

编辑:这对我来说没有错误......我正在使用sql fiddle对其进行测试,并将其设置为MySQL 5.5.31。告诉我你得到的确切错误(我们无法帮助你,除非你告诉我们错误说的是什么,我不可能猜到)以及你正在使用什么数据库及其版本。


这是基于您上面的编辑。您键入0000-00-00'0000-00=00',您需要使用正常的单引号而不是反引号(我认为?)。

Create table computer_inventory ( 
     assetnumber int(10) not null default 0,
     manufacturer varchar(15) not null default ' ', 
     originalcost decimal(12,2) not null default 0, 
     currentvalue decimal(12,2) not null default 0, 
     boughtfrom varchar(20) not null default ' ', 
     instock tinyint(1) not null default 0, 
     currentuser varchar(20) not null default ' ',
     userphonenum varchar(13) not null default ' ',
     boughtdate datetime not null default '0000-00-00'
 );
相关问题