Creating SQL table, foreign_key confusion - sql

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)

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

sql integrity 1-1 or 1-0

I have a question on an exam that i don't know how to solve.
this is the question:
How do you keep the integrity of a 1-1 or 1-0 relationship between 2 tables?
Now a primary key and foreign key wouldn't work because then you can have a 1-m relationship. Maybe you can use unique in some way or alternative key?
We need to give the create table expressions and explain why this would work.
Here's an example of a 1-1 relationship in PostgreSQL. It requires the standard SQL feature of deferrable constraints:
create table employee (
id int primary key not null,
name varchar(20),
desk_id int not null,
constraint uq1 unique (desk_id),
constraint fk1 foreign key (desk_id) references desk (id)
deferrable initially immediate
);
create table desk (
id int primary key not null,
desk_code varchar(20).
employee_id int not null,
constraint uq2 unique (employee_id),
constraint fk2 foreign key (employee_id) references employee (id)
deferrable initially immediate
);
In simple English: every row of employee points to a desk. Every desk points to an employee.
Deferrable constraints are only implemented in PostgreSQL and Oracle, as far as I know.
I am going to give example from oracle
Here is 1:1 relationship example
create table state
(
sid number(3) ,
name varchar2(100),
gid number(3) not null,
constraint state_pk primary key (sid),
constraint gov_state_fk foreign key (gid) references gov(gid)
);
Here is 1:0..1 relationship (a.k.a. Surjection)
create table state
(
sid number(3) ,
name varchar2(100),
gid number(3) ,
constraint state_pk primary key (sid),
constraint gov_state_fk foreign key (gid) references gov(gid)
);
basicly what differs is,
In 1:0 relationship gid can be nullable
In 1:0..1 relationship gid cannot be null
EDIT: I also added Unicity for preserving 1:0..1 and 1:1 relationship.

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

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.

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