2017-11-18 58 views
-2

我想在sql server中創建幾個表,但是我得到了下面的錯誤。你能幫助我嗎?在sql server中創建表時出現錯誤(關鍵字'PRIMARY'附近的語法不正確)

This error

CREATE TABLE AIRPORT 
(
    Airport_code NVARCHAR(10) PRIMARY KEY, 
    Name NVARCHAR(25) NOT NULL, 
    City NVARCHAR(25) NOT NULL, 
    State NVARCHAR(25) NOT NULL 
); 

CREATE TABLE FLIGHT 
(
    Flight_number NVARCHAR(15) PRIMARY KEY, 
    Airline NVARCHAR(25) NOT NULL, 
    Weekdays INTEGER DEFAULT 0 
); 

CREATE TABLE FLIGHT_LEG 
(
    Flight_number NVARCHAR(15) NOT NULL, 
    Leg_number INTEGER NOT NULL, 
    Departure_airport_code NVARCHAR(10) NOT NULL, 
    Scheduled_departure_time TIME NOT NULL, 
    Arrival_airport_code NVARCHAR(10) NOT NULL, 
    Scheduled_arrival_time TIME NOT NULL, 
    CONSTRAINT PRIMARY KEY 
     Pk_Flight_Leg (Flight_number, Leg_number), 
    CONSTRAINT FOREIGN KEY 
     Fk_Flight_Leg_Flight (Flight_number) REFERENCES FLIGHT (Flight_number) 
      ON UPDATE CASCADE ON DELETE CASCADE 
); 

CREATE TABLE FARE 
(
    Flight_number NVARCHAR(15) NOT NULL, 
    Fare_code NVARCHAR(15) NOT NULL, 
    Amount DECIMAL(10,2) NOT NULL, 
    Restrictions NVARCHAR(50), 
    CONSTRAINT PRIMARY KEY 
     Pk_Fare (Flight_number, Fare_code), 
    CONSTRAINT FOREIGN KEY 
     Fk_Fare_Flight (Flight_number) REFERENCES FLIGHT (Flight_number) 
      ON UPDATE CASCADE ON DELETE CASCADE 
); 

CREATE TABLE AIRPLANE_TYPE 
(
    Airplane_type_name NVARCHAR(25) PRIMARY KEY, 
    Max_seats INTEGER NOT NULL, 
    Company NVARCHAR(25) 
); 

CREATE TABLE AIRPLANE 
(
    Airplane_id NVARCHAR(25) PRIMARY KEY, 
    Total_number_of_seats INTEGER NOT NULL, 
    Airplane_type NVARCHAR(25) NOT NULL, 
    CONSTRAINT FOREIGN KEY 
     Fk_Airplane_Airplane_Type (Airplane_type) REFERENCES    AIRPLANE_TYPE (Airplane_type_name) 
      ON UPDATE CASCADE ON DELETE CASCADE 
); 

CREATE TABLE LEG_INSTANCE 
(
    Flight_number NVARCHAR(15) NOT NULL, 
    Leg_number INTEGER NOT NULL, 
    Leg_instance_date Date NOT NULL, 
    Number_of_available_seats INTEGER, 
    Airplane_id NVARCHAR(25) NOT NULL, 
    Departure_airport_code NVARCHAR(10) NOT NULL, 
    Departure_time TIME NOT NULL, 
    Arrival_airport_code NVARCHAR(10) NOT NULL, 
    Arrival_time TIME NOT NULL, 
    CONSTRAINT PRIMARY KEY 
     Pk_Leg_Instance (Flight_number, Leg_number, Leg_instance_date), 
    CONSTRAINT FOREIGN KEY 
     Fk_Leg_Instance_Flight_Leg (Flight_number, Leg_number) 
     REFERENCES FLIGHT_LEG (Flight_number, Leg_number) 
     ON UPDATE CASCADE ON DELETE CASCADE, 
    CONSTRAINT FOREIGN KEY 
     Fk_Leg_Instance_Airplane (Airplane_id) 
     REFERENCES AIRPLANE (Airplane_id) 
     ON UPDATE CASCADE ON DELETE CASCADE 
); 

CREATE TABLE CAN_LAND 
(
    Airplane_type_name NVARCHAR(25) NOT NULL, 
    Airport_code NVARCHAR(10) NOT NULL, 
    CONSTRAINT PRIMARY KEY 
     Pk_Can_Land (Airplane_type_name, Airport_code), 
    CONSTRAINT FOREIGN KEY 
     Fk_Can_Land_Airplane_Type (Airplane_type_name) 
     REFERENCES AIRPLANE_TYPE (Airplane_type_name) 
     ON UPDATE CASCADE ON DELETE CASCADE, 
    CONSTRAINT FOREIGN KEY 
     Fk_Can_Land_Airport (Airport_code) 
     REFERENCES AIRPORT (Airport_code) 
     ON UPDATE CASCADE ON DELETE CASCADE 
); 

CREATE TABLE SEAT_RESERVATION 
(
    Flight_number NVARCHAR(15) NOT NULL, 
    Leg_number INTEGER NOT NULL, 
    Leg_instance_date Date NOT NULL, 
    Seat_number INTEGER NOT NULL, 
    Customer_name NVARCHAR(50) NOT NULL, 
    Customer_phone NVARCHAR(20), 
    CONSTRAINT PRIMARY KEY 
     Pk_Seat_Reservation (Flight_number, Leg_number, Leg_instance_date, Seat_number), 
    CONSTRAINT FOREIGN KEY 
     Fk_Seat_Reservation_Leg_Instance (Flight_number, Leg_number, Leg_instance_date) 
     REFERENCES LEG_INSTANCE (Flight_number, Leg_number, Leg_instance_date)   
); 

我得到這些錯誤:

消息156,級別15,狀態1,行關鍵字近23
不正確的語法 '主要'。

Msg 156,Level 15,State 1,Line 37
關鍵字'PRIMARY'附近的語法不正確。

Msg 156,Level 15,State 1,Line 56
關鍵字'FOREIGN'附近的語法不正確。

Msg 156,Level 15,State 1,Line 73
關鍵字'PRIMARY'附近的語法不正確。

Msg 156,Level 15,State 1,Line 90
關鍵字'PRIMARY'附近的語法不正確。

Msg 156,Level 15,State 1,Line 111
關鍵字'PRIMARY'附近的語法不正確。

+1

您應該首先命名FK約束 – Sami

+0

例如'CONSTRAINT Pk_Flight_Leg PRIMARY KEY(Flight_number,Leg_number),' – Sami

+0

okey nice謝謝 – BRKZKN

回答

0

您必須命名CONSTRAINT首先你寫CONSTRAINT關鍵字剛過:

CREATE TABLE AIRPORT (
    Airport_code NVARCHAR(10) PRIMARY KEY, 
    Name NVARCHAR(25) NOT NULL, 
    City NVARCHAR(25) NOT NULL, 
    State NVARCHAR(25) NOT NULL 
); 



CREATE TABLE FLIGHT (
    Flight_number NVARCHAR(15) PRIMARY KEY, 
    Airline NVARCHAR(25) NOT NULL, 
    Weekdays INTEGER DEFAULT 0 
); 



CREATE TABLE FLIGHT_LEG (
    Flight_number NVARCHAR(15) NOT NULL, 
    Leg_number INTEGER NOT NULL, 
    Departure_airport_code NVARCHAR(10) NOT NULL, 
    Scheduled_departure_time TIME NOT NULL, 
    Arrival_airport_code NVARCHAR(10) NOT NULL, 
    Scheduled_arrival_time TIME NOT NULL, 
    CONSTRAINT Pk_Flight_Leg PRIMARY KEY (Flight_number, Leg_number), 
    CONSTRAINT Fk_Flight_Leg_Flight FOREIGN KEY (Flight_number) 
    REFERENCES 
     FLIGHT (Flight_number) 
    ON UPDATE CASCADE ON DELETE CASCADE 
); 



CREATE TABLE FARE (
    Flight_number NVARCHAR(15) NOT NULL, 
    Fare_code NVARCHAR(15) NOT NULL, 
    Amount DECIMAL(10,2) NOT NULL, 
    Restrictions NVARCHAR(50), 
    CONSTRAINT Pk_Fare PRIMARY KEY 
     (Flight_number, Fare_code), 
    CONSTRAINT Fk_Fare_Flight FOREIGN KEY 
     (Flight_number) 
    REFERENCES 
     FLIGHT (Flight_number) 
    ON UPDATE CASCADE ON DELETE CASCADE 
); 



CREATE TABLE AIRPLANE_TYPE (
    Airplane_type_name NVARCHAR(25) PRIMARY KEY, 
    Max_seats INTEGER NOT NULL, 
    Company NVARCHAR(25) 
); 



CREATE TABLE AIRPLANE (
    Airplane_id NVARCHAR(25) PRIMARY KEY, 
    Total_number_of_seats INTEGER NOT NULL, 
    Airplane_type NVARCHAR(25) NOT NULL, 
    CONSTRAINT Fk_Airplane_Airplane_Type FOREIGN KEY 
     (Airplane_type) 
    REFERENCES 
     AIRPLANE_TYPE (Airplane_type_name) 
    ON UPDATE CASCADE ON DELETE CASCADE 
); 



CREATE TABLE LEG_INSTANCE (
    Flight_number NVARCHAR(15) NOT NULL, 
    Leg_number INTEGER NOT NULL, 
    Leg_instance_date Date NOT NULL, 
    Number_of_available_seats INTEGER, 
    Airplane_id NVARCHAR(25) NOT NULL, 
    Departure_airport_code NVARCHAR(10) NOT NULL, 
    Departure_time TIME NOT NULL, 
    Arrival_airport_code NVARCHAR(10) NOT NULL, 
    Arrival_time TIME NOT NULL, 
    CONSTRAINT Pk_Leg_Instance PRIMARY KEY 
     (Flight_number, Leg_number, Leg_instance_date), 
    CONSTRAINT Fk_Leg_Instance_Flight_Leg FOREIGN KEY 
     (Flight_number, Leg_number) 
    REFERENCES 
     FLIGHT_LEG (Flight_number, Leg_number) 
    ON UPDATE CASCADE ON DELETE CASCADE, 
    CONSTRAINT Fk_Leg_Instance_Airplane FOREIGN KEY 
     (Airplane_id) 
    REFERENCES 
     AIRPLANE (Airplane_id) 
    ON UPDATE CASCADE ON DELETE CASCADE 
); 



CREATE TABLE CAN_LAND (
    Airplane_type_name NVARCHAR(25) NOT NULL, 
    Airport_code NVARCHAR(10) NOT NULL, 
    CONSTRAINT Pk_Can_Land PRIMARY KEY 
     (Airplane_type_name, Airport_code), 
    CONSTRAINT Fk_Can_Land_Airplane_Type FOREIGN KEY 
     (Airplane_type_name) 
    REFERENCES 
     AIRPLANE_TYPE (Airplane_type_name) 
    ON UPDATE CASCADE ON DELETE CASCADE, 
    CONSTRAINT Fk_Can_Land_Airport FOREIGN KEY 
     (Airport_code) 
    REFERENCES 
     AIRPORT (Airport_code) 
    ON UPDATE CASCADE ON DELETE CASCADE 
); 



CREATE TABLE SEAT_RESERVATION (
    Flight_number NVARCHAR(15) NOT NULL, 
    Leg_number INTEGER NOT NULL, 
    Leg_instance_date Date NOT NULL, 
    Seat_number INTEGER NOT NULL, 
    Customer_name NVARCHAR(50) NOT NULL, 
    Customer_phone NVARCHAR(20), 
    CONSTRAINT Pk_Seat_Reservation PRIMARY KEY 
     (Flight_number, Leg_number, Leg_instance_date, Seat_number), 
    CONSTRAINT Fk_Seat_Reservation_Leg_Instance FOREIGN KEY 
     (Flight_number, Leg_number, Leg_instance_date) 
    REFERENCES 
     LEG_INSTANCE (Flight_number, Leg_number, Leg_instance_date) 

); 
1

你必須在代碼中的兩個問題。

  1. 當您想命名約束時,使用CONSTRAINT關鍵字。
  2. 列出主鍵和外鍵列時不要重複表名。

例如,對於FLIGHT_LEG,定義應該是這樣的:

CREATE TABLE FLIGHT_LEG (
    Flight_number NVARCHAR(15) NOT NULL, 
    Leg_number INTEGER NOT NULL, 
    Departure_airport_code NVARCHAR(10) NOT NULL, 
    Scheduled_departure_time TIME NOT NULL, 
    Arrival_airport_code NVARCHAR(10) NOT NULL, 
    Scheduled_arrival_time TIME NOT NULL, 
    PRIMARY KEY (Flight_number, Leg_number), 
    FOREIGN KEY (Flight_number) REFERENCES FLIGHT (Flight_number) 
      ON UPDATE CASCADE ON DELETE CASCADE 
); 

我會鼓勵你想想合成鍵表格 - 數字標識列,而不是字符串。數字鍵在索引和外鍵關係中效率更高。

相關問題