简体   繁体   English

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

[英]Error when creating table in Oracle Database 11g SQL

I'm new to SQL and I'm trying to create a new table however I get an error when I run my script in the SQL command line, the errors I'm getting are either ORA-00942 missing right parenthesis or ORA-00942 table or view does not exist. 我是SQL的新手,正在尝试创建新表,但是在SQL命令行中运行脚本时遇到错误,我得到的错误是ORA-00942缺少右括号或ORA-00942表或视图不存在。

Oh and yes I know I have probably written some terrible script but as mentioned earlier I'm learning so any meaningful criticism would be appreciated along with some help :). 哦,是的,我知道我可能已经写了一些糟糕的脚本,但是如前所述,我正在学习,所以任何有意义的批评以及一些帮助都将不胜感激:)。

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)
);

STAFF Table miss a , at the end STAFF表错过,在结束

Staff_Town varchar(255)

Sales Table too Sales

Property_Town varchar(255) 
Staff_ID varchar(5)

Also you can't define constrains to table not created yet. 另外,您不能为尚未创建的表定义约束。 I could found the error removing those constrain. 我可以找到删除这些约束的错误。

Can't define the foreign key references until the table being referred to is created. 在创建要引用的表之前,无法定义外键引用。 So, if the foreign key references are circular, in that A => B => C => A, they you will first have to create the tables, then use ALTER TABLE to define the foreign keys. 因此,如果外键引用是循环的,则A => B => C => A,则必须首先创建表,然后使用ALTER TABLE定义外键。 Otherwise, create the tables in order, where first you create a table, then create the table that is referencing it. 否则,请按顺序创建表,首先在其中创建表,然后创建引用该表的表。

Your script references tables that haven't been created yet. 您的脚本引用了尚未创建的表。 Ie, look at your first create table . 即,查看您的第一个create table by that time, STAFF and other tables don't exist yet, but you're setting them as foreign key references. 到那时, STAFF和其他表还不存在,但是您将它们设置为外键引用。

You should create your tables first, and then apply constraints after the dependencies are in place, using alter : 你应该先创建表,然后将约束后的依赖到位,使用alter

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

because you have tables that references each other you need create the references after the tables are created 因为您有相互引用的表,所以需要在创建表创建引用

--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);

Your syntax is not clear on your keys. 您的键上语法不清楚。

Here is an example of what a create table with a primary key and a foreign key (fk) would look like: 这是一个带有主键和外键(fk)的创建表的示例:

 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));

It appears that the modeling in this set of tables might benefit from some additions and changes. 看起来这组表中的建模可能会受益于一些添加和更改。

As an example, the STAFF table references the BRANCH table - but BRANCH also references STAFF. 例如,STAFF表引用BRANCH表-但是BRANCH也引用STAFF。 This means that BRANCH can only reference one STAFF, and vice versa, which I suspect is not what's wanted. 这意味着BRANCH只能引用一个STAFF,反之亦然,我怀疑这不是想要的。 It appears that you want a many-to-many relationship between BRANCH and STAFF, and the standard solution for enabling this is a junction table - one which contains the primary keys of both tables. 看来您希望BRANCH和STAFF之间存在多对多关系,而启用此功能的标准解决方案是联结表-一个包含两个表的主键的表。 Such a junction table between BRANCH and STAFF might look like: 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);

Best of luck. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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