I don't have so many experience in this..I've got this error when I tried to insert into table.
Here's code:
CREATE TABLE factory
(idfactory INT NOT NULL,
location_id INT NOT NULL,
owner INT NOT NULL,
CONSTRAINT factory_id_pk PRIMARY KEY(idfactory),
CONSTRAINT f_location_id_fk FOREIGN KEY(location_id) REFERENCES location(idLocation),
CONSTRAINT s_owner_id_fk FOREIGN KEY(owner) REFERENCES employees(idEmployee));
CREATE TABLE location
(idLocation INT NOT NULL,
Name VARCHAR(45),
region_id INT NOT NULL,
CONSTRAINT location_id_pk PRIMARY KEY(idLocation),
CONSTRAINT p_location_id_fk FOREIGN KEY(region_id) REFERENCES region(idRegion));
CREATE TABLE employees
(idEmployee INT NOT NULL,
Name VARCHAR(20) NOT NULL,
location_id INT NOT NULL,
email VARCHAR(45),
CONSTRAINT emp_id_pk PRIMARY KEY(idEmployee),
CONSTRAINT emp_loc_fk FOREIGN KEY(location_id) REFERENCES location(IdLocation);
Insert:
INSERT INTO factory(factory_id_sequence.NEXTVAL,43,23);
And i got this error..I can't see what's mistake.
Thanks a lot!
You need to have the VALUES keyword in the insert statement:
INSERT INTO factory VALUES (factory_id_sequence.NEXTVAL,43,23);
Related
I got some help last night on this code but now I am getting a different error. My professor is STILL not answering me so I am coming to you guys. Here is the code:
--Create Volunteer Supervisor
CREATE TABLE Volunteer_Supervisor
(
PH_Person_ID Number(10) NOT NULL,
EM_Person_ID Number(10) NOT NULL,
VO_Person_ID Number(10) NOT NULL,
End_Date Date NOT NULL,
Begin_Date Date NOT NULL,
Hours_Worked Number(4) NULL,
PWork_Unit_ID Number(4) NULL,
PRIMARY KEY (PWork_Unit_ID),
CONSTRAINT CCPHPersonID_FK FOREIGN KEY (PH_Person_ID) References Physician (PH_Person_ID),
CONSTRAINT CCEMPersonID_FK FOREIGN KEY (EM_Person_ID) References Employee (EM_Person_ID),
CONSTRAINT CCVOPersonID_FK FOREIGN KEY (VO_Person_ID) References Volunteer (VO_Person_ID),
CONSTRAINT CCPWorkUnitID_PK FOREIGN KEY (PWork_Unit_ID) References Work_Unit (PWork_Unit_ID)
);
Now I have changed the names but still getting this error:
Error report -
ORA-00955: name is already used by an existing object
00000 - "name is already used by an existing object"
*Cause:
*Action:
What am I missing?
If you are sure that no such table Volunteer_Supervisor exists, You may try below code -
CREATE TABLE Volunteer_Supervisor
(
PH_Person_ID Number(10) NOT NULL,
EM_Person_ID Number(10) NOT NULL,
VO_Person_ID Number(10) NOT NULL,
End_Date Date NOT NULL,
Begin_Date Date NOT NULL,
Hours_Worked Number(4) NULL,
PWork_Unit_ID Number(4) NULL,
PRIMARY KEY (PWork_Unit_ID),
FOREIGN KEY (PH_Person_ID) References Physician (PH_Person_ID),
FOREIGN KEY (EM_Person_ID) References Employee (EM_Person_ID),
FOREIGN KEY (VO_Person_ID) References Volunteer (VO_Person_ID),
FOREIGN KEY (PWork_Unit_ID) References Work_Unit (PWork_Unit_ID)
);
Table One:
CREATE TABLE Customer
(CustNo VARCHAR2(8) CONSTRAINT CustNoNotNull NOT NULL,
CustName VARCHAR2(30) CONSTRAINT CustNameNotNull NOT NULL,
Address VARCHAR2(50) CONSTRAINT AddressNotNull NOT NULL,
Internal CHAR(1) CONSTRAINT InternalNotNull NOT NULL,
Contact VARCHAR2(35) CONSTRAINT ContractNotNull NOT NULL,
Phone VARCHAR2(11) CONSTRAINT CPhoneNotNull NOT NULL,
City VARCHAR2(30) CONSTRAINT CityNotNull NOT NULL,
State VARCHAR2(2) CONSTRAINT StateNotNull NOT NULL,
Zip VARCHAR2(10) CONSTRAINT zipNotNull NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CustNo) ) ;
Table Two:
CREATE TABLE Facility
(FacNo VARCHAR2(8) CONSTRAINT FacNoNotNull NOT NULL,
FacName VARCHAR2(30) CONSTRAINT FacNameNotNull NOT NULL,
CONSTRAINT PK_FACILITY PRIMARY KEY (FacNo)
CONSTRAINT Unique_FacName UNIQUE(FacName) );
Table Three:
CREATE TABLE EVENTREQUEST
( EVENTNO VARCHAR2(8) CONSTRAINT EVENTNONOTNULL NOT NULL,
DATEHELD DATE CONSTRAINT DATEHELDNOTNULL NOT NULL,
DATEREQ DATE CONSTRAINT DATEREQNOTNULL NOT NULL,
CUSTNO VARCHAR2(8) CONSTRAINT CUSTNONOTNULL NOT NULL ,
FACNO VARCHAR2(8) CONSTRAINT FACNONOTNULL NOT NULL,
DATEAUTH DATE CONSTRAINT DATEAUTHNULL NULL,
STATUS VARCHAR2(10) CONSTRAINT STATUSNOTNULL NOT NULL,
ESTCOST VARCHAR2(25) CONSTRAINT ESTCOSTNOTNULL NOT NULL,
ESTAUDIENCE VARCHAR2(10) CONSTRAINT ESTAUDIENCENOTNULL NOT NULL,
BUDNO VARCHAR2(8) CONSTRAINT BUDNONULL NULL,
CONSTRAINT PK_EVENTREQUEST PRIMARY KEY (EVENTNO),
CONSTRAINT FK_CUSTNO FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER (CUSTNO),
CONSTRAINT FK_FACNO FOREIGN KEY (FACNO) REFERENCES FACILITY (FACNO),
CONSTRAINT CHECK_EVENTREQUEST_STATUS CHECK(STATUS IN('PENDING','DENIED','APPROVED')));
I get this error executing Table Three:
"The specified constraint name has to be unique. *Action: Specify a unique constraint name for the constraint
How can I prevent this error from occurring?
In Oracle, constraints are a type of object, and they have an identifier (a name by which they are distinguished from other objects). All constraints, on all tables within a schema, share the same name space. Which means you can't have two constraints with the same name in the same schema, even if they are on different tables.
Moreover, identifiers by default are case insensitive. On the second table you defined a constraint FacNoNotNull, and on the third table you are trying to define a constraint FACNONOTNULL. Since identifiers are case insensitive, this is the same name - so you get an exception.
A completely wrong approach (which would work, unfortunately - so many people may be inclined to do it even though it's wrong) is to enclose names in double-quotes, which makes them case sensitive. Don't do that!
Rather, one has to ask - why do you need to name your NOT NULL constraints in the first place? Just add the key words NOT NULL after the column definition; it is very hard to see when or where you would need to know the name of each such constraint.
CREATE TABLE student_detailS01
(USN VARCHAR(20) NOT NULL,
STUDENT_NAME VARCHAR2(20) NOT NULL,
BRANCH VARCHAR2(20) NOT NULL,
CONTACT_NO NUMBER(10) NOT NULL,
FATHERS_NAME VARCHAR2(20) NOT NULL,
HOME_ADDRESS VARCHAR(50) NOT NULL,
DOB DATE NOT NULL,
LOCAL_ADDRESS VARCHAR2(50),
EMAIL_ID VARCHAR2(20),
DOJ DATE,
HID NUMBER NOT NULL,
PRIMARY KEY (USN,HID)
);
CREATE TABLE MESS(
S_NO NUMBER NOT NULL,
HID NUMBER REFERENCES STUDENT_DETAILS01(HID),
NO_OF_BREAKFAST INT,
NO_OF_MEALS INT,
AMT_OF_BREAKFAST INT,
AMT_OF_MEALS INT,
TOTAL INT NOT NULL,
PRIMARY KEY(S_NO)
);
student_details01 table is executed, but mess table gives the following error:
no matching unique or primary key for this column-list.
The ORA-2270 : no matching unique or primary key for this column-list error is quite simple: it happens when the columns we reference in the foreign key do not match a primary key or unique constraint on the parent table. Common reasons for this are
the parent lacks a constraint altogether
the parent table's constraint is a compound key and we haven't
referenced all the columns in the foreign key statement.
To fix your issue try as below:
CREATE TABLE MESS
(
S_NO NUMBER,
S_HID NUMBER,
S_UNO VARCHAR (20), --Added Column
NO_OF_BREAKFAST INT,
NO_OF_MEALS INT,
AMT_OF_BREAKFAST INT,
AMT_OF_MEALS INT,
TOTAL INT NOT NULL,
CONSTRAINT MESS_pk PRIMARY KEY (S_NO),
CONSTRAINT fk_id_dtls FOREIGN KEY
(S_HID, S_UNO)
REFERENCES student_detailS01 (HID, USN)
);
while making foreign key in player table it shows following error
ORA-02270: no matching unique or primary key for this column-list
create table person
(
per_ssn number(10) not null,
per_name varchar2(30) not null,
CONSTRAINT pk_PersonID PRIMARY KEY (per_ssn,per_name)
);
create table Player
(
player_ssn number(10) not null,
player_name varchar2(30) not null,
football_club_name varchar2(30) not null,
p_age number(2) not null,
p_weight number(3) not null,
p_height number(10) not null,
country varchar2(20) not null,
p_starting_date date not null,
p_ending_date date not null
);
alter table Player
add constraint player_ssn
FOREIGN KEY (player_ssn)
REFERENCING person (per_ssn)on delete cascade
I want to make two primary keys in person table and then want to refer these
primary keys in player table.
If I make one primary key and then refer it in player table, then it does not show error but I want to make two primary keys.
You should be referencing per_ssn,per_name because that is your PK on person.
Anyway, think about making per_ssn your PK in person table
alter table Player
add constraint player_ssn
FOREIGN KEY (player_ssn,player_name)
REFERENCING person (per_ssn,per_name)on delete cascade
I am trying to create a table with foreign key using oracle. My syntax is as follows
CREATE TABLE product (
product_id INT(7) NOT NULL,
supplier_id INT(7) NOT NULL,
product_name VARCHAR2(30),
product_price DOUBLE(4),
product_category VARCHAR2(30),
product_brand VARCHAR2(20),
product_expire DATE,
PRIMARY KEY (product_id),
FOREIGN KEY (supplier_id)
)
I got a error, saying
Error at Command Line:2 Column:14 Error report: SQL Error: ORA-00907:
missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Please help!
Your foreign key should refference another column on another table.
Here is the documentation you need to fix your issue (how to write the query with the correct syntax for foreign key)
Also, change your data type for column product_price from DOULBE(4) to NUMBER(12,4).
You should not use limit for int type...oracle will take default length for int type .
Instead of int you can use Number type to make it run. And DOUBLE PRECISION is a data type in oracle but Double is not there. Also , syntax for foreign key is wrong.
so this query will work for sure :
CREATE TABLE product(
product_id number(7) NOT NULL,
supplier_id number(7) NOT NULL,
product_name VARCHAR2(30),
product_price DOUBLE PRECISION,
product_category VARCHAR2(30),
product_brand VARCHAR2(20),
product_expire DATE,
PRIMARY KEY (product_id),
FOREIGN KEY (supplier_id)
REFERENCES parent_table (supplier_id)
);
You create like foreign key references parent table that is the proper syntax for creating the foreign key
CREATE TABLE product(
product_id number(7) NOT NULL,
supplier_id number(7) NOT NULL,
product_name VARCHAR(30),
product_price DOUBLE PRECISION,
product_category VARCHAR(30),
product_brand VARCHAR(20),
product_expire DATE,
PRIMARY KEY (product_id),
FOREIGN KEY (supplier_id)
REFERENCES parent_table (supplier_id)
);