sql integrity 1-1 or 1-0 - sql

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.

Related

Foreign Key Reference

I addressed in class that the 2 foreign keys in the FlightData table are Depart_Code and Ariv_Code that there isn't any table to make references to them being a primary key in, in the relational schema we were given.
In class I was told that they reference Airport_Code in the Airport table. I was wondering I would go about doing that? I feel like I am missing something obvious. I appreciate any help offered I am still new to database in general and I am currently on Oracle 11g.
Airport table
CREATE TABLE Airport
(
Airport_Code VARCHAR2(7) CONSTRAINT pk_Airport Primary Key,
City_Code VARCHAR2(3),
CONSTRAINT fk_Airport_City_Code
FOREIGN KEY(City_Code) REFERENCES City,
Airport_Name VARCHAR2(30)
);
FlightData table:
CREATE TABLE FlightData
(
Flt_Nbr VARCHAR2(3) CONSTRAINT pk_FlightData Primary Key,
Depart_Code VARCHAR2(30),
Ariv_Code VARCHAR2(30)
);
To make sure Depart_Code and Ariv_Code always reference an airport in the Airport table you need to:
Make these columns NOT NULL.
Ensure they have the same data type as the key in Airport. Make them have a length of 7.
Add two foreign key constraints, each one based on each column.
For example, the second table could look like:
CREATE TABLE FlightData (
Flt_Nbr VARCHAR2(3) CONSTRAINT pk_FlightData Primary Key,
Depart_Code VARCHAR2(7) not null,
constraint fk1 foreign key (Depart_Code) references Airport (Airport_Code),
Ariv_Code VARCHAR2(7) not null,
constraint fk2 foreign key (Ariv_Code) references Airport (Airport_Code)
);

Translate the schema provided in into an “equivalent” relational schema (tables) in SQL

Person(p#,name,birthdate,nationality,gender)
Actor(p#,aguild#)
FK (p#) refs Person
Director(p#,dguild#)
FK (p#) refs Person
Writer(p#, wguild#)
FK (p#) refs Person
Studio(name)
ScreenPlay(title,year)
Authored(title,year,writer)
FK (title, year) refs ScreenPlay
FK (writer) refs Writer (p#)
Movie(title,studio,year,genre,director,length)
FK (studio) refs Studio (name)
FK (title, year) refs ScreenPlay
FK (director) refs Director (p#)
Cast(title,studio,year,role,actor,minutes)
FK (title, studio, year) refs Movie
FK (actor) refs Actor (p#)
Affiliated(director,studio)
FK (director) refs Director (p#)
FK (studio) refs Studio (name)
I am unable to make the table for the author, I get an error saying "does not conform to the description
of the parent key of table or nickname"
CREATE TABLE person (
p# int,
name varchar(255),
birthdate DATE,
nationality varchar(255),
gender varchar(255),
constraint person_pk
primary key (p#)
);
CREATE TABLE actor (
p# int,
aguild varchar(255),
constraint actor_pk   
primary key (p#),
constraint actor_fk_person
foreign key (p#) references person
);
CREATE TABLE director (
p# int,
dguild varchar(255),
constraint director_pk   
primary key (p#),
constraint director_fk_person
foreign key (p#) references person
);
CREATE TABLE writer (
p# int,
wguild varchar(255),
constraint writer_pk   
primary key (p#),
constraint writer_fk_person
foreign key (p#) references person
);
CREATE TABLE studio (
name varchar(255) not null,
constraint studio_pk   
primary key (name)
);
CREATE TABLE screenplay (
title varchar(255) not null,
year varchar(255) not null,
constraint screenplay_pk   
primary key (title, year)
);
CREATE TABLE authored (
title varchar(255) not null,
year varchar(255) not null,
writer varchar(255) not null,
constraint authored_pk
primary key (title,year,writer),
constraint authored_fk_titleyear
foreign key (title, year) references screenplay,
constraint authored_fk_writer
foreign key (writer) references writer (p#) on delete no action
);
CREATE TABLE movie (
title varchar(255) not null,
studio varchar(255) not null,
year varchar(255) not null,
genre varchar(255),
director varchar(255),
length varchar(255),
constraint movie_pk   
primary key (title, studio, year),
constraint movie_fk_studio
foreign key (studio) references studio(name),
constraint movie_fk_titleyear
foreign key (title, year) references screenplay,
constraint movie_director
foreign key (director) references director(p#)
);
CREATE TABLE cast (
title varchar(255) not null,
studio varchar(255) not null,
year varchar(255) not null,
role varchar(255) not null,
actor varchar(255) not null,
minutes varchar(255),
constraint cast_pk   
primary key (title, studio, year, role, actor),
constraint cast_fk_titlestudioyear
foreign key (title, studio, year) references movie,
constraint cast_fk_actor
foreign key (actor) references actor(p#)
);
CREATE TABLE affiliated (
director varchar(255) not null,
studio varchar(255) not null,
constraint affiliated_pk   
primary key (director, studio),
constraint affiliated_fk_director
foreign key (director) references director(p#)
constraint affiliated_fk_studio
foreign key (studio) references studio(name)
);
You have several elementary mistakes. You appear to be learning, so take the time to study basic rules of RDBMS and data modelling/normalisation.
It's unwise to use column names with special characters like # because it will give maintenance and usability issues. You are using "p#" to indicate a primary key that is a surrogate, but that is unwise, consider using a naming convention instead for column-names of surrogate key. For example : person_pk, actor_pk, director_pk, writer_pk.
Some of your tables use surrogate keys (p#) while others use natural keys(example studio.name). If you use natural-keys that are long (example varchar(255)), then child tables that use those natural-keys will use much more space and searches will be less efficient than if you use a surrogate key of type integer or bigint for the pk.
Next if you want a column to act as a primary key then it must be NOT NULL. some of your tables break this rule so will fail to create.
When you have a foreign key, its column(s) datatype(s) must match that of the relevant parent table column(s). Your example breaks that rule so you will get syntax errors.
authored_fk_writer needs to reference a key of writers but you have omitted any key field, and the relevant foreign key column is varchar(255) which does not correspond to the primary key of writer (which is int).
For some of your compound foreign keys that use natural-keys, the compound length exceeds documented limits. Consider normalising your model to use correct datatypes for the foreign key columns (i.e. don't duplicate the natural key in the child table, but instead use the surrogate key column of the parent table).
The clause 'references writer ()' is not correct in DB2. You must either not specify any columns including parentheses (the parent table must have PK in this case), or specify the corresponding columns of the parent table explicitly inside the parentheses (the parent table must have PK or Unique Constraint on them).
Check the REFERENCES clause syntax of the CREATE TABLE statement.

Make attribute primary and foreign key in sql

How can I make an attribute a primary key within a table but also as a foreign key which references another table using sql in sql developer?
I know how to make it an attribute as a foreign key and a primary separate but not as a primary key as well as a foreign key
That's perfectly normal. For example:
create table employee (
id number(6) primary key not null,
name varchar2(50)
);
create table employee_desk (
desk_id number(6) primary key not null, -- PK and FK!
location varchar2(20),
constraint fk1 foreign key (desk_id) references employee (id)
);
The column desk_id is the primary key of the table employee_desk, and also a foreign key that points to the table employee.
Below is the example of primary key with foreign key
create table animals (id integer primary key);
create table cats (
id integer primary key
, name varchar(100) not null
, constraint d_cats_animals_fk foreign key (id) references animals (id)
);

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.

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)