Oracle: Make a composite Key containing three Foregin keys - sql

This is my code:
create table orderline
(
Order_No number(4) constraint orderno_fk references order_detail(Order_No),
Product_Code varchar2(6) constraint productcode2_fk references product(Product_Code),
Product_Size char(1) constraint productsize_fk references product_stock(Product_Size),
Product_Quantity number(4) not null
constraint orderline_comp primary key (Order_No,Product_Code, Product_Size)
);
I get the error (with the star underneath the left parenthesis before 'Order'):
ERROR at line 7:
ORA-00907: missing right parenthesis

You need a , before the constraint orderline...

Related

My inputs for the foreign key are not working and im not sure

I was not sure if the multiple column for distance foreign key was correct.
Here is my code:
DROP TABLE Trips;
DROP SEQUENCE Trips_seq;
CREATE TABLE Trips(
Trips_ID NUMBER NOT NULL,
Date_of_Trip DATE NOT NULL,
Payment NUMBER NOT NULL,
Destination_Town VARCHAR2(50) NOT NULL,
Source_Town VARCHAR2 (50) NOT NULL,
Customer_ID NUMBER NOT NULL
CONSTRAINT Trip_pk
PRIMARY KEY (Trips_ID, Trips_seq),
CONSTRAINT Customer_fk
FOREIGN KEY (Customer_ID) REFERENCES Customer (Customer_ID),
CONSTRAINT Owner_fk
FOREIGN KEY (Owner_ID) REFERENCES Vehicle_Owners (Owner_ID),
CONSTRAINT Payment_fk
FOREIGN KEY (Payment_ID) REFERENCES Payment (Payment_ID),
CONSTRAINT Distances_fk
FOREIGN KEY (Destination_Town, Source_Town) REFERENCES
Distances (Destination_Town, Source_Town)s
);
CREATE SEQUENCE Trips_seq START WITH 1 INCREMENT BY 1;
I count 12 left parenthesis and 11 right parenthesis in your code. It looks like you need to add a right parenthesis before your 's' e.g.:
Distances (Destination_Town, Source_Town)) s

I'm trying to create a table with a foreign key but i keep getting the ORA-00904 error. What am I doing wrong?

create table reservation (
reserve_id number PRIMARY KEY,
date_in TIMESTAMP,
date_out TIMESTAMP,
made_by number(4),
constraint LocationID_fk foreign key (locId) references location(locId),
constraint fk_guest_id foreign key (guest_id) references guest(guest_id)
);
--these are the parent tables
create table guest(
guest_id NUMBER(3) PRIMARY KEY,
fname varchar(10),
lname varchar(5),
email varchar(10)
);
Create table location (
locId NUMBER(4) PRIMARY KEY,
loc_name varchar(10),
manager_name varchar(15)
);
-------error that keeps coming up
Error report -
SQL Error: ORA-00904: "LOCID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
I think you misunderstand what the constraint definitions do:
constraint LocationID_fk foreign key (locId) references location(locId),
constraint fk_guest_id foreign key (guest_id) references guest(guest_id)
Maybe you are under the impression that by defining the foreign key constraints on locId and guest_id, that it also automatically defines the 2 columns on the reservation table? That is not the case. You have to explicitly define the 2 columns besides the foreign key constraint definition. Something like:
create table reservation (
reserve_id number PRIMARY KEY,
date_in TIMESTAMP,
date_out TIMESTAMP,
made_by number(4),
locId number(4), -- explicitly defined
guest_id number(3) -- explicitly defined
constraint LocationID_fk foreign key (locId) references location(locId),
constraint fk_guest_id foreign key (guest_id) references guest(guest_id)
);

Error with oracle ORA-00907: missing right parenthesis

I'm using Oracle10g Express edition and I tried to create this tables but an Error appeared and I need some help fixing the "ORA-00907: missing right parenthesis" problem. I searched for a solution to this error and it looks like the main reason is not the "missing right parenthesis" but I still can't fix the code.
CREATE TABLE Pays
(
codePays NUMBER(4) CONSTRAINT pk_Pays PRIMARY KEY,
nomPays VARCHAR(20)
);
CREATE TABLE Equipe
(
codeEquipe NUMBER(4) CONSTRAINT pk_Equipe PRIMARY KEY,
nomEquipe VARCHAR(4),
);
CREATE TABLE Etape
(
numEtape NUMBER(4) CONSTRAINT pk_Etape PRIMARY KEY,
);
CREATE TABLE Coureur
(
numCoureur NUMBER(4) CONSTRAINT pk_Coureur PRIMARY KEY,
codeEquipe NUMBER(4),
codePays NUMBER(4),
CONSTRAINT FK_Equipe_Coureur FOREIGN KEY(codeEquipe) REFERENCES Equipe(codeEquipe);
CONSTRAINT FK_Pays_Coureur FOREIGN KEY(codePays) REFERENCES Pays(codePays);
);
You have two semicolons in the code for creating the Coureur table. You also have a dangling comma in the code for creating the Equipe table. Replace your code with this:
CREATE TABLE Pays
(
codePays NUMBER(4) CONSTRAINT pk_Pays PRIMARY KEY,
nomPays VARCHAR(20)
);
CREATE TABLE Equipe
(
codeEquipe NUMBER(4) CONSTRAINT pk_Equipe PRIMARY KEY,
nomEquipe VARCHAR(4)
);
CREATE TABLE Etape
(
numEtape NUMBER(4) CONSTRAINT pk_Etape PRIMARY KEY
);
CREATE TABLE Coureur
(
numCoureur NUMBER(4) CONSTRAINT pk_Coureur PRIMARY KEY,
codeEquipe NUMBER(4),
codePays NUMBER(4),
CONSTRAINT FK_Equipe_Coureur FOREIGN KEY(codeEquipe) REFERENCES Equipe(codeEquipe),
CONSTRAINT FK_Pays_Coureur FOREIGN KEY(codePays) REFERENCES Pays(codePays)
);
Here is what I believe is happening leading to the exact error you are seeing. Your Oracle workbench is parsing the Coureur table definition, and it hits a semicolon on the line for the FK_Equipe_Courer constraint. It interprets this as the end of the table definition, but it doesn't see a closing right parenthesis before this semicolon, so it gives you the error you see.

Creating a table with a foreign key

Hey all I'm trying to create a table that contains a Foreign Key, and for some reason I am getting an error. The error says 00907. 00000 - "missing right parenthesis" which is odd because I don't have a random left parenthesis. I looked up how to create a table with a Foreign Key and that led to the following code:
Create Table EMPHIREINFO
(
empname VARCHAR2(10) NOT NULL FOREIGN KEY REFERENCES EMPADDRESS(empname),
empno NUMBER(4,0) NOT NULL PRIMARY KEY,
startdt DATE,
enddt DATE,
cntrlgth NUMBER(5,0)
)
I tried it with and without the REFERENCES EMPADDRESS(empname) and I still get the same error. Any help is appreciated, thanks.
You need to specify column after FOREIGN KEY. However, I'd prefer to use naming constraints, for instance
Create Table EMPHIREINFO
(
empname VARCHAR2(10) NOT NULL ,
empno NUMBER(4,0) NOT NULL ,
startdt DATE,
enddt DATE,
cntrlgth NUMBER(5,0),
CONSTRAINT PK_EMPHIREINFO PRIMARY KEY(empno) USING INDEX
(CREATE UNIQUE INDEX IDXU_EMPHIREINFO_empno ON EMPHIREINFO(empno) ),
CONSTRAINT FK_EMPHIREINFO_EMPNAME FOREIGN KEY(empname)
REFERENCES EMPADDRESS(empname)
)

SQLite Syntax error

Hi I'm having a problem with one of the tables in my database. I'm loading the tables from a .txt file, when I load the database i get
ERROR NEAR line 49: near "(": syntax error
the table below starts from line 49
create table LEASE(
P_ID integer,
I_ID varchar2,
C_ID integer,
DATE date,
TRENT decimal(6,2),
RENTPM decimal(4,2),
RENTUTD varchar2 constraint rentutd_value (RENTUTD in ('Y','N')),
LENGTH varchar2(15),
SDATE date,
EDATE date,
NOTE varchar2(150),
G_ID integer,
A_ID integer,
constraint fkey_lea1 foreign key (P_ID) references PROPERTY(P_ID),
constraint fkey_lea2 foreign key (I_ID) references INSTITUTION(I_ID),
constraint fkey_lea3 foreign key (C_ID) references CLIENT(C_ID),
constraint fkey_lea4 foreign key (G_ID) references GUARANTOR(G_ID),
constraint fkey_lea5 foreign key (A_ID) references AGENT(A_ID),
constraint pkey_lea primary key (P_ID,I_ID,C_ID,DATE)
);
Looks like the syntax on your rentutd column needs to be a little different:
RENTUTD varchar2 constraint rentutd_value CHECK ( RENTUTD in ('Y','N'))
See this sqlfiddle.
The SQLite syntax diagrams are great to figure this stuff out.