How can I reference 2 different columns in 2 different tables simultaneously - database

This is an assignment and I have been given this diagram for reference - https://imgur.com/gallery/wRkArmQ
There are other tables that I will include but the issue seems to be with the tables section and time_slot. In the diagram, they reference each other but I don't know how to. Any comments are helpful! I am using Oracle btw, here is the code -
create table department (
dept_name varchar(15) primary key,
building varchar(10),
budget int
);
create table instructor (
id varchar(10) primary key,
name varchar(15) not null,
dept_name varchar(20) not null
references department(dept_name) on delete cascade,
salary int not null
);
create table course (
course_id varchar(10) primary key,
title varchar(10),
dept_name varchar(15)
references department(dept_name) on delete cascade,
credits int
);
create table classroom (
building varchar(10) not null,
room_no varchar(5) not null,
capacity int not null,
primary key (building, room_no)
);
create table section (
course_id varchar(10) not null
references course(course_id),
sec_id varchar(10) not null,
semester varchar(10) not null,
year varchar(10) not null,
building varchar(10) not null
references classroom(building),
room_no varchar(5) not null
references classroom(room_no),
time_slot_id varchar(5) not null
references time_slot(time_slot_id),
primary key (course_id, sec_id, semester, year)
);
create table time_slot (
time_slot_id varchar(5) not null
references section(time_slot_id),
day varchar(10) not null,
start_time date not null,
end_time date not null,
primary key (time_slot_id, day, start_time)
);
create table teaches (
id varchar(10) primary key
references instructor(id) on delete cascade,
course_id varchar(15) not null
references section(course_id),
sec_id varchar(5) not null
references section(sec_id),
semester varchar(15)
references section(semester),
year int not null
references section(year)
);
create table student (
id varchar(10) not null primary key,
name varchar(20) not null,
dept_name varchar(15) not null
references department(dept_name) on delete cascade,
tot_cred int not null
);
create table advisor (
s_id varchar(10) primary key
references student(id) on delete cascade,
i_id varchar(6)
references instructor(id) on delete cascade
);
create table prereq (
course_id varchar(10)
references course(course_id) on delete cascade,
prereq_id varchar(10)
references course(course_id) on delete cascade,
primary key (course_id, prereq_id)
);
create table takes (
id varchar(10) not null
references student(id) on delete cascade,
course_id varchar(10) not null
references section(course_id) on delete cascade,
sec_id varchar(10) not null
references section(sec_id) on delete cascade,
semester varchar(10) not null
references section(semester) on delete cascade,
year int not null
references section(year) on delete cascade,
grade varchar(10) not null,
primary key (id, course_id, sec_id, semester, year, grade)
);
This is the output -
Table created.
Table created.
Table created.
Table created.
ORA-02270: no matching unique or primary key for this column-list
ORA-00942: table or view does not exist
ORA-00942: table or view does not exist

Related

Associative table is erroring

I have a school project where I need to create a database in SQL Server. I am currently having issues entering in my tables.
I have successfully entered in the first two tables, but the associative table is erroring, as are other tables following.
create table Home
(
HomeID INT Identity (1,1) Primary key,
Address1 varchar(250) not null,
City varchar(25) not null,
State1 varchar(25) not null,
Zipcode CHAR(5) not null,
SmokeDetectors INT,
Co2Detectors INT,
SecuritySystem CHAR(1),
TotalSqFt int,
Rooms INT,
AdditionalBuilding CHAR(1),
AssessedValue DEC(10,2),
YearAssessed char(4),
CompanyID int not null,
)
create table MortgageCompany
(
CompanyID INT Identity (1,1) Primary key,
CompanyName varchar(100) not null,
Address1 varchar(250),
City varchar(25),
State1 varchar(25),
Zipcode char(5),
HomeID int not null
)
create table HomeMortgageCompany
(
HomeID INT,
CompanyID INT,
foreign key CompanyID references MortgageCompany(CompanyID),
foreign key HomeID references Home(HomeID),
Primary key (HomeID,CustomerID)
)
create table Loan
(
LoanID INT Identity (1,1) Primary key,
LoanNumber int not null,
MortgageAmount dec(10,2),
CompanyID int
foreign key CompanyID references MortgageCompany(CompanyID)
)
create table PersonalProperty
(
PropertyID INT Identity (1,1) Primary key,
ItemName int,
Type1 varchar(150) not null,
Value1 Dec(10,2) not null,
HomeID int
foreign key HomeID references Home(HomeID)
)
create table Policy
(
PolicyID INT Identity (1,1) Primary key not null,
PolicyYear char(4),
PremiumCharged dec(10,2),
EffectiveDate date not null,
ExpirationDate date not null,
ReviewDateTime datetim,
Decision char(8),
HomeID int
foreign key HomeID references Home(HomeID)
)
create table Bank
(
AccountID int Identity (1,1) Primary Key,
BankName varchar(150) not null,
AccountNumber int not null,
RoutingNumber int not null,
Amount Dec(10,2),
PolicyID int not null
)
create table PolicyBank
(
PolicyID int,
AccountID INT,
foreign key PolicyID references Policy(PolicyID),
foreign key AccountID references Bank(AccountID),
Primary Key (PolicyID, AccountID)
)
Create table PolicyHolder
(
SSN char(9) Primary key not null,
FirstName varchar(50) not null,
LastName varchar(50) not null,
)
create table PolicyPolicyHolder
(
SSN char(9),
PolicyID INT,
foreign key PolicyID references Policy(PolicyID),
foreign key SSN references PolicyHolder(SSN),
Primary key (SSN, PolicyID)
)
create table HomePolicyHolder
(
SSN char(9),
HomeID Int,
foreign key HomeID references Home(HomeID),
foreign key SSN references PolicyHolder(SSN),
Primary key (SSN, HomeID)
)
create table Employment
(
SSN char(9) Primary Key,
EmployerFirstName varchar(50)not null,
EmployerLastName varchar(50) not null,
Address1 varchar(250),
City varchar(25),
State1 varchar(25),
Zipcode char(5),
StartDate date,
CurrentSalary dec(10,2) not null,
JobTitle varchar (75),
RecordExtractionDate date not null,
foreign key SSN references PolicyHolder(SSN)
)
Create table Document
(
DocumentID INT Identity (1,1) Primary key,
DocumentType varchar(50),
DateTimeSent datetime not null,
SSN char(9) not null,
PolicyID int,
foreign key PolicyID references Policy(PolicyID)
)
create table PolicyHolderDocument
(
DocumentID int,
SSN char(9),
foreign key DocumentID references Document(DocumentID),
foreign key SSN references PolicyHolder(SSN)
);
For the third table definition:
create table HomeMortgageCompany (
HomeID INT,
CompanyID INT,
foreign key CompanyID references MortgageCompany(CompanyID),
foreign key HomeID references Home(HomeID),
Primary key (HomeID,CustomerID)
)
Problems with this code:
the column name in the defina of the foreign key needs to be surrounded with parentheses
the primary key refers to column CustomerID, but there is no column in the table
Presumably, you meant:
create table HomeMortgageCompany (
HomeID INT,
CompanyID INT,
foreign key (CompanyID) references MortgageCompany(CompanyID),
foreign key (HomeID) references Home(HomeID),
Primary key (HomeID,CompanyID)
)

Foreign key reference mismatched data type error

The ArtistID column in Piece table refers to the ArtistID in the Artist table. Likewise, the LocationID column in Piece refers to LocationID in the GeographicLocation table. However, both foreign key references throw a "not the same data type as referencing column" error. What am I doing wrong?
CREATE TABLE dbo.Artist
(
ArtistID SMALLINT PRIMARY KEY IDENTITY,
LastName VARCHAR(50) NOT NULL,
FirstName VARCHAR(40) NOT NULL,
Nationality VARCHAR(10) NULL,
BirthYear SMALLINT NOT NULL CHECK(BirthYear <= 1980),
DeathYear SMALLINT NULL,
Sex CHAR(1) NOT NULL CHECK(Sex = 'F' OR Sex = 'M')
)
CREATE TABLE dbo.Piece
(
PieceID SMALLINT PRIMARY KEY IDENTITY(1, 5),
ArtistID SMALLINT NOT NULL
FOREIGN KEY REFERENCES Artist(ArtistID),
LocationID SMALLINT NOT NULL
FOREIGN KEY REFERENCES GeographicLocation(LocationID),
CommonName VARCHAR(100) NULL,
YearProduced TINYINT NULL,
Period VARCHAR(50) NULL,
Medium VARCHAR(35) NOT NULL,
Frame VARCHAR(35) NULL,
AppraisedValue MONEY NOT NULL,
AppraiserID SMALLINT NOT NULL
FOREIGN KEY REFERENCES Appraiser(AppraiserID)
)
CREATE TABLE dbo.GeographicLocation
(
LocationID SMALLINT PRIMARY KEY IDENTITY,
Country VARCHAR(25) NOT NULL,
City VARCHAR(50) NOT NULL
)
I think you missed a table here, which is AppraiserID column in Piece table refers to the AppraiserID in the Appraisertable
So, when I execute your SQL I am getting below error:
Foreign key 'FK__Piece__Appraiser__322C6448' references invalid table 'Appraiser'.
First Create a table called Appraiser, then create Foreign key references for those column.
Below SQL I am able to create all tables and Foreign key references:
CREATE TABLE dbo.Artist
(ArtistID SMALLINT PRIMARY KEY IDENTITY,
LastName VARCHAR(50) NOT NULL,
FirstName VARCHAR(40) NOT NULL,
Nationality VARCHAR(10) NULL,
BirthYear SMALLINT NOT NULL CHECK(BirthYear <= 1980),
DeathYear SMALLINT NULL,
Gender CHAR(1) NOT NULL CHECK(Gender = 'F' OR Gender = 'M'))
CREATE TABLE dbo.GeographicLocation
(LocationID SMALLINT PRIMARY KEY IDENTITY,
Country VARCHAR(25) NOT NULL,
City VARCHAR(50) NOT NULL)
CREATE TABLE dbo.Appraiser
(AppraiserID SMALLINT PRIMARY KEY IDENTITY(1, 5),
AppraisedValue MONEY NOT NULL,
AppraisedName VARCHAR(100) NULL)
CREATE TABLE dbo.Piece
(PieceID SMALLINT PRIMARY KEY IDENTITY(1, 5),
ArtistID SMALLINT NOT NULL,
LocationID SMALLINT NOT NULL,
CommonName VARCHAR(100) NULL,
YearProduced TINYINT NULL,
Period VARCHAR(50) NULL,
Medium VARCHAR(35) NOT NULL,
Frame VARCHAR(35) NULL,
AppraisedValue MONEY NOT NULL,
AppraiserID SMALLINT NOT NULL)
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_ArtistID FOREIGN KEY(ArtistID)
REFERENCES dbo.Artist (ArtistID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_ArtistID
GO
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_AppraiserID FOREIGN KEY(AppraiserID)
REFERENCES dbo.Appraiser (AppraiserID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_AppraiserID
GO
ALTER TABLE dbo.Piece WITH CHECK ADD CONSTRAINT FK_Piece_LocationID FOREIGN KEY(LocationID)
REFERENCES dbo.GeographicLocation (LocationID)
GO
ALTER TABLE dbo.Piece CHECK CONSTRAINT FK_Piece_LocationID
GO

Percent revenue increase since previous year per country

I need to get the percentage increase revenue since last year, per country.
create table Customer
(
customer_mail_address varchar(255) not null,
lastname varchar(50) not null,
firstname varchar(50) not null,
payment_method varchar(10) not null,
payment_card_number varchar(30) not null,
contract_type varchar(20) not null,
subscription_start date not null,
subscription_end date ,
user_name varchar(30) not null,
password varchar(50) not null,
country_name varchar(50) not null,
gender char(1) ,
birth_date date ,
constraint pk_Customer primary key(customer_mail_address),
constraint fk_Customer1 foreign key(country_name) references Country(country_name) ON UPDATE CASCADE,
constraint fk_Customer2 foreign key(contract_type) references Contract(contract_type) ON UPDATE CASCADE,
constraint fk_Customer3 foreign key(payment_method) references Payment(payment_method) ON UPDATE CASCADE,
constraint chk_Customer1 check(subscription_start < subscription_end),
constraint uc_Customer unique(user_name),
constraint chk_Customer2 check(len([password]) >= 8 AND [password] like '%[0-9]%'),
constraint chk_Customer3 check(birth_date < subscription_start),
constraint chk_Customer4 check(user_name NOT LIKE '%[^A-Z0-9]%')
)
create table Watchhistory
(
movie_id integer not null,
customer_mail_address varchar(255)not null,
watch_date date not null,
price numeric(5,2)not null,
invoiced bit not null,
constraint pk_Watchhistory primary key(movie_id, customer_mail_address, watch_date),
constraint fk_Watchhistory1 foreign key(movie_id) references Movie(movie_id),
constraint fk_Watchhistory2 foreign key(customer_mail_address) references Customer(customer_mail_address) ON UPDATE CASCADE ON DELETE CASCADE
)
These two tables are what I use to calculate this. I tried a lot of stuff but nothing really did the trick. This is what I have now to calculate the revenue for each year per country but not the percentage increase per country.
CREATE VIEW OmzetStijgingDalingPerLand AS
SELECT DATEPART(yy,watch_date) as [jaar], country_name, SUM(price) AS [omzet]
FROM Watchhistory w
INNER JOIN Customer c ON w.customer_mail_address = c.customer_mail_address
GROUP BY DATEPART(yy,watch_date), country_name
As it is described in article mentioned before, LAG function is the key
SELECT jaar,
country_name,
omzet,
omzet / LAG(omzet, 1) OVER(PARTITION BY country_name ORDER BY jaar) AS increase_percent
FROM OmzetStijgingDalingPerLand

on sql Foreign key

Can someone help me in SQL I'm using Oracle SQL*plus
Create the following tables.
Table Student:
stdNo CHAR(5) This is the Primary Key
lastname VARCHAR(25) Must be Not Null
givennames VARCHAR(50) Must be Not Null
Dept CHAR(4)
Table Course:
courseID CHAR(8) This is the Primary Key
courseTitle VARCHAR(50) Must be Unique and Not Null
Cost DECIMAL(6,2) Ensure Cost is greater or equal to zero
Credits INT Ensure that Credits are between 0 and 200. Also the default value is 2
Table Semester:
semesterID CHAR(5) This is the Primary Key
semesterCode INT Ensure that semesterCode is Between 1 and 4
Year INT Ensure that year is Between 2000 and 9999
Table Register:
stdNo CHAR(5) Foreign Key referenced to stdNo in Student table, On update cascade On delete cascade
courseID CHAR(8) Foreign Key referenced to courseID in Course table, On update cascade On delete cascade
semesterID CHAR(5) Foreign Key referenced to semesterID in Semester table, On update cascade On delete cascade
Grade CHAR(2)
Mark DECIMAL(4,2) Mark should be between 0.00 and 100.00
Primary Key (stdNo, courseID, semesterID)
Here in SQL (I'm using Oracle SQL*plus) .. table STD , SEMESTER works with me, but table COURSE I did not know how to put default value is 2 and table REGISTER dose not work with me at all :(
CREATE TABLE STD
(
STDNO CHAR(5) PRIMARY KEY,
LASTNAME VARCHAR(25) NOT NULL,
GIVENNAME VARCHAR(50) NOT NULL,
DEPT CHAR(4)
);
CREATE TABLE SEMESTER
(
SEMESTERID CHAR(5) PRIMARY KEY,
SEMESTERCODE INT CHECK(SEMESTERCODE BETWEEN 1 AND 4),
YEARS INT CHECK(YEARS BETWEEN 2000 AND 9999)
);
CREATE TABLE COURSE
(
COURSEID CHAR(8) PRIMARY KEY,
COURSETITLE VARCHAR(50) NOT NULL UNIQUE,
COST DECIMAL(6,2) CHECK(COST >= 0),
CREDITS INT CHECK(CREDITS BETWEEN 0 AND 200)
);
CREATE TABLE REGISTER
(
STDNO CHAR(5)
FOREIGN KEY REFERENCES STD(STDNO)
ON UPDATE CASCADE ON DELETE CASCADE,
COURSEID CHAR(5)
FOREIGN KEY REFERENCES COURSE(COURSEID)
ON UPDATE CASCADE ON DELETE CASCADE,
SEMESTERID CHAR(5)
FOREIGN KEY REFERENCES SEMESTER(SEMESTERID)
ON UPDATE CASCADE ON DELETE CASCADE,
GRADE CHAR(2),
MARK DECIMAL(4,2) CHECK(MARK BETWEEN 0.00 AND 100.0),
PRIMARYKEY(STDNO,COURSEID,SEMESTERID)
);
Please find below the solution.
COURSE - please use the below script to have a default value for CREDITS column
CREATE TABLE COURSE
(
COURSEID CHAR(8) PRIMARY KEY,
COURSETITLE VARCHAR(50) NOT NULL UNIQUE,
COST DECIMAL(6,2) CHECK(COST >= 0),
CREDITS INT DEFAULT 2 CHECK(CREDITS BETWEEN 0 AND 200)
);
REGISTER - Oracle Does not allow a Foreign Key Constraint with "ON UPDATE CASCADE". So you will have to use triggers for the same. Find below the scripts that you could make use of,
CREATE TABLE REGISTER
(
STDNO CHAR(5),
COURSEID CHAR(8), -- UPDATED THE DATATYPE TO SAME AS MASTER TABLE
SEMESTERID CHAR(5),
GRADE CHAR(2),
MARK DECIMAL(4,2) CHECK(MARK BETWEEN 0.00 AND 100.0),
CONSTRAINT register_pk PRIMARY KEY(STDNO,COURSEID,SEMESTERID),
CONSTRAINT register_fk1 FOREIGN KEY (STDNO) REFERENCES STD(STDNO)
ON DELETE CASCADE,
CONSTRAINT register_fk2 FOREIGN KEY (COURSEID) REFERENCES COURSE(COURSEID)
ON DELETE CASCADE,
CONSTRAINT register_fk3 FOREIGN KEY (SEMESTERID) REFERENCES SEMESTER(SEMESTERID)
ON DELETE CASCADE
);
CREATE OR REPLACE TRIGGER cascade_stdno_update
AFTER UPDATE OF stdno ON std
FOR EACH ROW
BEGIN
UPDATE REGISTER
SET stdno = :NEW.stdno
WHERE stdno = :OLD.stdno;
END;
/
CREATE OR REPLACE TRIGGER cascade_courseid_update
AFTER UPDATE OF courseid ON course
FOR EACH ROW
BEGIN
UPDATE REGISTER
SET courseid = :NEW.courseid
WHERE courseid = :OLD.courseid;
END;
/
CREATE OR REPLACE TRIGGER cascade_semesterid_update
AFTER UPDATE OF semesterid ON semester
FOR EACH ROW
BEGIN
UPDATE REGISTER
SET semesterid = :NEW.semesterid
WHERE semesterid = :OLD.semesterid;
END;
/

How to create a reference key from given details

Create table test1
(
id bigint Not Null,
name varchar(10) Not Null
constraint pk_test primary key(id,name)
)
Create table test2
(
Mid bigint Not Null references test1(id) ,
MSalary varchar(10) Not Null
)
In test2 I am not able to create a reference to test1 id Please help me..
Thanks in Advance!!!!
You need a foreign key in order to reference a table. Try this:
CREATE TABLE test1
(
id bigint NOT NULL PRIMARY KEY,
name varchat(10) NOT NULL
)
CREATE TABLE test2
(
Mid bigint NOT NULL,
MSalary varchar(10) NOT NULL,
FOREIGN KEY (Mid) REFERENCES test1(id)
)

Resources