ORA-00904: : invalid identifier - sql

I am trying to create a Table in Oracle and getting the error : ORA-00904: : invalid identifier
Here is my command. I really can't see any problem in it. Please help me to identify the error. Thanks.
CREATE TABLE Sale (
CustomerId INT NOT NULL ,
BarCode INT NOT NULL ,
SalesId INT NOT NULL ,
Date DATE NULL ,
CheckOut TINYINT(1) NULL ,
PRIMARY KEY (CustomerId, BarCode, SalesId) ,
CONSTRAINT fk_Customer_has_Product_Customer
FOREIGN KEY (CustomerId )
REFERENCES Customer (CustomerId )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT fk_Customer_has_Product_Product1
FOREIGN KEY (BarCode )
REFERENCES Product (BarCode )
ON DELETE NO ACTION
ON UPDATE NO ACTION);

The maximum length for an Oracle identifier is 30 characters. These exceed that, are 32 chars long:
fk_Customer_has_Product_Customer
fk_Customer_has_Product_Product1
See Schema Object Naming Rules

As previously mentioned, change "DATE" to something more descriptive and not reserved. also, it seems TINYINT does not work in a table create so change that to NUMBER(1), as well as Tony's correct suggestion of reducing the name size (<=30 chrs)
CREATE TABLE Sale
(
CustomerId INT NOT NULL ,
BarCode INT NOT NULL ,
SalesId INT NOT NULL ,
SaleDate DATE NULL , --DATE is reserved, changed to SaleDate
CheckOut number(1) NULL , --tinyint(1) did not work so changed to number(1)
PRIMARY KEY( CustomerId, BarCode, SalesId ) ,
CONSTRAINT fk_SaleCustCusID FOREIGN KEY( CustomerId ) REFERENCES Customer( CustomerId ) ON
DELETE NO ACTION ON
UPDATE NO ACTION,
CONSTRAINT fk_SaleCustBarCode FOREIGN KEY( BarCode ) REFERENCES Product( BarCode ) ON
DELETE NO ACTION ON
UPDATE NO ACTION
);

Related

Can have in my case foreign key duplicated value?

can have in this case foreign key duplicated value? Or better just have an index on column?
Here is my table structure:
CREATE TABLE customers(
id INT (10) NOT NULL,
name VARCHAR (50) NOT NULL,
city VARCHAR (50) NOT NULL
);
CREATE TABLE orders(
cus_id INT (10) NOT NULL ,
order_date DATETIME NOT NULL
);
CREATE TABLE products(
id INT (5) NOT NULL,
product_name VARCHAR(50) NOT NULL,
product_price INT(10) NOT NULL
);
But in orderitems table (Where I have stored the ordered products, the customer can have multiple products ordered so the foreign key value (cus_id) can be duplicated)
CREATE TABLE ordered_items(
id INT (10) NOT NULL,
cus_id INT (10) NOT NULL,
product_id INT(5) NOT NULL
);
ALTER TABLE customers ADD CONSTRAINT customer_id PRIMARY KEY ( id ) ;
ALTER TABLE orders ADD CONSTRAINT customers_id_fr FOREIGN KEY ( cus_id ) REFERENCES customers ( id );
ALTER TABLE ordered_items ADD CONSTRAINT ordered_items_fr FOREIGN KEY ( cus_id ) REFERENCES customers ( id );
EDIT:
Sorry the ordered_items table have a unique ID column as well.
Yes! you can have multiple value of cus_id inside ordered_items. however, your intention is better served if you replace cus_id by order_id from orders, Thus your relationship would sound like,
a **customer** can have multiple *orders*,
an **order** can have multiple *order items*,
and an **order item** can only have single *product*
your sql would look like this
CREATE TABLE customers(
id INT (10) NOT NULL,
name VARCHAR (50) NOT NULL,
city VARCHAR (50) NOT NULL
);
ALTER TABLE customers ADD CONSTRAINT customer_id PRIMARY KEY ( id ) ;
CREATE TABLE orders(
id INT (5) NOT NULL,
cus_id INT (10) NOT NULL ,
order_date DATETIME NOT NULL
);
ALTER TABLE orders ADD CONSTRAINT order_id PRIMARY KEY ( id ) ;
ALTER TABLE orders ADD CONSTRAINT customers_id_fr FOREIGN KEY ( cus_id ) REFERENCES customers ( id );
CREATE TABLE products(
id INT (5) NOT NULL,
product_name VARCHAR(50) NOT NULL,
product_price DOUBLE NOT NULL
);
ALTER TABLE products ADD CONSTRAINT product_id PRIMARY KEY ( id ) ;
CREATE TABLE ordered_items(
id INT (10) NOT NULL,
order_id INT (10) NOT NULL,
product_id INT(5) NOT NULL
);
ALTER TABLE ordered_items ADD CONSTRAINT ordrItm_id PRIMARY KEY ( id ) ;
ALTER TABLE ordered_items ADD CONSTRAINT ordrItm_order_frK FOREIGN KEY ( order_id ) REFERENCES orders ( id );
ALTER TABLE ordered_items ADD CONSTRAINT ordrItm_prd_frK FOREIGN KEY ( product_id) REFERENCES products ( id );

multiple one-to-one relationship mysql

is it possible to create table with multiple one-to-one relationships?
I am tring to obtain it via below query but get SQL error.
Tables are:
Order:
id
cart id (1:1)
user id (1:1)
payment_method_id
shipping_method_id
total price
User
id
email
phone
first name
last name
address
post code
city
password NULL
Cart:
id
cookie
cartItem_id(1:many)
grandTotal
I would like to create table Order with two columns having one-to-one association.
create table order
( id int auto_increment
, cart_id int
, user_id int
, payment_method_id int
, shipping_method_id int
, total_price int
, primary key(user_id)
, primary key(cart_id));
I copied the below query but I am getting error and don't know why.
create table order(id int auto_increment, cart_id int, user_id int, payment_method_id int, shipping_method_id int, total_price int, primary key(id), foreign key (user_id) references user(id), foreign key (cart_id) references cart(id));
it says:
[42000][1064] You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'order(id int auto_increment, cart_id int, user_id int,
payment_method_id int, sh' at line 1
What is it I cannot see? I only changed Id into id.
Firstly, You can't have more than one Primary key on a Table.
Secondly, To have a relation you need to use Foreign Key
Try below Query:
create table order
( id int auto_increment
, cart_id int
, user_id int
, payment_method_id int
, shipping_method_id int
, total_price int
, primary key(id)
, FOREIGN KEY (user_id) REFERENCES User(Id)
, FOREIGN KEY (cart_id) REFERENCES Cart(Id));

SQL integrity constraints primary and foreign keys

I am following some examples and the following should work, but I am getting some errors. Can someone tell me what is wrong?
/* Data Definition */
CREATE TABLE CUSTOMERS
(CUST_NO INTEGER ,
CUST_NAME VARCHAR(40) ,
STREET VARCHAR(40) ,
TOWN VARCHAR(40) ,
POSTCODE INTEGER ,
CR_LIMIT INTEGER ,
CURR_BALANCE INTEGER ,
PRIMARY KEY (CUST_NO)
);
CREATE TABLE PRODUCTS
(PROD_COD VARCHAR(10) ,
DESCRIPTION VARCHAR(50) ,
PROD_GROUP CHAR(1) ,
LIST_PRICE INTEGER ,
QTY_ON_HAND INTEGER ,
REMAKE_LEVEL INTEGER ,
REMAKE_QTY INTEGER ,
PRIMARY KEY (PROD_COD)
);
CREATE TABLE ORDERS
(ORDER_NO INTEGER ,
ORDER_DATE DATE ,
CUST_NO INTEGER ,
PRIMARY KEY (ORDER_NO),
FOREIGN KEY (CUST_NO) REFERENCES CUSTOMERS(CUST_NO)
);
CREATE TABLE ORDER_DETAILS
(ORDER_NO INTEGER ,
PROD_COD VARCHAR(10) ,
ORDER_QTY INTEGER ,
ORDER_PRICE INTEGER ,
PRIMARY KEY (ORDER_NO, PROD_COD),
FOREIGN KEY (ORDER_NO) REFERENCES ORDERS(ORDER_NO),
FOREIGN KEY (PROD_COD) REFERENCES PRODUCTS(PROD_COD)
);
And I am getting these errors when I try to insert values in to the tables:
I can't work it out as it seems that I am referencing the right things, but obviously I am not.
Is it because of the composite primary key? That seemed to work in the example.
I am using Oracle SQL Developer 4.
It would seem you are trying to insert an order for a customer that does not exist. Is there a row in customers with cust_no = 13144 ?

SQL Server Foreign Key Constraint References Invalid Table

I have the following code:
CREATE TABLE _CLIENT
(
client_id int ,
client_name varchar(50),
type varchar(50),
constraint _CLIENT_pk PRIMARY KEY(client_id),
constraint _CLIENT_ch CHECK (client_id>0),
typee_id INT NOT NULL REFERENCES CLIENT_TYPE(typee_id)
)
CREATE TABLE CLIENT_TYPE
(
typee_id int NOT NULL,
name_type varchar(50),
constraint CLIENT_TYPE_pk PRIMARY KEY(typee_id)
)
The foreign key throws an error saying:
Foreign key 'FK__Number__Name__1CF15040' references invalid table 'Users.Name'
what's the wrong here?
I don't know what exact error message you are getting, but you have error in the current script and I think you mean this error:
Foreign key 'FK___CLIENT__typee_i__55BFB948' references invalid table
'CLIENT_TYPE'.
You should first create CLIENT_TYPE table, so the script should look like:
CREATE TABLE CLIENT_TYPE
(
typee_id INT NOT NULL ,
name_type VARCHAR(50) ,
CONSTRAINT CLIENT_TYPE_pk PRIMARY KEY ( typee_id )
)
CREATE TABLE _CLIENT
(
client_id INT ,
client_name VARCHAR(50) ,
type VARCHAR(50) ,
CONSTRAINT _CLIENT_pk PRIMARY KEY ( client_id ) ,
CONSTRAINT _CLIENT_ch CHECK ( client_id > 0 ) ,
typee_id INT NOT NULL
REFERENCES CLIENT_TYPE ( typee_id )
)
As a general rule, you should first create base tables and then tables which depend on those ones.

Error in creating tables in SQL

I'm trying to create a few tables in a new database, but when I try to create them it creates a couple of errors. I'm currently using Microsoft SQL Server Management Studio.
The errors seem to be in the end of the code where I'm trying to add constraints for foreign keys. Any help would be greatly appreciated thank you.
Here's the code, it's meant to generate 3 tables with 1 table containing 2 foreign keys to the other tables with matching column names.
CREATE TABLE Customers
(
CustomerID INT NOT NULL PRIMARY KEY IDENTITY,
ContactName VarChar(50) NULL,
Company VarChar(45) NULL,
Phone VarChar(12) NULL,
)
CREATE TABLE Shippers
(
ShipperID INT NOT NULL PRIMARY KEY IDENTITY,
Company VarChar(45) NULL,
Phone VarChar(12) NULL,
)
CREATE TABLE Orders
(
OrderID INT NOT NULL PRIMARY KEY IDENTITY,
OrderDate DATETIME NULL,
ShippedDate DATETIME NULL,
ShipperID INT NULL,
Freight DECIMAL NULL,
CustomerID INT NULL,
CONSTRAINT fk_Orders_Shippers
FOREIGN KEY ShipperID
REFERENCES Shippers(ShipperID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
CONSTRAINT fk_Orders_Customers
FOREIGN KEY CustomerID
REFERENCES Customers(CustomerID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
And these are the errors I get:
Msg 102, Level 15, State 1, Line 21
Incorrect syntax near 'ShipperID'.
Msg 102, Level 15, State 1, Line 23
Incorrect syntax near 'ACTION'.
Msg 102, Level 15, State 1, Line 28
Incorrect syntax near 'ACTION'.
Any ideas what the problem is?
The error messages point you to a couple of surplus commas, a missing comma and improper foreign key syntax due to missing parentheses.
This is the correct version:
CREATE TABLE Customers (
CustomerID INT NOT NULL PRIMARY KEY IDENTITY,
ContactName VarChar(50) NULL,
Company VarChar(45) NULL,
Phone VarChar(12) NULL -- SURPLUS COMMA REMOVED
)
CREATE TABLE Shippers (
ShipperID INT NOT NULL PRIMARY KEY IDENTITY,
Company VarChar(45) NULL,
Phone VarChar(12) NULL -- SURPLUS COMMA REMOVED
)
CREATE TABLE Orders (
OrderID INT NOT NULL PRIMARY KEY IDENTITY,
OrderDate DATETIME NULL,
ShippedDate DATETIME NULL,
ShipperID INT NULL,
Freight DECIMAL NULL,
CustomerID INT NULL,
CONSTRAINT fk_Orders_Shippers
FOREIGN KEY (ShipperID) -- PARENTHESES ADDED
REFERENCES Shippers(ShipperID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
, -- COMMA ADDED
CONSTRAINT fk_Orders_Customers
FOREIGN KEY (CustomerID) -- PARENTHESES ADDED
REFERENCES Customers(CustomerID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)