0

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

1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行的“DEFAULT_AUTOINCREMENT, firstName varchar (30) NOT_NULL, 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)
) 
;

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

4

3 回答 3

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)
) 
;
于 2013-11-13T17:07:50.323 回答
1

尝试改变所有的DEFAULT_AUTOINCREMENTtoAUTO_INCREMENT和所有的NOT_NULLtoNOT NULLprimary_keytoprimary keyforeign_keytoforeign key

一般是因为代码与 MySQL 支持的语法不匹配。

于 2013-11-13T17:03:04.740 回答
0

I am not an expert in php but i also experience this kind of problem . what i did was : 1. export the tables using phpmyadmin. 2. copy the exported tables inside the c:\xampp\mysql\bin DIRECTORY 3. go to COMMAND PROMPT and type 4. cd\ 5. cd c:\xampp\mysql\bin 6. execute this simple command
mysql -u root -p newdatabase < names.sql

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

i hope it can help you.

于 2016-01-15T16:33:01.597 回答