Foreign key references mutiple columns in the database - sql

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);

Related

Referencing the primary key twice as foreign key in MS Access using SQL is causing an error

I get an error when I try to reference the primary key as a foreign key in other queries:
There is already a relationship named " DepartmentNameFK" in the current database.
CREATE TABLE PROJECT
(
ProjectID varchar(50),
Name varchar(50),
MaxHours FLOAT,
StartDate Date,
EndDate Date,
DepartmentName varchar(50),
CONSTRAINT ProjectIDPK PRIMARY KEY (ProjectID),
CONSTRAINT DepartmentNameFK FOREIGN KEY (DepartmentName) **[ so here I am referencing for the second time, THIS IS NOT PART OF CODE, JUST EXPLANATION]**
REFERENCES DEPARTMENT ON DELETE NO ACTION ON UPDATE CASCADE
);
The error seems very clear - you already have a foreign key constraint by the name of DepartmentNameFK - but all database objects (and constraints are database objects) must have a unique, separate name so you can refer to them.
So here - just call your FK constraint something else:
CREATE TABLE PROJECT
(
ProjectID varchar(50),
Name varchar(50),
MaxHours FLOAT,
StartDate Date,
EndDate Date,
DepartmentName varchar(50),
CONSTRAINT ProjectIDPK PRIMARY KEY (ProjectID),
-- just call the constraint **SOMETHING ELSE** other than "DepartmentNameFK"
CONSTRAINT DepartmentNameFK2
FOREIGN KEY (DepartmentName)
REFERENCES DEPARTMENT ON DELETE NO ACTION ON UPDATE CASCADE
);
My suggestion would be to name your FK constraints
FK_(parent table)_(referenced table)
so in your case:
FK_Project_Department
If your other FK is in another table, it would be
FK_OtherTable_Department
and thus you would not have any clashes of already existing names.....

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)
);

Use Foreign Key to access data on different database

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

Cannot create table. SQL Error 02270

This is the table I am trying to create. However, I get the error
SQL Error: ORA-02270: no matching unique or primary key for this column-list
SQL:
create table Meets_In
(
cid char(20),
rno integer,
time char(20),
CONSTRAINT PRIM_KEY PRIMARY KEY(time),
constraint meets_fk1 foreign key(cid) references COURSES(CID),
constraint meets_fk2 foreign key(rno) references ROOMS(RNO)
);
These are the parent tables:
create table Courses
(
cid char(20),
cname char(20),
credits integer,
constraint CoursesKey Primary Key (cid, cname)
);
CREATE TABLE ROOMS
(
rno INTEGER,
address CHAR(20),
capacity INTEGER,
CONSTRAINT room_key PRIMARY KEY(rno)
);
I don't understand why I am getting this error.
Cause
The ORA-2270, as the error message suggests, happens when there is no matching unique or primary key for this column-list. This could be because
the parent lacks a constraint altogether
the parent table's constraint is a compound key and we haven't referenced all the columns in the foreign key statement.
Now in your COURSES table, CID is not a primary key. It is a combination of cid,cname. So for every cid, there can be multiple rows.
Now when you reference cid as foreign key for meets_in, it will not work as it violates the second point as I mentioned above.
Workaround
Add column cname in your meets_in table as well. Then use it like below.
create table Meets_In
(
cid char(20) not null,
cname char(20),
rno integer not null,
time1 char(20) not null,
CONSTRAINT PRIM_KEY PRIMARY KEY(time1),
constraint meets_fk1 foreign key(cid,cname) references COURSES (cid,cname), /*Added cid,cname */
constraint meets_fk2 foreign key(rno) references ROOMS (RNO)
);
Meets_In is acting as an associative table. Therefore its primary key should include the foreign keys into the tables it is associating.
Try a primary key consisting of: cid, cname, rno and time.
As others have noted, your primary key for courses is (cid, cname), so you also need to include both of these in your foreign key constraint meets_fk1. Or, if possible, ensure that cid only is the primary key on courses.
(I think time may be a reserved word, so perhaps consider renaming it.)

Creating SQL table, foreign_key confusion

One question, I have supervisor set as foreign key.
He collect information through Activity Participant and take it from person ID.
Question:
How should I create the Activity table? what do I have to write down about supervisor?
CREATE TABLE activity
(
act_id VARCHAR(8) CONSTRAINT activity_pk PRIMARY KEY,
act_type VARCHAR2(20),
act_desc VARCHAR2(30),
act_date DATE,
mor_aft VARCHAR2(9),
CONSTRAINT activity_sup_fk FOREIGN KEY (act_supVisor) REFERENCES person()
);
A foreign key must reference a unique key of the referenced table. Either the table's primary key, or else a secondary unique key.
CONSTRAINT activity_sup_fk FOREIGN KEY (act_supVisor)
REFERENCES person(Person_id)