1

我正在尝试建立一个简单的数据库,以便我可以练习我的 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 

它一定很简单,但我不知道是什么问题。有人可以帮助我吗?

4

3 回答 3

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');

或者

-- 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');
....
于 2012-09-01T15:22:23.867 回答
3
telefone VARCHAR (12);

应该

telefone VARCHAR (12),

祝你好运!

于 2012-09-01T15:23:05.770 回答
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不存在,您需要使用测试(来自 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)
于 2012-09-01T15:44:46.360 回答