ERROR 1064(42000):在第10行的')'附近使用正确的语法

时间:2017-01-05 04:01:36

标签: php mysql macos

所以我正在尝试使用MYSQL创建一个数据库,它似乎没有将表添加到终端上的mysql,以下错误代码显示:

**ERROR 1064 (42000): 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 10
ERROR 1064 (42000): 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 9
ERROR 1064 (42000): 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 '(2),
PRIMARY KEY (guest_id),
FOREIGN KEY (res_nr)
)' at line 10
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
mysql> SHOW TABLES;
+---------------------------------+
| Tables_in_xxxxxx_coursework_1 |
+---------------------------------+
| Reservations                    |
| Room                            |
+---------------------------------+
2 rows in set (0.00 sec)**

但是这段代码工作得更早,因为当我输入“SHOW TABLES”时它显示前两个表房间和预订所以故障不能在前两个表中。

CREATE TABLE Room (
    id INT AUTO_INCREMENT,
    room_id INT (3),
    room_type VARCHAR (15),
    guest_id INT (8),
    res_nr INT (6),
    price DECIMAL (6,2) unsigned,
    extras_id INT (2),
    PRIMARY KEY (id)
);

CREATE TABLE Reservations (
    id INT AUTO_INCREMENT,
    res_no INT (6),
    guest_id INT (8),
    hotel_branch INT (5),
    room_genre VARCHAR (12),
    extras_id INT (2),
    room_id int (3),
    PRIMARY KEY (id),
    FOREIGN KEY (room_id)
    REFERENCES Room (id)
);

CREATE TABLE Employees (
    id INT AUTO_INCREMENT,
    employee_id INT (6),
    ni_id INT (9),
    first_name VARCHAR (25),
    surname VARCHAR (25),
    address VARCHAR (100),
    PRIMARY KEY (id),
    FOREIGN KEY (first_name)
);

CREATE TABLE Hotel (
    id INT AUTO_INCREMENT,
    hotel_branch INT (5),
    employee_id INT (6),
    hotel_name CHAR (20),
    town VARCHAR(20),
    num_rooms INT (3),
    PRIMARY KEY (id),
);

CREATE TABLE Guest (
    id INT AUTO_INCREMENT,
    guest_id INT (8),
    res_nr INT(6),
    first_name CHAR (25),
    surname CHAR (25),
    phone_no INT(12),
    email VARCHAR (25),
    room_id INT(3),
    extras_id(2),
    PRIMARY KEY (guest_id)
    FOREIGN KEY (res_nr)
);

CREATE TABLE Bill (
    id INT AUTO_INCREMENT,
    invoice_id INT (9),
    guest_id INT (8),
    room_charge DECIMAL (8,2) unsigned,
    extra_charge DECIMAL (6,2) unsigned,
    PRIMARY KEY (invoice_id),
    FOREIGN KEY (guest_id)
    REFERENCES Guest (id)
);

1 个答案:

答案 0 :(得分:0)

你是构造错误的表结构

        CREATE TABLE Room (
        id INT AUTO_INCREMENT,
        room_id INT (3),
        room_type VARCHAR (15),
        guest_id INT (8),
        res_nr INT (6),
        price DECIMAL (6,2) unsigned,
        extras_id INT (2),
        PRIMARY KEY (id)
        );

        CREATE TABLE Guest ( 
        guest_id INT (8),
         res_nr INT(6), 
         first_name CHAR (25),
         surname CHAR (25), 
         phone_no INT(12),
         email VARCHAR (25),
         room_id INT(3), 
         extras_id int (2), 
         PRIMARY KEY (guest_id), 
         CONSTRAINT fk_res FOREIGN KEY (res_nr) REFERENCES Room (id) )

如上所述,我使用房间ID作为房间表的主键创建了FK res_nr。你要做的是引用一个不起作用的整数值。 尝试详细了解Fk。[http://www.w3schools.com/sql/sql_foreignkey.asp]