在Oracle Database 11g SQL中创建表时出错

时间:2015-11-24 22:34:27

标签: sql oracle ddl

我是SQL的新手,我正在尝试创建一个新表,但是当我在SQL命令行中运行脚本时出现错误,我得到的错误是ORA-00942缺少右括号或ORA-00942表或视图不存在。

哦,是的,我知道我可能写了一些可怕的剧本,但如前所述我正在学习,所以任何有意义的批评都会受到赞赏并提供一些帮助:)。

CREATE TABLE Branch
(
Branch_ID varchar(5),
Branch_Name varchar(255),
Branch_Address varchar(255),
Branch_Town varchar(255),
Branch_Postcode varchar(10),
Branch_Phone varchar(50),
Branch_Fax varchar(50),
Branch_Email varchar(50),
Property_ID varchar(5),
Contract_ID varchar(5),
Staff_ID varchar(5),
PRIMARY KEY (Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id)
); 

CREATE TABLE Staff 
(
Staff_ID varchar(5),
Staff_Forename varchar(255),
Staff_Surname varchar(255),
Staff_Address varchar(255),
Staff_Town varchar(255)
Staff_Postcode varchar(10),
Staff_Phone varchar(50),
Staff_DOB varchar(50),
Staff_NIN varchar(10),
Staff_Salary varchar(50),
Staff_Date_Joined varchar(100),
Staff_Viewing_Arranged varchar(100),
Branch_ID varchar(5),
Sales_ID varchar(5),
PRIMARY KEY (Staff_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Sales_ID) REFERENCES Sales(Sales_Id)
);

CREATE TABLE Sales 
(
Sales_ID varchar(5),
Property_Address varchar(255),
Property_Town varchar(255)
Property_Postcode varchar(10),
Property_Type varchar(255),
Num_Rooms varchar(50),
Date_of_Sale varchar(10),
Sales_Bonus varchar(100),
Branch_ID varchar(5),
Property_ID varchar(5),
Staff_ID varchar(5)
Seller_ID varchar(5),
PRIMARY KEY (Sales_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_Id)
);

CREATE TABLE Contract 
(
Contract_ID varchar(5),
Contract_Signed_Date varchar(50),
Property_ID varchar(5),
Buyer_ID varchar(5),
Seller_ID varchar(5),
Branch_ID varchar(5),
PRIMARY KEY (Contract_ID),
FOREIGN KEY (Branch_ID) REFERENCES Branch(Branch_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_Id),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_Id)
);

CREATE TABLE Buyer 
(
Buyer_ID varchar(5),
Viewing_Data varchar(255),
Maximum_Budject varchar(255),
Purchase_Price varchar (50),
Buyer_Forename varchar(255),
Buyer_Surname varchar(255),
Buyer_Address varchar(255),
Buyer_Town varchar(255),
Buyer_Postcode varchar(10),
Property_ID varchar(5),
Contract_ID varchar(5),
Survey_ID varchar(5),
PRIMARY KEY (Buyer_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id),
FOREIGN KEY (Survey_ID) REFERENCES Survey(Survey_Id)
);

CREATE TABLE Seller 
(
Seller_ID varchar(5),
Seller_Forename varchar(255),
Seller_Surname varchar(255),
Seller_Address varchar(255),
Seller_Town varchar(255),
Seller_Postcode varchar(10),
Seller_Property_ID varchar(5),
No_of_Bed varchar(5),
Contract_ID varchar(5),
Property_ID varchar(5),
Sales_ID varchar(5),
PRIMARY KEY (Seller_ID),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_Id),
FOREIGN KEY (Sales_ID) REFERENCES Sales(Sales_Id)
);

CREATE TABLE Property 
(
Property_ID varchar(5),
Property_Address varchar(255),
Property_Town varchar(255),
Property_Postcode varchar(10),
Asking_Price varchar(20),
Date_Registered varchar(50),
Property_Fixtures varchar(255),
Size_of_Rooms varchar(100),
Buyer_ID varchar(5),
Staff_ID varchar(5),
Contract_ID varchar(5),
Seller_ID varchar(5),
PRIMARY KEY (Property_ID),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_ID),
FOREIGN KEY (Seller_ID) REFERENCES Seller(Seller_ID),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Contract_ID) REFERENCES Contract(Contract_Id)
);

CREATE TABLE Survey 
(
Survey_ID varchar(5),
No_of_Survey varchar(10),
Survey_Type varchar(255),
Organised_By varchar(255),
Property_ID varchar(5),
Staff_ID varchar(5),
Buyer_ID varchar(5),
PRIMARY KEY (Survey_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID),
FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id),
FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_Id)
);

CREATE TABLE Advert
(
Survey_ID Advert_ID varchar(5),
No_of_Adverts varchar(10),
Advert_Website varchar(255),
Advert_Newspaper varchar(255),
Property_ID varchar(5),
PRIMARY KEY (Advert_ID),
FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID)
);

6 个答案:

答案 0 :(得分:1)

STAFF表格最后错过,

Staff_Town varchar(255)

Sales表格

Property_Town varchar(255) 
Staff_ID varchar(5)

此外,您无法为尚未创建的表定义约束。我可以找到错误删除那些约束。

答案 1 :(得分:0)

在创建引用的表之前,无法定义外键引用。因此,如果外键引用是循环的,则A => B => C => A,他们首先要创建表,然后使用ALTER TABLE来定义外键。否则,按顺序创建表,首先创建表,然后创建引用它的表。

答案 2 :(得分:0)

您的脚本引用尚未创建的表。即,看看你的第一个create table。到那时,STAFF和其他表还不存在,但你将它们设置为外键引用。

您应首先创建表,然后使用alter在依赖项到位后应用约束:

ALTER TABLE BRANCH
ADD CONSTRAINT fk_staff_id
  FOREIGN KEY (Staff_ID) REFERENCES Staff(Staff_Id);

答案 3 :(得分:0)

因为您有相互引用的表,所以需要在创建表后创建

--create table Buyer
CREATE TABLE Buyer 
(
Buyer_ID varchar(5),
Property_ID varchar(5),
PRIMARY KEY (Buyer_ID)
);

--create table Property
CREATE TABLE Property 
(
Property_ID varchar(5),
Buyer_ID varchar(5),
PRIMARY KEY (Property_ID)
);

--now you can set your reference constraints
ALTER TABLE Property Add FOREIGN KEY (Buyer_ID) REFERENCES Buyer(Buyer_ID);
ALTER TABLE Buyer Add FOREIGN KEY (Property_ID) REFERENCES Property(Property_ID);

答案 4 :(得分:0)

您的密钥上的语法不明确。

以下是具有主键和外键(fk)的create table的示例:

 CREATE TABLE animals (
             animal_id             NUMBER(10) PRIMARY KEY,
             animal_name           VARCHAR2(50) NOT NULL,
             animal_species_code   NUMBER(50) NOT NULL
    CONSTRAINT fk_animal_species REFERENCES animal_species
                         (species_code));

答案 5 :(得分:0)

看来,这组表中的建模可能会受益于一些添加和更改。

例如,STAFF表引用BRANCH表 - 但BRANCH也引用STAFF。这意味着BRANCH只能引用一个STAFF,反之亦然,我怀疑这不是我们想要的。看起来你想要BRANCH和STAFF之间的多对多关系,而启用它的标准解决方案是一个联结表 - 包含两个表的主键。 BRANCH和STAFF之间的这种联结表可能看起来像:

CREATE TABLE BRANCH_STAFF
  (BRANCH_ID  VARCHAR(5)
     CONSTRAINT BRANCH_STAFF_FK1
       REFERENCES BRANCH(BRANCH_ID),
   STAFF_ID   VARCHAR(5)
     CONSTRAINT BRANCH_STAFF_FK2
       REFERENCES STAFF(STAFF_ID),
   CONSTRAINT PK_BRANCH_STAFF
     PRIMARY KEY (BRANCH_ID, STAFF_ID)
     USING INDEX);

祝你好运。