SQL integrity constraints primary and foreign keys - sql

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 ?

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

composite foreign key not working

i have a product table with a composite primary key and the composite primary key is working as excpected but whenever i try to use this composite primary key as a foreign key in another table , it dosent work
primary table product:
create table product
(
product_id number(4) ,
sub_product_id number(3) ,
category_id references category,
product_name varchar2(40),
product_brand varchar2(30),
price varchar2(15) ,
image_url varchar2(200) ,
color varchar2(30) ,
intmemory varchar2(30) ,
extmemory varchar2(30) ,
primary key(product_id,sub_product_id)
)
foreign key table cart :
create table cart
(
userid references register1 ,
product_id,
sub_product_id,
quantity number(3) ,
delivery_time varchar2(100) ,
foreign key(product_id ,sub_product_id) references product(product_id ,sub_product_id)
)
primary table is working fine but foreign keys are not working , they dont give error when i insert same (product_id , sub_product_id)
All I want is that the cart table dosen't have multiply entries of the same product_id, sub_product_id
As Jens mentioned, a foreign key is not the same as a unique key. You can create a unique key simply:
ALTER TABLE cart
ADD CONSTRAINT constraint_name
UNIQUE (product_id , sub_product_id);

Understanding how a Primary Key of one Table can be a Foreign Key too

I've been given the code below:
-- Create Customers table
CREATE TABLE Customers
(
cust_id char(10) NOT NULL ,
cust_name char(50) NOT NULL ,
cust_address char(50) NULL ,
cust_city char(50) NULL ,
cust_state char(5) NULL ,
cust_zip char(10) NULL ,
cust_country char(50) NULL ,
cust_contact char(50) NULL ,
cust_email char(255) NULL
);
-- Create OrderItems table
CREATE TABLE OrderItems
(
order_num int NOT NULL ,
order_item int NOT NULL ,
prod_id char(10) NOT NULL ,
quantity int NOT NULL ,
item_price decimal(8,2) NOT NULL
);
-- Create Orders table
CREATE TABLE Orders
(
order_num int NOT NULL ,
order_date datetime NOT NULL ,
cust_id char(10) NOT NULL
);
-- Create Products table
CREATE TABLE Products
(
prod_id char(10) NOT NULL ,
vend_id char(10) NOT NULL ,
prod_name char(255) NOT NULL ,
prod_price decimal(8,2) NOT NULL ,
prod_desc varchar(1000) NULL
);
-- Create Vendors table
CREATE TABLE Vendors
(
vend_id char(10) NOT NULL ,
vend_name char(50) NOT NULL ,
vend_address char(50) NULL ,
vend_city char(50) NULL ,
vend_state char(5) NULL ,
vend_zip char(10) NULL ,
vend_country char(50) NULL
);
-- Define primary keys
ALTER TABLE Customers WITH NOCHECK ADD CONSTRAINT PK_Customers PRIMARY KEY CLUSTERED (cust_id);
ALTER TABLE OrderItems WITH NOCHECK ADD CONSTRAINT PK_OrderItems PRIMARY KEY CLUSTERED (order_num, order_item);
ALTER TABLE Orders WITH NOCHECK ADD CONSTRAINT PK_Orders PRIMARY KEY CLUSTERED (order_num);
ALTER TABLE Products WITH NOCHECK ADD CONSTRAINT PK_Products PRIMARY KEY CLUSTERED (prod_id);
ALTER TABLE Vendors WITH NOCHECK ADD CONSTRAINT PK_Vendors PRIMARY KEY CLUSTERED (vend_id);
-- Define foreign keys
ALTER TABLE OrderItems ADD
CONSTRAINT FK_OrderItems_Orders FOREIGN KEY (order_num) REFERENCES Orders (order_num),
CONSTRAINT FK_OrderItems_Products FOREIGN KEY (prod_id) REFERENCES Products (prod_id);
ALTER TABLE Orders ADD
CONSTRAINT FK_Orders_Customers FOREIGN KEY (cust_id) REFERENCES Customers (cust_id);
ALTER TABLE Products ADD
CONSTRAINT FK_Products_Vendors FOREIGN KEY (vend_id) REFERENCES Vendors (vend_id);
I have a couple of questions:
What's the purpose of the Composite Key in table Orders?
Since order_num is a Primary Key as well as Foreign Key, I can't
understand how it can be Clustered in one table and
Non-Clustered in another.
Also, I don't understand the purpose of WITH NOCHECK when defining
the Primary Keys.
Thanks
What does order_item mean? From the look of things, you can remove it from the PK.
An FK basically says "the value here must be one of the values in that other table". Think of a dropdown box: the list of available choices is provided that the column in the REFERENCES ... (...) clause. CLUSTERED has no impact on it. You can make an FK references non-primary key column too.
When you create the primary key, SQL Server checks the table to ensure that the PK column contains no duplicate value. WITH NOCHECK disables check on existing data, but will check for any new data coming into the table. Its usage is not recommended.

I have an issue with SQL statement

Create Table Person (
ID integer primary key
, [name] character(15) not null
, address character(15)
, phone integer
);
Create Table Surveyor (
surveyID integer
, certificationDate Datetime
, ID integer
, primary KEY (surveyID,ID)
, FOREIGN KEY (ID) REFERENCES Person
);
Create Table Points (
PntID integer primary key
, N float not null
, E float not null
, Height float not null
);
Create Table Polygon (
polyID integer primary key
, area float not null
, atleastonepoint integer not null
, foreign key (atleastonepoint) references Points(PntID)
, check (area > 0)
);
Create Table Block (
blockID integer
, blockName character (15) not null
, polyID integer
, polyLength float not null
, primary KEY (blockID,polyID)
, onemunicipalAuthority character (15) not null
, foreign key (polyID) references polygon
, check (polyLength > 0)
);
Create Table Parcel (
parcelname character (15)
, blockID integer
, polyID integer
, primary KEY (parcelname,blockID,polyID)
, foreign key (polyID) references polygon
, foreign key (blockID) REFERENCES block
);
ERROR : Relationship must be on the same number of fields with the same data types, when I try to implement the last one , CREATE => Parcel
thanx in advance
Try this command:
Create Table Parcel (
parcelname character (15)
, blockID integer
, polyID integer
, primary KEY (parcelname,blockID,polyID)
, foreign key (polyID) references polygon
, foreign key (blockID,polyID) REFERENCES block
);

ORA-00904: : invalid identifier

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