Use Foreign Key to access data on different database - sql

I have a central database Student that all my other tables Foriegn Key to, which in turn Foreign Keys to various records in my Courses database.
CREATE TABLE student(
FIRST_NAME varchar(50),
LAST_NAME varchar(50),
OSIS number(10,0),
OFCL varchar(5),
GRADE number(4,0),
COURSE_0 varchar(5),
COURSE_1 varchar(5),
COURSE_2 varchar(5),
COURSE_3 varchar(5),
COURSE_4 varchar(5),
COURSE_5 varchar(5),
COURSE_6 varchar(5),
COURSE_7 varchar(5),
COURSE_8 varchar(5),
COURSE_9 varchar(5),
COURSE_10 varchar(5),
CONSTRAINT pk_student PRIMARY KEY (OSIS),
CONSTRAINT course_0 FOREIGN KEY (COURSE_0) REFERENCES course(COURSE_ID),
CONSTRAINT course_1 FOREIGN KEY (COURSE_1) REFERENCES course(COURSE_ID),
CONSTRAINT course_2 FOREIGN KEY (COURSE_2) REFERENCES course(COURSE_ID),
CONSTRAINT course_3 FOREIGN KEY (COURSE_3) REFERENCES course(COURSE_ID),
CONSTRAINT course_4 FOREIGN KEY (COURSE_4) REFERENCES course(COURSE_ID),
CONSTRAINT course_5 FOREIGN KEY (COURSE_5) REFERENCES course(COURSE_ID),
CONSTRAINT course_6 FOREIGN KEY (COURSE_6) REFERENCES course(COURSE_ID),
CONSTRAINT course_7 FOREIGN KEY (COURSE_7) REFERENCES course(COURSE_ID),
CONSTRAINT course_8 FOREIGN KEY (COURSE_8) REFERENCES course(COURSE_ID),
CONSTRAINT course_9 FOREIGN KEY (COURSE_9) REFERENCES course(COURSE_ID),
CONSTRAINT course_10 FOREIGN KEY (COURSE_10) REFERENCES course(COURSE_ID)
)
CREATE TABLE course(
TITLE varchar(25),
COURSE_ID varchar(5),
PERID number(10,0),
OSIS number(10,0),
CONSTRAINT fk_teacher FOREIGN KEY (OSIS) REFERENCES teacher(OSIS),
CONSTRAINT pk_course PRIMARY KEY(COURSE_ID)
)
I also have an Absent database that consists of Foreign Keys to the Student database.
CREATE TABLE absent(
OSIS number(10,0),
CONSTRAINT student_absent FOREIGN KEY (OSIS) REFERENCES student(OSIS)
)
I'm looking for a way to access data on Student using Foreign Keys on Absent. For example, if I had an OSIS number 1234567890 on Student and Absent, I'd like to access data paired with 1234567890 on Student using the 1234567890's Foreign Key on Absent. I've found relatively little documentation on invoking Foreign Keys (only setting them up), so what steps should I take to invoke Absent's Foreign Keys?

Foreign keys are constraints not program constructs. They prevent us inserting a record in absence which doesn't reference an existing record in student.
So all you need to do is join the tables:
select s.*
from student s
join absence a
on a.osis = s.osis
where a.osis = 124466 -- or whatever
Incidentally all those course columns are really ugly: you should normalize the data model and introduce an intersection table, say student_course, between the two tables

Related

Foreign key references mutiple columns in the database

When I get my EER converted to the relational schema I got this table which has a composite primary key like following,
CREATE TABLE branch
(
branch_number varchar(20),
b_code varchar(20),
name varchar(20),
address varchar(20),
--CONSTRAINTS
CONSTRAINT pk_branch PRIMARY KEY(branch_number,b_code),
CONSTRAINT fk_branch FOREIGN KEY(b_code) REFERENCES bank(b_code)
)
This above table is a weak entity and therefore it's having a key combination as a primary key. (due to the identifying relationship)
Then this branch table is in a one-to-many relation with the following table
CREATE TABLE account
(
account_no varchar(20),
balance float,
branch_number varchar(20),
b_code varchar(20),
a_code varchar(20),
--CONSTRAINTS
CONSTRAINT pk_account PRIMARY KEY(account_no),
CONSTRAINT fk_account_1 FOREIGN KEY(b_code) REFERENCES branch(b_code),
CONSTRAINT fk_account_2 FOREIGN KEY(branch_number) REFERENCES branch(branch_number),
CONSTRAINT fk_account_3 FOREIGN KEY(a_code) REFERENCES account_type(a_code)
)
How do I make the relation between these two tables? How to implement the foreign key for the account table with branch when I have one column referencing to two other columns?
Your fk_account_1 will fail when you try to compile it because b_code is not a primary or unique key on table branch. Same problem with fk_account_2, because branch_number is not primary or unique on branch.
The primary key on branch is (branch_number,b_code) so your foreign key should be
CONSTRAINT FK_ACCOUNT_1
FOREIGN KEY (BRANCH, B_CODE)
REFERENCES BRANCH (BRANCH, B_CODE);

Problems when creating a table referencing other with FK

I'm trying to create a database for a project that works like a school with some teachers,courses,classes etc.
I need to create a new Table involving 2 tables however I'm getting an error when creating the table Classes_Teachers.
There are no primary or candidate keys in the referenced table 'Classes' that match the referencing column list in the foreign key 'FK__Classes_T__Class__11007AA7'.
Here is my SQL Code:
CREATE TABLE Teachers(
id INT,
name varchar(40),
email varchar(30) FOREIGN KEY REFERENCES Users(email),
PRIMARY KEY(id)
);
CREATE TABLE Courses(
name varchar(20),
acr varchar(4),
teacher int FOREIGN KEY REFERENCES Teachers(id),
PRIMARY KEY(acr)
);
CREATE TABLE Classes(
id varchar(2),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
year_Semesters varchar(5),
PRIMARY KEY(id,courses_acronym),
);
CREATE TABLE Classes_Teacher(
Classes_id varchar(2) FOREIGN KEY REFERENCES Classes(id),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Classes(courses_acronym),
PRIMARY KEY(Classes_id,courses_acronym)
);
The error comes from the foreign keys declared in table Classes_Teacher. To relate to the Classes table, you want to use both primary columns of the referred tables, instead of twn separated foreign keys.
Consider:
CREATE TABLE Classes_Teacher(
Classes_id varchar(2),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4),
FOREIGN KEY (Classes_id, courses_acronym) REFERENCES Classes(id, courses_acronym),
PRIMARY KEY(Classes_id,courses_acronym)
);
Demo on DB Fiddle.
In table Classes you define a Primary Key on two columns. This is probably not needed if courses_acronym depends on ID.
CREATE TABLE Classes(
id varchar(2),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
year_Semesters varchar(5),
PRIMARY KEY(id),
);
In table Classes_Teacher you refer Classes twice. Shouldn't the second Foreign Key refer Courses?
CREATE TABLE Classes_Teacher(
Classes_id varchar(2) FOREIGN KEY REFERENCES Classes(id),
Teachers_id INT FOREIGN KEY REFERENCES Teachers(id),
courses_acronym varchar(4) FOREIGN KEY REFERENCES Courses(acr),
PRIMARY KEY(Classes_id,courses_acronym)
);

Oracle error building table from 2 foreign keys

I'm in the process of building a database and have already successfully created 2 primary key tables however when I try to bring them in as 2 foreign keys to another table I am running into a
"CLIENTID" invalid identifier
Unsure how to resolve as not the best at this.
CREATE TABLE Booking(
BookingID number(10) NOT NULL,
CONSTRAINT Client_FK FOREIGN KEY (ClientID) REFERENCES client (ClientID),
CONSTRAINT Course_FK FOREIGN KEY (CourseID) REFERENCES course (CourseID),
CONSTRAINT Booking_PK PRIMARY KEY (ClientID, CourseID)
);
You are missing the columns on which your primary key and foreign keys are created; you need something like the following, to be edited with the right type for your columns:
CREATE TABLE Booking(
BookingID number(10) NOT NULL,
ClientId number, -- missing
CourseID number, -- missing
CONSTRAINT Client_FK FOREIGN KEY (ClientID) REFERENCES client (ClientID),
CONSTRAINT Course_FK FOREIGN KEY (CourseID) REFERENCES course (CourseID),
CONSTRAINT Booking_PK PRIMARY KEY (ClientID, CourseID)
)

Recursive relationship SQL error

I am pretty new to SQL and I have a problem. I want to make a recursive relationship (a table that relates to itself), but I get an error when I try to execute my code. It's working fine without the Coordinator_Office_ID foreign key.
The error is:
The number of columns in the foreign-key referencing list is not equal
to the number of columns in the referenced list.
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade,
Constraint lg_cord_id Foreign key (Coordinator_Office_ID) References Logistican(Office_ID)
);
Yes, that's cause you have defined composite primary key like Primary key (Office_ID, Worker_ID) and thus your FK should include both of them else it will result in PFD (partial functional dependency)
Add the constraint with alter table:
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade
);
alter table Logistican
add Constraint lg_cord_id
Foreign key (Coordinator_Office_ID, Worker_Id) References Logistican(Office_ID, Worker_Id);
The relationship needs all elements of the primary key to be valid. I'm not sure if it needs to be a separate statement in Oracle.

Invalid FK and PK reference

Unable to create table as oracle shows ' no matching unique or primary key for this column-list' when I did label the primary key reference for the required table.
First table created successfully:
CREATE TABLE TEST
(
TESTno VARCHAR2(6) NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TEST_Date DATE NOT NULL,
ACTUAL DATE,
PREDICTED Date,
CONSTRAINT TESTPKs PRIMARY KEY (TEST_Date, TESTno, ExamNo),
CONSTRAINT TTESTNO_Fk FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
Here's the table i want to create and gives me error:
CREATE TABLE Assignment
(
TEST_Date DATE NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TestNo VARCHAR2(6) NOT NULL,
Type VARCHAR2(20),
Hours_Spent Decimal(4,2),
CONSTRAINT ASSIGNPKS PRIMARY KEY (TEST_Date, TestNo , ExamNo),
CONSTRAINT ASSIGNTESTDATE_FK FOREIGN KEY (TEST_Date) REFERENCES TEST(TEST_Date) ON
DELETE CASCADE,
CONSTRAINT ASSIGNTESTNO_FK FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
May i know where's the issue that it keeps giving me no matching unique primary keys? I already tried to recreate and labelled the 'test_Date' as my primary key. But oracle can't seems to find.
Thanks
The PK you refer to is PRIMARY KEY (TEST_Date, TESTno, ExamNo) — hence the foreign key should be FOREIGN KEY (TEST_Date, TESTno, ExamNo) as well. The error you're getting is due to your attempt to refer to a part of TEST's PK.
See also http://download.oracle.com/docs/cd/B10500_01/server.920/a96524/c22integ.htm
Check the tables you are referencing in your foreign keys. Those columns must be the primary key or otherwise unique on the foreign table.