-1

I am creating a sample IRTC database that includes tables ticket and train_ticket_fare. I created a table ticket with a column as ticket_class varchar(6).
In the table train_ticket_fare, I referenced it using this ticket_class references ticket.

I re-created the tables to check but couldn't find the error.

SQL> desc ticket
 Name                    Null?    Type
----------------------- -------- ----------------
 PNR_NO                  NOT NULL NUMBER(10) 
 TRANSACTIONID                    NUMBER(10)
 FROM_STATION                     VARCHAR2(20)
 TO_STATION                       VARCHAR2(20)
 DATE_OF_JOURNEY                  DATE
 TICKET_CLASS                     VARCHAR2(6) //this
 DATE_OF_BOOKING                  DATE
 TICKET_FARE                      NUMBER(8,2)
 TRAIN_NUMBER                     NUMBER(5)

SQL> create table Train_Ticket_fare
  2    (
  3      train_number        references train,
  4      ticket_class        references ticket, //this
  5      base_fare           number(7,2),
  6      reservation_charge  number(6,2),
  7      superfast_charge    number(6,2),
  8      other_charge        number(6,2),
  9      tatkal_charge       number(6,2),
 10      service_tax         number(6,2),
 11      primary key(train_number, ticket_class)
 12    );

Table created.

SQL> desc Train_Ticket_fare
 Name                    Null?    Type
 ----------------------- -------- ----------------
 TRAIN_NUMBER            NOT NULL NUMBER(5)
 TICKET_CLASS            NOT NULL NUMBER(10)  //this
 BASE_FARE                        NUMBER(7,2)
 RESERVATION_CHARGE               NUMBER(6,2)
 SUPERFAST_CHARGE                 NUMBER(6,2)
 OTHER_CHARGE                     NUMBER(6,2)
 TATKAL_CHARGE                    NUMBER(6,2)
 SERVICE_TAX                      NUMBER(6,2)

Resolved! Thanks @stickybit. The table Train_ticket_fare must have TICKET_CLASS as primary key which is to be referenced to the table Ticket. I did the opposite and referenced it wrong.

4

3 回答 3

0

也许你想要这样的东西:

create table train
( train_number        number(5) constraint train_pk primary key
);

create table ticket_class
( ticket_class        varchar2(6) constraint ticket_class_pk primary key
);

create table ticket
( pnr_no              number(10)  constraint ticket_pk primary key
, transactionid       number(10)
, from_station        varchar2(20)
, to_station          varchar2(20)
, date_of_journey     date
, ticket_class        references ticket_class
, date_of_booking     date
, ticket_fare         number(8,2)
, train_number        number(5)
);

create table train_ticket_fare
( train_number        references train
, ticket_class        references ticket_class
, base_fare           number(7,2)
, reservation_charge  number(6,2)
, superfast_charge    number(6,2)
, other_charge        number(6,2)
, tatkal_charge       number(6,2)
, service_tax         number(6,2)
, constraint train_ticket_fare primary key (train_number, ticket_class)
);

SQL> @desc train_ticket_fare
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 TRAIN_NUMBER                              NOT NULL NUMBER(5)
 TICKET_CLASS                              NOT NULL VARCHAR2(6)
 BASE_FARE                                          NUMBER(7,2)
 RESERVATION_CHARGE                                 NUMBER(6,2)
 SUPERFAST_CHARGE                                   NUMBER(6,2)
 OTHER_CHARGE                                       NUMBER(6,2)
 TATKAL_CHARGE                                      NUMBER(6,2)
 SERVICE_TAX                                        NUMBER(6,2)

具有相同名称的表和列 (TICKET_CLASS) 并不好。它有效,但可能会导致模棱两可和混乱。避免这种情况的一种方法是使用复数形式命名表,例如 TRAINS、TICKET_CLASSES、TICKETS。

于 2019-08-11T10:35:25.137 回答
0

这里的问题是由于引用。表 Train_ticket_fare 必须以 TICKET_CLASS 作为主键,该表要引用到表 Ticket。我做了相反的事情并错误地引用了它。这就是为什么它显示错误的数据类型。谢谢@stickybit 抱歉,如果我发布了错误的问题。我是平台新手。

于 2020-04-25T16:35:10.977 回答
0

子表foreign key中的一般会引用primary key父表中的a。

在你的情况下,我假设那TICKET_CLASSprimary keytable TRAIN

您可以根据您的业务逻辑更改primary keyand 。foreign key以下只是基于假设的示例:

CREATE TABLE TICKET (
    PNR_NO            NUMBER(10) NOT NULL,
    TRANSACTIONID     NUMBER(10),
    FROM_STATION      VARCHAR2(20),
    TO_STATION        VARCHAR2(20),
    DATE_OF_JOURNEY   DATE,
    TICKET_CLASS      VARCHAR2(6),
    DATE_OF_BOOKING   DATE,
    TICKET_FARE       NUMBER(8, 2),
    TRAIN_NUMBER      NUMBER(5),
    CONSTRAINT TICKET_PK PRIMARY KEY ( TICKET_CLASS )
);

CREATE TABLE TRAIN_TICKET_FARE (
    TRAIN_NUMBER         NUMBER(10)
        REFERENCES TRAIN ( TRAIN_NUMBER ), 
       -- DATA TYPE MUST BE SAME AS TRAIN_NUMBER OF TABLE TRAIN
    TICKET_CLASS         VARCHAR2(6)
        REFERENCES TICKET ( TICKET_CLASS ),  
       -- ASSUMING THAT ticket_class IS PRIMARY KEY OF TABLE ticket_class 
    BASE_FARE            NUMBER(7, 2),
    RESERVATION_CHARGE   NUMBER(6, 2),
    SUPERFAST_CHARGE     NUMBER(6, 2),
    OTHER_CHARGE         NUMBER(6, 2),
    TATKAL_CHARGE        NUMBER(6, 2),
    SERVICE_TAX          NUMBER(6, 2),
    PRIMARY KEY ( TRAIN_NUMBER,
                  TICKET_CLASS )
       -- below is the another way of defining the foreign key
       --CONSTRAINT Train_Ticket_fare_FK FOREIGN KEY (ticket_class) REFERENCES ticket(TICKET_CLASS)
);

表的列必须使用自己的数据类型定义,并且应该使用外键约束来引用,如示例中所示。(不是这样的ticket_class references ticket

我希望你能找到方向。

有关外键的更多信息,请参阅此链接

干杯!!

于 2019-08-11T04:26:26.877 回答