这个 table 不能在 oracle apex 中工作 有人可以帮我弄清楚为什么吗

this table isnt working in oracle apex can someone help me figure out why

这个 table 在 oracle apex 中不工作 有人能帮我弄清楚为什么吗

CREATE TABLE booking (
Booking_ID int GENERATED BY DEFAULT AS IDENTITY  NOT NULL,
Guest_ID int NOT NULL,
Room_ID int  NOT NULL,
Employee_ID int NOT NULL,
booking_date date NOT NULL,
booking_time timestamp NOT NULL, 
booking_desc varchar(100) NOT NULL,
constraint GuestID_FK  references guest(Guest_ID),
constraint RoomID_FK  references room(Room_ID),
constraint EmployeeID_FK  references employee(Employee_ID),
primary key(Booking_ID));

如果您指定在 运行 此代码时遇到的错误,这将有所帮助。

由于 table 引用了其他一些 table,我将首先创建它们(这样外键约束就不会因此而失败);我将创建仅包含主键列的 dummy tables:

SQL> create table guest (guest_id int primary key);

Table created.

SQL> create table room (room_id int primary key);

Table created.

SQL> create table employee (employee_id int primary key);

Table created.

好的;现在,您的代码:

SQL> CREATE TABLE booking (
  2  Booking_ID int GENERATED BY DEFAULT AS IDENTITY  NOT NULL,
  3  Guest_ID int NOT NULL,
  4  Room_ID int  NOT NULL,
  5  Employee_ID int NOT NULL,
  6  booking_date date NOT NULL,
  7  booking_time timestamp NOT NULL,
  8  booking_desc varchar(100) NOT NULL,
  9  constraint GuestID_FK  references guest(Guest_ID),
 10  constraint RoomID_FK  references room(Room_ID),
 11  constraint EmployeeID_FK  references employee(Employee_ID),
 12  primary key(Booking_ID));
constraint GuestID_FK  references guest(Guest_ID),
                                       *
ERROR at line 9:
ORA-00907: missing right parenthesis


SQL>

啊哈。那么,它有什么问题呢?它是一个大纲约束子句,必须遵循一定的语法。

SQL> create table booking (
  2  booking_id   int generated by default as identity,
  3  guest_id     int not null,
  4  room_id      int not null,
  5  employee_id  int not null,
  6  booking_date date not null,
  7  booking_time timestamp not null,
  8  booking_desc varchar2(100) not null,
  9  constraint guestid_fk    foreign key (guest_id)    references guest(guest_id),
 10  constraint roomid_fk     foreign key (room_id)     references room(room_id),
 11  constraint employeeid_fk foreign key (employee_id) references employee(employee_id),
 12  constraint booking_pk primary key (booking_id));

Table created.

SQL>

或者,您可以使用内联约束:

SQL> create table booking (
  2  booking_id   int generated by default as identity constraint pk_book primary key,
  3  guest_id     int constraint fk_book_guest references guest (guest_id) not null,
  4  room_id      int constraint fk_book_room  references room (room_id) not null,
  5  employee_id  int constraint fk_book_emp   references employee (employee_id) not null,
  6  booking_date date not null,
  7  booking_time timestamp not null,
  8  booking_desc varchar2(100) not null);

Table created.

注意

  • 您不必为主键列指定 NOT NULL 约束;根据定义他们不能接受 null
  • 给约束命名总是一个好主意;您不必这样做,但 Oracle 随后会创建 system-generated 名称(例如 SYS_C008464),这些名称告诉您 nothing
  • Oracle 推荐我们使用 varchar2 而不是 varchar 数据类型
  • 您可能不需要 booking_datebooking_time;一列可以: booking_date date as date Oracle 中的数据类型同时包含日期和时间; timestamp - 如果您只想将它​​用于时间 - 不应该用于该目的,因为它不包含时间;它包含日期和时间(带小数秒)。您需要如此精确地了解预订时间吗?不知何故,我对此表示怀疑