一段简单的代码中的Mysql错误

时间:2012-09-01 15:20:45

标签: mysql sql

我正在尝试使用一个简单的数据库,这样我就可以使用我的PHP,我正在从一本书中复制它,这是代码:

USE testeDB;

CREATE TABLE test(
id INT not null AUTO_INCREMENT,
nome VARCHAR (25),
telefone VARCHAR (12);
PRIMARY KEY (id),
);

INSERT INTO teste VALUES ('', 'Joaquim', '1111');
INSERT INTO teste VALUES ('', 'Carlos', '2233');
INSERT INTO teste VALUES ('', 'Antonio', '3333');
INSERT INTO teste VALUES ('', 'Roque Santeiro', '6969');

我使用phpmyadmin创建数据库,但是当我运行该代码来创建表时,我收到以下错误:

#1064 - 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 '' at line 4 

它必须是简单的东西,但我无法确定问题是什么。 有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:3)

创建表

时出现

语法错误

CREATE TABLE test(
id INT not null AUTO_INCREMENT,
nome VARCHAR (25),
telefone VARCHAR (12),  -- <=== here
PRIMARY KEY (id),
);

因为id为auto incremented,你可以这样做,

-- pass NULL value to column ID since the server automatically
-- assigns the value of the ID for it. also notice that you have specify 
-- column ID as the primary so it won't have duplicated value for ID
-- NULL is NOT THE SAME as empty string
-- NULL is NOTHING (or unknown) and 
-- Empty string is a zero-length string
INSERT INTO test VALUES (null, 'Joaquim', '1111');    
INSERT INTO test VALUES (null, 'Carlos', '2233');
INSERT INTO test VALUES (null, 'Antonio', '3333');
INSERT INTO test VALUES (null, 'Roque Santeiro', '6969');

OR

-- explicitly supply the column you want to insert value
INSERT INTO test (nome, telephone) VALUES ('Joaquim', '1111');
INSERT INTO test (nome, telephone) VALUES ('Carlos', '2233');
INSERT INTO test (nome, telephone) VALUES ('Antonio', '3333');
....

答案 1 :(得分:3)

telefone VARCHAR (12);

应该是

telefone VARCHAR (12),
祝你好运!

答案 2 :(得分:0)

另一个问题是:

CREATE TABLE test(
id INT not null AUTO_INCREMENT,
nome VARCHAR (25),
telefone VARCHAR (12);
PRIMARY KEY (id),
);

INSERT INTO teste VALUES ('', 'Joaquim', '1111');
INSERT INTO teste VALUES ('', 'Carlos', '2233');
INSERT INTO teste VALUES ('', 'Antonio', '3333');
INSERT INTO teste VALUES ('', 'Roque Santeiro', '6969')

teste不存在,您需要使用test(来自CREATE TABLE语法),插入它看起来像:

   INSERT INTO test VALUES ('Joaquim', '1111');
    INSERT INTO test VALUES ('Carlos', '2233');
    INSERT INTO test VALUES ('Antonio', '3333');
    INSERT INTO test VALUES ('Roque Santeiro', '6969')


-- Not need to put the '' either since the value is auto incrementing (more here:http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html)