user1817081 user1817081 - 1 month ago 12
SQL Question

SQL Create table missing parenthesis

I am running these two commands to create new tables, but the second create table command keeps giving me error.

CREATE TABLE TEAMSTADIUM(
stadium_name varchar2(40) not null,
stadium_max_capcity number(10) not null,
stadium_field_serface varchar2(40) not null,
stadium_year_built number(4) not null,
stadium_location varchar2(40) not null,
Primary KEY(stadium_name)
)


CREATE TABLE TEAMINFO(
team_name varchar2(40) not null,
team_owner varchar2(40) not null,
team_coach varchar2(40) not null,
team_created Date() not null,
PRIMARY KEY(team_name)
foreign key(stadium_name) references TEAMSTADIUM(stadium_name)
)

Answer

Your TEAMINFO table references TEAMSTADIUM.stadium_name, but has no such column of its own. Add it, and ensure it has exactly the same data type as the parent table:

CREATE TABLE TEAMINFO(
  team_name varchar2(40) not null,
  team_owner varchar2(40) not null,
  team_coach varchar2(40) not null,
  team_created Date not null,
  -- Remove ()    ^^
  -- This column must exist in both tables
  stadium_name varchar2(40) not null,
  PRIMARY KEY(team_name),
  -- missing comma     ^^
  foreign key(stadium_name) references TEAMSTADIUM(stadium_name)
)

After applying the three modifications above, it will execute correctly: http://sqlfiddle.com/#!4/883a4

Comments