I want to create this below table but I'mm getting the error I don't understand why, could someone explain?
CREATE TABLE STUD (
RNO NUMBER(3) CONSTRAINT PK_RNO_STUD PRIMAY KEY,
SNAME VARCHAR2(15) CONSTRAINT NN_SNAME_STUD NOT NULL,
COURSE VARCHAR2(10) CONSTRAINT NN_COURSE_STUD NOT NULL,
FEE NUMBER(5) CONSTRAINT NN_FEE_STUD NOT NULL,
MOBILE NUMBER(10) CONSTRAINT UK_MOBIE_STUD UNIQUE,
CONSTRAINT CK_RNO_STUD CHECK(RNO BETWEEN 1 AND 60),
CONSTRAINT CK_COURSE_STUD CHECK(COURSE IN('ORACLE','SQL SERVER','UNIX')),
CONSTRAINT CK_FEE_STUD CHECK(FEE BETWEEN 5000 AND 10000)
);
Error report - ORA-02253: constraint specification not allowed here
02253. 00000 - "constraint specification not allowed here"
*Cause: Constraint specification is not allowed here in the statement.
*Action: Remove the constraint specification from the statement.
You have misspelled PRIMARY KEY:
CREATE TABLE STUD (
RNO NUMBER(3) CONSTRAINT PK_RNO_STUD PRIMARY KEY,
SNAME VARCHAR2(15) CONSTRAINT NN_SNAME_STUD NOT NULL,
COURSE VARCHAR2(10) CONSTRAINT NN_COURSE_STUD NOT NULL,
FEE NUMBER(5) CONSTRAINT NN_FEE_STUD NOT NULL,
MOBILE NUMBER(10) CONSTRAINT UK_MOBIE_STUD UNIQUE,
CONSTRAINT CK_RNO_STUD CHECK(RNO BETWEEN 1 AND 60),
CONSTRAINT CK_COURSE_STUD CHECK(COURSE IN('ORACLE','SQL SERVER','UNIX')),
CONSTRAINT CK_FEE_STUD CHECK(FEE BETWEEN 5000 AND 10000)
);
Related
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.
I have stared at this until my eyeballs bleed, where am I missing a parenthesis? It does also say
Error starting at line: 1 in command-".
The cause and action section of the error report is blank.
CREATE TABLE EVENTREQUEST(
EVENTNO VARCHAR2(8) CONSTRAINT EVENTNO_NOTNULL NOT NULL,
DATEHELD DATE CONSTRAINT DATEHELD_NOTNULL NOT NULL,
DATEREQ DATE CONSTRAINT DATEREQ_NOTNULL NOT NULL,
CUSTNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
FACNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
DATEAUTH DATE,
STATUS VARCHAR2(15) CHECK (STATUS IN ('Pending', 'Denied', 'Approved')) CONSTRAINT STATUS_NOTNULL NOT NULL,
ESTCOAST VARCHAR2(30) CONSTRAINT ESTCOAST_NOTNULL NOT NULL,
ESTAUDIENCE VARCHAR2(30) CHECK(ESTAUDIENCE > 0) CONSTRAINT ESTAUDIENCE_NOTNULL NOT NULL,
BUDNO VARCHAR2(8),
CONSTRAINT PK_EVENTNO PRIMARY KEY,
CONSTRAINT FK_CUSTONO FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER(CUSTNO),
CONSTRAINT FK_FACNO FOREIGN KEY (FACNO) REFERENCES FACILITY(FACNO)
);
This just because you missed column name while declaring primary key. There is another problem: constraint name for both fourth and fifth columns are same. I have changed that too.
And there is no need to declare CONSTRAINT EVENTNO_NOTNULL NOT NULL since you are declaring it as primary key.
CREATE TABLE EVENTREQUEST(
EVENTNO VARCHAR2(8) CONSTRAINT EVENTNO_NOTNULL NOT NULL,
DATEHELD DATE CONSTRAINT DATEHELD_NOTNULL NOT NULL,
DATEREQ DATE CONSTRAINT DATEREQ_NOTNULL NOT NULL,
CUSTNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
FACNO VARCHAR2(8) CONSTRAINT FACNO_NOTNULL2 NOT NULL,
DATEAUTH DATE,
STATUS VARCHAR2(15) CHECK (STATUS IN ('Pending', 'Denied', 'Approved')) CONSTRAINT STATUS_NOTNULL NOT NULL,
ESTCOAST VARCHAR2(30) CONSTRAINT ESTCOAST_NOTNULL NOT NULL,
ESTAUDIENCE VARCHAR2(30) CHECK(ESTAUDIENCE > 0) CONSTRAINT ESTAUDIENCE_NOTNULL NOT NULL,
BUDNO VARCHAR2(8),
CONSTRAINT PK_EVENTNO PRIMARY KEY (EVENTNO),
CONSTRAINT FK_CUSTONO FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER(CUSTNO),
CONSTRAINT FK_FACNO FOREIGN KEY (FACNO) REFERENCES FACILITY(FACNO));
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.
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.
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.