missing left parenthesis, i cant find a mistake. can someone help me? - sql

CREATE TABLE PRESCRIPTION
(
prescription_no NUMBER (7),
CONSTRAINT prescription_no_pk PRIMARY KEY,
pr_patient_no VARCHAR2(6),
CONSTRAINT pr_patient_no_fk FOREIGN KEY(patient) REFERENCES (patient_no),
pr_drug_no NUMBER(5),
CONSTRAINT pr_drug_no_fk FOREIGN KEY (drug) REFERENCES (drug_no),
drug_start_date DATE,
units_per_day NUMBER(3,2),
drug_end_date DATE
);

Try below query I altered your query :
CREATE TABLE PRESCRIPTION
(
prescription_no NUMBER (7) CONSTRAINT prescription_no_pk PRIMARY KEY,
pr_patient_no VARCHAR2(6) CONSTRAINT pr_patient_no_fk FOREIGN KEY(patient)
REFERENCES (patient_no),
pr_drug_no NUMBER(5) CONSTRAINT pr_drug_no_fk FOREIGN KEY (drug)
REFERENCES (drug_no),
drug_start_date DATE,
units_per_day NUMBER(3,2),
drug_end_date DATE
);

FOREIGN KEY constraint has the following syntax:
FOREIGN KEY (<this table's column name>) REFERENCES <another table>(<another table's column name>).
In your example, supposing that patient has the following schema:
CREATE TABLE patient (
patient_no VARCHAR2(6),
primary key (patient_no)
)
you reference it as follows:
CREATE TABLE PRESCRIPTION (
prescription_no NUMBER (7),
pr_patient_no VARCHAR2(6) ,
CONSTRAINT prescription_no_pk PRIMARY KEY (prescription_no),
CONSTRAINT pr_patient_no_fk FOREIGN KEY (pr_patient_no) REFERENCES patient(patient_no)
)
and so on.

Related

CREATED OR MODIFY A TABLE WHICH HAVE A COMBINED PRIMARY KEY, FORM FROM 3 OF THEIR ATTRIBUTES

I'm trying to created a table, and give as a primary key, the 3 attributes, which it have.
The code is below:
CREATE TABLE compra
(
dni_cli VARCHAR2(50),
cod_prod NUMBER(10) NOT NULL,
cantidad NUMBER (10),
CONSTRAINT pk_compra PRIMARY KEY (dni_cli, cod_prod, cantidad);
CONSTRAINT pelicula_codigo_prod_fg FOREIGN KEY (cod_prod) REFERENCES producto(cod_prod),
CONSTRAINT pelicula_codigo_cli_fg FOREIGN KEY (dni_cli) REFERENCES cliente(dni)
);
I also tried to create the table first, and then, add the constraint in order to create the combined PRIMARY KEY with the alter table, but is doesn't work.
The code is this:
ALTER TABLE CONSTRAINT pk_compra PRIMARY KEY (dni_cli, cod_prod, cantidad);
If the primary key already exists you need to drop the key:
ALTER TABLE compra drop constraint pk_compra;
then add the key:
ALTER TABLE compra add constraint pk_compra primary key (city_id, buildtime, time);
If the primary key does not exist in the table then just use the second command line.
Also as #stickybit noted your table has an error. This is the code without an error:
CREATE TABLE compra (
dni_cli VARCHAR2(50),
cod_prod NUMBER(10) NOT NULL,
cantidad NUMBER (10),
CONSTRAINT pk_compra PRIMARY KEY (dni_cli,cod_prod,cantidad), --<<change here
CONSTRAINT pelicula_codigo_prod_fg FOREIGN KEY (cod_prod) REFERENCES producto(cod_prod),
CONSTRAINT pelicula_codigo_cli_fg FOREIGN KEY (dni_cli) REFERENCES cliente(dni));

'Missing keyword' this error is coming while i create a table

'Missing keyword' this error is coming while I create a table using the below query:
CREATE TABLE ACCTS
(
ACCT_NO NUMBER (12),
ACCTH_NAME VARCHAR2(50),
ACCTH_ADD VARCHAR2(100),
ACCTH_STATE VARCHAR2(50),
ACCTH_DOB DATE ,
ACCT_DT_CREATED DATE,
BRANCH_CODE NUMBER(5),
ACCT_TYPE_CODE NUMBER(6),
CONSTRAINT ACCT_NO_PK PRIMARY KEY (ACCT_NO),
CONSTRAINT ACCTH_STATE_FK1 FOREIGN KEY (ACCTH_STATE) REFERENCES STATES (STATE_ID),
CONSTRAINT ACCT_TYPE_CODE_FK2 FOREIGN KEY (ACCT_TYPE_CODE) REFERENCES ACCT_TYPES(ACCT_TYPE_CODE)
)
I added your query to SQL Fiddle (adding the missing tables so the foreign key constraints will work) and the query works:
CREATE TABLE STATES
(
STATE_ID VARCHAR2(50),
CONSTRAINT ACCT_STATE_ID PRIMARY KEY (STATE_ID)
);
CREATE TABLE ACCT_TYPES
(
ACCT_TYPE_CODE NUMBER(6),
CONSTRAINT ACCT_TYPES_ID PRIMARY KEY (ACCT_TYPE_CODE)
);
CREATE TABLE ACCTS
(
ACCT_NO NUMBER (12),
ACCTH_NAME VARCHAR2(50),
ACCTH_ADD VARCHAR2(100),
ACCTH_STATE VARCHAR2(50),
ACCTH_DOB DATE ,
ACCT_DT_CREATED DATE,
BRANCH_CODE NUMBER(5),
ACCT_TYPE_CODE NUMBER(6),
CONSTRAINT ACCT_NO_PK PRIMARY KEY (ACCT_NO),
CONSTRAINT ACCTH_STATE_FK1 FOREIGN KEY (ACCTH_STATE) REFERENCES STATES (STATE_ID),
CONSTRAINT ACCT_TYPE_CODE_FK2 FOREIGN KEY (ACCT_TYPE_CODE) REFERENCES ACCT_TYPES(ACCT_TYPE_CODE)
)
http://sqlfiddle.com/#!4/b726da

How to solve ORA-00907: missing right parenthesis error?

I am unable to find an error in this code, it shows ORA-00907: missing right parenthesis for both. I'm doing this on Oracle live SQL.
CREATE table Final_chart
(
T_id int FOREIGN KEY REFERENCES Train(T_id),
User_id varchar(10) FOREIGN KEY REFERENCES Passenger(User_id),
Seat_id int FOREIGN KEY REFERENCES Train_Seats(Seat_id),
CONSTRAINT PNR PRIMARY KEY (T_id,User_id,Seat_id)
)
CREATE table Train_seats
(
T_id int FOREIGN KEY REFERENCES Train(T_id),
Seat_id int PRIMARY KEY,
Waiting int NOT NULL,
Available int NOT NULL,
Booked_seat int NOT NULL
)
Working example for one table. And please use varchar2 instead on varchar in oracle.
CREATE table Final_chart
(T_id integer,
User_id varchar2(10),
Seat_id integer,
CONSTRAINT t2_fk FOREIGN KEY (T_id) REFERENCES Train(T_id),
CONSTRAINT t1_fk FOREIGN KEY (User_id) REFERENCES Passenger(User_id),
CONSTRAINT t3_fk FOREIGN KEY (Seat_id) REFERENCES Train_Seats(Seat_id),
CONSTRAINT Pkr PRIMARY KEY (T_id, User_id, Seat_id)
)
The problem is your reference to the foreign key. this is the way you should do it
CONSTRAINT FK_Train FOREIGN KEY (T_id) REFERENCES Train(T_id)

query not working for foreign key as a composite primary key of another table oracle SQL

I am trying to add two foreign keys for an associative entity in SQL Oracle. The primary key that I am referring from another table is a composite primary key. When I try to enter the SQL it says
no matching unique or primary key for this column-list
The code that I used to create the tbl_customer
CREATE TABLE tbl_Customer(
customer_id NUMBER(4)
CONSTRAINT pk_customer PRIMARY KEY,
CustomerName VARCHAR2(50) NOT NULL,
Telephone VARCHAR2(10),
CusEmail VARCHAR2(20),
CONSTRAINT cus_email UNIQUE(CusEmail),
location_id NUMBER(4)
CONSTRAINT fk_location_id references tbl_Location(location_id));
SQL to create tbl_Vehicle
CREATE TABLE tbl_Vehicle(
vehicle_id NUMBER(4),
PlateNo VARCHAR2(10),
CONSTRAINT pk_v PRIMARY KEY(vehicle_id,PlateNo),
Brand VARCHAR2(20),
Model VARCHAR2(10),
TotalNoSeats NUMBER(2),
Class VARCHAR2(4) NOT NULL,
CONSTRAINT no_seats CHECK (TotalNoSeats<100),
driver_id NUMBER(4)
CONSTRAINT fk_driveridV references tbl_Driver(driver_id),
location_id NUMBER(4)
CONSTRAINT fk_locationV references tbl_Location(location_id));
The associative table is
CREATE TABLE tbl_Customer_Vehicle(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4)
CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
);
where the error is in this line
CONSTRAINT fk_vehicle_id references tbl_Vehicle(vehicle_id)
*
Is this error because vehicle_id is a composite primary key?
Please help!!
You need to add PlateNo column in tbl_Customer_Vehicle table and define foreign key on it.
CREATE TABLE tbl_Customer_Vehicle
(
customer_id NUMBER(4)
CONSTRAINT fk_customer_id references tbl_Customer(customer_id),
vehicle_id NUMBER(4),
PlateNo VARCHAR2(10),
CONSTRAINT fk_vehicle_id_PlateNo FOREIGN KEY(vehicle_id,PlateNo)
references tbl_Vehicle(vehicle_id,PlateNo)
);

ORA-02270 - Can't find fault

Having trouble with a ORA-02270 error. After going through the primary keys and foreign keys over and over, I can't find what the problem is.
CREATE TABLE CUSTACC(
REFNO CHAR(4),
ACCNO NUMBER(7),
CONSTRAINT CUSTACC_PK PRIMARY KEY (REFNO, ACCNO));
CREATE TABLE CUST(
CUSTNO CHAR(4),
NAME VARCHAR2(30),
ADDRESS VARCHAR2(50),
AREA VARCHAR2(10),
CONSTRAINT CUSTNO_PK PRIMARY KEY(CUSTNO),
CONSTRAINT CUSTNO_FK FOREIGN KEY (CUSTNO)
REFERENCES CUSTACC(REFNO));
CREATE TABLE ACC(
ACCNO NUMBER(7),
BALANCE NUMBER(8,2),
BRANCH VARCHAR2(10),
OPENED DATE,
BONUS NUMBER(8,2),
CONSTRAINT ACCNO_PK PRIMARY KEY (ACCNO),
CONSTRAINT ACCNO_FK FOREIGN KEY (ACCNO)
REFERENCES CUSTACC(ACCNO));
I must be blind but I've checked everything people have suggested before.
The primary key on CUSTACC consists of two columns: REFNO and ACCNO.
The foreign key reference should include both of them. So, your tables need both columns.
I suspect, in fact, that you want the foreign key reference in the first table, something like this:
CREATE TABLE CUST (
CUSTNO CHAR(4),
NAME VARCHAR2(30),
ADDRESS VARCHAR2(50),
AREA VARCHAR2(10),
CONSTRAINT CUSTNO_PK PRIMARY KEY(CUSTNO)
);
CREATE TABLE ACC (
ACCNO NUMBER(7),
BALANCE NUMBER(8,2),
BRANCH VARCHAR2(10),
OPENED DATE,
BONUS NUMBER(8,2),
CONSTRAINT ACCNO_PK PRIMARY KEY (ACCNO)
);
CREATE TABLE CUSTACC (
REFNO CHAR(4),
ACCNO NUMBER(7),
CONSTRAINT CUSTACC_PK PRIMARY KEY (REFNO, ACCNO),
CONSTRAINT CUSTNO_FK FOREIGN KEY (REFNO)
REFERENCES CUST(CUSTNO),
CONSTRAINT ACCNO_FK FOREIGN KEY (ACCNO)
REFERENCES ACC(ACCNO)
);
Here is a SQL Fiddle.