Home > Software engineering >  I can't figure out the errors in my sql file
I can't figure out the errors in my sql file

Time:02-12

I'm using replit for making databases and executing sql statements. I was good at it but now that I'm working with SQL after a long time, I can't recall most of the things I have learned. This file was a part of my lab manual for DBMS. I wrote it all myself but I have no idea why these errors are happening. This are syntactical errors and I tried reading about them but I can't figure out what's actually wrong with this.

Any help will be much appreciated. As I will be able to finish this part of my lab exercise.

Thank you!

The errors are:

Error: near line 27: near "Cat_code": syntax error

Error: near line 38: near "ticket_no": syntax error

Error: near line 61: near "name": syntax error

Error: near line 71: near "place_id": syntax error

The code is here:

CREATE TABLE Category_Header 
(
    Cat_code INTEGER PRIMARY KEY NOT NULL, 
    Cate_desc TEXT NOT NULL
);

CREATE TABLE route_header 
(
    Route_id INTEGER PRIMARY KEY NOT NULL,
    Route_no INTEGER NOT NULL,
    Cat_code INTEGER NOT NULL,
    Origin TEXT NOT NULL,
    Destination TEXT NOT NULL,
    Fare INTEGER NOT NULL,
    Distance INTEGER NOT NULL,
    Capacity INTEGER NOT NULL,

    FOREIGN KEY (Cat_code)
        REFERENCES Category_Header(Cat_code)
);

CREATE TABLE place_header 
(
    place_id int PRIMARY KEY NOT NULL,
    place_name text NOT NULL,
    place_address varchar(100) NOT NULL,
    bus_station text NOT NULL
);

CREATE TABLE fleet_header 
(
    fleet_id int PRIMARY KEY NOT NULL,
    day date NOT NULL,
    Route_id int NOT NULL,

    FOREIGN KEY (Route_id)
        REFERENCES route_header(Route_id),

    Cat_code integer NOT NULL,

    FOREIGN KEY (Cat_code)
        REFERENCES Category_Header(Cat_code)
);

CREATE TABLE ticket_Header 
(
    fleet_id int NOT NULL,

    FOREIGN KEY (fleet_id)
        REFERENCES fleet_header(fleet_id),

    ticket_no int PRIMARY KEY NOT NULL,
    doi date NOT NULL,
    dot date NOT NULL,
    time_travel time NOT NULL,
    board_place text NOT NULL,
    origin text NOT NULL,

    FOREIGN KEY(origin)
         REFERENCES route_header(origin),

    destination text NOT NULL,

    FOREIGN KEY(destination)
        REFERENCES route_header(destination),
    adults int NOT NULL,
    children int NOT NULL,
    total_fare int NOT NULL,
    Route_id int NOT NULL,

    FOREIGN KEY (Route_id)
        REFERENCES route_header(Route_id)
);

CREATE TABLE ticket_detail 
(
    ticket_no int NOT NULL,
    FOREIGN KEY(ticket_no)
        REFERENCES ticket_Header(ticket_no),
    name text NOT NULL,
    sex char NOT NULL,
    age int NOT NULL,
    fare float NOT NULL
);

CREATE TABLE route_detail
(
    Route_id int NOT NULL,
    FOREIGN KEY (Route_id)
         REFERENCES route_header (Route_id),
    place_id int NOT NULL,
    FOREIGN KEY (place_id)
         REFERENCES place_header (place_id),
    nonstop char NOT NULL
);

CodePudding user response:

Try moving your FOREIGN KEY after your variables.

CREATE TABLE Category_Header 
(
    Cat_code INTEGER PRIMARY KEY NOT NULL, 
    Cate_desc TEXT NOT NULL
);

CREATE TABLE route_header 
(
    Route_id INTEGER PRIMARY KEY NOT NULL,
    Route_no INTEGER NOT NULL,
    Cat_code INTEGER NOT NULL,
    Origin TEXT NOT NULL,
    Destination TEXT NOT NULL,
    Fare INTEGER NOT NULL,
    Distance INTEGER NOT NULL,
    Capacity INTEGER NOT NULL,

    FOREIGN KEY (Cat_code)
        REFERENCES Category_Header(Cat_code)
);

CREATE TABLE place_header 
(
    place_id int PRIMARY KEY NOT NULL,
    place_name text NOT NULL,
    place_address varchar(100) NOT NULL,
    bus_station text NOT NULL
);

CREATE TABLE fleet_header 
(
    fleet_id int PRIMARY KEY NOT NULL,
    day date NOT NULL,
    Route_id int NOT NULL,
    Cat_code integer NOT NULL,
 FOREIGN KEY (Route_id)
        REFERENCES route_header(Route_id)
    FOREIGN KEY (Cat_code)
        REFERENCES Category_Header(Cat_code)
);

CREATE TABLE ticket_Header 
(
    fleet_id int NOT NULL,
    ticket_no int PRIMARY KEY NOT NULL,
    doi date NOT NULL,
    dot date NOT NULL,
    time_travel time NOT NULL,
    board_place text NOT NULL,
    origin text NOT NULL,
    destination text NOT NULL,
    adults int NOT NULL,
    children int NOT NULL,
    total_fare int NOT NULL,
    Route_id int NOT NULL,

    FOREIGN KEY (fleet_id)
        REFERENCES fleet_header(fleet_id)
    FOREIGN KEY(origin)
         REFERENCES route_header(origin)
    FOREIGN KEY(destination)
        REFERENCES route_header(destination)
    FOREIGN KEY (Route_id)
        REFERENCES route_header(Route_id)
);

CREATE TABLE ticket_detail 
(
    ticket_no int NOT NULL,
   
    name text NOT NULL,
    sex char NOT NULL,
    age int NOT NULL,
    fare float NOT NULL,
    FOREIGN KEY(ticket_no)
        REFERENCES ticket_Header(ticket_no)
);

CREATE TABLE route_detail
(
    Route_id int NOT NULL,
    place_id int NOT NULL,
    nonstop char NOT NULL,

    FOREIGN KEY (Route_id)
         REFERENCES route_header (Route_id)
    FOREIGN KEY (place_id)
         REFERENCES place_header (place_id)
);
  • Related