ERROR导入sql数据库

时间:2013-11-13 17:01:12

标签: mysql

我是sql数据库的初学者。我正在尝试将数据库导入phpmyadmin,我收到一条错误消息:

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法   在'DEFAULT_AUTOINCREMENT附近,       firstName varchar(30)NOT_NULL,       第2行的lastName var'

我的sql代码看起来像这样:

use myAddressBook
;

create table names (
    personID int DEFAULT_AUTOINCREMENT,
    firstName varchar (30) NOT_NULL,
    lastName varchar (30) NOT_NULL,
    constraint pk_name primary_key (personID)
) 
;
create table addresses (
    addressID int DEFAULT_AUTOINCREMENT,
        personID int NOT_NULL,
    address1 varchar (50),
    address2 varchar (50),
    city varchar (30),
    state varchar (2),
    zipcode varchar (10),
    constraint fk_addresses foreign_key (personID)
        references names (personID),
    constraint pk_addresses primary_key (addressID)
) 
;
create table phoneNumbers (
    phoneID int DEFAULT_AUTOINCREMENT,
        personID int NOT_NULL,
    phoneNumber varchar (20),
    constraint fk_phoneNumbers foreign_key (personID)
        references names (personID),
    constraint pk_phoneNumbers primary_key (phoneID)
) 
;
create table emailAddresses (
    emailID int DEFAULT_AUTOINCREMENT,
        personID int NOT_NULL,
    emailAddress varchar (50),
    constraint fk_emailAddresses foreign_key (personID)
        references names (personID),
    constraint pk_emailAddresses primary_key (emailID)
) 
;

有谁能请我指出正确的方向,我哪里出错了。 感谢。

3 个答案:

答案 0 :(得分:2)

您的代码存在一些问题。你必须使用正确的MySQL关键词:

        Wrong          |     Correct
=========================================
DEFAULT_AUTOINCREMENT ===> AUTO_INCREMENT
NOT_NULL              ===> NOT NULL
FOREIGN_KEY           ===> FOREIGN KEY
PRIMARY_KEY           ===> PRIMARY KEY

应该是这样的:

USE myAddressBook
;

CREATE TABLE NAMES (
    personID INT AUTO_INCREMENT,
    firstName VARCHAR (30) NOT NULL,
    lastName VARCHAR (30) NOT NULL,
    CONSTRAINT pk_name PRIMARY KEY (personID)
) 
;
CREATE TABLE addresses (
    addressID INT AUTO_INCREMENT,
        personID INT NOT NULL,
    address1 VARCHAR (50),
    address2 VARCHAR (50),
    city VARCHAR (30),
    state VARCHAR (2),
    zipcode VARCHAR (10),
    CONSTRAINT fk_addresses FOREIGN KEY (personID)
        REFERENCES NAMES (personID),
    CONSTRAINT pk_addresses PRIMARY KEY (addressID)
) 
;
CREATE TABLE phoneNumbers (
    phoneID INT AUTO_INCREMENT,
        personID INT NOT NULL,
    phoneNumber VARCHAR (20),
    CONSTRAINT fk_phoneNumbers FOREIGN KEY (personID)
        REFERENCES NAMES (personID),
    CONSTRAINT pk_phoneNumbers PRIMARY KEY (phoneID)
) 
;
CREATE TABLE emailAddresses (
    emailID INT AUTO_INCREMENT,
        personID INT NOT NULL,
    emailAddress VARCHAR (50),
    CONSTRAINT fk_emailAddresses FOREIGN KEY (personID)
        REFERENCES NAMES (personID),
    CONSTRAINT pk_emailAddresses PRIMARY KEY (emailID)
) 
;

答案 1 :(得分:1)

尝试将所有DEFAULT_AUTOINCREMENT更改为AUTO_INCREMENT,将所有NOT_NULL更改为NOT NULL,将primary_key更改为primary key和{{1} } foreign_key

一般情况下,这是因为代码与MySQL支持的语法不匹配。

答案 2 :(得分:0)

我不是php的专家,但我也遇到过这种问题。我做的是: 1.使用phpmyadmin导出表。 2.复制c:\ xampp \ mysql \ bin DIRECTORY中的导出表 3.转到COMMAND PROMPT并输入 光盘 5. cd c:\ xampp \ mysql \ bin 6.执行这个简单的命令
    mysql -u root -p newdatabase< names.sql

       note : newdatabase is DATABASE_NAME
              names.sql is table.sql that you exported from your database.

我希望它可以帮到你。