what's wrong with writing the code: ORA-00907: missing right parenthesis - sql

CREATE TABLE comenzi
(
id_comanda NUMBER(5) CONSTRAINT id_comanda_pk PRIMARY KEY,
nume_companie VARCHAR2(40) NOT NULL,
persoana_contact VARCHAR2(40) NOT NULL,
data_comanda DATE,
data_expediere DATE,
loc_expediere VARCHAR2(24) UNIQUE;
)
CREATE TABLE detalii_comenzi
(
id_comanda NUMBER(5) CONSTRAINT id_comanda_fk REFERENCES comenzi(id_comenzi),
id_produs NUMBER(5) CONSTRAINT id_produs_pk PRIMARY KEY,
pret_unitar NUMBER(7),
cantitate NUMBER(7) NOT NULL;
)

You are not using right data types. Please try using below code.
CREATE TABLE #comenzi
(
ID_COMANDA INT CONSTRAINT id_comanda_pk PRIMARY KEY,
NUME_COMPANIE VARCHAR(40) NOT NULL,
PERSOANA_CONTACT VARCHAR(40) NOT NULL,
DATA_COMANDA DATE,
DATA_EXPEDIERE DATE,
LOC_EXPEDIERE VARCHAR(24) UNIQUE
)
CREATE TABLE detalii_comenzi
(
id_comanda INT CONSTRAINT id_comanda_fk REFERENCES #comenzi (ID_COMANDA),
id_produs INT CONSTRAINT id_produs_pk PRIMARY KEY,
pret_unitar INT,
cantitate INT NOT NULL
)

Related

Dbeaver: Why does code not work correctly?

It doesn't create all my tables and also says I have syntax errors. Can anyone help??
CREATE table project (
project_id int not null,
budget_id int not null,
division_id int not null,
project_name varchar(40),
project_begin_date date,
project_end_date date
);
ALTER table project
add constraint pk_project_project_id
primary key(project_id)
;
ALTER table project
add constraint fk_project_budget_id
foreign key(budget_id) references budget(budget_id)
;
ALTER table project
add constraint fk_project_division_id
foreign key (division_id) references division(division_id)
;
CREATE table division(
division_id int not null,
division_name varchar(40),
constraint pk_division_division_id primary key(division_id)
);
CREATE table committee(
committee id int not null,
committee_name varchar(40),
constraint pk_committee_committee_id primary key(committe_id)
);
create table employee(
employee_id int not null,
given_name varchar(40),
middle_name varchar(40),
family_name varchar(40),
form_of_address varchar(10),
name_suffix varchar(25),
work_phone_number varchar(20),
hourly_budget_rate numeric(8,2),
constraint pk_employee_employee_id primary key(employee_id)
);
CREATE table budget(
budget_id int not null,
budget_description varchar(100),
constraint pk_budget_budget_id primary key(budget_id)
);
CREATE table section(
section_id int not null,
division_id int,
budget_id int,
section_name varchar(40),
constraint pk_section_section_id primary key(section_id),
constraint pk_section_buget_id foreign key(budget_id) references budget(budget_id),
constraint pk_section_division_id foreign key(division_id) references division(division_id)
);
CREATE table employee_job_assignment(
job_assignment_id int not null,
employee_id int not null,
section_id int not null,
begin_datetime date,
end_datetime date,
job_title varchar(100),
constraint pk_employee_job_assignment primary key(job_assignment_id),
constraint pk_employee_job_assignment foreign key(employee_id) references employee(employee_id),
constraint pk_employee_job_assignment foreign key(section_id) references section(section_id)
);
CREATE table budget_category(
budget_category_code char(5),
budget_category_description char(100),
constraint pk_budget_category primary key(budget_category_code)
);
CREATE table budget_charge(
transaction_id int not null,
budget_category_code char(5),
budget_id int not null,
transaction_date date,
transaction_description varchar(100),
transaction_type char(5),
constraint pk_budget_charge_transaction_id primary key(transaction_id),
constraint pk_budget_charge_budget_category_code foreign key(budget_category_code) references budget_category(budget_category_code),
constraint pk_budget_charge_budget_id foreign key(budget_id) references budget(budget)
);
CREATE office(
office_id int not null,
office_type char(1),
office_building char(5),
office_location varchar(20),
constraint pk_office primary key(office_id)
);
CREATE table employee_office(
office id int not null,
employee_id int not null,
constraint pk_employee_office_office_id foreign key(office_id) references office(office_id),
constraint pk_employee_office_employee_id foreign key(employee id) references employee(employee_id)
);
CREATE table project_employee(
employee_id int not null,
project_id int not null,
project_role varchar(25),
constraint pk_project_employee_employee_id foreign key(employee_id) references employee(employee_id),
constraint pk_project_employee_project_id foreign key(project_id) references project(project_id)
);
CREATE table purchase_expense_charge(
transaction_id int not null,
purchase_expense_dollars numeric(8,2),
constraint pk_purchase_expense_charge_transaction_id primary key(transaction_id),
constraint pk_purchase_expense_charge_transaction_id foreign key(transaction_id) references budget_charge(transaction_id)
);
CREATE table hours_charge(
transaction_id int not null,
employee_id int not null,
hours_charged numeric(7,2),
time_period_begin_date date,
time_period_end_date date,
constraint pk_hours_charge_transaction_id primary key(transaction_id),
constraint pk_hours_charge_transaction_id foreign key(transaction_id) references budget_charge(transaction_id),
constraint pk_hours_charge_employee_id foreign key(employee_id) references employee(employee_id)
);
CREATE table budget_line_item(
budget_category_code char(5),
budget_id int not null,
line_item_budget_dollars numeric(8,2),
constraint pk_budget_line_item_budget_category_code foreign key(budget_category_code) references budget_category(budget_category_code),
constraint pk_budget_line_item_budget_category_code foreign key(budget_id) references budget(budget_id)
);
CREATE table travel_expense_charge(
transaction_id int not null,
employee_id int not null,
trip_expense_dollars NUMERIC(8,2),
trip_destination VARCHAR(100),
trip_begin_date date,
trip_end_date date,
constraint pk_travel_expense_charge_transaction_id primary key (transaction_id),
constraint pk_travel_expense_charge_transaction_id foreign key (transaction_id) references budget_charge(transaction_id),
constraint pk_travel_expense_charge_employee_id foreign key (employee_id) references employee(employee_id)
);
CREATE table committee_member(
committee_id int not null,
employee_id int not null,
committee_role varchar(25),
constraint pk_committee_member_committee_id foreign key(committee_id) references committee(committee_id)
constraint pk_comittee_member_employee_id foreign key(employee id) references employee(employee_id)
);

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)

Problems referencing primary key with foreign key

This is code to create the beginnings of a book library database with Microsoft SQL Server Management Studio.
CREATE DATABASE BOOK_LIBRARY
CREATE TABLE LIBRARY_USER
(
usr_id int not null primary key,
f_name varchar(30) not null,
m_init char(1),
l_name varchar(30) not null,
balance decimal(6,2),
join_date date,
addrss_1 varchar(30) not null,
addrss_2 varchar(30),
city varchar(30) not null,
addrss_state char(2) not null,
zip_code varchar(10) not null,
email varchar(30)
);
CREATE TABLE LIBRARY_TRANSACTIONS
(
transaction_id int not null primary key,
maximum_borrow_duration int not null,
strt_date date not null,
actual_return_date date not null,
borrow_usr_id int not null,
foreign key (borrow_usr_id) references LIBRARY_USER(usr_id)
);
CREATE TABLE BOOKS
(
isbn varchar(17) not null primary key,
title varchar(30) not null,
number_of_copies int not null,
Author varchar(30) not null,
Number_of_pages int not null,
publish_year int not null,
book_type varchar(20)
);
CREATE TABLE DIGITAL_BOOKS
(
digital_id int not null primary key,
format varchar(30) not null,
size_mb int not null,
digital_isbn varchar(17) not null,
foreign key(digital_isbn) references BOOKS(isbn)
);
CREATE TABLE PHYSICAL_BOOKS
(
physical_id int not null primary key,
condition varchar(20) not null,
physical_isbn varchar(17) not null,
foreign key(physical_isbn) references BOOKS(isbn)
);
CREATE TABLE BOOK_COPY
(
digi_id int not null,
phys_id int not null,
primary key(digi_id, phys_id),
foreign key(digi_id) references DIGITAL_BOOKS(digital_id),
foreign key(phys_id) references PHYSICAL_BOOKS(physical_id)
);
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id) references BOOK_COPY(digi_id),
foreign key(phys_id) references BOOK_COPY(phys_id)
);
Despite me being able to look and see that digi_id and phys_id are in fact primary keys of the book_copy table, I keep getting this error:
Msg 1776, Level 16, State 0, Line 66
There are no primary or candidate keys in the referenced table 'BOOK_COPY' that match the referencing column list in the foreign key 'FK__CONTNS__digi_id__37A5467C'.
Msg 1750, Level 16, State 1, Line 66
Could not create constraint or index. See previous errors.
Am I missing something obvious? I'm just starting out with using this program, so any help is greatly appreciated.
Here:
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id) references BOOK_COPY(digi_id),
foreign key(phys_id) references BOOK_COPY(phys_id)
);
You are creating two foreign keys to parent table BOOK_COPY. But that table has a compound primary key (ie a multi-column primary key)
CREATE TABLE BOOK_COPY
(
digi_id int not null,
phys_id int not null,
primary key(digi_id, phys_id), --> here
...
)
As a consequence, you need a compound foreign key:
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id, phys_id) references BOOK_COPY(digi_id, phys_id)
);

SQL invalid identifier?

I finished typing out SQL statements but now I'm getting errors codes when I run it on omega. I keep getting the invalid identifier code...
Here's what I get once I ran it in omega.
SQL>
SQL> --question 14
SQL>
SQL> SELECT to_char(Avg(ProductPrice),'99,999.99') AS Avg_Price
2 FROM Product_mys;
SELECT to_char(Avg(ProductPrice),'99,999.99') AS Avg_Price
*
ERROR at line 1:
ORA-00904: "PRODUCTPRICE": invalid identifier
These are the sql tables where the data is being pulled from
CREATE TABLE Dept_mys (
DeptID Number(3) NOT NULL,
DeptName VARCHAR(20) NOT NULL,
PRIMARY KEY (DeptID)
) ;
CREATE TABLE Commission_mys (
CommClass CHAR(1) NOT NULL,
CommRate Number(2,2) NOT NULL,
PRIMARY KEY (CommClass)
) ;
CREATE TABLE Category_mys (
CatID Number(3) NOT NULL,
catName VARCHAR(20) NOT NULL,
PRIMARY KEY (CatID)
) ;
CREATE TABLE SalesRep_mys (
SalesRepID NUMBER(4) NOT NULL,
SalesRepFName VARCHAR(20) NOT NULL,
SalesRepLName VARCHAR(20) NOT NULL,
DeptID NUMBER(3) NOT NULL,
CommClass CHAR(1) NOT NULL,
PRIMARY KEY (SalesRepID),
FOREIGN KEY (DeptID) REFERENCES Dept_mys,
FOREIGN KEY (CommClass) REFERENCES Commission_mys
) ;
CREATE TABLE Customer_mys (
CustID CHAR(5) NOT NULL,
CustFName VARCHAR(20) NOT NULL,
CustLName VARCHAR(20) NOT NULL,
CustPhone CHAR(10),
SalesRepID NUMBER(4) NOT NULL,
PRIMARY KEY (CustID),
FOREIGN KEY (SalesRepID) REFERENCES SalesRep_mys
) ;
CREATE TABLE Order_mys (
OrderID NUMBER(3) NOT NULL,
OrderDate DATE NOT NULL,
CustID CHAR(5) NOT NULL,
PRIMARY KEY (OrderID),
FOREIGN KEY (CustID) REFERENCES Customer_mys
) ;
CREATE TABLE Product_mys (
ProductID NUMBER(3) NOT NULL,
ProductName VARCHAR(30) NOT NULL,
CatID Number(3) NOT NULL,
PRIMARY KEY (ProductID),
FOREIGN KEY (CatID) REFERENCES Category_mys
) ;
CREATE TABLE OrderDetail_mys (
OrderID NUMBER(3) NOT NULL,
ProductID NUMBER(3) NOT NULL,
ProductQty NUMBER(4) NOT NULL,
ProductPrice NUMBER(6,2) NOT NULL,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES Order_mys,
FOREIGN KEY (ProductID) REFERENCES Product_mys
) ;
Your query references Sales_Rep_mys but you create SalesRep_mys. So that's at least some of the problem.
Same with Order_Detail_mys and OrderDetail_mys
Looks like those 2 fixes should do it.

PostgreSQL constraint problems

I am having these two errors :
ERROR: there is no unique constraint matching given keys for referenced table "flight"
*** Error ***
ERROR: there is no unique constraint matching given keys for referenced table "flight"
This is my code :
CREATE TABLE Staff (
EmployeeNumber int NOT NULL,
FirstName char(15) NOT NULL,
LastName char(15) NOT NULL,
SocialSecurity int NOT NULL,
Sex char(1) NOT NULL,
Address char(20) NOT NULL,
City char(20) NOT NULL,
Province char(15) NOT NULL,
Country char(20) NOT NULL,
Primary Key (EmployeeNumber)
);
CREATE TABLE FlightAttendent (
FALN int,
StaffRole char (20) NOT NULL,
EmployeeNumber int NOT NULL,
Foreign Key (EmployeeNumber) References Staff(EmployeeNumber),
Primary Key (FALN)
);
Create TABLE AircraftType (
ACType char (10),
Instrument char(1) NOT NULL,
Engines int NOT NULL,
CrewCount int NOT NULL,
PassengerCount int NOT NULL,
Primary Key (ACType)
);
CREATE TABLE Pilot (
PILN int,
MedicalValid date NOT NULL,
StaffRole char (20) NOT NULL,
EmployeeNumber int NOT NULL,
AircraftType char (10) NOT NULL,
Foreign Key (EmployeeNumber) references Staff(EmployeeNumber),
Foreign Key (AircraftType) References AircraftType(ACType),
Primary Key (PILN)
);
Create TABLE Aircraft (
AircraftID char(6) NOT NULL,
AircraftManufacturer char(10) NOT NULL,
AircraftType char(10) NOT NULL,
Foreign Key (AircraftType) References AircraftType(ACType),
Primary Key (AircraftID)
);
CREATE Table Airport (
AirportCode char(4) NOT NULL,
AirportName char(40) NOT NULL,
City char(20) NOT NULL,
Country char(20) NOT NULL,
Continent char(20) NOT NULL,
Primary Key (AirportCode)
);
Create TABLE Flight (
FlightID char (20),
FlightDate date,
AircraftID char(6) NOT NULL,
ArrivalAirport char(4) NOT NULL,
DepartureAirport char(4) NOT NULL,
PRIMARY KEY (FlightID, FlightDate),
FOREIGN Key (ArrivalAirport) references Airport(AirportCode),
FOREIGN Key (DepartureAirport) references Airport(AirportCode),
FOREIGN KEY (AircraftID) references Aircraft(AircraftID)
);
Create TABLE FlightCrew (
FlightID char (20) REFERENCES Flight(FlightID) ON DELETE CASCADE,
FlightDate date REFERENCES Flight(FlightDate) ON DELETE CASCADE,
EmployeeNumber int NOT NULL,
StaffRole char(20) NOT NULL,
PRIMARY KEY(FlightID, FlightDate),
Foreign Key (EmployeeNumber) references Staff(EmployeeNumber)
);
CREATE Table Passenger (
PassengerNumber int,
PassportNumber int NOT NULL,
Citizenship char (20) NOT NULL,
FirstName char (20) NOT NULL,
LastName char (20) NOT NULL,
Primary Key (PassengerNumber)
);
CREATE Table PassengerManifest (
FlightID char(20),
FlightDate date,
PassengerNumber int NOT NULL,
Foreign Key (FlightDate) References Flight(FlightDate),
Foreign Key (PassengerNumber) References Passenger(PassengerNumber),
Primary Key (FlightID, FlightDate)
);
What did I do wrong? Thanks!
When you have multiple values in a primary key, you need to reference it differently as a foreign key.
Basically, when you say
FlightID char (20) REFERENCES Flight(FlightID) ON DELETE CASCADE,
PostgreSQL checks for that primary key, which doesn't exist (since the primary key on that table is (flightid, flightdate)).
So drop the REFERENCES clauses when referencing the flight table, and add
FOREIGN KEY (FlightID, FlightDate) REFERENCES Flight (FlightID, FlightDate)
In the manner you have in some of the other table definitions.