Sql Fiddle errors - sql

I am trying to create SQL statements for an assignment in SQL Fiddle and I keep getting the error "Cannot add foreign key constraint". I have tried various things but I recieve different errors when I change things. What am I doing wrong?
CREATE TABLE Person (
ID int NOT NULL,
FName varchar(255),
LName varchar(255),
Preferred_Name varchar(255),
PRIMARY KEY (ID)
)
;
CREATE TABLE Song (
ID varchar(255) NOT NULL,
Title varchar(255),
Run_Time varchar(255),
Lyrics varchar(255),
LeadID int,
FOREIGN KEY (LeadID) REFERENCES Person(ID),
PRIMARY KEY (ID)
)
;
CREATE TABLE Album (
Title varchar(255) NOT NULL,
Run_Time int,
Release_Year TIMESTAMP,
PRIMARY KEY (Title)
)
;
CREATE TABLE Has (
Album_Title varchar(255),
Song_Title varchar(255),
FOREIGN KEY (Album_Title) REFERENCES Album(Title),
FOREIGN KEY (Song_Title) REFERENCES Song(ID)
)
;
CREATE TABLE Part_Of (
PersonID int,
SongID int,
Role varchar(255) NOT NULL,
FOREIGN KEY (PersonID) REFERENCES Person(ID),
FOREIGN KEY (SongID) REFERENCES Song(ID),
PRIMARY KEY (Role)
)
;

In the Song and Has tables you defined Song.ID and Has.Song_Title as a varchar(255). In the Part_Of table you defined SongID as an int. This is why the foreign key is failing. Use the same datatype in all tables (INT seems like a good option) to fix this.
CREATE TABLE Song (
ID varchar(255) NOT NULL, <------
Title varchar(255),
Run_Time varchar(255),
Lyrics varchar(255),
LeadID int,
FOREIGN KEY (LeadID) REFERENCES Person(ID),
PRIMARY KEY (ID)
)
;
CREATE TABLE Has (
Album_Title varchar(255),
Song_Title varchar(255), <-----
FOREIGN KEY (Album_Title) REFERENCES Album(Title),
FOREIGN KEY (Song_Title) REFERENCES Song(ID)
)
;
CREATE TABLE Part_Of (
PersonID int,
SongID int, <-----
Role varchar(255) NOT NULL,
FOREIGN KEY (PersonID) REFERENCES Person(ID),
FOREIGN KEY (SongID) REFERENCES Song(ID),
PRIMARY KEY (Role)
)
;

Related

There are no primary or candidate keys in the referenced table 'tblgender' that match the referencing column list in the foreign key 'giFK'

create database my_data
create table tblperson(
pid int primary key,
pname varchar(15),
pgender varchar(15)
)
create table tblgender(
gid varchar(15) primary key,
gender varchar(15)
)
Alter table tblperson add constraint giFK
Foreign key (pgender) references tblgender(gid)

There is no unique constraint matching given keys for referenced table " exam_subjects"

CREATE TABLE student
(
student_id INT PRIMARY KEY,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
birth_day DATE NOT NULL,
sex VARCHAR(1) NOT NULL,
student_email_address VARCHAR(40) NOT NULL UNIQUE,
student_password VARCHAR(10) NOT NULL UNIQUE,
student_nick_name VARCHAR(10) NOT NULL,
student_qualification VARCHAR(10) NOT NULL,
student_documents VARCHAR(255) NOT NULL,
student_image VARCHAR(100) NOT NULL
);
CREATE TABLE student_feedback
(
sr_no BIGSERIAL PRIMARY KEY,
student_id INT NOT NULL,
feedback_type VARCHAR(10) NOT NULL,
feedback_text VARCHAR(200) NOT NULL,
FOREIGN KEY (student_id) REFERENCES student(student_id)
);
CREATE TABLE online_exam
(
exam_id INT PRIMARY KEY,
exam_title VARCHAR(20) UNIQUE NOT NULL,
exam_duration_min INT NOT NULL,
total_questions INT NOT NULL,
marks_per_right_answer INT NOT NULL,
marks_per_wrong_answer INT NOT NULL,
passing_marks INT NOT NULL,
exam_status VARCHAR(2)
);
CREATE TABLE exam_subjects
(
sub_id INT,
exam_id INT,
sub_name VARCHAR(20) NOT NULL,
sub_desc VARCHAR(20) NOT NULL,
UNIQUE(sub_id,exam_id),
PRIMARY KEY(sub_id,exam_id),
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE
);
CREATE TABLE sub_questions
(
sub_id1 INT,
ques_id INT,
ques_text VARCHAR(150) NOT NULL,
ques_attachments VARCHAR(255),
option_1 VARCHAR(20) NOT NULL,
option_2 VARCHAR(20) NOT NULL,
option_3 VARCHAR(20) NOT NULL,
option_4 VARCHAR(20) NOT NULL,
UNIQUE(sub_id1,ques_id),
PRIMARY KEY (sub_id1,ques_id),
FOREIGN KEY (sub_id1) REFERENCES exam_subjects(sub_id) ON DELETE CASCADE
);
CREATE TABLE ques_answers
(
ans_id INT,
ques_id INT,
ans VARCHAR(20) NOT NULL,
PRIMARY KEY (ans_id,ques_id),
FOREIGN KEY (ques_id) REFERENCES sub_questions(ques_id) ON DELETE CASCADE
);
CREATE TABLE exam_registration
(
student_id INT,
exam_id INT,
exam_date_time TIMESTAMP NOT NULL,
PRIMARY KEY (student_id,exam_id),
FOREIGN KEY (student_id) REFERENCES student(student_id) ON DELETE CASCADE,
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE
);
CREATE TABLE exam_result
(
student_id INT,
exam_id INT,
sub_id INT,
marks_scored INT NOT NULL,
correct_ques INT NOT NULL,
wrong_ques INT NOT NULL,
grade VARCHAR(10) NOT NULL,
PRIMARY KEY(student_id,exam_id,sub_id),
FOREIGN KEY (student_id) REFERENCES student(student_id) ON DELETE CASCADE,
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE,
FOREIGN KEY (sub_id) REFERENCES exam_subjects(sub_id) ON DELETE CASCADE
);
Output :
CREATE TABLE,
CREATE TABLE,
CREATE TABLE,
CREATE TABLE,
There is no unique constraint matching given keys for referenced table "exam_subjects", relation "sub_questions" do not exist,
CREATE TABLE,
There is no unique constraint matching given keys for referenced table "exam_subjects".
Why am I getting "there is no unique..." error in postgresql? How to solve it? Please help.
In your table exam_subjects the primary key is a compossed key (PRIMARY KEY(sub_id, exam_id))
Therefore, your table sub_questions must have both columns to create the constraint, otherwise you get the error about unique constraints (or you can create a unique field acting as primary key, and create a unique index on both columns sub_id and exam_id)

Error Code 1005. Can't create table : "takes" and "Teaches"

Would appreciate help. Can not create the "Teaches" and "takes" table.
CREATE DATABASE university;
use university;
CREATE TABLE classroom(
building VARCHAR(20),
room_number INT,
capacity INT,
PRIMARY KEY (building,room_number)
) ;
CREATE TABLE Sales(
item VARCHAR(20),
color VARCHAR(20),
clothes_size VARCHAR(20),
quantity INT,
PRIMARY KEY (item,color,clothes_size,quantity)
);
CREATE TABLE Department (
dept_name VARCHAR(20),
building VARCHAR(20),
budget INT,
FOREIGN KEY (building) references classroom(building),
PRIMARY KEY (dept_name,building,budget)
);
CREATE TABLE Course (
course_id VARCHAR(20)PRIMARY KEY,
title VARCHAR(20),
dept_name VARCHAR(20),
credits INT,
FOREIGN KEY (dept_name) references Department(dept_name)
);
CREATE TABLE Instructor(
ID VARCHAR(20) PRIMARY KEY,
neme VARCHAR(20),
dept_name VARCHAR(20),
salary INT,
FOREIGN KEY (dept_name) references Department(dept_name)
);
CREATE TABLE section(
course_id VARCHAR(20),
sec_id INT,
semester VARCHAR(20),
year1 INT,
building1 VARCHAR(20),
room_number INT,
time_slot_id CHAR,
FOREIGN KEY (course_id) REFERENCES Course(course_id),
FOREIGN KEY (building1,room_number) references classroom(building,room_number),
PRIMARY KEY(course_id,sec_id,semester,year1)
);
CREATE TABLE Teaches(
ID VARCHAR(20),
course_id VARCHAR(20),
sec_id VARCHAR(20),
semester VARCHAR(20),
year1 INT,
FOREIGN KEY (ID) references Instructor(ID),
FOREIGN KEY (sec_id) references section(sec_id),
FOREIGN KEY (semester,year1) references section(semester,year1),
FOREIGN KEY (course_id) references course(course_id),
PRIMARY KEY (ID,course_id,sec_id,year1,semester)
);
CREATE TABLE student(
ID VARCHAR(20) PRIMARY KEY,
name1 VARCHAR(20),
dept_name VARCHAR(20),
tot_cred INT);
CREATE TABLE Takes(
ID VARCHAR(20),
course_id VARCHAR(20),
sec_id INT,
semester VARCHAR(20),
year1 INT,
grade char,
FOREIGN KEY (ID) references student(ID),
FOREIGN KEY (sec_id) references section(sec_id),
FOREIGN KEY (semester) references section(semester),
FOREIGN KEY (year1) references section(year1),
FOREIGN KEY (course_id) references course(course_id),
PRIMARY KEY(ID,sec_id,semester,year1,course_id)
);
Foreign key references need to have the same types as the primary keys they refer to. So consider:
CREATE TABLE Department (
dept_name VARCHAR(20),
building VARCHAR(20),
budget INT,
FOREIGN KEY (building) references classroom(building),
PRIMARY KEY (dept_name,building,budget)
);
CREATE TABLE Instructor (
ID VARCHAR(20) PRIMARY KEY,
neme VARCHAR(20),
dept_name VARCHAR(20),
salary INT,
FOREIGN KEY (dept_name) references Department(dept_name)
);
The primary key for Department has three components -- dept_name, building, budget. However, the reference has only one component. They cannot match; the number is not correct, much less the types.
I would recommend that you use auto-incremented ids for your primary keys. You can also specify uniqueness constraints, if you like. So for this small example:
create table buildings (
building_id int auto_increment primary key,
building varchar(20),
constraint unq_buildings_building unique(building)
);
create table classrooms (
classroom_id int auto_increment primary key,
building_id int,
room_number int,
capacity int,
constraint fk_classrooms_building_id foreign key (building_id) references buildings (building_id)
) ;
create table Departments (
department_id int auto_increment primary key,
dept_name varchar(20),
building_id int,
budget INT,
constraint fk_departments_building_id foreign key (building_id) references buildings(building_id)
);
create table instructors (
instructor_id int auto_increment primary key,
id varchar(20) unique,
name varchar(20),
department_id int,
salary int,
foreign key instructors_department_id foreign key (department_id) references departments (department_id)
);
This is just a small sample of your tables. But the following are some rules that I follow:
Table names are plural.
They have a primary key which is the singular form followed by _id (or Id).
Foreign key references are only to primary keys.
To the extent possible, foreign keys and primary keys have the exact same name.
Explicit constraints are given names. The names follow a very precise naming convention.
I also added a buildings table. It is referenced in at least two places, so it seems worthy of being its own entity.
This should give you some ideas on how to build your database.

Foreign key 'id_client' references invalid column 'id_client' in referencing table 'nrcomanda'

Foreign key 'id_client' references invalid column 'id_client' in
referencing table 'nrcomanda'.
use Logistica
create table client
(
id_client int primary key identity(1,1),
nume varchar(20) not null,
prenume varchar(20) not null,
id_nrc int foreign key references nrcomanda(id_nrc)
)
create table categorie
(
id_categorie int primary key identity(1,1),
categorie varchar(50),
)
create table pachet
(
id_pachet int primary key identity(1,1),
tip_pachet varchar(50)
)
create table transport
(
id_transport int primary key identity(1,1),
tip_transport varchar(20)
)
create table nrcomanda
(
id_nrc int primary key identity(1,1),
nrcomanda varchar not null,
greutate decimal(7,2),
asigurare varchar(50),
foreign key(id_client) references client(id_client),
foreign key(id_categorie) references categorie(id_categorie),
foreign key(id_pachet) references pachet(id_pachet),
foreign key(id_transport) references transport(id_transport),
foreign key(id_adresa) references adresa(id_adresa)
)
create table raion
(
id_raion int primary key identity(1,1),
nume varchar(50)
)
create table localitate
(
id_loc int primary key identity(1,1),
id_raion int,
nume varchar(50)
foreign key(id_raion) references raion(id_raion)
)
create table adresa
(
id_adresa int primary key identity(1,1),
id_raion int foreign key references raion(id_raion),
id_loc int foreign key references localitate(id_loc),
id_nrc int foreign key references nrcomanda(id_nrc),
id_tara int foreign key references tara(id_tara),
strada varchar(30),
nr varchar(6),
ap varchar(6),
bloc varchar(6),
activ bit default 1
)
create table tara
(
id_tara int primary key identity(1,1),
nume varchar(20)
)
i think you have miss some column definitions in your create table :
create table nrcomanda
(
id_nrc int primary key identity(1,1),
nrcomanda varchar not null,
greutate decimal(7,2),
asigurare varchar(50),
Id_client int,
Id_categorie int,
...
Id_adresa int,
foreign key(id_client) references client(id_client),
foreign key(id_categorie) references categorie(id_categorie),
foreign key(id_pachet) references pachet(id_pachet),
foreign key(id_transport) references transport(id_transport),
foreign key(id_adresa) references adresa(id_adresa)
)

SQL how can I get list of users and departments with filter by position?

Please help me, how can i get a list of users of the department with a filter by position?
CREATE TABLE COMPANY
(
ID INT GENERATED BY DEFAULT AS IDENTITY,
NAME VARCHAR(255),
PRIMARY KEY (ID),
UNIQUE KEY COMPANY_NAME (NAME)
);
CREATE TABLE USER
(
ID INT GENERATED BY DEFAULT AS IDENTITY,
NAME VARCHAR(255),
LASTNAME VARCHAR(255),
DATE_OF_BIRTH DATE ,
PRIMARY KEY (ID),
);
CREATE TABLE POSITION
(
ID INT NOT NULL GENERATED BY DEFAULT AS IDENTITY,
POSITION VARCHAR (50),
USERID INT NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (USERID) REFERENCES USER(ID)
);
CREATE TABLE DEPARTMENT
(
ID INT GENERATED BY DEFAULT AS IDENTITY,
USER_ID INT NOT NULL,
COMPANY_ID INT NOT NULL,
DEPARTMENT_CATEGORY VARCHAR(50),
PRIMARY KEY (ID),
FOREIGN KEY (USER_ID) REFERENCES USER(ID),
FOREIGN KEY (COMPANY_ID) REFERENCES COMPANY(ID)
);
CREATE TABLE CATEGORY
(
ID INT NOT NULL GENERATED BY DEFAULT AS IDENTITY,
NAME VARCHAR (50),
PRIMARY KEY (ID)
);
CREATE TABLE DEPARTMENT_CATEGORY
(
DEPARTMENT_ID INT NOT NULL,
CATEGORY_ID INT NOT NULL,
FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENT(ID),
FOREIGN KEY (CATEGORY_ID) REFERENCES CATEGORY(ID)
);
Please help me!!
it look like you wanted to display results from department and position table that has both the data you need, but be specific about your filter (position) as u need to provide it in your where clause
select user_ID, department
from department as a
join user as b
on a. user_id = b. user_id
where position = 'put the specific position you wanted to be filtered here'
but your two userID columns are written differently as (User_id or USERID) correct that too not get failure result when you execute the task