Oracle Missing expression - sql

I only have this error now.. I cant seem to see where it is too long (ERROR at bottom of page)
CREATE TABLE Menu_Item_Ingredient
(
Menu_Item_Number Number(5,0)CONSTRAINT NN_MenuItemIngredient_MenuItemNumber Not null,
CONSTRAINT FK_MenuItemIngredient_MenuItemNumber Foreign Key(Menu_Item_Number) References Bill_Item(Menu_item_Number),
Ingredient_Number Number(5,0) CONSTRAINT NN_MenuItemIngredient_IngredientNumber Not null,
CONSTRAINT FK_MenuItemIngredient_IngredientNumber Foreign Key(Ingredient_Number) References Ingredient(Ingredient_Number),
Quantity_Needed Number(5,2) DEFAULT 0 CONSTRAINT NN_MenuItemIngredient_QuantityNeeded Not null
CONSTRAINT CK_MenuItemIngredient_QuantityNeeded CHECK(Quantity_Needed >= 0),
CONSTRAINT PK_BillItem_Ingredient Primary key(Menu_Item_Number,Ingredient_Number)
)
Error report -
SQL Error: ORA-00972: identifier is too long
00972. 00000 - "identifier is too long"
*Cause: An identifier with more than 30 characters was specified.
*Action: Specify at most 30 characters.

Do something below. In your query, a comma is missing when you are declaring discount column in bill_item table.
CREATE TABLE Bill_Item3
(
Bill_Number NUMBER(6,0) CONSTRAINT NN_BillItem_BillNumber NOT NULL,
Menu_Item_Number NUMBER(5,0) CONSTRAINT NN_BillItem_MenuItemNumber NOT NULL,
Discount NUMBER(5,2) CONSTRAINT N_BillItem_Discount NULL,
--colum Discount format '%'00.00,
Quaintity_Sold NUMBER(3,0)CONSTRAINT NN_BillItem_QuaintitySold NOT NULL,
Selling_Price NUMBER(6,2) default 0 CONSTRAINT NN_BillItem_SellingPrice NOT NULL,
CONSTRAINT CK_BillItem_SellingPrice CHECK (Selling_Price >= 0 ),
CONSTRAINT PK_Bill_MenuItem PRIMARY KEY (Bill_Number,Menu_Item_Number),
CONSTRAINT FK_BillItem_Bill FOREIGN KEY (Bill_Number) REFERENCES bill (Bill_Number),
CONSTRAINT FK_BillItem_MenuItemNumber FOREIGN KEY (Menu_Item_Number) REFERENCES Menu_Item_Ingredient(Menu_Item_Number),
CONSTRAINT CK_BillItem_Discount CHECK (Discount BETWEEN 0 AND 100)
);

Your problem is with the constraints,
A foreign key constraint will be used to give reference to a column in another table, in your queries you have given constraints like below -
"CONSTRAINT FK_BillItem_Bill Foreign key (Bill_Number) References Primary(Bill_Number), what are you trying to do with this?
The syntax you need to use should be -
CONSTRAINT constraint_name FOREIGN KEY (column_in_current_table) REFERENCES Other_Table_Name(column_name_you_want_to_refer_in_other_table)
As far as I've seen, this will not work, you cannot use DEFAULT in a check constraint.
constraint CK_BillItem_SellingPrice check (Selling_Price >= 0 OR DEFAULT = 0)
If you want to enforce a DEFAULT constraint, you have to use something as below -
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);
Your final table can be as follows -
CREATE TABLE Bill_Item
(
Bill_Number Number(6,0) constraint NN_BillItem_BillNumber not null,
--CONSTRAINT FK_BillItem_Bill Foreign key (Bill_Number) References Primary(Bill_Number),
Menu_Item_Number Number(5,0) CONSTRAINT NN_BillItem_MenuItemNumber Not null,
--CONSTRAINT FK_BillItem_MenuItemNumber Foreign Key (Menu_Item_Number) References Primary(Menu_Item_Number),
Discount Number(5,2) CONSTRAINT N_BillItem_Discount Null
CONSTRAINT CK_BillItem_Discount Check (Discount BETWEEN 0 and 100),
--colum Discount format '%'00.00,
Quaintity_Sold Number(3,0)CONSTRAINT NN_BillItem_QuaintitySold not null,
Selling_Price Number(6,2) DEFAULT 0.0 CONSTRAINT NN_BillItem_SellingPrice not null, -- You can remove "DEFAULT 0.0" if you do not want to default your Selling_Price to 0.0
constraint CK_BillItem_SellingPrice check (Selling_Price >= 0),
CONSTRAINT PK_Bill_MenuItem Primary key (Bill_Number,Menu_Item_Number)
);
All of your other tables also have the same issue, remove the DEFAULT from check constraint as I did in the above example, and specify "Default" constraint at the column definition itself.
Also, create the table which will be referenced in other tables first, for example, your second table "Bill" has a foreign key constraint which is referencing to "Waiter" table, so you have to create "Waiter" table first.
If you are still having concerns let me know.

Related

My inputs for the foreign key are not working and im not sure

I was not sure if the multiple column for distance foreign key was correct.
Here is my code:
DROP TABLE Trips;
DROP SEQUENCE Trips_seq;
CREATE TABLE Trips(
Trips_ID NUMBER NOT NULL,
Date_of_Trip DATE NOT NULL,
Payment NUMBER NOT NULL,
Destination_Town VARCHAR2(50) NOT NULL,
Source_Town VARCHAR2 (50) NOT NULL,
Customer_ID NUMBER NOT NULL
CONSTRAINT Trip_pk
PRIMARY KEY (Trips_ID, Trips_seq),
CONSTRAINT Customer_fk
FOREIGN KEY (Customer_ID) REFERENCES Customer (Customer_ID),
CONSTRAINT Owner_fk
FOREIGN KEY (Owner_ID) REFERENCES Vehicle_Owners (Owner_ID),
CONSTRAINT Payment_fk
FOREIGN KEY (Payment_ID) REFERENCES Payment (Payment_ID),
CONSTRAINT Distances_fk
FOREIGN KEY (Destination_Town, Source_Town) REFERENCES
Distances (Destination_Town, Source_Town)s
);
CREATE SEQUENCE Trips_seq START WITH 1 INCREMENT BY 1;
I count 12 left parenthesis and 11 right parenthesis in your code. It looks like you need to add a right parenthesis before your 's' e.g.:
Distances (Destination_Town, Source_Town)) s

Troubles creating tables

I'm trying to make two tables execute but I keep getting errors on them. Transaction table says it can't execute without the other being created and then I try to create the broker table and it says missing right parenthesis
SQL Error: ORA-00907: missing right parenthesis
but I checked my code and it looks well. Would any of you know what is the problem? thanks to all who comment!
Create table Broker
(
Broker_Number Number(7,0)
Constraint PK_Broker_Broker_Number Primary Key
Constraint NN_Broker_Broker_Number Not Null,
First_Name Varchar2(25)
Constraint NN_Broker_First_Name Not Null,
Last_Name Varchar2(3O)
Constraint NN_Broker_Last_Name Not Null,
Area_Code Number(3,0)
Default '780'
Constraint NL_Broker_Area_Code Null,
Phone_Number Number(7,0)
Constraint NL_Broker_Phone_Number Null,
Email_Address Varchar2(50)
Constraint CK_Broker_Email_Address Check (REGEXP_like(Email_Address,'%#%.%'))
Constraint NN_Broker_Email_Address Not Null
);
Create table Transaction
(
Portfolio_Number Number(7,0) Not Null,
Constraint FK_Trans_Portfolio_Number
Foreign Key (Portfolio_Number)
References Portfolio(Portfolio_Number),
Stock_Code Varchar2(10) Not Null,
Constraint FK_Transaction_Stock_Code
Foreign Key (Stock_Code)
References Stock(Stock_Code),
Transaction_Date Date
Constraint PK_Trans_Transaction_Date Not Null,
Exchange_Code Varchar2(4) Not Null,
Constraint FK_Transaction_Exchange_Code
Foreign Key (Exchange_Code)
References Exchange(Exchange_Code),
Broker_Number Number(7,0) Not Null,
Constraint FK_Transaction_Broker_Number
Foreign Key (Broker_Number)
References Broker(Broker_Number),
Buy_Sell Char(1)
Constraint CK_Transaction_Buy_Sell Check (Buy_Sell in ('B','S') )
Constraint NN_Transaction_Buy_Sell Not Null,
Quantity Number(7,0)
Constraint NN_Transaction_Quantity Not Null,
Price_Per_Share Number(6,2)
Constraint NN_Transaction_Price_Per_Share Not Null
)
It's this line
Last_Name Varchar2(3O)
That's not 30, it is 3O, as in the letter O after the number three.
One way to debug such error statements is to remove one column at a time, run the statement, and repeat the process until you don't encounter the error any more. In this case, I deleted this specific line and it worked fine. That's when I noticed the O instead of 0 in the editor.

ORA-02270 Foreign key, Can't find fault

I am having a problem connecting my Sample_Measure_FK in the Sample table to the Measurement Table.
There sections of code are:
create table Sample
(
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Scientist_Num varchar2(7) not null,
Comments varchar2(4000), -- or CLOB
constraint Sample_PK primary key (Site_ID, Recorded_On),
constraint Sample_Site_FK foreign key (Site_ID) references Site,
constraint Sample_Scientist_FK foreign key (Scientist_Num) references Scientist(Scientist_Num),
-- the following is the problem:
constraint Sample_Measure_FK foreign key (Recorded_On) references Measurement(Recorded_On)
);
create table Measurement
(
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Name varchar2(50),
Value numeric(10,8),
Outlier_Indicator varchar2(50),
constraint Measurement_PK primary key(Site_ID, Recorded_On),
);
The error message I receive is:
Error starting at line : 65 in command -
create table Sample
(
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Scientist_Num varchar2(7) not null,
Comments varchar2(4000), -- or CLOB
constraint Sample_PK primary key (Site_ID, Recorded_On),
constraint Sample_Site_FK foreign key (Site_ID) references Site,
constraint Sample_Scientist_FK foreign key (Scientist_Num) references Scientist(Scientist_Num),
constraint Sample_Measure_FK foreign key (Recorded_On) references Measurement(Recorded_On)
)
Error report -
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
The Other foreign keys work, but the one in bold does not.
Perhaps it is the naming, but I would expect a single sample to have multiple measurements, suggesting that the foreign key relationship is on the wrong table:
create table Sample (
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Scientist_Num varchar2(7) not null,
Comments varchar2(4000), -- or CLOB
constraint Sample_PK primary key (Site_ID, Recorded_On)
constraint Sample_Site_FK foreign key (Site_ID) references Site,
constraint Sample_Scientist_FK foreign key (Scientist_Num) references Scientist(Scientist_Num)
);
create table Measurement (
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Name varchar2(50),
Value numeric(10, 8),
Outlier_Indicator varchar2(50),
constraint Measurement_Sample_FK foreign key (Site_ID, Recorded_On) references Sample(Site_ID, Recorded_On),
constraint Measurement_PK primary key (Site_ID, Recorded_On, Name)
);
This does work.
The Measurement table needs to be created before the Sample table
The foreign key is validated, so the referenced table must already exist.
A foreign key needs to point to a primary key of another table
constraint Sample_Measure_FK foreign key (Recorded_On) references Measurement(Recorded_On)
Well, Recorded_on is not the primary key on Measurement. Alternatively it could be a unique constraint also, but it's not either.
You usually point foreign keys to primary keys of other tables.

I want to create a table with "WITH CHECK OPTION"

create table STAFF
(StaffID TINYINT IDENTITY NOT NULL,
fName varchar(20) NOT NULL,
lname varchar(20) NOT NULL,
Phone varchar(10) NOT NULL,
Gender char(01),
DoB date NOT NULL,
Mentor TINYINT,
Payment_ID TINYINT NOT NULL,
constraint staff_pk primary key (StaffID),
constraint staff_fk foreign key (Payment_ID) references PAYMENT(Payment_ID),
constraint mentor_fk foreign key (Mentor) references staff(StaffID)
For the Gender column I want to insert ONLY 'M','F','O' characters only.
How can I do with this the "WITH CHECK OPTION"
I don't think you want with check option. You want a check constraint:
alter table staff
add constraint chk_staff_gender check (gender in ('M', 'F', 'O'));
(You can put this in the create table statement as well.)
with check option is an option on views that ensures that data remains consistent even when the data in underlying tables changes (see here) .
add , constraint gender_chk check (Gender in ('M','F','O'))
the with check option is on by default.
create table STAFF
(StaffID TINYINT IDENTITY NOT NULL,
fName varchar(20) NOT NULL,
lname varchar(20) NOT NULL,
Phone varchar(10) NOT NULL,
Gender char(01),
DoB date NOT NULL,
Mentor TINYINT,
Payment_ID TINYINT NOT NULL,
constraint staff_pk primary key (StaffID)
constraint staff_fk foreign key (Payment_ID) references PAYMENT(Payment_ID),
constraint mentor_fk foreign key (Mentor) references staff(StaffID)
, constraint gender_chk check (Gender in ('M','F','O'))
)
From the MSDN Documentation for ALTER TABLE (emphasis added):
WITH CHECK | WITH NOCHECK
Specifies whether the data in the table is or is not validated against a newly added or re-enabled FOREIGN KEY or CHECK constraint. If not specified, WITH CHECK is assumed for new constraints, and WITH NOCHECK is assumed for re-enabled constraints.
If you do not want to verify new CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK. We do not recommend doing this, except in rare cases. The new constraint will be evaluated in all later data updates. Any constraint violations that are suppressed by WITH NOCHECK when the constraint is added may cause future updates to fail if they update rows with data that does not comply with the constraint.
The query optimizer does not consider constraints that are defined WITH NOCHECK. Such constraints are ignored until they are re-enabled by using ALTER TABLE table WITH CHECK CHECK CONSTRAINT ALL.
You are refering to check constraint
create table STAFF
(StaffID TINYINT IDENTITY NOT NULL,
fName varchar(20) NOT NULL,
lname varchar(20) NOT NULL,
Phone varchar(10) NOT NULL,
Gender char(01) NOT NULL,
DoB date NOT NULL,
Mentor TINYINT,
Payment_ID TINYINT NOT NULL,
constraint staff_pk primary key (StaffID),
constraint staff_fk foreign key (Payment_ID) references PAYMENT(Payment_ID),
constraint mentor_fk foreign key (Mentor) references staff(StaffID),
constraint Gender_ck check (Gender in ('M','F','O'))
)
"WITH CHECK OPTION" is an optional clause for of a view definition
Any attempt to update/insert a record, through the view, that cannot be selected by the view, will raise an error.

Multiple constraints on single column

Can we add multiple constraints on a single column?
like-
create table x(x varchar2(20), y number(2) not null,
constraint fk_cons foreign key(x) references user_info(user_id),
constraint null_cons not null(x)
)
this query is returning error ora-00904: invalid identifier....
You have wrong syntax when create null_cons constraint:
Use this (table level check constraint):
CREATE TABLE x(
x VARCHAR2(20),
y NUMBER(2) NOT NULL,
CONSTRAINT fk_cons FOREIGN KEY(x) REFERENCES user_info(user_id),
CONSTRAINT null_cons CHECK(x IS NOT NULL)
)
Or (use NOT NULL constraint on column):
CREATE TABLE x(
x VARCHAR2(20) NOT NULL,
y NUMBER(2) NOT NULL,
CONSTRAINT fk_cons FOREIGN KEY(x) REFERENCES user_info(user_id)
)
Or (Use column level check constraint):
CREATE TABLE x(
x VARCHAR2(20) CHECK (X IS NOT NULL),
y NUMBER(2) NOT NULL,
CONSTRAINT fk_cons FOREIGN KEY(x) REFERENCES user_info(user_id)
)
create table x(x varchar2(20), y number(2) not null,
constraint fk_cons foreign key(x) references user_info(user_id),
constraint null_cons check(x is not null)
)
If the RDBMS is SQL Server, you can define multiple constraints at the column by doing this (I changed some of the column names to make the constraint naming strategy more obvious - naming is subjective):
CREATE TABLE SomeTable(
User_Id VARCHAR(20) CONSTRAINT FK_SomeTable_User_Id FOREIGN KEY REFERENCES dbo.User_Info(User_Id) CONSTRAINT UIX_NC_SomeTable_User_id NONCLUSTERED NOT NULL,
SomeOtherColumn DECIMAL(2, 0) NOT NULL
)
create table x(x varchar2(20) not null, y number(2) not null,
constraint fk_cons foreign key(x) references user_info(user_id)
)