Getting missing parenthesis in my sql code [closed] - sql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
Using ORACLE SQL Developer
CREATE TABLE PAYMENT (
Payment_ID VARCHAR2(300) NOT NULL,
Account_ID VARCHAR(300) NOT NULL,
Discount_Code VARCHAR(50),
Gift_Card VARCHAR(50),
Cust_Rewards INTEGER,
Credit_Card VARCHAR(50),
Checking VARCHAR(50),
Bank_Account VARCHAR(80) NOT NULL,
Store_Card VARCHAR(50),
CONSTRAINT PK_PAYMENT_ID PRIMARY KEY REFERENCES PAYMENT(Payment_ID),
CONSTRAINT FK_PAYMENT_Account_ID FOREIGN KEY (Account_ID) REFERENCES ACCOUNT(Account_ID)
);
I get an error report that states "ORA-00906: missing left parenthesis:
How can I fix this??
EDIT:
Full code being ran:
CREATE TABLE CUSTOMER (
Customer_ID VARCHAR(300) NOT NULL,
Cust_FName VARCHAR(20) NOT NULL,
Cust_LName VARCHAR(20) NOT NULL,
Cust_Email VARCHAR(50),
Cust_Address VARCHAR(100),
CONSTRAINT PK_CUSTOMER PRIMARY KEY (Customer_ID)
);
/*Account table*/
CREATE TABLE ACCOUNT (
Account_ID VARCHAR(300) NOT NULL,
Customer_ID VARCHAR(300) NOT NULL,
Cust_Username VARCHAR(50) NOT NULL,
Cust_Password VARCHAR(50) NOT NULL,
Pet_Name VARCHAR(25),
Pet_DOB INTEGER,
Cust_Rewards INTEGER,
CONSTRAINT PK_ACCOUNT PRIMARY KEY (Account_ID,Customer_ID),
CONSTRAINT FK_ACCOUNT FOREIGN KEY (Customer_ID) REFERENCES CUSTOMER(Customer_ID)
);
CREATE TABLE PAYMENT (
Payment_ID VARCHAR2(300) NOT NULL,
Account_ID VARCHAR(300) NOT NULL,
Discount_Code VARCHAR(50),
Gift_Card VARCHAR(50),
Cust_Rewards INTEGER,
Credit_Card VARCHAR(50),
Checking VARCHAR(50),
Bank_Account VARCHAR(80) NOT NULL,
Store_Card VARCHAR(50),
CONSTRAINT PK_PAYMENT_ID PRIMARY KEY REFERENCES PAYMENT(Payment_ID),
CONSTRAINT FK_PAYMENT_Account_ID FOREIGN KEY (Account_ID) REFERENCES ACCOUNT(Account_ID)
);
Error starting at line : 32 in command-
CREATE TABLE PAYMENT (
Payment_ID VARCHAR2(300) NOT NULL,
Account_ID VARCHAR(300) NOT NULL,
Discount_Code VARCHAR(50),
Gift_Card VARCHAR(50),
Cust_Rewards INTEGER,
Credit_Card VARCHAR(50),
Checking VARCHAR(50),
Bank_Account VARCHAR(80) NOT NULL,
Store_Card VARCHAR(50),
CONSTRAINT PK_PAYMENT_ID PRIMARY KEY REFERENCES PAYMENT(Payment_ID),
CONSTRAINT FK_PAYMENT_Account_ID FOREIGN KEY (Account_ID) REFERENCES ACCOUNT(Account_ID)
)
Error report-
ORA-00906: missing left parentesis
00906. 0000 - "missing left parenthesis"
*Cause:
*Action:

A foreign key must reference the primary key of the parent table - the entire primary key. (SQL. How to reference a composite primary key Oracle?)
here is the fiddle http://sqlfiddle.com/#!4/eacfa48
CREATE TABLE PAYMENT (
Payment_ID VARCHAR2(300) NOT NULL ,
Account_ID VARCHAR(300) NOT NULL,
Customer_ID VARCHAR(300) NOT NULL,
Discount_Code VARCHAR(50),
Gift_Card VARCHAR(50),
Cust_Rewards INTEGER,
Credit_Card VARCHAR(50),
Checking VARCHAR(50),
Bank_Account VARCHAR(80) NOT NULL,
Store_Card VARCHAR(50),
CONSTRAINT PK_PAYMENT_ID PRIMARY KEY (Payment_ID),
CONSTRAINT FK_PAYMENT FOREIGN KEY (Account_ID, Customer_ID) REFERENCES ACCOUNT(Account_ID, Customer_ID)
);
one of the reasons I never use composite primary keys.

Related

SQL Error: ORA-02267 column type incompatible with referenced column type

Source code for oracle 12c
CREATE TABLE CUSTOMER (
CustomerID Char(20) NOT NULL,
CustomerFirstName Char(20) NOT NULL,
CustomerLastName Char(25) NOT NULL,
CustomerAddress Char(45) NOT NULL,
CustomerEmail Char(100) NOT NULL,
CONSTRAINT Customer_PK Primary Key(CustomerID)
CREATE TABLE EMPLOYEE (
EmployeeID VarChar(10) NOT NULL,
EmployeeFName VarChar(20) NOT NULL,
EmployeeRole VarChar(30) NOT NULL, CONSTRAINT
Employee_PK Primary Key(EmployeeID)
CREATE TABLE PRODUCT (
ProductID VarChar(10) NOT NULL,
ProductName VarChar(20) NOT NULL,
ProductType VarChar(20) NOT NULL,
ProductPrice Number(8,2) NOT NULL,
CONSTRAINT Product_PK Primary Key(ProductID)
CREATE TABLE CUSTORDER (
CustOrderID VarChar(10) NOT NULL,
CustOrderDate Date NOT NULL,
CustShippingStatus VarChar(20) NOT NULL,
SupplierID VarChar(20) NOT NULL,
CONSTRAINT CustOrder_PK Primary Key (CustOrderID),
CONSTRAINT CustOrder_FK FOREIGN KEY(SupplierID)
REFERENCES SUPPLIER(SupplierID)
CREATE TABLE SUPPLIER ( SupplierID Char(10) NOT NULL,
SupplierName Char(20) NOT NULL,
SupplierAddress Char(45) NOT NULL,
CONSTRAINT Supplier_PK Primary Key (SupplierID)
CREATE TABLE INVOICE (
InvoiceID Char(20) NOT NULL,
TotalItems Int NOT NULL,
TotalCost Number(8,2) NOT NULL,
SalesDate Date NOT NULL,
PaymentType VarChar(10) NOT NULL,
ProductID VarChar(10) NOT NULL,
EmployeeID VarChar(10) NOT NULL,
CustomerID Char(20) NOT NULL,
SupplierID Char(10) NOT NULL,
CONSTRAINT Invoice_PK Primary Key(InvoiceID),
CONSTRAINT Invoice_Product_FK Foreign Key(ProductID)
REFERENCES PRODUCT(ProductID),
CONSTRAINT Invoice_Employee_FK Foreign Key(EmployeeID)
REFERENCES EMPLOYEE(EmployeeID),
CONSTRAINT Invoice_Customer_FK Foreign Key(CustomerID)
REFERENCES CUSTOMER(CustomerID),
CONSTRAINT Invoice_Supplier_FK Foreign Key(SupplierID)
REFERENCES SUPPLIER(SupplierID)
You need to align the datatype and length of the referencing column of each foreign key with the column it references in the source table.
In your code, SUPPLIER(SupplierID) is declared as Char(20). On the other hand, referencing column INVOICE(SupplierID) is Char(10) and CUSTORDER(SupplierID) is VarChar(20). If you make these two columns Char(20), you code just works.
Aside: the referenced table must exist at the time when the referencing table is created - your code createst CUSTORDER before SUPPLIER. However I assume that's a typo when writing the question, otherwise you would be getting a different error: ORA-00942: table or view does not exist.
You also have a missing right parentheses at the end of each and every create table statement: I would consider these typos too.
Demo on DB Fiddle

Sql cannot create table missing right parenthesis

I created the table employee and Salaire. But I get an error when creating the table Zone:
00907-0000 missing right parenthesis
Code:
Create table Employee
(
CodeEmploye int,
NAS Number (9),
Nom Varchar(20) not null,
Prenom Varchar(20) not null,
NomJeuneFille varchar(20) not null,
DateNaissance Date,
Adresse varchar(100) not null,
Sexe Char(1),
Telephone Varchar(14),
Fonction Varchar(20) not null,
Service varchar(13) not null,
Taux Number(6,2) default (0),
Grade Varchar(2),
Constraint PK_Employe Primary key (CodeEmployee),
Constraint NAS_Employe Unique (NAS),
Constraint Verfi_Sexe Check Sexe IN ('F', 'M'),
Constraint verif_Service Check Service IN ('adminstratif', 'surveillance', 'medical'),
Constraint verfi_Grade Check grade in ('G1', 'G2','G3', 'G4', 'G5')
);
Create Table Salaire
(
CodeEmployee int,
Mois int,
Salaire Number(11,2) default (0),
Constraint PK__Salaire Primary key (CodeEmployee, Mois),
Constraint verif_Mois Check Mois between 1 and 12,
Constraint FK_Salaire_Employe Foreign Key (CodeEmploye) references Employee(CodeEmployee)
);
Create Table Zone
(
CodeZone int,
NomZone Varchar(20), not null
ChefZone Varchar(20),
Constraint PK_Zone Primary Key (CodeZone),
Constraint FK_Zone_Employee Foreign Key (ChefZone) references Employee(Taux, Grade) On delete set null on update cascade
you need to add parenthesis after check
Create table Employe (
CodeEmploye int,
NAS Number (9),
Nom Varchar(20) not null,
Prenom Varchar(20) not null,
NomJeuneFille varchar(20) not null,
DateNaissance Date,
Adresse varchar(100) not null,
Sexe Char(1),
Telephone Varchar(14),
Fonction Varchar(20) not null,
Service varchar(30) not null,
Taux Number(6,2) default 0,
Grade Varchar(2),
Constraint PK_Employe Primary key (CodeEmploye),
CONSTRAINT NAS_Employe Unique (NAS),
Constraint Verfi_Sexe Check (Sexe IN ('F', 'M')),
Constraint verif_Service Check ( Service IN ('adminstratif', 'surveillance', 'medical')),
Constraint verfi_Grade Check (grade in ('G1', 'G2','G3', 'G4', 'G5'))
);
Create Table Salaire(
CodeEmploye int,
Mois int,
Salaire Number(11,2) default (0),
Constraint PK__Salaire Primary key (CodeEmploye, Mois),
Constraint verif_Mois Check (Mois between 1 and 12),
Constraint FK_Salaire_Employe Foreign Key (CodeEmploye) references Employe(CodeEmploye));
and for 3rd table you need to correct your foregin key definition

Informix - Cant find syntax error

This may be a stupid mistake but I'm new to Informix and I can't seem to figure out why none of my CREATE TABLE statements won't run. I keep getting a syntax error on all of my CREATE TABLE statements.
CREATE TABLE customer(
store_num INTEGER NOT NULL,
store_name VARCHAR(20) NOT NULL,
addr VARCHAR(20),
addr2 VARCHAR(20),
city VARCHAR(15),
state VARCHAR(2),
zip-code VARCHAR(5),
contact_name VARCHAR(30),
phone VARCHAR(18),
CONSTRAINT cust_pk PRIMARY KEY(store_num)
);
create table orders(
order_num INTEGER NOT NULL,
order_date DATE NOT NULL,
store_num INTEGER NOT NULL,
fac_code CHAR(3),
ship_instr CHAR(10),
promo CHAR(1) NOT NULL,
CONSTRAINT orders_pk PRIMARY KEY(order_num)
);
create table factory(
fac_code CHAR(3) NOT NULL,
fac_name CHAR(15) NOT NULL,
CONSTRAINT fac_pk PRIMARY KEY(fac_code)
);
create table stock(
stock_num INTEGER NOT NULL,
fac_code CHAR(3) NOT NULL,
description CHAR(15) NOT NULL,
reg_price DECIMAL(8,2) NOT NULL,
promo_price DECIMAL(8,2),
price_updated DATE,
unit CHAR(4) NOT NULL,
CONSTRAINT stock_pk PRIMARY KEY(stock_num)
);
create table items(
order_num INTEGER NOT NULL,
stock_num INTEGER NOT NULL,
quantity SMALLINT NOT NULL,
price DECIMAL(8,2) NOT NULL,
CONSTRAINT items_pk PRIMARY KEY(order_num, stock_num)
);
create table state(
state_code CHAR(2) NOT NULL,
state_name CHAR(15) NOT NULL,
CONSTRAINT state_pk PRIMARY KEY(state_code)
);
Any help will be appreciated.
For reasons I don't understand, Informix requires the constraint name after the constraint, whereas standard SQL requires the constraint name before the constraint.
Thus, in addition to changing zip-code to zip_code (as pointed out by daniel.shih in an answer), you need:
CREATE TABLE customer(
store_num INTEGER NOT NULL,
store_name VARCHAR(20) NOT NULL,
addr VARCHAR(20),
addr2 VARCHAR(20),
city VARCHAR(15),
state VARCHAR(2),
zip_code VARCHAR(5),
contact_name VARCHAR(30),
phone VARCHAR(18),
PRIMARY KEY(store_num) CONSTRAINT cust_pk
);
You can try customer's field zip_code instead of zip-code

Oracle Problems with Composite Keys

bit confusing question here:
I am trying to create a composite FK from a composite PK. I'll show you my tables in question that I'm having problems with;
CREATE TABLE Weapons (
Weapon_ID VARCHAR(10) NOT NULL,
Weapon_Name VARCHAR(30),
Range_In_Meters INT,
Maximum_Number_Of_Uses INT,
Damage_Factor INT,
Cost INT,
primary key(Weapon_ID));
CREATE TABLE WeaponInventory (
Inventory_ID VARCHAR(30) NOT NULL,
WeaponInventory_ID Varchar(10) NOT NULL,
Weapon_ID VARCHAR(10) REFERENCES Weapons(Weapon_ID) NOT NULL,
primary key(Inventory_ID, WeaponInventory_ID));
CREATE TABLE Avatars (
Avatar_ID VARCHAR(10) NOT NULL,
Avatar_Name VARCHAR(30),
AvA_DOB DATE,
Age VARCHAR(30),
Gender VARCHAR(30),
Strength_Indicated INT,
Hoard INT,
Avatar_Level VARCHAR(30),
Skill VARCHAR(30),
Original_Owner VARCHAR(30),
Family_ID VARCHAR(10) NOT NULL,
Species_ID VARCHAR(10) NOT NULL,
Inventory_ID VARCHAR(30) NOT NULL,
Weapon_ID VARCHAR(10) NOT NULL,
Player_ID VARCHAR(10) NOT NULL,
PRIMARY KEY (Avatar_ID),
FOREIGN KEY (Inventory_ID, Weapon_ID)
REFERENCES WeaponInventory (Inventory_ID, Weapon_ID));
In the table Avatars, I have been trying to create this Foreign key but I'm having no luck. I have errors such as:
ORA-02270: no matching unique or primary key for this column-list
and:
ORA-02270: no matching unique or primary key for this column-list
Any help would be greatly appreciated! I just keep getting confused and ending up in the same place! (:
I think you planned to reference column WeaponInventory_ID (instead of Weapon_ID) in table WeaponInventory:
FOREIGN KEY (Inventory_ID, Weapon_ID)
REFERENCES WeaponInventory (Inventory_ID, WeaponInventory_ID) -- see here
Note that Weapon_ID is not part of the PK in WeaponInventory:
CREATE TABLE WeaponInventory (
Inventory_ID VARCHAR(30) NOT NULL,
WeaponInventory_ID Varchar(10) NOT NULL,
Weapon_ID VARCHAR(10) REFERENCES Weapons(Weapon_ID) NOT NULL,
primary key(Inventory_ID, WeaponInventory_ID) -- Weapon_ID is not part of PK
);
Check this Fiddle.

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;