How can I write simple data mining queries for the following schemas? - sql

For the following given schema, can I please guided on how to write simple Data mining queries using oracle with sqlplus?
CREATE TABLE Location(Location_id NUMBER(5) NOT NULL, Location_name varchar(15) NOT NULL,PRIMARY KEY (Location_id));
CREATE TABLE Customer(customer_id NUMBER(5) NOT NULL, customer_name varchar(15) NOT NULL, customer_credit NUMBER(6) NOT NULL, PRIMARY KEY (customer_id));
CREATE TABLE Product(product_id NUMBER(5) NOT NULL, product_name varchar(10) NOT NULL, product_price NUMBER(6) NOT NULL, product_quantity NUMBER(6) NOT NULL, PRIMARY KEY (product_id));
CREATE TABLE Supplier(supplier_id NUMBER(5) NOT NULL, supplier_name varchar(15) NOT NULL, supplier_quantity NUMBER(5) NOT NULL,PRIMARY KEY (supplier_id));
CREATE TABLE Store (customer_id NUMBER(5) NOT NULL, supplier_id NUMBER(5) NOT NULL, product_id NUMBER(5) NOT NULL, location_id NUMBER(5) NOT NULL,sale NUMBER(9) NOT NULL,category varchar(9) NOT NULL);
ALTER TABLE Store ADD CONSTRAINT Store_fk0 FOREIGN KEY (customer_id) REFERENCES Customer(customer_id);
ALTER TABLE Store ADD CONSTRAINT Store_fk1 FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id);
ALTER TABLE Store ADD CONSTRAINT Store_fk2 FOREIGN KEY (product_id) REFERENCES Product(product_id);
ALTER TABLE Store ADD CONSTRAINT Store_fk3 FOREIGN KEY (location_id) REFERENCES Location(Location_id);
I have the following OLAP query which I wrote which I don't see different from a Data mining query. Also can I be guided on how to write more complex queries for the following:
SELECT CUSTOMER.CUSTOMER_NAME,PRODUCT.PRODUCT_NAME,SALE FROM STORE INNER JOIN CUSTOMER ON STORE.CUSTOMER_ID = CUSTOMER.CUSTOMER_ID INNER JOIN PRODUCT ON STORE.PRODUCT_ID = PRODUCT.PRODUCT_ID WHERE STORE.SALE = (SELECT MAX(SALE) FROM STORE);
I will appreciate any help.

This question is too vague to have a good answer. Please refer to Oracle Data Mining for more info about Data Mining.
Data Mining start from a simple query like:
BEGIN
DBMS_PREDICTIVE_ANALYTICS.EXPLAIN(
data_table_name => 'mining_data_build_v',
explain_column_name => 'affinity_card',
result_table_name => 'ai_explain_output');
END;
/
And the skies are the limit...

Related

How can I correct the following SQL script?

I am assigned to create a database in SQL based on an ERD that I studied and recreated a week prior. I am using this app called "Oracle SQL Developer" and trying to learn about creating tables, primary keys, foreign keys, sequences, views, etc. I tested my drafts out on the developer and they keep on coming up with the following errors:
[enter image description here][1]
[1]: https://i.stack.imgur.com/vk0cu.png this is some syntax error due to partially recognized rules.
Other errors involve missing right parentheses, tables having more than one primary key, etc. So far, this is my best effort at starting a database:
/* CREATE A TABLE FOR CUSTOMER INFORMATION FROM THE GREETING CARD CUSTOMIZATION APPLICATION */
CREATE TABLE CUSTOMER
(CUST_EMAIL VARCHAR(10) PRIMARY KEY,
CUST_NAME VARCHAR(10) NOT NULL,
CUST_PHONE NUMERIC(10) NOT NULL,
CUST_ADDRESS VARCHAR(10) NOT NULL,
CUST_CITY VARCHAR(10) NOT NULL,
CUST_STATE VARCHAR(10) NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUST_EMAIL)
);
/* CREATE A TABLE FOR GREETING CARD AND ENVELOPE ORDER INFORMATION */
CREATE TABLE PRODUCTS
(ORDER_NO NUMERIC(10) PRIMARY KEY,
CUST_EMAIL VARCHAR(10) FOREIGN KEY,
TRACK_ID NUMERIC(10) NOT NULL,
CONF_NO NUMERIC(10) NOT NULL,
ORDER_DATE DATE(10) NOT NULL,
SHIP_DATE DATE(10) NOT NULL,
CONSTRAINT PK_PRODUCTS PRIMARY KEY (ORDER_NO)
CONSTRAINT FK_PRODUCTS_CUST_EMAIL FOREIGN KEY (CUST_EMAIL) REFERENCES CUSTOMER);
/* CREATE A TABLE FOR PAYMENT INFORMATION */
CREATE TABLE PAYMENT
(PAY_ID NUMERIC(10) PRIMARY KEY,
ORDER_NO NUMERIC(10) FOREIGN KEY,
CARD_TYPE VARCHAR(10) NOT NULL,
PRICE NUMERIC(10) NOT NULL,
PAY_DATE DATE(10) NOT NULL,
PAY_CONF INTEGER(10) NOT NULL,
CONSTRAINT PK_PAYMENT PRIMARY KEY (PAY_ID),
CONSTRAINT FK_PAYMENT_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS);
/* CREATE A TABLE FOR PRODUCT DELIVERY INFORMATION */
CREATE TABLE DELIVERY
(DEL_ID NUMERIC(10) PRIMARY KEY,
ORDER_NO NUMERIC(10) FOREIGN KEY,
SHIP_DATE DATE(10) FOREIGN KEY,
DEL_DATE DATE(10) NOT NULL,
STATUS VARCHAR(10) NOT NULL,
DEL_MODE VARCHAR(10) NOT NULL,
INVOICE_NO INTEGER(10) NOT NULL,
CONSTRAINT PK_DELIVERY PRIMARY KEY (DEL_ID),
CONSTRAINT FK_DELIVERY_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS
CONSTRAINT FK_DELIVERY_SHIP_DATE FOREIGN KEY (SHIP_DATE) REFERENCES PRODUCTS);
/* CREATE A TABLE FOR RECIPIENT INFORMATION */
CREATE TABLE RECIPIENT
(STREET_ADDRESS VARCHAR(10) PRIMARY KEY,
NAME VARCHAR(10) NOT NULL,
CITY VARCHAR(10) NOT NULL,
STATE VARCHAR(10) NOT NULL,
ZIP INTEGER(10) NOT NULL,
CONSTRAINT PK_RECIPIENT PRIMARY KEY (STREET_ADDRESS)
);
Where should I place my parentheses if the app is correct in saying that I am missing some of them? Where do I even have more than one primary key and how can I rephrase my lines to reduce them? How can I take my rules from partially recognized to fully recognized?
This is for a college project on relational database systems. I just need to create some tables, primary keys, and foreign keys so I can be allowed to create sequences.
DATE and INTEGER do not have a precision.
Either declare the PRIMARY KEY inline or out-of-line but you cannot do both.
Same for foreign keys (and inline foreign keys need the REFERENCES keyword and not the FOREIGN KEY keywords).
VARCHAR would be better as VARCHAR2
You cannot have a FOREIGN KEY that refers to a non-primary key, non-unique column (i.e. SHIP_DATE). While you could create a UNIQUE composite key on ORDER_NO and SHIP_DATE and reference that (example below); it is probably better to entirely remove SHIP_DATE from the DELIVERY table (and then you don't need a foreign key) and just keep it in a single table so the tables are in 3rd normal form. If you want the information to display it then JOIN the tables using the ORDER_NO foreign key.
/* CREATE A TABLE FOR CUSTOMER INFORMATION FROM THE GREETING CARD CUSTOMIZATION APPLICATION */
CREATE TABLE CUSTOMER(
CUST_EMAIL VARCHAR2(10),
CUST_NAME VARCHAR2(10) NOT NULL,
CUST_PHONE NUMERIC(10) NOT NULL,
CUST_ADDRESS VARCHAR2(10) NOT NULL,
CUST_CITY VARCHAR2(10) NOT NULL,
CUST_STATE VARCHAR2(10) NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUST_EMAIL)
);
/* CREATE A TABLE FOR GREETING CARD AND ENVELOPE ORDER INFORMATION */
CREATE TABLE PRODUCTS(
ORDER_NO NUMERIC(10),
CUST_EMAIL VARCHAR2(10),
TRACK_ID NUMERIC(10) NOT NULL,
CONF_NO NUMERIC(10) NOT NULL,
ORDER_DATE DATE NOT NULL,
SHIP_DATE DATE NOT NULL,
CONSTRAINT PK_PRODUCTS PRIMARY KEY (ORDER_NO),
CONSTRAINT U_PRODUCTS UNIQUE (ORDER_NO, SHIP_DATE),
CONSTRAINT FK_PRODUCTS_CUST_EMAIL FOREIGN KEY (CUST_EMAIL) REFERENCES CUSTOMER
);
/* CREATE A TABLE FOR PAYMENT INFORMATION */
CREATE TABLE PAYMENT(
PAY_ID NUMERIC(10),
ORDER_NO NUMERIC(10),
CARD_TYPE VARCHAR2(10) NOT NULL,
PRICE NUMERIC(10) NOT NULL,
PAY_DATE DATE NOT NULL,
PAY_CONF INTEGER NOT NULL,
CONSTRAINT PK_PAYMENT PRIMARY KEY (PAY_ID),
CONSTRAINT FK_PAYMENT_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS
);
/* CREATE A TABLE FOR PRODUCT DELIVERY INFORMATION */
CREATE TABLE DELIVERY(
DEL_ID NUMERIC(10),
ORDER_NO NUMERIC(10),
SHIP_DATE DATE, -- Delete this line
DEL_DATE DATE NOT NULL,
STATUS VARCHAR2(10) NOT NULL,
DEL_MODE VARCHAR2(10) NOT NULL,
INVOICE_NO INTEGER NOT NULL,
CONSTRAINT PK_DELIVERY PRIMARY KEY (DEL_ID),
CONSTRAINT FK_DELIVERY_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS,
CONSTRAINT FK_DELIVERY_SHIP_DATE FOREIGN KEY (ORDER_NO, SHIP_DATE) REFERENCES PRODUCTS (ORDER_NO, SHIP_DATE) -- Delete this line.
);
/* CREATE A TABLE FOR RECIPIENT INFORMATION */
CREATE TABLE RECIPIENT(
STREET_ADDRESS VARCHAR2(10),
NAME VARCHAR2(10) NOT NULL,
CITY VARCHAR2(10) NOT NULL,
STATE VARCHAR2(10) NOT NULL,
ZIP INTEGER NOT NULL,
CONSTRAINT PK_RECIPIENT PRIMARY KEY (STREET_ADDRESS)
);
db<>fiddle here

SQL ORACLE - "Table or view does not exist"

CREATE TABLE Order_Item
(
Tree_ID VARCHAR(30) PRIMARY KEY
REFERENCES Tree(ID),
Order_ID VARCHAR(30) NOT NULL,
FOREIGN KEY (Order_ID) REFERENCES Order_Form(Order_ID)
);
CREATE TABLE Tree
(
ID VARCHAR(30) PRIMARY KEY,
Roottype VARCHAR(30) NOT NULL,
FOREIGN KEY (Roottype) REFERENCES Rootstock(Rootstocktype),
Variety VARCHAR(60) NOT NULL,
FOREIGN KEY (Variety) REFERENCES Variety(Name),
Age NUMBER(9) NOT NULL,
CHECK (Age >= 0),
Pot_Size NUMBER(9) NOT NULL,
CHECK (Pot_Size >= 0),
Price NUMBER(9) NOT NULL,
CHECK (Price >= 0),
Available_On VARCHAR(100) NOT NULL
);
Can someone explain to me why im getting this error code?
Thank You
Oracle checks if all objects you are creating exists. When you try to create table Order_Item, your schema is referencing another table that you are creating later. The solution is create first tables without references and finally most complex tables.

Oracle PLSQL Cascade Delete doesn't work?

I have some tables with foreign keys which should be deleted. I Put the "on delete cascade" everywhere I needed it, but when I try to Drop the Table i get following error:
*Cause: An attempt was made to drop a table with unique or
primary keys referenced by foreign keys in another table.
This is the Table I want to drop:
DROP TABLE Author;
CREATE TABLE Author (
id NUMBER(4) NOT NULL,
first_name VARCHAR2(30) NOT NULL,
last_name VARCHAR2(30) NOT NULL,
date_of_birth DATE NOT NULL,
date_of_death DATE NULL,
CONSTRAINT Author_PK PRIMARY KEY (id)
);
And this is the Table that is in relation to the Author table:
CREATE TABLE Book (
id NUMBER(4) NOT NULL,
author NUMBER(4) NULL,
title VARCHAR2(30) NOT NULL,
ISBN VARCHAR2(13) NOT NULL,
book_language VARCHAR2(2) NOT NULL,
book_genre VARCHAR2(20) NOT NULL,
CONSTRAINT Book_PK PRIMARY KEY (id),
CONSTRAINT Book_Author FOREIGN KEY (author) REFERENCES Author(id) ON DELETE cascade
);
DROP is a DDL. It has nothing to do with DELETE (DML) (affected by the way you created the foreign key constraint).
Drop child table first; then drop its parent.
I resolved it now with help from #a_horse_with_no_name
If you have the same issue, just write
drop table table_name cascade constraints;

Simple SQL issues, cannot figure out what I'm doing wrong to get these errors - ORA-00942 / 907 / 922

This is a supposed to be a simple SQL project, but I'm stuck on the first step. I've never worked with SQL before, so I'm pretty lost. Can someone please tell me what I did wrong that's causing these errors?
Here is the file:
SPOOL output.log;
DROP TABLE rental CASCADE CONSTRAINTS;
DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE customer CASCADE CONSTRAINTS;
DROP TABLE distributor CASCADE CONSTRAINTS;
DROP TABLE rental_store CASCADE CONSTRAINTS;
CREATE TABLE rental (
inventory_id CHAR(10) PRIMARY KEY,
transaction_id CHAR(10) NOT NULL UNIQUE,
late DECIMAL(6,2),
damaged DECIMAL(6,2),
fail_rewind DECIMAL(6,2),
taxes DECIMAL(6,2) NOT NULL,
discount DECIMAL(6,2),
customer_id CHAR(10) NOT NULL UNIQUE,
CONTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id));
CREATE TABLE movie (
title_id_number CHAR(10) PRIMARY KEY,
genre VARCHAR2(20) NOT NULL,
actor VARCHAR2(30) NOT NULL,
director VARCHAR2(30) NOT NULL,
awards VARCHAR2(30),
running_length INTEGER NOT NULL,
rating VARCHAR2(5) NOT NULL,
year_released INTEGER NOT NULL,
media_type CHAR(5) NOT NULL,
inventory_id CHAR(10) NOT NULL UNIQUE,
title VARCHAR2(30) NOT NULL,
distrib_serial INTEGER NOT NULL UNIQUE,
cat_mov_id CHAR(10) NOT NULL UNIQUE));
CREATE TABLE customer (
customer_id CHAR(10) PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
address VARCHAR2(50) NOT NULL,
tele_number CHAR(10) NOT NULL UNIQUE,
rent_history INTEGER NOT NULL));
CREATE TABLE distributor (
distributor_name VARCHAR2(30) PRIMARY KEY,
catalog VARCHAR2(30) NOT NULL,
genres_offered VARCHAR2(50) NOT NULL));
CREATE TABLE rental_store (
store_name VARCHAR2(30) PRIMARY KEY,
address VARCHAR2(50) NOT NULL,
owner VARCHAR2(30) NOT NULL));
SPOOL OFF;
The errors for the DROP statements are to be expected because the tables do not yet exist. You can safely ignore them.
Most of your problems come from the second closing parenthesis, e.g.
CREATE TABLE movie (
...
cat_mov_id CHAR(10) NOT NULL UNIQUE));
^ ---- here
You need to remove them. It is only needed in the first statement because of the column list of the foreign key.
The first (real) error you get is because you misspelled CONSTRAINT (you wrote CONTRAINT. The full clause needs to be
CONSTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
^ -- the "s" was missing here
However you can't create a foreign key constraint to a table that does not exist. So you need to change the order of the create statements to first create the customer table, then you can create the rental table.
Putting all that together, your script should look like this:
DROP TABLE rental CASCADE CONSTRAINTS;
DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE customer CASCADE CONSTRAINTS;
DROP TABLE distributor CASCADE CONSTRAINTS;
DROP TABLE rental_store CASCADE CONSTRAINTS;
CREATE TABLE customer (
customer_id CHAR(10) PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
address VARCHAR2(50) NOT NULL,
tele_number CHAR(10) NOT NULL UNIQUE,
rent_history INTEGER NOT NULL
);
CREATE TABLE rental (
inventory_id CHAR(10) PRIMARY KEY,
transaction_id CHAR(10) NOT NULL UNIQUE,
late DECIMAL(6,2),
damaged DECIMAL(6,2),
fail_rewind DECIMAL(6,2),
taxes DECIMAL(6,2) NOT NULL,
discount DECIMAL(6,2),
customer_id CHAR(10) NOT NULL UNIQUE,
CONSTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE movie (
title_id_number CHAR(10) PRIMARY KEY,
genre VARCHAR2(20) NOT NULL,
actor VARCHAR2(30) NOT NULL,
director VARCHAR2(30) NOT NULL,
awards VARCHAR2(30),
running_length INTEGER NOT NULL,
rating VARCHAR2(5) NOT NULL,
year_released INTEGER NOT NULL,
media_type CHAR(5) NOT NULL,
inventory_id CHAR(10) NOT NULL UNIQUE,
title VARCHAR2(30) NOT NULL,
distrib_serial INTEGER NOT NULL UNIQUE,
cat_mov_id CHAR(10) NOT NULL UNIQUE
);
CREATE TABLE distributor (
distributor_name VARCHAR2(30) PRIMARY KEY,
catalog VARCHAR2(30) NOT NULL,
genres_offered VARCHAR2(50) NOT NULL
);
CREATE TABLE rental_store (
store_name VARCHAR2(30) PRIMARY KEY,
address VARCHAR2(50) NOT NULL,
owner VARCHAR2(30) NOT NULL
);
Unrelated, but: you do not want to use the CHAR data type. It's more efficient and will give you less headaches if you use VARCHAR for every character column.
Of course the first time you run the script your DROP TABLEs will fail because you haven't created them yet. That's to be expected and is acceptable in such a script.
Beyond that, if you are going to create FK relationships, you need to create the parent table before you create the child, which you will see after you correct your misspelled key word.
I will leave it as an exercise for the student to take that clue and locate the mis-spelled key word in the first CREATE TABLE statement.

How to insert multiple rows into an Order table?

So I set up a simple database and I am at the point where I am trying to test that it is working correctly. I am trying to figure out how I get multiple entries into an Order table. I need to have an order# and be able to order multiple items and use multiple shippers and such. I just can't seem to figure out a way to get the data into my DB properly.
Below is how I have my DB set up. Could someone please explain to me how to get the my test data into the DB.
Here are the reference tables:
CREATE TABLE Product_Table (
ProductID INT NOT NULL,
Product_Name char(50) NOT NULL,
Product_Cost number(9,2) NOT NULL,
Product_In_Stock INT NOT NULL,
CONSTRAINT Products_PK PRIMARY KEY (ProductID)
);
CREATE TABLE Payment_Terms_Table (
PayTermNum INT NOT NULL,
Payment_Time_Frame CHAR(20) NOT NULL,
CONSTRAINT Payment_Terms_PK PRIMARY KEY (PayTermNum)
);
CREATE TABLE Shipper_Table (
ShipperNum INT NOT NULL,
Shipper_Name CHAR(50) NOT NULL,
Shipper_Phone CHAR(22) NULL,
CONSTRAINT ShipperNum_PK PRIMARY KEY (ShipperNum)
);
CREATE TABLE Supplier_Table (
SupplierID INT NOT NULL,
Supplier_Name CHAR(50) NOT NULL,
Sup_Address CHAR(50) NOT NULL,
Sup_City CHAR(20) NOT NULL,
Sup_State CHAR(20) NOT NULL,
Sup_Zip CHAR(9) NOT NULL,
Sup_Phone CHAR(22) NULL,
ShipperNum INT NOT NULL,
PayTermNum INT NOT NULL,
CONSTRAINT Supplier_PK PRIMARY KEY (SupplierID),
CONSTRAINT ShipperNum_Relationship FOREIGN KEY (ShipperNum)
REFERENCES Shipper_Table (ShipperNum),
CONSTRAINT PayTermNum_Relationship FOREIGN KEY (PayTermNum)
REFERENCES Payment_Terms_Table (PayTermNum)
);
Here is my Order table:
CREATE TABLE Order_Table (
OrderID INT NOT NULL,
ProductID INT NOT NULL,
SupplierID INT NOT NULL,
Wholesale_Price NUMBER (9,2) NOT NULL,
Units_Ordered INT NOT NULL,
Order_Date DATE DEFAULT SYSDATE NOT NULL,
Order_Received DATE NULL,
CONSTRAINT Order_PK PRIMARY KEY (OrderID),
CONSTRAINT ProductID_Relationship FOREIGN KEY (ProductID)
REFERENCES Product_Table (ProductID),
CONSTRAINT SupplierID_Relationship FOREIGN KEY (SupplierID)
REFERENCES Supplier_Table (SupplierID)
);
Your problem is that you have a defined one table to hold Orders, so you can have only one Item per Order. Normally we handle this scenario by having two tables: a Header with the information for the whole Order and a Line table for each ordered Item.
CREATE TABLE Order_Header (
OrderID INT NOT NULL,
Order_Date DATE DEFAULT SYSDATE NOT NULL,
Order_Received DATE NULL,
CONSTRAINT Order_PK PRIMARY KEY (OrderID)
)
/
CREATE TABLE Order_Line (
OrderID INT NOT NULL,
LineNo INT NOT NULL,
ProductID INT NOT NULL,
SupplierID INT NOT NULL,
Wholesale_Price NUMBER (9,2) NOT NULL,
Units_Ordered INT NOT NULL,
CONSTRAINT Order_Line_PK PRIMARY KEY (OrderID, LineNo),
CONSTRAINT Order_Line_Header_FK FOREIGN KEY (OrderID)
REFERENCES Order_Header (OrderID)
CONSTRAINT ProductID_Relationship FOREIGN KEY (ProductID)
REFERENCES Product_Table (ProductID),
CONSTRAINT SupplierID_Relationship FOREIGN KEY (SupplierID)
REFERENCES Supplier_Table (SupplierID)
)
/
I have declared a composite primary key because it is easier to understand what's happening.
Now that you have two tables you can easily create orders with more than one line.
On the subject of normal practice, an Order should have a CUSTOMER. That would be an attribute of ORDER_HEADER. Also you have a SHIPPER table but don't use it. Again that would probably be an attribute of ORDER_HEADER.
Also your naming convention is ugly. There's no need to include _TABLE: just name the objects for teh things they represent. Likewise your foreign key needs are opaque; it doesn't matter so much in this toy example but in a real database you will find it helpful to specify the child and parent tables in the FK names.