ERROR: No unique constraint matching given keys for referenced table - sql

For some reason I'm getting an error* in my code. I'm quite new to PostgreSQL, and simply SQL. What is causing this error?
*there is no unique constraint matching given keys for referenced table "tech".
BEGIN;
CREATE TABLE Person (
person_id SERIAL PRIMARY KEY,
firstname VARCHAR(128),
lastname VARCHAR(128),
email_adr VARCHAR(128),
UNIQUE(person_id, email_adr)
);
CREATE TABLE Phone (
person_id INT REFERENCES Person(person_id),
phone_nr INT PRIMARY KEY,
UNIQUE(phone_nr)
);
CREATE TABLE Tech (
tech_id INT REFERENCES Person(person_id),
username VARCHAR(80) PRIMARY KEY,
password VARCHAR(80) NOT NULL,
location Varchar(128),
UNIQUE(username, tech_id)
);
CREATE TABLE Customer (
customer_id INT REFERENCES Persons(person_id),
addresse VARCHAR(255) NOT NULL,
UNIQUE(customer_id)
);
CREATE TABLE Task (
task_id SERIAL PRIMARY KEY,
payment MONEY,
tech INT REFERENCES Tech(tech_id) NOT NULL,
customer INT REFERENCES Customer(customer_id) NOT NULL,
start_date DATE NOT NULL,
end_dato DATE,
UNIQUE(tech, customer, start_date, end_date)
);
COMMIT;

In table Task you trying to reference to table Tech by tech_id. To do that you must add UNIQUE CONSTRAINT to tech_id in Tech.
Right now in table Tech you have UNIQUE(username, tech_id) that means that values in column tech_id could by doubled Ex.
Tech
-------------------------------
tech_id username, ....
------------------------------
1 'John'
2 'Tony'
1 'Nataly'
Acctually the better idea is to set reference by PRIMARY KEY, so in your case username in table Tech.
If you want to leave structure the way present in question, you should just add UNIQUE(tech_id) in column Tech.

What do you think of this code?
BEGIN;
CREATE TABLE Person (
person_id SERIAL PRIMARY KEY,
firstname VARCHAR(128),
lastname VARCHAR(128),
email_adr VARCHAR(128),
UNIQUE(person_id),
UNIQUE(email_adr)
);
CREATE TABLE Phone (
person_id INT,
phone_nr INT PRIMARY KEY,
);
CREATE TABLE Tech (
tech_id INT,
username VARCHAR(80) PRIMARY KEY,
password VARCHAR(80) NOT NULL,
location Varchar(128),
FOREIGN KEY(tech_id) REFERENCES Person(person_id),
UNIQUE(username),
UNIQUE(tech_id)
);
CREATE TABLE Customer (
customer_id INT REFERENCES Persons(person_id),
addresse VARCHAR(255) NOT NULL,
FOREIGN KEY(tech_id) REFERENCES Person(person_id),
UNIQUE(customer_id)
);
CREATE TABLE Task (
task_id SERIAL PRIMARY KEY,
payment MONEY,
tech varchar(80) REFERENCES Tech(username) NOT NULL,
customer INT REFERENCES Customer(customer_id) NOT NULL,
start_date DATE NOT NULL,
end_dato DATE,
UNIQUE(tech, customer, start_date, end_date)
);
COMMIT;

Related

"Incorrect syntax near 'DESCRIBE'. [41,1]" No other context, can't find syntax error

New to SQL, can't figure out what is wrong in my given code. all it says is:
Incorrect syntax near 'DESCRIBE'. [41,1]
I have tried taking off the semi-colons. I really just don't know what it wants from me.
Here is my code. Anything helps, thank you!
-- Write the query to create the 4 tables below.
CREATE TABLE client (
id INT NOT NULL IDENTITY(1,1),
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
PRIMARY KEY (id),
CONSTRAINT (full_name) UNIQUE (first_name, last_name)
);
CREATE TABLE employee (
id INT NOT NULL IDENTITY(1,1),
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
date_joined DATE NOT NULL,
CONSTRAINT (full_name) UNIQUE (first_name, last_name),
PRIMARY KEY (id)
);
CREATE TABLE project (
id INT NOT NULL IDENTITY(1,1),
cid INT NOT NULL,
name VARCHAR(255) NOT NULL,
notes TEXT,
UNIQUE (name),
FOREIGN KEY (cid) REFERENCES client(id)
);
CREATE TABLE works_on (
eid INT NOT NULL,
pid INT NOT NULL,
start_date DATE NOT NULL,
PRIMARY KEY (eid, pid),
FOREIGN KEY (eid) REFERENCES employee(id),
FOREIGN KEY (pid) REFERENCES project(id)
);
-- Leave the queries below untouched. These are to test your submission correctly.
-- Test that the tables were created
DESCRIBE client;
DESCRIBE employee;
DESCRIBE project;
DESCRIBE works_on;
-- Test that the correct foreign keys were created
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_SCHEMA = 'grade';
For MariaDB (and MySQL) the correct syntax for IDENTITY(1,1), is AUTO_INCREMENT, and CONSTRAINT names are not enclosed in (). Any column that is defined as AUTO_INCREMENT must also be declared as a PRIMARY KEY (this is only an issue with the project table). So your CREATE TABLE commands should look like this:
CREATE TABLE client (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
PRIMARY KEY (id),
CONSTRAINT full_name UNIQUE (first_name, last_name)
);
CREATE TABLE employee (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
date_joined DATE NOT NULL,
CONSTRAINT full_name UNIQUE (first_name, last_name),
PRIMARY KEY (id)
);
CREATE TABLE project (
id INT NOT NULL AUTO_INCREMENT,
cid INT NOT NULL,
name VARCHAR(255) NOT NULL,
notes TEXT,
PRIMARY KEY (id),
UNIQUE (name),
FOREIGN KEY (cid) REFERENCES client(id)
);
CREATE TABLE works_on (
eid INT NOT NULL,
pid INT NOT NULL,
start_date DATE NOT NULL,
PRIMARY KEY (eid, pid),
FOREIGN KEY (eid) REFERENCES employee(id),
FOREIGN KEY (pid) REFERENCES project(id)
);
Demo on dbfiddle

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

ERROR: there is no unique constraint matching given keys for referenced table

I am getting an error when i create my tabels.
The problem is that AssCode is not unique, so i can set it to unique, the combination of courseCode and AssCode is unique, thats why they are set as primary keys. I am using postgressql
here is the error:
ERROR: there is no unique constraint matching given keys for referenced table "assignments"
SQL state: 42830
here is my code:
CREATE TABLE Teachers (
BSN int primary key,
Surname varchar(40) NOT NULL,
Name varchar(40) NOT NULL
);
CREATE TABLE Courses (
CourseCode varchar(10) primary key,
Name varchar(20) NOT NULL
);
CREATE TABLE Assignments (
CourseCode varchar(10) REFERENCES Courses ON DELETE CASCADE,
AssCode varchar(10),
primary key(CourseCode,AssCode),
DependOn varchar(10),
Year date,
week int
);
CREATE TABLE WorkOn (
BSN int REFERENCES Teachers(BSN),
CourseCode varchar(10) REFERENCES Assignments(CourseCode),
AssCode varchar(10) REFERENCES Assignments(AssCode),
primary key (CourseCode,BSN,AssCode)
);
I found the answer:
CREATE TABLE WorkOn (
BSN int primary key REFERENCES Teachers(BSN),
CourseCode varchar(10),
AssCode varchar(10),
foreign key (CourseCode, AssCode) references Assignments (CourseCode, AssCode)
);

Sturctured Query Language

create table customer(
custno number(10) constraint foo primary key,
custname character(15),
city character(15),
phone number(10));
create table invoice(
invo number(10) constraint inv primary key,
invdate date(10),
constarint c references customer(custno));
I cannot able to create the second table and am having doubt of the data type date and If I neglected that attribute(invdate) still I cannot able to create the second table.Reason please.
create table customer
( custno int constraint foo primary key
,custname character(15)
,city character(15)
,phone int
)
go
create table invoice
( invo int constraint inv primary key
constraint ck references customer(custno)
,invdate date
)

Need help in the query (oracle 11 g)

First of all, these are my create_table statements.
/*--- Base tables ---*/
CREATE TABLE CUSTOMER (
CUST_ID VARCHAR(10) NOT NULL,
CUST_FNAME VARCHAR(15),
CUST_LNAME VARCHAR(15),
CUST_HP NUMBER(10),
CUST_EMAIL VARCHAR(40),
primary key (CUST_ID)
);
CREATE TABLE STAFF(
STAFF_ID NUMBER(12) NOT NULL,
STAFF_FNAME VARCHAR(15),
STAFF_LNAME VARCHAR(15),
STAFF_DOB Date,
STAFF_AGE NUMBER(2),
STAFF_ADDRESS VARCHAR(70),
STAFF_HP NUMBER(10),
STAFF_SALARY NUMBER(6,2),
primary key (STAFF_ID)
);
CREATE TABLE SUPPLIER(
SUPP_ID VARCHAR(10) NOT NULL,
SUPP_NAME VARCHAR(30),
SUPP_TEL NUMBER(10),
SUPP_EMAIL VARCHAR(25),
SUPP_ADDRESS VARCHAR(80),
SUPP_CITY VARCHAR(20),
SUPP_STATE VARCHAR(10),
SUPP_ZIP NUMBER(5),
primary key (SUPP_ID)
);
CREATE TABLE PRODUCT(
PROD_ID VARCHAR(10) NOT NULL,
PROD_NAME VARCHAR(30),
PROD_DESC VARCHAR(70),
R_UNIT_PRICE NUMBER(3,2) DEFAULT 4.50,
L_UNIT_PRICE NUMBER(3,2) DEFAULT 5.00,
primary key (PROD_ID)
);
CREATE TABLE INGREDIENT(
ING_ID VARCHAR(10) NOT NULL,
ING_NAME VARCHAR(20),
ING_DESC VARCHAR(60),
primary key (ING_ID)
);
CREATE TABLE TOPPING(
TOP_ID VARCHAR(10) NOT NULL,
TOP_NAME VARCHAR(20),
TOP_DESC VARCHAR(40),
TOP_PRICE NUMBER(2,2),
primary key (TOP_ID)
);
/*--- Child tables ---*/
CREATE TABLE ORDERS(
ORDERS_ID VARCHAR(10) NOT NULL,
CUST_ID VARCHAR(10) NOT NULL,
STAFF_ID NUMBER(12) NOT NULL,
TOTAL_PRICE NUMBER(5,2),
ORDERS_DATE DATE,
primary key (ORDERS_ID),
foreign key (CUST_ID) references CUSTOMER(CUST_ID),
foreign key (STAFF_ID) references STAFF(STAFF_ID)
);
CREATE TABLE ORD_DETAIL(
SK1_ID NUMBER(4) NOT NULL,
ORDERS_ID VARCHAR(10) NOT NULL,
PROD_ID VARCHAR(10) NOT NULL,
TOP_ID VARCHAR(10) NOT NULL,
TEATYPE VARCHAR(8),
CUPSIZE CHAR(1),
QTY NUMBER(2),
CONSTRAINT CHK_CUPSIZE CHECK (CUPSIZE = 'R' OR CUPSIZE = 'L'),
CONSTRAINT CHK_TEATYPE CHECK (TEATYPE = 'Oolong' OR TEATYPE = 'Jasmine'),
primary key (SK1_ID),
foreign key (ORDERS_ID) references ORDERS(ORDERS_ID),
foreign key (PROD_ID) references PRODUCT(PROD_ID),
foreign key (TOP_ID) references TOPPING(TOP_ID)
);
CREATE TABLE PROD_ING(
PROD_ID VARCHAR(10) NOT NULL,
ING_ID VARCHAR(10) NOT NULL,
primary key (PROD_ID , ING_ID),
foreign key (PROD_ID) references PRODUCT(PROD_ID),
foreign key (ING_ID) references INGREDIENT(ING_ID)
);
CREATE TABLE ING_SUPP(
ING_ID VARCHAR(10) NOT NULL,
SUPP_ID VARCHAR(10) NOT NULL,
SUPP_PRICE NUMBER(6,2),
primary key (ING_ID, SUPP_ID),
foreign key (ING_ID) references INGREDIENT(ING_ID),
foreign key (SUPP_ID) references SUPPLIER(SUPP_ID)
);
And my query is this:Which employee served the most number of customers?
But when I executed these statements:
SELECT MAX (COUNT (O.CUST_ID)) AS "Served customer"
FROM STAFF S, ORDERS O
WHERE (S.STAFF_ID = O.STAFF_ID)
GROUP BY O.STAFF_ID
HAVING COUNT (O.CUST_ID) > 0;
What I got was there was a column named "Served customer" with the maximum number only.
So, now my question is how to display the employee's ID, first name and last name along with
"Served customer" column.
The question is: "Which employee served the most number of customers? " Your query does not even return information about the employee, only about the number of customers.
The following aggregation returns for each staff member, the number of customers who were served:
SELECT O.STAFF_ID, COUNT(O.CUST_ID) AS "Served customer"
FROM STAFF SS JOIN
ORDERS O
ON S.STAFF_ID = O.STAFF_ID
GROUP BY O.STAFF_ID;
To get the maximum number we just need to order this by the count and take the first row. In Oracle, this uses a subquery:
SELECT so.*
FROM (SELECT O.STAFF_ID, COUNT(O.CUST_ID) AS "Served customer"
FROM STAFF SS JOIN
ORDERS O
ON S.STAFF_ID = O.STAFF_ID
GROUP BY O.STAFF_ID
ORDER BY COUNT(O.CUST_ID) DESC
) so
WHERE rownum = 1;