I have done this and it works.
CREATE TABLE Employee (
Employee_ID varchar2(10) PRIMARY KEY NOT NULL,
Office_ID varchar2(7) NOT NULL,
Emp_FirstName varchar2(20) NOT NULL,
Emp_LastName varchar2(20) NOT NULL,
Emp_Gender varchar2(1) NOT NULL,
Emp_DateOfBirth Date NOT NULL,
Hire_Date date NOT NULL,
Emp_CurrentDate date default sysdate,
Emp_Telephone varchar2(11) NOT NULL,
Emp_Email varchar2(30) NOT NULL,
Emp_AddressLine varchar2(50) NOT NULL,
Emp_PostCode varchar2(8) NOT NULL,
Emp_Speciality varchar2(20) NOT NULL,
Emp_Qualification varchar2(20) NOT NULL,
Emp_AwardingBody varchar2(30) NOT NULL,
Emp_Salary number(6) NOT NULL,
Emp_Supervised_By varchar2(10) NOT NULL,
Employment_History1 varchar2(50),
Employment_History2 varchar2(50),
Employment_History3 varchar2(50) ,
CONSTRAINT fk_staff_office FOREIGN KEY (Office_ID) REFERENCES office (Office_ID),
CONSTRAINT Hire_Date_CK check (Hire_Date < Emp_CurrentDate AND (Hire_Date - Emp_DateOfBirth)/365 > 18),
CONSTRAINT Emp_DateOfBirth_CK check (Emp_DateOfBirth > TO_DATE('1900-01-01', 'YYYY-MM-DD')),
CONSTRAINT Emp_Salary_CK check (Emp_Salary > 0 AND Emp_Salary < 150000),
CONSTRAINT Emp_Gender_CK CHECK (Emp_Gender in ('M','F')),
CONSTRAINT Emp_Email_CK CHECK ( Emp_Email like '%_#__%._%'),
CONSTRAINT Emp_Telephone_CK CHECK (regexp_like(Emp_Telephone, '^[0123456789]{11}$') AND Emp_Telephone like '0%'),
CONSTRAINT Emp_FirstName_CK CHECK (regexp_like(Emp_FirstName, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]{1,20}$')),
CONSTRAINT Emp_LastName_CK CHECK (regexp_like(Emp_LastName, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]{1,20}$')),
CONSTRAINT Emp_PostCode_CK CHECK (regexp_like ( Emp_PostCode , '([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2})')),
CONSTRAINT Emp_Speciality_CK CHECK (regexp_like(Emp_Speciality, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,20}$')),
CONSTRAINT Emp_Qualification_CK CHECK (regexp_like(Emp_Qualification, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,20}$')),
CONSTRAINT Emp_AwardingBody_CK CHECK (regexp_like(Emp_AwardingBody, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,30}$'))
);
What I want to do now is to create a constraint that enables me to set the value of the attribute Emp_Supervised_By automatically as the same value of the Employee_ID attribute when the Emp_Speciality is 'Manager'.
I tried to do it like this but it doesn't work:
CONSTRAINT Emp_Supervised_By_CK check (CASE WHEN Emp_Speciality = 'MANAGER' THEN Emp_Supervised_By = Employee_ID)
Is a requirement to use CONSTRAINTS NOT TRIGGERS.
Because you mentioned computed columns, maybe your business requirement can be satisfied with the below instead of a constraint.
CREATE TABLE Employee (
...
Employee_ID varchar2(10) PRIMARY KEY NOT NULL,
Emp_Speciality varchar2(20) NOT NULL,
Emp_Supervised_By AS (CASE WHEN Emp_Speciality = 'Manager' THEN Employee_ID ELSE NULL END)
....
);
I did this and it works:
CREATE TABLE OrderLine (
Invoice_ID varchar2(10) PRIMARY KEY NOT NULL,
Item_ID varchar2(10) NOT NULL,
Unit_Price number(6,2) NOT NULL,
Quantity number(6) NOT NULL,
Total_Price number(38,2) AS (CASE WHEN Quantity >= 50 THEN
((Unit_Price*Quantity)-((Unit_Price*Quantity)*0.80)) ELSE Unit_Price*Quantity END) NOT NULL,
CONSTRAINT fk_item_invoice FOREIGN KEY(Invoice_ID) REFERENCES Invoice (Invoice_ID),
CONSTRAINT fk_item_item FOREIGN KEY(Item_ID) REFERENCES Item (Item_ID),
CONSTRAINT Unit_Price_CK CHECK (Unit_Price > 0),
CONSTRAINT Quantity_CK CHECK (Quantity > 0),
CONSTRAINT Total_Price_CK CHECK (Total_Price > 0)
);
CREATE TABLE Invoice (
Invoice_ID varchar2(10) PRIMARY KEY NOT NULL,
Office_ID varchar2(7) NOT NULL,
Invoice_Date Date NOT NULL,
Total_Cost number (9) NOT NULL,
CONSTRAINT fk_order_Office FOREIGN KEY(Office_ID) REFERENCES Office (Office_ID),
CONSTRAINT Total_Cost_CK CHECK (Total_Cost > 0)
);
I want to calculate The TotalCost like the sum of TotalPrice(s).
Is there a way to use the same approach ass before ?
Related
I need to modify the structure of the sample database such that after the
modifications it is possible to store information about the total number of times each product has been ordered by the customers.
Please note that some products may have
not been ordered at all. It is important to find the best design. I need to enforce
appropriate consistency constraints.
After this, the script saves in the sample database information about the total number of
times each product has been ordered by the customers.
I am using oracle SQL developer.
Here is what I have tried so far:
CREATE TABLE TOTAL_NUM(
CUSTOMER_CODE VARCHAR(30) NOT NULL,
PRODUCT_NAME VARCHAR(30) NOT NULL,
TOTAL_ORDERED DECIMAL(2) NOT NULL,
CONSTRAINT TOTALO_PKEY1 PRIMARY KEY(CUSTOMER_CODE),
CONSTRAINT TOTALO_FKEY1 FOREIGN KEY(CUSTOMER_CODE) REFERENCES CUSTOMER(CUSTOMER_CODE),
CONSTRAINT TOTALO_UNIQUE_NAME UNIQUE(PRODUCT_NAME),
CONSTRAINT TOTALO_FKEY2 FOREIGN KEY(PRODUCT_NAME) REFERENCES PRODUCT(PRODUCT_NAME),
CONSTRAINT TOTALO_CHECK CHECK(TOTAL_ORDERED > 0));
INSERT INTO TOTAL_NUM(
SELECT ORDERS.CUSTOMER_CODE, COUNT(*) AS TOTAL_ORDERED
FROM ORDERS
LEFT JOIN ORDER_DETAIL ON ORDERS.ORDER_ID = ORDER_DETAIL.ORDER_ID);
I have created a seperate table to store this information, however when I try to insert the values into the table, I get an error "not enough values".
Here are the other tables in the database:
CREATE TABLE CATEGORY
(
CATEGORY_NAME VARCHAR(30) NOT NULL,
DESCRIPTION VARCHAR(2000) NOT NULL,
PICTURE VARCHAR(255) NOT NULL,
CONSTRAINT PK_CATEGORY PRIMARY KEY (CATEGORY_NAME)
);
CREATE TABLE CUSTOMER
(
CUSTOMER_CODE VARCHAR(5) NOT NULL,
COMPANY_NAME VARCHAR(40) NOT NULL,
CONTACT_NAME VARCHAR(30) NOT NULL,
CONTACT_TITLE VARCHAR(30) NOT NULL,
ADDRESS VARCHAR(60) NOT NULL,
CITY VARCHAR(15) NOT NULL,
REGION VARCHAR(15) NULL,
POSTAL_CODE VARCHAR(10) NULL,
COUNTRY VARCHAR(15) NOT NULL,
PHONE VARCHAR(24) NOT NULL,
FAX VARCHAR(24) NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUSTOMER_CODE)
);
CREATE TABLE EMPLOYEE
(
EMPLOYEE_ID NUMBER(9) NOT NULL,
LASTNAME VARCHAR(20) NOT NULL,
FIRSTNAME VARCHAR(10) NOT NULL,
TITLE VARCHAR(30) NOT NULL,
TITLE_OF_COURTESY VARCHAR(25) NOT NULL,
BIRTHDATE DATE NOT NULL,
HIREDATE DATE NOT NULL,
ADDRESS VARCHAR(60) NOT NULL,
CITY VARCHAR(15) NOT NULL,
REGION VARCHAR(15) NULL,
POSTAL_CODE VARCHAR(10) NOT NULL,
COUNTRY VARCHAR(15) NOT NULL,
HOME_PHONE VARCHAR(24) NOT NULL,
EXTENSION VARCHAR(4) NOT NULL,
PHOTO VARCHAR(255) NOT NULL,
NOTES VARCHAR(2000) NOT NULL,
REPORTS_TO NUMBER(9) NULL,
CONSTRAINT PK_EMPLOYEE PRIMARY KEY (EMPLOYEE_ID)
);
CREATE TABLE SUPPLIER
(
COMPANY_NAME VARCHAR(40) NOT NULL,
CONTACT_NAME VARCHAR(30) NOT NULL,
CONTACT_TITLE VARCHAR(30) NOT NULL,
ADDRESS VARCHAR(60) NOT NULL,
CITY VARCHAR(15) NOT NULL,
REGION VARCHAR(15) NULL,
POSTAL_CODE VARCHAR(10) NOT NULL,
COUNTRY VARCHAR(15) NOT NULL,
PHONE VARCHAR(24) NOT NULL,
FAX VARCHAR(24) NULL,
HOME_PAGE VARCHAR(500) NULL,
CONSTRAINT PK_SUPPLIER PRIMARY KEY (COMPANY_NAME)
);
CREATE TABLE SHIPPER
(
COMPANY_NAME VARCHAR(40) NOT NULL,
PHONE VARCHAR(24) NULL,
CONSTRAINT PK_SHIPPER PRIMARY KEY (COMPANY_NAME),
CONSTRAINT CK_SHIPPER UNIQUE (PHONE)
);
CREATE TABLE PRODUCT
(
PRODUCT_NAME VARCHAR(40) NOT NULL,
SUPPLIER_NAME VARCHAR(40) NOT NULL,
CATEGORY_NAME VARCHAR(30) NOT NULL,
QUANTITY_PER_UNIT VARCHAR(20) NULL,
UNIT_PRICE NUMBER(10,2) DEFAULT 0,
UNITS_IN_STOCK NUMBER(9) DEFAULT 0,
UNITS_ON_ORDER NUMBER(9) DEFAULT 0,
REORDER_LEVEL NUMBER(9) DEFAULT 0,
DISCONTINUED CHAR(1) DEFAULT 'N',
CONSTRAINT PK_PRODUCT PRIMARY KEY (PRODUCT_NAME),
CONSTRAINT FK_CATEGORY_NAME FOREIGN KEY (CATEGORY_NAME) REFERENCES CATEGORY(CATEGORY_NAME),
CONSTRAINT FK_SUPPLIER_NAME FOREIGN KEY (SUPPLIER_NAME) REFERENCES SUPPLIER(COMPANY_NAME),
CONSTRAINT CK_PRODUCT_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
CONSTRAINT CK_PRODUCT_UNITS_IN_STOCK CHECK (UNITS_IN_STOCK >= 0),
CONSTRAINT CK_PRODUCT_UNITS_ON_ORDER CHECK (UNITS_ON_ORDER >= 0),
CONSTRAINT CK_PRODUCT_REORDER_LEVEL CHECK (REORDER_LEVEL >= 0),
CONSTRAINT CK_PRODUCT_DISCONTINUED CHECK (DISCONTINUED in ('Y','N'))
);
CREATE TABLE ORDERS
(
ORDER_ID NUMBER(9) NOT NULL,
CUSTOMER_CODE VARCHAR(5) NOT NULL,
EMPLOYEE_ID NUMBER(9) NOT NULL,
ORDER_DATE DATE NOT NULL,
REQUIRED_DATE DATE NOT NULL,
SHIPPED_DATE DATE NOT NULL,
SHIP_VIA VARCHAR(40) NOT NULL,
FREIGHT NUMBER(10,2) DEFAULT 0,
SHIP_NAME VARCHAR(40) NOT NULL,
SHIP_ADDRESS VARCHAR(60) NOT NULL,
SHIP_CITY VARCHAR(15) NOT NULL,
SHIP_REGION VARCHAR(15) NULL,
SHIP_POSTAL_CODE VARCHAR(10) NULL,
SHIP_COUNTRY VARCHAR(15) NOT NULL,
CONSTRAINT PK_ORDERS PRIMARY KEY (ORDER_ID),
CONSTRAINT FK_CUSTOMER_CODE FOREIGN KEY (CUSTOMER_CODE) REFERENCES CUSTOMER(CUSTOMER_CODE),
CONSTRAINT FK_EMPLOYEE_ID FOREIGN KEY (EMPLOYEE_ID) REFERENCES EMPLOYEE(EMPLOYEE_ID),
CONSTRAINT FK_SHIP_VIA FOREIGN KEY (SHIP_VIA) REFERENCES SHIPPER(COMPANY_NAME)
);
CREATE TABLE ORDER_DETAIL
(
ORDER_ID NUMBER(9) NOT NULL,
PRODUCT_NAME VARCHAR(40) NOT NULL,
UNIT_PRICE NUMBER(10,2) DEFAULT 0.0,
QUANTITY NUMBER(9) DEFAULT 1,
DISCOUNT NUMBER(4,2) DEFAULT 0.0,
CONSTRAINT PK_ORDER_DETAIL PRIMARY KEY (ORDER_ID, PRODUCT_NAME),
CONSTRAINT FK_ORDER_ID FOREIGN KEY (ORDER_ID) REFERENCES ORDERS (ORDER_ID),
CONSTRAINT FK_PRODUCT_NAME FOREIGN KEY (PRODUCT_NAME) REFERENCES PRODUCT (PRODUCT_NAME),
CONSTRAINT CK_ORDER_DETAIL_UNIT_PRICE CHECK (UNIT_PRICE >= 0),
CONSTRAINT CK_ORDER_DETAIL_QUANTITY CHECK (QUANTITY > 0),
CONSTRAINT CK_ORDER_DETAIL_DISCOUNT CHECK (DISCOUNT between 0 and 1)
);
You're only inserting two columns into TOTAL_NUM, but it has three columns. You skipped PRODUCT_NAME. You need to use GROUP BY, otherwise it will aggregate all orders into a single row to insert.
INSERT INTO TOTAL_NUM (CUSTOMER_CODE, PRODUCT_NAME, TOTAL_ORDERED)
SELECT ORDERS.CUSTOMER_CODE, ORDER_DETAIL.PRODUCT_NAME, COUNT(*) AS TOTAL_ORDERED
FROM ORDERS
INNER JOIN ORDER_DETAIL ON ORDERS.ORDER_ID = ORDER_DETAIL.ORDER_ID
GROUP BY ORDERS.CUSTOMER_CODE ORDER_DETAIL.PRODUCT_NAME
You also should probably use INNER JOIN rather than LEFT JOIN. Otherwise you'll create a row with a null PRODUCT_NAME for any orders with no products.
I am unsure as to why I can't insert org_id and vol_id as foreign keys. It returns the error in the title ORA-00904: invalid identifier
CREATE TABLE VOLUNTEERS (
vol_id varchar2(3) not null,
vol_fname varchar2(10) not null,
vol_lname varchar2(10) not null,
vol_address varchar2(20) not null,
vol_telephone varchar2(10) not null,
primary key (vol_id));
CREATE TABLE ORGANIZATIONS (
org_id varchar2(3) not null,
org_name varchar2(30) not null,
org_contact varchar2(20) not null,
org_address varchar2(20) not null,
org_telephone varchar2(10) not null,
primary key (org_id));
alter table volunteers
add foreign key (org_id) references ORGANIZATIONS;
alter table organizations
add foreign key (vol_id) references VOLUNTEERS;
ALTER TABLE child_table
ADD CONSTRAINT fk_name
FOREIGN KEY (col_name) REFERENCES table_name(col_name);
A foreign key constraint is a constraint on an existing column. You have not defined the columns in each of the tables:
CREATE TABLE VOLUNTEERS (
vol_id varchar2(3) not null,
vol_fname varchar2(10) not null,
vol_lname varchar2(10) not null,
vol_address varchar2(20) not null,
vol_telephone varchar2(10) not null,
org_id varchar2(3),
------^
primary key (vol_id)
);
CREATE TABLE ORGANIZATIONS (
org_id varchar2(3) not null,
org_name varchar2(30) not null,
org_contact varchar2(20) not null,
org_address varchar2(20) not null,
org_telephone varchar2(10) not null,
vol_id varchar2(3),
------^
primary key (org_id)
);
Once the columns are defined, the foreign key constraints will work.
The query i wrote as
CREATE TABLE Shopper (
Shopperid NUMBER(4) CONSTRAINT shpr_shprid_pk PRIMARY KEY,
ShopperName VARCHAR2(20) CONSTRAINT shpr_shprname_nn NOT NULL,
Gender CHAR(6) CONSTRAINT shpr_gdr_chk CHECK(Gender IN ('Male','Female')),
MobileNo NUMBER(10) CONSTRAINT shpr_mobno_nn NOT NULL,
Address VARCHAR2(50)
);
The issue is with NUMBER type definitions, try:
CREATE TABLE Shopper (
Shopperid NUMBER CONSTRAINT shpr_shprid_pk PRIMARY KEY,
ShopperName VARCHAR2(20) CONSTRAINT shpr_shprname_nn NOT NULL,
Gender CHAR(6) CONSTRAINT shpr_gdr_chk CHECK(Gender IN ('Male','Female')),
MobileNo NUMBER CONSTRAINT shpr_mobno_nn NOT NULL,
Address VARCHAR2(50)
);
CREATE TABLE CUSTOMER1
(
CUSTOMERID VARCHAR2(2) NOT NULL,
FIRSTNAME VARCHAR2(50) NOT NULL,
LASTNAME VARCHAR2(50) NOT NULL,
PHONENUMBER VARCHAR2(50) NOT NULL,
BIRTHDATE DATE NOT NULL,
DRIVERLICENSENUMBER NUMBER,
STATUS VARCHAR2(50),
CREDITCARENUMBER NUMBER,
CONSTRAINT CustomerID_PK PRIMARY KEY(CUSTOMERID)
);
CREATE TABLE CCATEGORY
(
CATEGORYID INT NOT NULL,
CATEGORYNAME VARCHAR2(50) NOT NULL,
CONSTRAINT categoryID_pk PRIMARY KEY (CATEGORYID)
);
CREATE TABLE TAPE
(
TAPEID NUMBER(5) NOT NULL,
TAPETITLE VARCHAR2(200) NOT NULL,
RELEASEYEAR NUMBER(5) NOT NULL,
DATEPURCHASED DATE NOT NULL,
PRICE NUMERIC(5) NOT NULL,
RENTEDOUT VARCHAR2(20) NOT NULL,
RATING VARCHAR2(10) NOT NULL,
ACTIONONRETURN VARCHAR2(50) NOT NULL,
PRIMARY KEY(TAPEID),
CONSTRAINT FK_CATEGORYID FOREIGN KEY(CATEGORYID) REFERENCES CCATEGORY(CATEGORYID),
CONSTRAINT FK_RESERVEDBY FOREIGN KEY(CUSTOMERID) REFERENCES CUSTOMER(CUSTOMERID)
);
Customer, and CCategory table are created. When I try to create TAPE table I get the following error: SQL Error: ORA-00904: "CATEGORYID": invalid identifier. Can't seem to figure out why. I created the customer table first, then the category table before trying to create the TAPE table.
Problem is that you have not defined CATEGORYID and CUSTOMERID in TAPE table. Add it and it would work.
Complete statement would be
CREATE TABLE TAPE
(
TAPEID NUMBER(5) NOT NULL,
TAPETITLE VARCHAR2(200) NOT NULL,
RELEASEYEAR NUMBER(5) NOT NULL,
DATEPURCHASED DATE NOT NULL,
PRICE NUMERIC(5) NOT NULL,
RENTEDOUT VARCHAR2(20) NOT NULL,
RATING VARCHAR2(10) NOT NULL,
ACTIONONRETURN VARCHAR2(50) NOT NULL,
CATEGORYID INT NOT NULL,
CUSTOMERID VARCHAR2(2) NOT NULL,
CONSTRAINT tape_primary_key PRIMARY KEY(TAPEID),
CONSTRAINT FK_CATEGORYID FOREIGN KEY(CATEGORYID) REFERENCES CCATEGORY(CATEGORYID),
CONSTRAINT FK_RESERVEDBY FOREIGN KEY(CUSTOMERID) REFERENCES CUSTOMER1(CUSTOMERID)
);
I finished typing out SQL statements but now I'm getting errors codes when I run it on omega. I keep getting the invalid identifier code...
Here's what I get once I ran it in omega.
SQL>
SQL> --question 14
SQL>
SQL> SELECT to_char(Avg(ProductPrice),'99,999.99') AS Avg_Price
2 FROM Product_mys;
SELECT to_char(Avg(ProductPrice),'99,999.99') AS Avg_Price
*
ERROR at line 1:
ORA-00904: "PRODUCTPRICE": invalid identifier
These are the sql tables where the data is being pulled from
CREATE TABLE Dept_mys (
DeptID Number(3) NOT NULL,
DeptName VARCHAR(20) NOT NULL,
PRIMARY KEY (DeptID)
) ;
CREATE TABLE Commission_mys (
CommClass CHAR(1) NOT NULL,
CommRate Number(2,2) NOT NULL,
PRIMARY KEY (CommClass)
) ;
CREATE TABLE Category_mys (
CatID Number(3) NOT NULL,
catName VARCHAR(20) NOT NULL,
PRIMARY KEY (CatID)
) ;
CREATE TABLE SalesRep_mys (
SalesRepID NUMBER(4) NOT NULL,
SalesRepFName VARCHAR(20) NOT NULL,
SalesRepLName VARCHAR(20) NOT NULL,
DeptID NUMBER(3) NOT NULL,
CommClass CHAR(1) NOT NULL,
PRIMARY KEY (SalesRepID),
FOREIGN KEY (DeptID) REFERENCES Dept_mys,
FOREIGN KEY (CommClass) REFERENCES Commission_mys
) ;
CREATE TABLE Customer_mys (
CustID CHAR(5) NOT NULL,
CustFName VARCHAR(20) NOT NULL,
CustLName VARCHAR(20) NOT NULL,
CustPhone CHAR(10),
SalesRepID NUMBER(4) NOT NULL,
PRIMARY KEY (CustID),
FOREIGN KEY (SalesRepID) REFERENCES SalesRep_mys
) ;
CREATE TABLE Order_mys (
OrderID NUMBER(3) NOT NULL,
OrderDate DATE NOT NULL,
CustID CHAR(5) NOT NULL,
PRIMARY KEY (OrderID),
FOREIGN KEY (CustID) REFERENCES Customer_mys
) ;
CREATE TABLE Product_mys (
ProductID NUMBER(3) NOT NULL,
ProductName VARCHAR(30) NOT NULL,
CatID Number(3) NOT NULL,
PRIMARY KEY (ProductID),
FOREIGN KEY (CatID) REFERENCES Category_mys
) ;
CREATE TABLE OrderDetail_mys (
OrderID NUMBER(3) NOT NULL,
ProductID NUMBER(3) NOT NULL,
ProductQty NUMBER(4) NOT NULL,
ProductPrice NUMBER(6,2) NOT NULL,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES Order_mys,
FOREIGN KEY (ProductID) REFERENCES Product_mys
) ;
Your query references Sales_Rep_mys but you create SalesRep_mys. So that's at least some of the problem.
Same with Order_Detail_mys and OrderDetail_mys
Looks like those 2 fixes should do it.