CREATE TABLE P_OWNER(
OWNERID CHAR(5) NOT NULL,
LNAME VARCHAR2(50) NOT NULL,
FNAME VARCHAR2(30) NULL,
SSN CHAR(9) NULL,
EMAIL VARCHAR2(50) NULL,
CONSTRAINT P_OWNER_PK PRIMARY KEY(OWNERID),
CONSTRAINT P_OWNER_AK1 ALTERNATE KEY(SSN),
CONSTRAINT P_OWNER_AK2 ALTERNATE KEY(EMAIL)
);
CREATE TABLE P_PROPERTY(
PROPERTYID CHAR(5) NOT NULL,
STREET1 VARCHAR2(50) NOT NULL,
STREET2 VARCHAR2(50) NULL,
CITY VARCHAR2(50) DEFAULT 'FITCHBURG',
STATE CHAR(2) DEFAULT 'MA',
ZIP CHAR(10) NULL,
SQFOOTAGE NUMBER(5,0) NULL,
HAS_AC CHAR(1) NULL,
ISHOUSE CHAR(1) NOT NULL,
CONSTRAINT P_PROPERTY_PK PRIMARY KEY(PROPERTYID),
CONSTRAINT P_PROPERTY_ADDRESS_AK1 ALTERNATE KEY(CITY, STATE, STREET1, STREET2, ZIP)
);
Error at Command Line:8 Column:57
Error report:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
You want a Unique Key instead of Alternate
CREATE TABLE P_OWNER(
OWNERID CHAR(5) NOT NULL,
LNAME VARCHAR2(50) NOT NULL,
FNAME VARCHAR2(30) NULL,
SSN CHAR(9) NULL,
EMAIL VARCHAR2(50) NULL,
CONSTRAINT P_OWNER_PK PRIMARY KEY(OWNERID),
CONSTRAINT P_OWNER_AK1 UNIQUE(SSN),
CONSTRAINT P_OWNER_AK2 UNIQUE(EMAIL)
);
CREATE TABLE P_PROPERTY(
PROPERTYID CHAR(5) NOT NULL,
STREET1 VARCHAR2(50) NOT NULL,
STREET2 VARCHAR2(50) NULL,
CITY VARCHAR2(50) DEFAULT 'FITCHBURG',
STATE CHAR(2) DEFAULT 'MA',
ZIP CHAR(10) NULL,
SQFOOTAGE NUMBER(5,0) NULL,
HAS_AC CHAR(1) NULL,
ISHOUSE CHAR(1) NOT NULL,
CONSTRAINT P_PROPERTY_PK PRIMARY KEY(PROPERTYID),
CONSTRAINT P_PROPERTY_ADDRESS_AK1 UNIQUE(CITY, STATE, STREET1, STREET2, ZIP)
);
Problem is with ALTERNATE KEY try with UNIQUE
CREATE TABLE P_OWNER(
OWNERID CHAR(5) NOT NULL,
LNAME VARCHAR2(50) NOT NULL,
FNAME VARCHAR2(30) NULL,
SSN CHAR(9) NULL,
EMAIL VARCHAR2(50) NULL,
CONSTRAINT P_OWNER_PK PRIMARY KEY(OWNERID),
CONSTRAINT P_OWNER_AK1 UNIQUE (SSN),
CONSTRAINT P_OWNER_AK2 UNIQUE (EMAIL)
);
CREATE TABLE P_PROPERTY(
PROPERTYID CHAR(5) NOT NULL,
STREET1 VARCHAR2(50) NOT NULL,
STREET2 VARCHAR2(50) NULL,
CITY VARCHAR2(50) DEFAULT 'FITCHBURG',
STATE CHAR(2) DEFAULT 'MA',
ZIP CHAR(10) NULL,
SQFOOTAGE NUMBER(5,0) NULL,
HAS_AC CHAR(1) NULL,
ISHOUSE CHAR(1) NOT NULL,
CONSTRAINT P_PROPERTY_PK PRIMARY KEY(PROPERTYID),
CONSTRAINT P_PROPERTY_ADDRESS_AK1 UNIQUE(CITY, STATE, STREET1, STREET2, ZIP)
);
An alternate key (or secondary key) is any candidate key which is not selected to be the primary key (PK). You cant create an alternate key in Oracle, its just conceptual.
Related
My problem: I am trying to create a table named User_T in Oracle apex.
I can't get rid of this displaying error... I don't know why. I've already checked my codes a couple of times.
CREATE TABLE User_T(
IdUser NUMBER(10,0) NOT NULL,
FirstName VARCHAR2(100) NOT NULL,
LastName VARCHAR(100) NOT NULL,
Email VARCHAR(50) NOT NULL,
UserName VARCHAR(30) NOT NULL UNIQUE,
UserPassword VARCHAR2(30) NOT NULL,
CreatedAt VARCHAR2(30),
UpdatedAt VARCHAR(30),
Enabled CHAR(10) CHECK,
CONSTRAINT IdUser_PK PRIMARY KEY(IdUser)
);
You have a CHECK keyword without specifying a constraint.
Either remove it:
CREATE TABLE User_T(
IdUser NUMBER(10,0) NOT NULL,
FirstName VARCHAR2(100) NOT NULL,
LastName VARCHAR(100) NOT NULL,
Email VARCHAR(50) NOT NULL,
UserName VARCHAR(30) NOT NULL UNIQUE,
UserPassword VARCHAR2(30) NOT NULL,
CreatedAt VARCHAR2(30),
UpdatedAt VARCHAR(30),
Enabled CHAR(10), -- Remove the CHECK
CONSTRAINT IdUser_PK PRIMARY KEY(IdUser)
);
else, specify the constraint:
CREATE TABLE User_T(
IdUser NUMBER(10,0) NOT NULL,
FirstName VARCHAR2(100) NOT NULL,
LastName VARCHAR(100) NOT NULL,
Email VARCHAR(50) NOT NULL,
UserName VARCHAR(30) NOT NULL UNIQUE,
UserPassword VARCHAR2(30) NOT NULL,
CreatedAt VARCHAR2(30),
UpdatedAt VARCHAR(30),
Enabled CHAR(10) CHECK (Enabled IN ('Foo ', 'Bar ')),
CONSTRAINT IdUser_PK PRIMARY KEY(IdUser)
);
db<>fiddle here
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.
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 am trying to create two tables, which is throwing ORA-00902 error, can't figure out what's wrong here...
CREATE TABLE VEHICLE_STORE
(
MODEL_NO VARCHAR2(12) NOT NULL,
DESCRIPTION VARCHAR2(1000) NOT NULL,
UNIT_PRICE NUMBER(10) NOT NULL,
CURRENT_STOCK NUMBER(5) NOT NULL,
CC NUMBER(3) NOT NULL,
CONSTRAINT vehicle_store_pk PRIMARY KEY(MODEL_NO)
);
CREATE TABLE VEHICLE_BOOKING
(
BOOKING_ID VARCHAR2(12) NOT NULL,
CUSTOMER_NAME VARCHAR2(30) NOT NULL,
EMAIL_ID VARCHAR2(30) NOT NULL,
CONTACT NUMBER(15) NOT NULL,
ADDRESS VARCHAR2(500) NOT NULL,
PINCODE NUMBER(10) NOT NULL,
MODEL_NO VARCHAR2(12) NOT NULL,
DATE_OF_BOOKING VARCHAR2(12) NOT NULL,
EXPECTED_DATE_OF_DELIVERY VARCHAR2(12) NOT NULL,
CONSTRAINT vehicle_booking_pk PRIMARY KEY(BOOKING_ID),
CONSTRAINT vehicle_booking_fk FOREIGN_KEY(MODEL_NO) REFERENCES VEHICLE_STORE(MODEL_NO)
);
Thanks in advance...
Change FOREIGN_KEY(MODEL_NO) to FOREIGN KEY (MODEL_NO) (no underscore).
I am getting this error message for the below code, however, can not see where the error is referring to. I thought that parenthesis referred to semicolons and commas. I assume there is something I am missing, any direction would be greatly appreciated.
CREATE TABLE BRANCH(
BranchID Int NOT NULL,
Street Char(50) NOT NULL,
City Char(30) NOT NULL,
Postcode Char(10) NOT NULL,
TelephoneNumber Int(11) NOT NULL,
ManagerName VarChar(40) NOT NULL,
CONSTRAINT BRANCH_PK PRIMARY KEY(BranchID)
);
Try this:
CREATE TABLE BRANCH(
BranchID Int NOT NULL,
Street Char(50) NOT NULL,
City Char(30) NOT NULL,
Postcode Char(10) NOT NULL,
TelephoneNumber Int NOT NULL,
ManagerName VarChar(40) NOT NULL,
CONSTRAINT BRANCH_PK PRIMARY KEY(BranchID)
);