CREATE Table orders(order_id INT PRIMARY KEY NOT NULL,
customer_id INT,
amount INT,
address_id varchar(50),
order_date DATE,
product_id INT,
quantity INT,
FOREIGN KEY(customer_id) REFERENCES customers(customer_id),
FOREIGN KEY(address_id) REFERENCES address(address_id),
FOREIGN KEY product(product_id) REFERENCES product(product_id));
Why did you change the last foreign key syntax?
SQL> CREATE TABLE orders
2 (
3 order_id INT PRIMARY KEY NOT NULL,
4 customer_id INT,
5 amount INT,
6 address_id VARCHAR2 (50),
7 order_date DATE,
8 product_id INT,
9 quantity INT,
10 FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
11 FOREIGN KEY (address_id) REFERENCES address (address_id),
12 FOREIGN KEY (product_id) REFERENCES product (product_id)
13 );
Table created.
SQL>
Related
CREATE TABLE Car (Car_ID int PRIMARY KEY,
Serial_Number int, Make VARCHAR(255),
Model VARCHAR(255), Colour VARCHAR(255),
Year int, Car_for_Sale_Y_N VARCHAR (25));
CREATE TABLE Salesperson
(Salesperson_ID int primary KEY,
Last_Name VARCHAR (255),
First_Name VARCHAR (255));
CREATE TABLE Customer
(Customer_ID int PRIMARY KEY,
Last_Name VARCHAR (255),
First_Name VARCHAR(255),
Phone_Number VARCHAR(255),
Address VARCHAR (255),
City VARCHAR(255),
STATE_PROVINCE VARCHAR (255),
Country VARCHAR (255),
Postal_Code VARCHAR (255));
CREATE TABLE Sales_Invoice
(Invoice_ID int PRIMARY KEY, Invoice_Number VARCHAR (255),
Date DATE,
Car_ID int FOREIGN KEY REFERENCES Car(Car_ID),
Customer_ID int FOREIGN KEY REFERENCES Customer(Customer_ID));
Output: ORA-00904: : invalid identifier
Please help what's wrong here its not working. In the Sales_invoice table when I create the foreign keys it shows that error what is the problem.
A few suggestions, if I may.
don't use varchar, but varchar2 datatype. Why? Oracle recommends so
you aren't one of my colleagues in disguise, are you? Their varchar2 columns are all (255), so that they are sure that most of information they put in there will fit. But, do you really need that? Postal code whose length is 255 characters? Come one, be serious!
name all your constraints. If you don't Oracle will create default name which looks like SYS_C007858 and you'll have no idea what it really is. But, if its name is e.g. pk_car, you'll know that it is a primary key on the car table
as commented, date is reserved for datatype, you can't use it as a column name (OK, you can if you enclose it into double quotes, but you shouldn't)
Finally:
SQL> create table car
2 (car_id int constraint pk_car primary key,
3 serial_number int,
4 make varchar2(255),
5 model varchar2(255),
6 colour varchar2(255),
7 year int,
8 car_for_sale_y_n varchar2 (25));
Table created.
SQL> create table salesperson
2 (salesperson_id int constraint pk_salesper primary key,
3 last_name varchar2(255),
4 first_name varchar2(255));
Table created.
SQL> create table customer
2 (customer_id int constraint pk_cust primary key,
3 last_name varchar2 (255),
4 first_name varchar2(255),
5 phone_number varchar2(255),
6 address varchar2(255),
7 city varchar2(255),
8 state_province varchar2(255),
9 country varchar2(255),
10 postal_code varchar2(255));
Table created.
SQL> create table sales_invoice
2 (invoice_id int constraint pk_salinv primary key,
3 invoice_number varchar2 (255),
4 datum date,
5 car_id int constraint fk_salinv_car references car(car_id),
6 customer_id int constraint fk_salinv_cust references customer(customer_id));
Table created.
SQL>
I am new to sql and have been trying to figure out what im doing wrong when adding the foreign keys. I am able to create all the tables and everything up to the point where i go to input values for the SALES table. I get an error :
ORA-02291: integrity constraint
(SQL_HLZTBRUASDUURQGIOAYPNRAFC.EMPLOYEE_ID) violated - parent key not
found ORA-06512: at "SYS.DBMS_SQL", line 1721:
Can anyone look over my code and help me with what to change?
CREATE TABLE EMPLOYEE
(EMPLOYEE_ID char(10) PRIMARY KEY,
EMPLOYEE_NAME varchar(30),
Address varchar(50),
PHONE number(10),
HIRING_DATE date not null,
SALARY number(6))
CREATE TABLE PRODUCTS
(PRODUCT_ID char(2) PRIMARY KEY,
PRICE CHAR(5),
TYPE varchar(10),
PRODUCT_NAME varchar(30),
AUTHOR varchar(20))
CREATE TABLE CUSTOMER
(CUSTOMER_ID char(5) PRIMARY KEY,
CUSTOMER_NAME varchar(30),
PHONE number(7),
ADDRESS varchar(30))
CREATE TABLE INVENTORY
(PRODUCT_NAME varchar(20) PRIMARY KEY,
PRODUCT_ID char(2),
UNIT_PRICE number(4),
SHELF_LOCATION varchar(2),
CURRENT_INVENTORY number(3),
MONTHLY_PURCHASES number(3))
CREATE TABLE SALES
(TRANSACTION_ID char(5) PRIMARY KEY,
PRODUCT_ID char(5),
SELL_DATE date not null,
CUSTOMER_ID char(5),
UNITS_SOLD number(2),
EMPLOYEE_ID char(10),
SALES_AMOUNT decimal(10,2),
CONSTRAINT CUSTOMER_ID
FOREIGN KEY (CUSTOMER_ID)
REFERENCES CUSTOMER(CUSTOMER_ID),
CONSTRAINT PRODUCT_ID
FOREIGN KEY(PRODUCT_ID)
REFERENCES PRODUCTS(PRODUCT_ID),
CONSTRAINT EMPLOYEE_ID
FOREIGN KEY (EMPLOYEE_ID)
REFERENCES EMPLOYEE(EMPLOYEE_ID))
When inserting the code below is when I get an error:
INSERT INTO SALES
VALUES('1','42','01-JAN-2015','4269','3','5678901234','135.00’)
The employee_id 5678901234 doesn't exist in the EMPLOYEE table.
Which causes the foreign key constraint on EMPLOYEE_ID to complain.
Those foreign key's are there to ensure referential integrity after all.
(The constraint is used to give a name to it)
So add the user with that id to the EMPLOYEE table first.
An extra advice.
It's safer to list the column names in the INSERT statement.
I have been given a task to normalise a sales order, create a relational schema and input the data into SQL developer.
I have normalised to 3NF and got this:
Customer(Customer_ID, Customer_name) ... Primary key = Customer_ID
Employee(Employee_ID, Employee_name) ... Primary key = Employee_ID
Sales_Order(Sales_order_ID, Employee_ID, Customer_ID, Sale_date, Order_total) ... Primary key = Sales_order_ID ... Foreign keys = Employee_ID, Customer_ID
Order_Line(Sales_order_ID, Product_ID, Quantity, Line_total) ... Composite key = Sales_order_ID ... Foreign key = Product_ID
Product(Product_ID, Product_name, Product_price, Product_colour) ... Primary key = Product_ID
I then inputted the tables, this is my SQL:
CREATE TABLE EMPLOYEE(
Employee_ID CHAR(3),
Employee_name CHAR(20),
CONSTRAINT pk_Employee PRIMARY KEY (Employee_name)
);
CREATE TABLE CUSTOMER(
Customer_ID CHAR(5),
Customer_Name CHAR(20),
CONSTRAINT pk_Customer PRIMARY KEY (Customer_ID)
);
CREATE TABLE PRODUCT(
Product_ID CHAR(5),
Product_Name CHAR(30),
Product_Colour CHAR(10),
Product_Price CHAR(5),
CONSTRAINT pk_Product PRIMARY KEY (Product_ID)
);
CREATE TABLE SALES_ORDER(
Sales_order_ID CHAR(6),
Employee_ID CHAR(3),
Customer_ID CHAR(5),
Sale_Date CHAR(10),
Order_total CHAR(7),
CONSTRAINT pk_Order PRIMARY KEY (Sales_order_ID),
CONSTRAINT fk_Order FOREIGN KEY (Employee_ID) REFERENCES EMPLOYEE (Employee_ID),
FOREIGN KEY (Customer_ID) REFERENCES CUSTOMER (Customer_ID)
);
CREATE TABLE ORDER_LINE(
Sales_order_ID CHAR(6),
Product_ID CHAR(5),
Quantity CHAR(3),
Line_total CHAR(5),
CONSTRAINT pk_Order_Line PRIMARY KEY (Sales_order_ID),
FOREIGN KEY (Product_ID) REFERENCES PRODUCT (Product_ID)
);
I am able to input tables Employee, Customer, Product, Sales_order but unable to input Order_line
I am told that the table or view does not exist!
What does this mean?
Have I normalised incorrectly?
Have I designed the relational schema incorrectly?
Any help would be deeply appreciated
When I run your code in this db fiddle, creation of table SALE_ORDER fails with the following message:
ORA-02270: no matching unique or primary key for this column-list
This is because of this foreign key:
CONSTRAINT fk_Order FOREIGN KEY (Employee_ID) REFERENCES EMPLOYEE (Employee_ID),
The underlying problem is that you have defined Employee_name as the primary key of table EMPLOYEE.
CREATE TABLE EMPLOYEE (
Employee_ID CHAR(3),
Employee_name CHAR(20),
CONSTRAINT pk_Employee PRIMARY KEY (Employee_ID)
);
This does not seem like a sensible option. In the real life, it is possible that two different employees would have the same name. Instead, you probably want to use Employee_ID as the primary key for EMPLOYEE.
Consider this definition for table EMPLOYEE:
CREATE TABLE EMPLOYEE (
Employee_ID CHAR(3),
Employee_name CHAR(20),
CONSTRAINT pk_Employee PRIMARY KEY (Employee_ID)
);
With this new set up, all tables are created successfully. You may now insert your data.
Demo on DB Fiddle
Side note: I forsee issues with table ORDER_LINE:
primary key should be Order_line_ID instead of Sales_order_ID
Sales_order_ID should have a foreign key constraint referencing SALE_ORDER(Sales_order_ID).
Consider this new definition for ORDER_LINE:
CREATE TABLE ORDER_LINE(
Order_line_ID CHAR(6),
Sales_order_ID CHAR(6),
Product_ID CHAR(5),
Quantity CHAR(3),
Line_total CHAR(5),
CONSTRAINT pk_Order_Line PRIMARY KEY (Order_line_ID),
FOREIGN KEY (Product_ID) REFERENCES PRODUCT (Product_ID),
FOREIGN KEY (Sales_order_ID) REFERENCES SALES_ORDER(Sales_order_ID)
);
Can't find the missing parenthesis or figure out why I'm getting the error.
create table course(
CourseNum number(10) constraint course_CourseNum_pk primary key,
courseName varchar2(40),
startDate date,
endDate date,
Ins_ID varchar2(10),
constraint course_Ins_ID_fk foreign key
references instructor(Ins_ID)
);
expecting a table with 5 columns to be created.
Should be like this:
SQL> create table instructor (ins_id varchar2(10) primary key);
Table created.
SQL> create table course(
2 CourseNum number(10) constraint course_CourseNum_pk primary key,
3 courseName varchar2(40),
4 startDate date,
5 endDate date,
6 Ins_ID varchar2(10),
7 constraint course_Ins_ID_fk foreign key (ins_id) --> you're missing "(ins_id)" here
8 references instructor (Ins_ID)
9 );
Table created.
SQL>
Or, alternatively:
SQL> create table course(
2 CourseNum number(10) constraint course_CourseNum_pk primary key,
3 courseName varchar2(40),
4 startDate date,
5 endDate date,
6 Ins_ID varchar2(10) constraint course_Ins_ID_fk references instructor (Ins_ID)
7 );
Table created.
SQL>
So I have table customers and table bookings
I want to add a Foreign Key to the script so that
the CustID from customers can make a column in bookings and connect.
create table customers(
CustID INT NOT NULL,
CustomerName VARCHAR2(25),
CustomerAddress VARCHAR2(50),
CustomerPhone NUMBER(10),
CONSTRAINT pk_cust PRIMARY KEY (CustID) );
and
create table bookings(
BookID INT NOT NULL,
HotelName VARCHAR2(10),
RoomType VARCHAR2(20),
RoomNumber NUMBER(3),
CustID INT,
PRIMARY KEY (BookID),
CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers(CustID)
StartDate VARCHAR2(25),
EndDate VARCHAR2(25),
Duration VARCHAR2(25));
error;
StartDate VARCHAR2(25),
*
ERROR at line 10:
ORA-00907: missing right parenthesis
create table bookings(
BookID INT NOT NULL,
HotelName VARCHAR2(10),
RoomType VARCHAR2(20),
RoomNumber NUMBER(3),
CustID INT,
StartDate VARCHAR2(25),
EndDate VARCHAR2(25),
Duration VARCHAR2(25),
PRIMARY KEY (BookID),
CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers(CustID));
You have to declare the primary key and other constraints after you finish declaring the columns.
create table bookings(
BookID INT NOT NULL,
HotelName VARCHAR2(10),
RoomType VARCHAR2(20),
RoomNumber NUMBER(3),
CustID INT,
PRIMARY KEY (BookID),
CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers1 (CustID),
StartDate VARCHAR2(25),
EndDate VARCHAR2(25),
Duration VARCHAR2(25));
see it carefully -- you Have to add comma after this CONSTRAINT fk_CustBook FOREIGN KEY (CustID)
REFERENCES customers1 (CustID),