纠正sql代码中的错误

时间:2013-11-08 19:49:34

标签: sql database oracle

我是这个网站的新手,我希望我能以正确的方式提出问题

-- Create a Database table to represent the "FACT" entity. 
CREATE TABLE FACT 
  ( 
     Time_id           DATE NOT NULL, 
     Area_id           INTEGER NOT NULL, 
     Reported_crime_id INTEGER NOT NULL, 
     Crime_status      VARCHAR(8) NOT NULL, 
     no_of_crime       INTEGER NOT NULL, 
     Max_crime         INTEGER NOT NULL, 
     Avg_crime         INTEGER NOT NULL, 
     Min_crime         INTEGER NOT NULL, 
     date_reported     DATE NOT NULL, 
     -- Specify the PRIMARY KEY constraint for table "FACT". 
     -- This indicates which attribute(s) uniquely identify each row of data. 
     CONSTRAINT pk_fact PRIMARY KEY (Time_id, Area_id, Reported_crime_id, 
     Crime_status) 
  ); 

-- Create a Database table to represent the "Crime_Dim" entity. 
CREATE TABLE Crime_Dim 
  ( 
     REPORTED_CRIME_ID INTEGER NOT NULL, 
     CRIME_TYPE_Desc   VARCHAR(50), 
     DATE_REPORTED     DATE NOT NULL, 
     Crime_type_id     INTEGER NOT NULL, 
     -- Specify the PRIMARY KEY constraint for table "Crime_Dim". 
     -- This indicates which attribute(s) uniquely identify each row of data. 
     CONSTRAINT pk_crime_dim PRIMARY KEY (REPORTED_CRIME_ID) 
  ); 

-- Create a Database table to represent the "Location_Dim" entity. 
CREATE TABLE Location_Dim 
  ( 
     AREA_ID   INTEGER NOT NULL, 
     AREA_Name VARCHAR(30) NOT NULL, 
     Area_code INTEGER NOT NULL, 
     Force_id  INTEGER NOT NULL, 
     -- Specify the PRIMARY KEY constraint for table "Location_Dim". 
     -- This indicates which attribute(s) uniquely identify each row of data. 
     CONSTRAINT pk_location_dim PRIMARY KEY (AREA_ID) 
  ); 

-- Create a Database table to represent the "Time_Dim" entity. 
CREATE TABLE Time_Dim 
  ( 
     Time_id  INTEGER NOT NULL, 
     day_id   INTEGER NOT NULL, 
     Month_id INTEGER NOT NULL, 
     Year     INTEGER, 
     -- Specify the PRIMARY KEY constraint for table "Time_Dim". 
     -- This indicates which attribute(s) uniquely identify each row of data. 
     CONSTRAINT pk_time_dim PRIMARY KEY (Time_id) 
  ); 

-- Create a Database table to represent the "Reported_crime_dim" entity. 
CREATE TABLE Reported_crime_dim 
  ( 
     Crime_status  VARCHAR(20) NOT NULL, 
     Date_reported DATE NOT NULL, 
     -- Specify the PRIMARY KEY constraint for table "Reported_crime_dim". 
     -- This indicates which attribute(s) uniquely identify each row of data. 
     CONSTRAINT pk_reported_crime_dim PRIMARY KEY (Crime_status) 
  ); 


-- i.e. tables may be referenced before they have been created.  This method is therefore safer.
-- Alter table to add new constraints required to implement the "FACT_Time_Dim" relationship 
-- This constraint ensures that the foreign key of table "FACT" 
-- correctly references the primary key of table "Time_Dim" 
ALTER TABLE FACT 
  ADD CONSTRAINT fk1_fact_to_time_dim FOREIGN KEY(Time_id) REFERENCES Time_Dim( 
  Time_id) ON DELETE RESTRICT ON UPDATE RESTRICT; 

-- Alter table to add new constraints required to implement the "FACT_Location_Dim" relationship
-- This constraint ensures that the foreign key of table "FACT" 
-- correctly references the primary key of table "Location_Dim" 
ALTER TABLE FACT 
  ADD CONSTRAINT fk2_fact_to_location_dim FOREIGN KEY(AREA_ID) REFERENCES 
  Location_Dim(AREA_ID) ON DELETE RESTRICT ON UPDATE RESTRICT; 

-- Alter table to add new constraints required to implement the "FACT_Crime_Dim" relationship
-- This constraint ensures that the foreign key of table "FACT" 
-- correctly references the primary key of table "Crime_Dim" 
ALTER TABLE FACT 
  ADD CONSTRAINT fk3_fact_to_crime_dim FOREIGN KEY(REPORTED_CRIME_ID) REFERENCES 
  Crime_Dim(REPORTED_CRIME_ID) ON DELETE RESTRICT ON UPDATE RESTRICT; 

-- Alter table to add new constraints required to implement the "FACT_Reported_crime_dim" relationship
-- This constraint ensures that the foreign key of table "FACT" 
-- correctly references the primary key of table "Reported_crime_dim" 
ALTER TABLE FACT 
  ADD CONSTRAINT fk4_fact_to_reported_crime_dim FOREIGN KEY(Crime_status) 
  REFERENCES Reported_crime_dim(Crime_status) ON DELETE RESTRICT ON UPDATE 
  RESTRICT; 
-------------------------------------------------------------- 
-- End of DDL file auto-generation 
-------------------------------------------------------------- 

有人可以帮我纠正上面的SQL代码中的错误吗?当我运行最后几个代码中提到的代码时,我需要创建约束以防止Fact表中的外键并将它们与原始表关联,如果我有这个权利的话...... 有人可以更正代码,让我知道PLZ

1 个答案:

答案 0 :(得分:2)

关注this answer,您必须删除ON DELETE RESTRICTON UPDATE RESTRICT。 Oracle中没有这样的选项。