简体   繁体   中英

Try to create a table in sql oracle 11g

I'm trying to create a table, but it kept giving me error msg "invalid identifier", I searched again and again, couldn't figure out what's wrong with my code..... please help.....here is my code..

create table ownership
(
   oID number not null,
   dID number not null,
   start date,
   end date,
   primary key (oID,dID)
   foreign key (oID) references owner(oID),
   foreign key (dID) references dogs(dID)
);

I imported dogs table from nyc open data, and I also created owner table.....The table names are not wrong, I checked over and over again, the oID in table owner is primary key, so does dID in table dogs..... I tried to delete not null constraint, same error msg, I tried to put constraint pk_ownership before primary key, still get the same error msg.....I really couldn't figure out why... This is for a school project, I'm super super new to sql, it's my 2nd day doing sql......if my question is stupid, please bare me.....Thank you!

You have two major errors:

  • you are missing a comma (on the primary)
  • start is a reserved word

end is also a keyword, so I would discourage using that as well.

I would suggest something like:

create table ownership (
   oID number not null,
   dID number not null,
   ownership_start date,
   ownership_end date,
   primary key (oID, dID),
   foreign key (oID) references owner(oID),
   foreign key (dID) references dogs(dID)
);

In Oracle start is a reserved word. You can use it if you quote it, as in:

create table ownership
(
   oID number not null,
   dID number not null,
   "start" date,
   end date,
   primary key (oID,dID),
   foreign key (oID) references owner(oID),
   foreign key (dID) references dogs(dID)
);

Or... you can simply use a different name such as start_ownership .

PS: You also had a minor syntax error. You had forgotten a comma at the end of the primary key definition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM