(Oracle) Adding a constraint after a table has been made - sql

I'm relatively new to Databases and Oracle and I'm atempting to make a database for a project involving 2 tables; Klant(customer) and Account_. These 2 both have each others PK as a foreign key. Since this is the case I had to make the foreign keys after the creation of the tables, resulting in this
DROP TABLE KLANT CASCADE CONSTRAINTS;
DROP TABLE ACCOUNT_ CASCADE CONSTRAINTS;
CREATE TABLE KLANT
(
"KlandId" INT PRIMARY KEY,
"AccountId" INT,
"Voornaam" VARCHAR2(64)NOT NULL,
"Achternaam" VARCHAR2(64) NOT NULL,
"GENDER" CHAR(1) DEFAULT 'M' CHECK (UPPER(GENDER) in ('M','F')),
"Tussenvoegsels" VARCHAR2(16),
"EmailAdres" VARCHAR2(64),
"Land" VARCHAR2(64) DEFAULT 'Nederland',
"Stad" VARCHAR2(64),
"Adres" VARCHAR2(64),
"Toevoeging" CHAR(1));
CREATE TABLE Account_
(
"AccountId" INT PRIMARY KEY,
"KlantId_" INT,
"GebruikersNaam" VARCHAR2(64)UNIQUE NOT NULL,
"Wachtwoord" VARCHAR2(64)
);
ALTER TABLE KLANT
ADD CONSTRAINT fk_accountId FOREIGN KEY (AccountId) REFERENCES "Account_"(AccountId);
ALTER TABLE Account_
ADD CONSTRAINT fk_klantId FOREIGN KEY (KlantId_) REFERENCES "Klant"(KlantId);
This yielded me the following error:
Error starting at line : 39 in command -
ALTER TABLE KLANT
ADD CONSTRAINT fk_accountId FOREIGN KEY (AccountId) REFERENCES "Account_"(AccountId)
Error report -
SQL Error: ORA-00904: "ACCOUNTID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Sorry for all the dutch words :(
My question being: Where did I mess up, because it probably is something riduculously stupid.

ALTER TABLE KLANT
ADD CONSTRAINT fk_accountId FOREIGN KEY ("AccountId") REFERENCES Account_("AccountId");
Name of the Account_ Table should not be in double quotes. Same for the other table .

Quote AccountId. In Oracle objects are generally uppercase. When you use AccountId without quoting it, Oracle will look for ACCOUNTID.

Related

I keep getting errors on my code SQL Oracle

So I've been working on this code for a few days now and I can't figure out why I'm getting errors. These are the errors I'm getting:
SQL Error: ORA-00904: "PATIENTID_FK": invalid identifier
00904. 00000 - "%s: invalid identifier"
and
SQL Error: ORA-02264: name already used by an existing constraint
02264. 00000 - "name already used by an existing constraint"
I've included the code that I wrote. The "..." symbolizes not important information from the table I was creating, such as "DoctorFirstName". Can someone help me figure out where I'm messing up? Thank you!
DROP TABLE HealthRecord;
DROP TABLE Patient;
DROP TABLE Insurance;
DROP TABLE Doctor;
DROP TABLE Hospital;
DROP TABLE Prescription;
CREATE TABLE Insurance (
InsuranceID number,
...
CONSTRAINT InsuranceID_pk
PRIMARY KEY (InsuranceID));
CREATE TABLE Prescription (
PrescriptionID number,
...
CONSTRAINT PrescriptionID_pk
PRIMARY KEY(PrescriptionID));
CREATE TABLE Hospital (
HospitalID number,
...
CONSTRAINT HospitalID_pk
PRIMARY KEY(HospitalID));
CREATE TABLE Doctor (
DoctorID number,
...
HospitalID number,
CONSTRAINT DoctorID_pk
PRIMARY KEY(DoctorID),
CONSTRAINT HospitalID_fk
FOREIGN KEY (HospitalID)
REFERENCES Hospital (HospitalID));
CREATE TABLE Patient (
PatientID number,
...
InsuranceID number,
CONSTRAINT PatientID_pk
PRIMARY KEY (PatientID),
CONSTRAINT InsuranceID_fk
FOREIGN KEY (InsuranceID)
REFERENCES Insurance(InsuranceID));
CREATE TABLE HealthRecord(
RecordID number,
...
DoctorID number,
PrescriptionID number,
PatientID number,
CONSTRAINT RecordID_pk
PRIMARY KEY(RecordID),
CONSTRAINT DoctorID_fk
FOREIGN KEY (DoctorID)
REFERENCES Doctor (DoctorID),
CONSTRAINT PrescriptionID_fk
FOREIGN KEY (PrescriptionID)
REFERENCES Prescription (PrescriptionID),
CONSTRAINT PatientID_fk
FOREIGN KEY (PatientID_fk)
REFERENCES Patient(PatientID));
Just dropping the table doesn't remove the constraints. So you constraints still exist. You should drop the constraints and indexes. Check this link out:
https://www.1keydata.com/sql/alter-table-drop-constraint.html
Reference the link below for dropping tables and the constraint in one statement (CASCADE CONSTRAINTS)
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9003.htm

Adding constraints on table

I am new to SQL Oracle.I have the following script:
create table students(
sid char(10),
honors char(10) not null,
primary key (sid),
Constraint studentConst foreign key (honors) references Courses(cid),
);
create table Courses(
cid char(10),
grader char(20) not null,
primary key (cid),
Constraint CoursesConst foreign key (grader) references students(sid),
);
SET CONSTRAINT studentConst,CoursesConst DEFERRED;
I get the following error on running the above script:
SQL Error: ORA-00904: : invalid identifier on line 5. Why do I get this error ?
I don't think you can create a foreign key constraint on a table that doesn't yet exist.
Since you have a two-way constraint, you'll need to create the first table without the constraint, then add it with alter table after the second table has been created.
Deferred constraints are for checking data. Deferral simply means the check won't be carried out until the end of the transaction. It does not mean "defer the creation of the constraints so I can set up a circular reference" :-)
It looks like line 5 is trying to reference Courses(cid). However, at this point, the Courses table does not exist, as it's created in the following SQL block.
Try creating dependent tables first.
Add the constraint after the tables are built. You can do this using an alter table statement:
create table students(
sid char(10),
honors char(10) not null,
primary key (sid)
);
create table Courses(
cid char(10),
grader char(20) not null,
primary key (cid),
Constraint CoursesConst foreign key (grader) references students(sid)
);
alter table students add constraint studentConst foreign key (honors) references Courses(cid)
The SQLFiddle is here.

How to set keys to a relationship table in Oracle10g

Please before judging bear in mind that I don't understand that's why I ask the question. I am discouraged by some down voting that has occured in the past.
I have to create a database in Oracle 10g and I am using the template to create a new table.
The database is about an imaginary facebook application.
Amongst others it includes relationship "like" with 3 attributes all of which are supposed to be keys:
like:
username: key
message_author: key
message_code: key
as I said like is a relationship between two entities user and message.
I tried to setup the table like as 3 foreign keys connected to the other tables but oracle will not accept it.
I get the following error message:
Failed Creating FK like_FK2 ORA-02270: no matching unique or primary key for this column-list
ORA-00942: table or view does not exist
ORA-06510: PL/SQL: unhandled user-defined exception
I suppose I am doing something wrong with the keys, but I can't understand what it is
SQL commands:
CREATE table "ΑΡΕΣΕΙ" (
"ΟΝΟΜΑΧΡΗΣΤΗ" VARCHAR2(30) NOT NULL,
"ΣΥΓΦΕΑΣ_ΜΗΝΤΟΣ" VARCHAR2(30) NOT NULL,
"ΚΩΔΜΗΝΥΜΑΤΟΣ" NUMBER NOT NULL
)
/
ALTER TABLE "ΑΡΕΣΕΙ" ADD CONSTRAINT "ΑΡΕΣΕΙ_FK"
FOREIGN KEY ("ΟΝΟΜΑΧΡΗΣΤΗ")
REFERENCES "ΧΡΗΣΤΗΣ" ("ΟΝΟΜΑΧΡΗΣΤΗ")
/
ALTER TABLE "ΑΡΕΣΕΙ" ADD CONSTRAINT "ΑΡΕΣΕΙ_FK2"
FOREIGN KEY ("ΣΥΓΦΕΑΣ_ΜΗΝΤΟΣ")
REFERENCES "ΜΗΝΥΜΑ" ("ΣΥΓΦΕΑΣ_ΜΗΝΤΟΣ")
/
ALTER TABLE "ΑΡΕΣΕΙ" ADD CONSTRAINT "ΑΡΕΣΕΙ_FK3"
FOREIGN KEY ("ΚΩΔΜΗΝΥΜΑΤΟΣ")
REFERENCES "ΜΗΝΥΜΑ" ("ΚΩΔΜΗΝΥΜΑΤΟΣ")
/
alter table "ΑΡΕΣΕΙ" add
constraint ΑΡΕΣΕΙ_UK1
unique ("ΚΩΔΜΗΝΥΜΑΤΟΣ")
/
Before you specify any relationship constraints and other constraints like UNIQUE, make sure that the tables exist before you code and run these statements. I am editing your code a little bit in my answer:
CREATE table "ΑΡΕΣΕΙ" (
"ΟΝΟΜΑΧΡΗΣΤΗ" VARCHAR2(30) NOT NULL,
"ΣΥΓΦΕΑΣ_ΜΗΝΤΟΣ" VARCHAR2(30) NOT NULL,
"ΚΩΔΜΗΝΥΜΑΤΟΣ" NUMBER(10,2) NOT NULL -- PRECISION AND SCALE MUST BE MENTIONED FOR NUMBER DATA TYPE
)
/
CREATE table "ΜΗΝΥΜΑ" (
"ΣΥΓΦΕΑΣ_ΜΗΝΤΟΣ" VARCHAR2(30) -- data types are recommended to be the same
"ΚΩΔΜΗΝΥΜΑΤΟΣ" NUMBER(10,2)
-- Other columns go here
)
/
CREATE TABLE "ΧΡΗΣΤΗΣ" (
"ΟΝΟΜΑΧΡΗΣΤΗ" VARCHAR(30)
-- Other columns go here
)
/
-- After you create all the tables, you can code the alter table statements
-- that would add all your necessary constraints.
ALTER TABLE "ΑΡΕΣΕΙ" ADD CONSTRAINT "ΑΡΕΣΕΙ_FK"
FOREIGN KEY ("ΟΝΟΜΑΧΡΗΣΤΗ")
REFERENCES "ΧΡΗΣΤΗΣ" ("ΟΝΟΜΑΧΡΗΣΤΗ")
/
ALTER TABLE "ΑΡΕΣΕΙ" ADD CONSTRAINT "ΑΡΕΣΕΙ_FK2"
FOREIGN KEY ("ΣΥΓΦΕΑΣ_ΜΗΝΤΟΣ")
REFERENCES "ΜΗΝΥΜΑ" ("ΣΥΓΦΕΑΣ_ΜΗΝΤΟΣ")
/
ALTER TABLE "ΑΡΕΣΕΙ" ADD CONSTRAINT "ΑΡΕΣΕΙ_FK3"
FOREIGN KEY ("ΚΩΔΜΗΝΥΜΑΤΟΣ")
REFERENCES "ΜΗΝΥΜΑ" ("ΚΩΔΜΗΝΥΜΑΤΟΣ")
/
alter table "ΑΡΕΣΕΙ" add
constraint ΑΡΕΣΕΙ_UK1
unique ("ΚΩΔΜΗΝΥΜΑΤΟΣ")
/
Also, keep in mind that there is a constraint called PRIMARY KEY which allows you to put unique and not null constraints on a column at the same time. I recommend you read the documentation provided by Oracle at docs.oracle.com for your reference.
Also, I hope you mention all your table names and column names in ASCII (a-z, A-Z, 0-9) characters. Unicode characters as object identifiers may be supported but are difficult to maintain.

Invalid Field Definition

I'm trying to create a table with a foreign key using SQL command but I keep getting this error
Invalid field definition 'CUS_CODE' in definition of index or relationship.
I'm using this command:
CREATE TABLE INVOICE(
INV_NUMBER CHAR(5) NOT NULL,
INV_DATE DateTime NOT NULL,
CONSTRAINT INV_PK PRIMARY KEY(INV_NUMBER),
CONSTRAINT INV_FK FOREIGN KEY(CUS_CODE) REFERENCES CUSTOMER(CUS_CODE)
);
It's because Cus_code is not a field in your invoice table.
It should be :
FOREIGN KEY(Your column name in your invoice table) REFERENCES Customer(Cus_Code)
Here's an tutorial about SQL FOREIGN KEY
It should help you to illustrate what it should looks like

Invalid datatype when adding constraint in Oracle

I want to create this table in Oracle. This is only the table SQL script.
-- CREATE TABLES SECTION -------------------------------------------------
-- TABLE DATACENTER
CREATE TABLE DATACENTER(
DATACENTERID INTEGER NOT NULL,
NAME VARCHAR2(80 ) NOT NULL,
LOCATION VARCHAR2(200 ),
DCALLOWEDWEIGHTKG NUMBER(9,0),
DCMAXIMUMWEIGHTKG NUMBER(9,0),
DCALLOWEDPOWERWATT NUMBER(9,0),
DCMAXPOWERWATT NUMBER(9,0),
DCALLOWCOOLINGPOWERBTU NUMBER(9,0),
DCMAXCOOLINGPOWERBTU NUMBER(9,0),
DESCRIPTION CLOB
)
/
-- ADD KEYS FOR TABLE DATACENTER
ALTER TABLE DATACENTER ADD CONSTRAINT DATACENTERID PRIMARY KEY (DATACENTERID)
/
-- TABLE COMPONENT
CREATE TABLE COMPONENT(
COMPONENTID INTEGER NOT NULL,
DATACENTERID INTEGER,
FKCOMPONENTID INTEGER,
COMPONENTSTATSID INTEGER NOT NULL
)
/
-- ADD KEYS FOR TABLE COMPONENT
ALTER TABLE COMPONENT ADD CONSTRAINT COMPONENTID PRIMARY KEY (COMPONENTID)
/
-- CREATE RELATIONSHIPS SECTION -------------------------------------------------
ALTER TABLE COMPONENT ADD CONSTRAINT IS PART OF A FOREIGN KEY (DATACENTERID) REFERENCES DATACENTER (DATACENTERID)
/
ALTER TABLE COMPONENT ADD CONSTRAINT IS A SUBPART OF FOREIGN KEY (FKCOMPONENTID) REFERENCES COMPONENT (COMPONENTID)
/
The error stack:
table DATACENTER created.
table DATACENTER altered.
table COMPONENT created.
table COMPONENT altered.
Error starting at line 39 in command:
ALTER TABLE COMPONENT ADD CONSTRAINT IS PART OF A FOREIGN KEY (DATACENTERID) REFERENCES DATACENTER (DATACENTERID)
Error report:
SQL Error: ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Error starting at line 42 in command:
ALTER TABLE COMPONENT ADD CONSTRAINT IS A SUBPART OF FOREIGN KEY (FKCOMPONENTID) REFERENCES COMPONENT (COMPONENTID)
Error report:
SQL Error: ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
EDIT I edited the code because I saw that some SQL statements are missing.
I can provide the schema if you want?
Best wishes
Your constraint name IS PART OF A is an illegal identifier (because of the spaces).
You need to remove the spaces, e.g. IS_PART_OF_A:
ALTER TABLE COMPONENT
ADD CONSTRAINT IS_PART_OF_A
FOREIGN KEY (DATACENTERID)
REFERENCES DATACENTER (DATACENTERID)
Edit
I also realized that your script is not correctly terminating the individual statements. There is a ; (or /) missing after the first CREATE TABLE. And the first ALTER TABLE is not properly terminated as well. I don't know if that is a copy & paste error though.
Edit2:
Here is the complete script with correct names, statement termination and in the correct order:
CREATE TABLE COMPONENT(
COMPONENTID INTEGER NOT NULL,
DATACENTERID INTEGER,
FKCOMPONENTID INTEGER,
COMPONENTSTATSID INTEGER NOT NULL
)
/
CREATE TABLE DATACENTER(
DATACENTERID INTEGER NOT NULL,
NAME VARCHAR2(80 ) NOT NULL,
LOCATION VARCHAR2(200 ),
DCALLOWEDWEIGHTKG NUMBER(9,0),
DCMAXIMUMWEIGHTKG NUMBER(9,0),
DCALLOWEDPOWERWATT NUMBER(9,0),
DCMAXPOWERWATT NUMBER(9,0),
DCALLOWCOOLINGPOWERBTU NUMBER(9,0),
DCMAXCOOLINGPOWERBTU NUMBER(9,0),
DESCRIPTION CLOB
)
/
ALTER TABLE DATACENTER ADD CONSTRAINT DATACENTERID PRIMARY KEY (DATACENTERID)
/
ALTER TABLE COMPONENT ADD CONSTRAINT COMPONENTID PRIMARY KEY (COMPONENTID)
/
ALTER TABLE COMPONENT ADD CONSTRAINT IS_PART_OF_A FOREIGN KEY (DATACENTERID) REFERENCES DATACENTER (DATACENTERID)
/