SQL 1064基本数据库出错

时间:2015-02-03 11:45:54

标签: mysql sql database

您好我正在尝试生成这个数据库,它有一些一对多的关系,但每次运行它时都会给我这个错误,我无法弄清楚我做错了什么。

#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 'FOREIGN KEY (user_id)
            REFERENCES users(user_id)

            )' at line 7 

以下是我正在使用的代码:

CREATE TABLE IF NOT EXISTS users(
            user_id VARCHAR(50) NOT NULL,
            pass VARCHAR(50),
            first_name VARCHAR(255),
            last_name VARCHAR(255),
            dob VARCHAR(30),
            address VARCHAR(1024),
            phone int(11),
            lastlogin DATETIME,
            PRIMARY KEY (user_id)
            );
CREATE TABLE IF NOT EXISTS mailboxes(
            mailbox_id int(10),
            mailbox_name VARCHAR(50),
            mailbox_type VARCHAR(255),
            user_id VARCHAR(50),
            PRIMARY KEY (mailbox_id)
            FOREIGN KEY (user_id)
            REFERENCES users(user_id)

            );
CREATE TABLE IF NOT EXISTS contacts(
                contact_id VARCHAR(50)AUTO_INCREMENT,
                email_address VARCHAR(50),
                first_name VARCHAR(255),
                last_name VARCHAR(255),
                dob VARCHAR(30),
                address VARCHAR(1024),
                user_id VARCHAR(50),
                PRIMARY KEY (contact_id), 
                FOREIGN KEY (user_id)
                REFERENCES users(user_id)

                );      
CREATE TABLE IF NOT EXISTS messages(
                message_id VARCHAR(50) AUTO_INCREMENT,
                mailbox_id int(10),
                contact_id VARCHAR(50),
                user_id VARCHAR(50),
                fromto_email VARCHAR(50),
                date_recieved DATETIME,
                date_sent DATETIME,
                message_data VARCHAR(1024),
                message VARCHAR(10240),
                PRIMARY KEY (message_id), 
                FOREIGN KEY (mailbox_id)
                REFERENCES mailboxes(mailbox_id)
                FOREIGN KEY (contact_id)
                REFERENCES contacts(contact_id) 
                FOREIGN KEY (user_id)
                REFERENCES users(user_id)

                );  

CREATE TABLE IF NOT EXISTS flags(
                message_id VARCHAR(50),
                read_r VARCHAR(10),
                replied VARCHAR(10),
                sent VARCHAR(10),
                draft VARCHAR(10),
                user_id VARCHAR(50),
                PRIMARY KEY (message_id)
                FOREIGN KEY(message_id)
                REFERENCES messages(message_id)
                FOREIGN KEY (user_id)
                REFERENCES users(user_id)

                );

提前致谢。

1 个答案:

答案 0 :(得分:1)

你在忘记密钥之间忘记了多个逗号。比如这里

PRIMARY KEY (message_id),   <--here 
FOREIGN KEY (mailbox_id) REFERENCES mailboxes(mailbox_id)

您的contact_idmessage_id必须是int而不是varchar,因为您希望它们自动增加。

这是完整的脚本

CREATE TABLE IF NOT EXISTS users(
            user_id VARCHAR(50) NOT NULL,
            pass VARCHAR(50),
            first_name VARCHAR(255),
            last_name VARCHAR(255),
            dob VARCHAR(30),
            address VARCHAR(1024),
            phone int(11),
            lastlogin DATETIME,
            PRIMARY KEY (user_id)
            );
CREATE TABLE IF NOT EXISTS mailboxes(
            mailbox_id int(10),
            mailbox_name VARCHAR(50),
            mailbox_type VARCHAR(255),
            user_id VARCHAR(50),
            PRIMARY KEY (mailbox_id),
            FOREIGN KEY (user_id)
            REFERENCES users(user_id)

            );
CREATE TABLE IF NOT EXISTS contacts(
                contact_id int AUTO_INCREMENT,
                email_address VARCHAR(50),
                first_name VARCHAR(255),
                last_name VARCHAR(255),
                dob VARCHAR(30),
                address VARCHAR(1024),
                user_id VARCHAR(50),
                PRIMARY KEY (contact_id), 
                FOREIGN KEY (user_id)
                REFERENCES users(user_id)

                );      
CREATE TABLE IF NOT EXISTS messages(
                message_id int AUTO_INCREMENT,
                mailbox_id int(10),
                contact_id int,
                user_id VARCHAR(50),
                fromto_email VARCHAR(50),
                date_recieved DATETIME,
                date_sent DATETIME,
                message_data VARCHAR(1024),
                message VARCHAR(10240),
                PRIMARY KEY (message_id), 
                FOREIGN KEY (mailbox_id)
                REFERENCES mailboxes(mailbox_id),
                FOREIGN KEY (contact_id)
                REFERENCES contacts(contact_id) ,
                FOREIGN KEY (user_id)
                REFERENCES users(user_id)

                );  

CREATE TABLE IF NOT EXISTS flags(
                message_id int,
                read_r VARCHAR(10),
                replied VARCHAR(10),
                sent VARCHAR(10),
                draft VARCHAR(10),
                user_id VARCHAR(50),
                PRIMARY KEY (message_id),
                FOREIGN KEY(message_id)
                REFERENCES messages(message_id),
                FOREIGN KEY (user_id)
                REFERENCES users(user_id)

                );