Oracle SQL Create Table Issue - sql

When I try to create a test table with some test rows, SQL Plus gives me the following error:
SQL> CREATE TABLE test_table
2 (
3 test_data0 NUMBER(3) CONSTRAINT test_data0_pk PRIMARY KEY,
4 test_data1 NUMBER(3) CONSTRAINT test_data1_fk REFERENCES test_table2, CONSTRAINT test_data1_condition NOT NULL,
5 test_data2 DATE CONSTRAINT test_data2_condition NOT NULL,
6 test_data3 NUMBER(2),
7 test_data4 NUMBER(2) DEFAULT 0
8 );
test_data1 NUMBER(3) CONSTRAINT test_data1_fk REFERENCES test_table2, C
ONSTRAINT test_data1_condition NOT NULL,
*
ERROR at line 4:
ORA-00904: : invalid identifier
If I only put one of the two constraints on line 4 I don't get any error. What is the problem?

You don't need the constraint key word for inline constraints. Of course, you can use it. But, in particular, not null is usually expressed without a constraint. So:
CREATE TABLE test_table (
test_data0 NUMBER(3) CONSTRAINT test_data0_pk PRIMARY KEY,
test_data1 NUMBER(3) NOT NULL CONSTRAINT test_data1_fk REFERENCES test_table2,
test_data2 DATE NOT NULL,
test_data3 NUMBER(2),
test_data4 NUMBER(2) DEFAULT 0
);
If you do use constraint more than once, you need to leave out the comma. That is the error you are getting. So that line would be:
test_data1 NUMBER(3) CONSTRAINT test_data1_fk REFERENCES test_table2
CONSTRAINT test_data1_condition NOT NULL,
I often leave out the constraint itself for the inline constraints:
CREATE TABLE test_table (
test_data0 NUMBER(3) PRIMARY KEY,
test_data1 NUMBER(3) NOT NULL REFERENCES test_table2,
test_data2 DATE NOT NULL,
test_data3 NUMBER(2),
test_data4 NUMBER(2) DEFAULT 0
);
Admittedly, you lose the name of the constraint when violated.

Related

Oracle SQL missing smth

Trying to build these tables and there's always an error or missing something, can someone help me?
Where's max_mice i'm checking if it's inbetween those values and doesn't work why?
Don't know what's wrong, already searched everywhere, don't know why they won't be created...
updated: now i got problems on table incidents...
update: the error was that i had: CONSTRAINT fun_maxmi_ch CHECK (200 > max_mice >= min_mice) instead of the code below.
CREATE TABLE Functions (
function VARCHAR(10) CONSTRAINT fun_fu_pk PRIMARY KEY,
min_mice NUMBER(3) CONSTRAINT fun_minmi_ch CHECK (min_mice > 5),
max_mice NUMBER(3),
CONSTRAINT fun_maxmi_ch CHECK (max_mice >= min_mice and max_mice < 200)
);
BUT still have a problem creating table Incidents don't know what's the problem!!!
CREATE TABLE Incidents (
nickname VARCHAR2(15),
enemy_name VARCHAR2(15),
incident_date DATE CONSTRAINT inc_indate_nn NOT NULL,
incident_desc VARCHAR2(50),
CONSTRAINT inc_con_pk PRIMARY KEY (nickname, enemy_name),
CONSTRAINT inc_nic_fk FOREIGN KEY (nickname) REFERENCES Cats(nickname),
CONSTRAINT inc_enname_fk FOREIGN KEY (enemy_name) REFERENCES Enemies(enemy_name),
);
Here's the full code:
CREATE TABLE Enemies (
enemy_name VARCHAR2(15),
hostility_degree NUMBER(2) CONSTRAINT hos_degree_ch CHECK (hostility_degree BETWEEN 1 AND 10),
species VARCHAR2(15),
bride VARCHAR2(20),
CONSTRAINT ene_name_pk PRIMARY KEY(enemy_name)
);
CREATE TABLE Functions (
function VARCHAR(10) CONSTRAINT fun_fu_pk PRIMARY KEY,
min_mice NUMBER(3) CONSTRAINT fun_minmi_ch CHECK (min_mice > 5),
max_mice NUMBER(3),
CONSTRAINT fun_maxmi_ch CHECK (max_mice >= min_mice and max_mice < 200)
);
CREATE TABLE Bands (
Band_no NUMBER(2) CONSTRAINT ban_no_pk PRIMARY KEY,
name VARCHAR2(20) CONSTRAINT ban_name_nn NOT NULL,
site VARCHAR2(15) CONSTRAINT ban_site_un UNIQUE,
band_chief VARCHAR(15) CONSTRAINT ban_chief_un UNIQUE
);
CREATE TABLE Cats (
name VARCHAR2(15) CONSTRAINT cat_name_nn NOT NULL,
gender VARCHAR2(1) CONSTRAINT cat_gen_ch CHECK (gender IN('M', 'W')),
nickname VARCHAR2(15) CONSTRAINT cat_pk PRIMARY KEY,
function VARCHAR2(10),
chief VARCHAR2(15),
in_herd_since DATE DEFAULT SYSDATE CONSTRAINT cat_inherd_nn NOT NULL,
mice_ration NUMBER(3),
mice_extra NUMBER(3),
band_no NUMBER(2),
CONSTRAINT cat_banno_fk FOREIGN KEY (band_no) REFERENCES Bands(band_no),
CONSTRAINT cat_chief_fk FOREIGN KEY (chief) REFERENCES Cats(nickname),
CONSTRAINT cat_fun_fk FOREIGN KEY (function) REFERENCES Functions(function)
);
ALTER TABLE Bands
ADD CONSTRAINT ban_chief_fk FOREIGN KEY (band_chief) REFERENCES Cats(nickname);
CREATE TABLE Incidents (
nickname VARCHAR2(15),
enemy_name VARCHAR2(15),
incident_date DATE CONSTRAINT inc_indate_nn NOT NULL,
incident_desc VARCHAR2(50),
CONSTRAINT inc_con_pk PRIMARY KEY (nickname, enemy_name),
CONSTRAINT inc_nic_fk FOREIGN KEY (nickname) REFERENCES Cats(nickname),
CONSTRAINT inc_enname_fk FOREIGN KEY (enemy_name) REFERENCES Enemies(enemy_name),
);
here's max_mice i'm checking if it's in between those values and doesn't work why?
When it comes to the first table in your code, Functions, the problem is with the declaration of the last check constraint:
max_mice NUMBER(3) CONSTRAINT fu_maxmi_ch CHECK (200 > max_mice >= min_mouse)
Issues:
the double inequality condition is not supported
a multi-column check constraint need to be declared at table level rather than at column level
This works:
CREATE TABLE Functions (
function VARCHAR(10) CONSTRAINT fu_fu_pk PRIMARY KEY,
min_mice NUMBER(3) CONSTRAINT fu_minmi_ch CHECK (min_mice > 5),
max_mice NUMBER(3),
CONSTRAINT fu_maxmi_ch CHECK (max_mice >= min_mice and max_mice < 200)
);
Note: I would not recommend naming a column function, since it obviously conflicts with a SQL keyword.

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.

Oracle (ORA-02270) : Have tried following other answers but can not figure it out

Complete beginner here. I have been trying to mess around with this code and I stripped it back to this:
create table Customer
(
Customer_Num varchar2(7) not null,
Surname varchar2(50) not null,
Other_Names varchar2(100) not null,
Email varchar2(320) not null,
Mobile_Phone varchar2(20) not null,
constraint Customer_PK primary key (Customer_Num)
);
create table Store
(
Store_ID varchar2(5) not null,
Region varchar2(50) not null,
constraint Store_PK primary key (Store_ID)
);
create table Sale
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Customer_Num varchar2(7) not null,
Comments varchar2(4000),
constraint Product_PK primary key (Store_ID, Recorded_On),
constraint Sale_Store_FK foreign key (Store_ID) references Store(Store_ID),
constraint Sale_Customer_FK foreign key (Customer_Num) references Customer(Customer_Num)
);
create table Product
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Product_Name varchar2(50),
Value varchar2(50),
constraint Product_PK primary key(Value),
constraint Product_FK foreign key(Store_ID) references Store(Store_ID),
constraint Product_FK foreign key(Recorded_On) references Sale(Recorded_On)
);
Error starting at line : 67 in command -
create table Product
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Product_Name varchar2(50),
Value varchar2(50),
constraint Product_PK primary key(Value),
constraint Product_FK foreign key(Store_ID) references Store,
constraint Product_FK foreign key(Recorded_On) references Sale(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
Thanks in advance!
UPDATE *
I have changed code as follows
create table Customer
(
Customer_Num varchar2(7) not null,
Surname varchar2(50) not null,
Other_Names varchar2(100) not null,
Email varchar2(320) not null,
Mobile_Phone varchar2(20) not null,
constraint Customer_PK primary key (Customer_Num)
);
create table Store
(
Store_ID varchar2(5) not null,
Region varchar2(50) not null,
constraint Store_PK primary key (Store_ID)
);
create table Sale
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null UNIQUE,
Customer_Num varchar2(7) not null,
Comments varchar2(4000),
constraint Sale_PK primary key (Store_ID, Recorded_On),
constraint Sale_Store_FK foreign key (Store_ID) references Store(Store_ID),
constraint Sale_Customer_FK foreign key (Customer_Num) references Customer
);
create table Product
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Product_Name varchar2(50),
Value varchar2(50),
constraint Product_PK primary key(Store_ID, Recorded_On),
constraint Product_Store_FK foreign key(Store_ID) references Store,
constraint Product_recorded_FK foreign key(Recorded_On) references Sale(Recorded_On)
);
Now I run into this error when inserting statement:
INSERT INTO Product (Store_ID, Recorded_On, Product_Name, Value) VALUES ('AB1', to_date('10/05/2016 13:11', 'DD/MM/YYYY HH24:MI'), 'Test', 2.0);
Error starting at line : 80 in command -
INSERT INTO Product (Store_ID, Recorded_On, Product_Name, Value) VALUES ('AB1', to_date('10/05/2016 13:11', 'DD/MM/YYYY HH24:MI'), 'Test', 2.0)
Error report -
SQL Error: ORA-02291: integrity constraint (Hemi89.PRODUCT_RECORDED_FK) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
I am little confused here as I believe I have set parent key in constraint Sale_PK primary key (Store_ID, Recorded_On) in the Sales Table.
The foreign key you're trying to create on Recorded_On column in product table must refer to a primary key or an unique key. Recorded_On column in your Sale table must be changed to unique or don't create any constraints on it.
After you resolve this error, the next problem you'll run into is constraints not having unique names.
ORA-02264: name already used by an existing constraint
Constraint Name Product_PK & Product_FK gets repeated twice.

Oracle Missing expression

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.

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)
)