How to assign foreign key in Oracle 10g? - oracle

I am try to assign foreign key in Oracle 10g. But the error shows like
ORA-907: missing right parentheses
I have three tables
TblCustomer
TblProducts
TblSales
create table tblSales
(SalesID int primary key,
ProductId int foreign key references tblProducts(ProductId),
CustomerID int foreign key references tblCustomer(CustomerID),
SalesPrice numeric,
SalesDate date);
Whats going wrong can you suggest me.

You syntax must look like:
create table tblSales (SalesID int,
ProductId int ,
CustomerID int ,
SalesPrice numeric,
SalesDate date,
CONSTRAINT sales_pk PRIMARY KEY (SalesID ),
CONSTRAINT fk_produkt
foreign key (ProductId)references tblProducts(ProductId),
CONSTRAINT fk_customer
foreign key (CustomerID)references tblCustomer(CustomerID)
);

For in inline foreign key you don't specify the foreign key keyword:
create table tblSales
(
SalesID int primary key,
ProductId int references tblProducts(ProductId),
CustomerID int references tblCustomer(CustomerID),
SalesPrice numeric,
SalesDate date
);
With inline foreign keys, you don't even need to specify the target column:
create table tblSales
(
SalesID int primary key,
ProductId int references tblProducts,
CustomerID int references tblCustomer,
SalesPrice numeric,
SalesDate date
);
SQLFiddle example: http://sqlfiddle.com/#!4/420b9c
As a side note: prefixing each and every table with tbl doesn't really make sense. If you are programming, do you prefix every class with Cls or if you are naming a person, do you prefix every name with Pers?

Related

I want to have only 3 columns (departure_City_Id, arrival_City_Id, Emp_number). However, when I come to insert value , it says that I have 4 columns

For the Epms table, I want to have only 3 columns (departure_City_Id, arrival_City_Id, Emp_number). However, when I come to insert value, it says that I have 4 columns. The extra one for City_Id. All I want is to avoid having City_Id column in this table. I declare it because I need it just as FK.
CREATE TABLE City (
City_Id char(3),
state varchar(30),
Primary key (City_Id)
);
create table Emps (
Emp_number varchar(30) primary key,
City_Id char(3),
departure_City_Id char(3),
arrival_City_Id char(3),
FOREIGN KEY (City_Id)
REFERENCES City(City_Id),
FOREIGN KEY (City_Id)
REFERENCES City(City_Id)
);
This is pretty straightforward, just specify e.g. , FOREIGN KEY (arrival_city_id) REFERENCES city(city_id) and you'll get an index.
CREATE TABLE emps (
emp_number varchar(30) NOT NULL,
city_id char(3) NOT NULL,
departure_city_id char(3) NOT NULL,
arrival_city_id char(3) NOT NULL,
PRIMARY KEY (emp_number),
KEY city_id (city_id),
KEY departure_city_id (departure_city_id),
KEY arrival_city_id (arrival_city_id),
CONSTRAINT emps_ibfk_1 FOREIGN KEY (city_id) REFERENCES city (city_id),
CONSTRAINT emps_ibfk_2 FOREIGN KEY (departure_city_id) REFERENCES city (city_id),
CONSTRAINT emps_ibfk_3 FOREIGN KEY (arrival_city_id) REFERENCES city (city_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
" I want is to avoid having City_Id column in this table. I declare it because I need it just as FK."
But you don't need it. The foreign key columns are the arrival and departure columns, and those are the ones you must reference in the constraint declarations:
create table Emps (
Emp_number varchar(30) primary key,
departure_City_Id char(3),
arrival_City_Id char(3),
FOREIGN KEY (departure_City_Id)
REFERENCES City(City_Id),
FOREIGN KEY (City_Id)
REFERENCES City(arrival_City_Id)
);
Naming constraints is optional but you may find it helpful when debugging foreign key failures, especially when a table has multiple constraints referencing the same parent key.

one attribute referencing, attributes in two different tables

I have 4 tables
customer: CustomerID - primary key, name
Magazine: name - primary key, cost, noofissues
Newspaper: name - primary key, cost, noofissues
subscription: custID - references CustomerID of Customer, name, startdate, enddate
In the above, can I reference the name from subscription table to reference name from Magazine and name from Newspaper?
I have created the tables Customer, Newspaper and Magazine. I only need to create Subscription.
Can you do something like this?
CREATE TABLE subscription (
custID INT
CONSTRAINT subscription__custid__fk REFERENCES Customer( CustomerId ),
name VARCHAR2(50)
CONSTRAINT subscription__mag_name__fk REFERENCES Magazine( Name )
CONSTRAINT subscription__news_name__fk REFERENCES Newspaper( Name ),
startdate DATE
CONSTRAINT subscription__startdate__nn NOT NULL,
enddate DATE
);
Yes, you can and you will have two foreign keys on the same column pointing to different tables but if the value in the column is non-null then it will expect there to be a matching name in both the magazines table and the newspapers table - which is probably not what you are after.
Can you have a foreign key that asks can the value be in either exclusively in this table or that table (but not in both)? No.
But you can re-factor your database so you merge the newspapers and magazines tables into a single table (which you can then easily reference); like this:
CREATE TABLE customer (
CustomerID INT
CONSTRAINT customer__CustomerId__pk PRIMARY KEY,
name VARCHAR2(50)
CONSTRAINT customer__name__nn NOT NULL
);
CREATE TABLE Publications (
id INT
CONSTRAINT publications__id__pk PRIMARY KEY,
name VARCHAR2(50)
CONSTRAINT publications__name__nn NOT NULL,
cost NUMBER(6,2)
CONSTRAINT publications__cost__chk CHECK ( cost >= 0 ),
noofissues INT,
type CHAR(1),
CONSTRAINT publications__type__chk CHECK ( type IN ( 'M', 'N' ) )
);
CREATE TABLE subscription (
custID INT
CONSTRAINT subscription__custid__fk REFERENCES Customer( CustomerId ),
pubID INT
CONSTRAINT subscription__pubid__fk REFERENCES Publications( Id ),
startdate DATE
CONSTRAINT subscription__startdate__nn NOT NULL,
enddate DATE
);
If you are asking whether you can create a foreign key constraint on subscription that references either the newspaper table or the magazine table, the answer is no, you cannot. A foreign key must reference exactly one primary key.
Since magazine and newspaper have the same set of attributes, the simple option is to combine them into a single periodical table with an additional periodical_type column to indicate whether it is a magazine or a newspaper. You could then create your foreign key to the periodical table.
Although it probably won't make sense in this particular example, you could also have separate columns in subscription for magazine_name and newspaper_name and create separate foreign key constraints on those columns along with a check constraint that ensured that exactly one of the values was non-NULL. That might make sense if the two different parent tables had radically different attributes.
Not related to your question but as a general bit of advice, I wouldn't use the name as the primary key. In addition to being rather long, names tend to change over time and names aren't necessarily unique. I would use a different attribute for the key, potentially a synthetic primary key generated from a sequence.

How do I perform a deletion of table entries from a full outer join?

What I have is several tables...two of them being:
CREATE TABLE Orders(
oid int NOT NULL,
rdate date,
sdate date,
cid int NOT NULL,
eid int NOT NULL,
PRIMARY KEY (oid),
FOREIGN KEY (cid) REFERENCES Customer(cid),
FOREIGN KEY (eid) REFERENCES Employee(eid));
CREATE TABLE PartOrder(
poid int NOT NULL,
pid int NOT NULL,
oid int NOT NULL,
PRIMARY KEY (poid),
FOREIGN KEY (pid) REFERENCES Part(pid),
FOREIGN KEY (oid) REFERENCES Orders(oid));
What I need to do is this:
Create and execute a query that deletes all PartOrder records for Orders for which the shipping date is in the past.
So, I came up with this...
DELETE
FROM (SELECT * FROM PartOrder FULL OUTER JOIN Orders ON partorder.oid=orders.oid)
WHERE sdate<sysdate;
This is giving me this error:
ORA-01752: cannot delete from view without exactly one key-preserved table
Can someone offer me some insight?
I'd write this as something like
DELETE FROM PARTORDER
WHERE POID IN (SELECT p.POID
FROM PARTORDER p
INNER JOIN ORDERS o
ON o.OID = p.OID
WHERE o.SDATE < SYSDATE);
Best of luck.

missing right parenthesis SQL

I am new to SQL.
I am trying to create a table:
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1) FOREIGN KEY REFERENCES account_type(TypeCode),
CustomerRef NUMBER(2) FOREIGN KEY REFERENCES bank_customer(CustomerRef),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));
However it comes up with: ORA-00907: missing right parenthesis
I know for a fact that you can have to foreign keys, so that's not the problem. Could someone give me a had with the solution of creating the table?
A foreign key actually has to refer to something. In your case you have to tell Oracle what AccountType and CustomerRef refer to. Usually it looks as follows:
AccountType VARCHAR(1) FOREIGN KEY REFERENCES TABLE_NAME(COLUMN_NAME)
Of course, you have to replace TABLE_NAME and COLUMN_NAME with the name of the table and the name of the column you're referring to.
There are two notations you can use when declaring constraints:
1) inline (a constraint):
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1) REFERENCES account_type(TypeCode),
CustomerRef NUMBER(2) REFERENCES bank_customer(CustomerRef),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));
2) out-of-line
CREATE TABLE account
(
AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1),
CustomerRef NUMBER(2),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2),
FOREIGN KEY(AccountType) REFERENCES account_type(TypeCode),
FOREIGN KEY(CustomerRef) REFERENCES account_type(TypeCode)
);
In both cases you can prepend the constraint declaration with CONSTRAINT <name> to give your name to a constraint, otherwise Oracle assigns its own name.
inline notation is applied to a column where the constraint is declared, out-of-line is applied to the table. There slight differences in syntax + some restrictions, for example you cannot declare out-of-line NULL constraint.
You can use inline and out-of-line syntax in CREATE TABLE and ALTER TABLE. Refer to Oracle documentation for more information
You need to define the foreign keys with REFERENCES. In the below snippet, replace AccountTypeTable and CustomerRefTable with the correct table names and replace typeColumn and refColumn with the correct column names in those tables these match to.
UPDATE
I added in the correct values from your comment on another answer
CREATE TABLE account (
AccountNo NUMBER(2) PRIMARY KEY,
AccountType VARCHAR(1),
CustomerRef NUMBER(2),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2),
CONSTRAINT account_fk1 FOREIGN KEY (AccountType) REFERENCES Account_Type(TypeCode),
CONSTRAINT account_fk2 FOREIGN KEY (CustomerRef) REFERENCES Bank_Customer(CustomerRef));
you are missing foreign key syntax...
CREATE TABLE account
(AccountNo NUMBER(2) PRIMARY KEY (P_ID),
AccountType VARCHAR(1) FOREIGN KEY (F1_ID) REFERENCES <table_name>(field_name>),
CustomerRef NUMBER(2) FOREIGN KEY (F2_ID) REFERENCES <table_name>(field_name>),
DateOpened DATE,
CurrentBalence NUMBER(6,2),
OverdraftLimit NUMBER(5,2));

How can I create a sequential SQL creation script for Oracle?

Something like:
create table Employee(
ID int primary key,
Name nvarchar(200)
IDArea int foreign key references Area(ID)
);
go
create table Area(
ID int primary key,
Name nvarchar(200)
);
Does something like this exist in Oracle?
Yes, just leave out the "GO" keywords and put the statements in a file:
create table Area(
ID int primary key,
Name nvarchar2(200)
);
create table Employee(
ID int primary key,
Name nvarchar2(200),
IDArea int references Area(ID)
);
You must create the Area primary key before the foreign key that references it, so I have swapped these around. Also the foreign key syntax is slightly different in Oracle.
You should first create the master table, forget nvarchar datatype, and eventually the script would be :
create table Area(
ID number primary key,
Name varchar2(200)
);
create table Employee(
ID number primary key,
Name varchar2(200) ,
IDArea number, constraint fk_idarea foreign key (idarea) references Area(ID)
);

Resources