Oracle SQL: trying to create tables with certain constraints but not able to add record. where is it going wrong? - sql

These are the tables i am trying to create. i am not able to find the mistake here at all.
CREATE TABLE Pharmaceutical (
PharName VARCHAR(30),
PharTelephone VARCHAR(25),
PRIMARY KEY (PharName)
);
CREATE TABLE Doctor (
DID VARCHAR2(20),
DName VARCHAR2(50),
Specialty VARCHAR2(70),
YearOfExp NUMBER,
PRIMARY KEY (DID)
);
CREATE TABLE Patient (
PID VARCHAR2(20),
PName VARCHAR2(50),
PDOB DATE,
PAddress VARCHAR2(70),
PPostalCode VARCHAR2(12),
FamilyDoctor VARCHAR2(20),
PRIMARY KEY (PID),
FOREIGN KEY (FamilyDoctor) REFERENCES Doctor(DID)
);
CREATE TABLE Drug (
PharName VARCHAR2(30),
Tradename VARCHAR2(30),
Formula VARCHAR2(70),
PRIMARY KEY (PharName, Tradename),
FOREIGN KEY (PharName) REFERENCES Pharmaceutical(PharName)
);
CREATE TABLE Prescription (
DID VARCHAR2(20),
PID VARCHAR2(20),
PharName VARCHAR2(30),
Tradename VARCHAR2(30),
PrescriptionDT DATE,
Quantity NUMBER,
CONSTRAINT checkQuantity CHECK (Quantity>0 AND Quantity<31),
PRIMARY KEY (DID, PID, PharName, Tradename, PrescriptionDT),
FOREIGN KEY (DID) REFERENCES Doctor(DID),
FOREIGN KEY (PID) REFERENCES Patient(PID),
FOREIGN KEY (PharName, Tradename) REFERENCES Drug(PharName, Tradename),
);
i also use drop.sql, to drop the tables:
DROP TABLE Pharmaceutical PURGE;
DROP TABLE Doctor PURGE;
DROP TABLE Patient PURGE;
DROP TABLE Drug PURGE;
DROP TABLE Prescription PURGE;
thank you!
P.S. --> removed the 'add' from the add contraint part. this is the error i get:
SP2-0734: unknown command beginning "PRIMARY KEY ..." - rest of line ignored.
getting this error for the last table's primary key and all of it's foreign keys, so assumed that the problem is with the contraint but i may be wrong.

In the definition of table Prescription, the last line before the ending ); reads
FOREIGN KEY (PharName, Tradename) REFERENCES Drug(PharName, Tradename),
Get rid of the trailing comma.
Best of luck.

Related

Trouble adding Foreign Key Constraint (error ORA-02270: no matching unique or primary key for this column-list)

I am learning how to create a small and simple database for class with Primary Key and Foreign Key restraints. I am getting ORA-02770 when attempting to run my ALTER TABLE statement, which as I understand is notifying me that the column I am referencing in the outside table is not listed as a primary key constraint. However, from what I can see my syntax is correct naming the primary keys in my CREATE TABLES statements. I searched inside the all_cons_columns table for the player_info table and it showed the primary keys listed as well. Could I get some guidance? Listed below is my current script:
CREATE TABLE Player_Game
( school varchar2(30),
player_number number(2,0),
game_number number(1,0),
CONSTRAINT playergame_pk PRIMARY KEY (school, player_number,game_number)
);
CREATE TABLE School
( school varchar2(30),
city varchar2(30),
coach varchar2(30),
team_name varchar2(30),
win_record number (2,0),
loss_record number (2,0),
CONSTRAINT school_pk PRIMARY KEY (school)
);
CREATE TABLE Game
( school varchar2(30),
game_number number(1,0),
game_date DATE,
game_score varchar2(15),
CONSTRAINT game_pk PRIMARY KEY (school, game_number)
);
CREATE TABLE player_info
( school varchar2(30),
player_number number(2,0),
player_name varchar2(25),
CONSTRAINT playerinfo_pk PRIMARY KEY (school, player_number)
);
CREATE TABLE city
( city varchar2(30),
population number(5,0),
CONSTRAINT city_pk PRIMARY KEY (city)
);
/*Here is the failing alter command */
ALTER TABLE Player_Game
ADD CONSTRAINT playergame_fk FOREIGN KEY (school) REFERENCES game(school);
You have incorrect column list in playergame_fk in the alter table statement. The column list of the foreign key must exactly match with the column list of the primary key it is referencing to.
Primary Key column list is school, game_number, therefore, your foreign key must have the same columns:
ALTER TABLE Player_Game
ADD CONSTRAINT playergame_fk FOREIGN KEY (school, game_number)
REFERENCES game(school, game_number);

'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 do I fix a ORA-00902: invalid datatype creating a table

I am creating a table in a command SQL sections into a script already populated I have created several tables already but in this one I get a message saying
ORA-00902: invalid datatype
CREATE TABLE Weapons
(
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2 (10),
CONSTRAINT pk_Weapons PRIMARY_KEY(id),
CONSTRAINT fk_Weapons_company
FOREIGN_KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo
FOREIGN_KEY(ammo_id) REFERENCES Ammo(id)
);
In the CONSTRAINT, it should be FOREIGN KEY and not FOREIGN_KEY. Also it should be PRIMARY KEY, not PRIMARY_KEY.
There is no underscore required as per syntax. So the query will be:
CREATE TABLE Weapons (
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2(10),
CONSTRAINT pk_Weapons PRIMARY KEY(id),
CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES Ammo(id)
);
About Foreign Keys: https://www.techonthenet.com/oracle/foreign_keys/foreign_keys.php
About Primary Keys: https://www.techonthenet.com/oracle/primary_keys.php
Here's a working example. I've created the AMMO table (whose description you didn't post, so I used only the ID column so that the foreign key constraint wouldn't fail). Pay attention to comments I wrote within the code.
SQL> create table ammo
2 ( id VARCHAR2(10),
3 CONSTRAINT pk_ammo PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Company
2 ( id VARCHAR(3),
3 name VARCHAR(30), --> switch from CHAR to VARCHAR2
4 CONSTRAINT pk_Company PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Weapons
2 ( id NUMBER(4),
3 name VARCHAR2(30),
4 damage NUMBER(4),
5 company_id VARCHAR2(3), --> should match COMPANY.ID datatype
6 ammo_id VARCHAR2(10), --> should match AMMO.ID datatype
7 CONSTRAINT pk_Weapons PRIMARY KEY(id),
8 CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
9 CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES ammo(id) );
Table created.
SQL>
In referential integrity constraint, you should match datatypes of the foreign and primary key columns. There's no sense in having a VARCHAR2(10) in the detail table which points to a VARCHAR2(3) column in the master table; you won't be able to put anything longer than 3 characters into the detail table's column anyway (foreign key constraint won't let you).

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

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.

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.