Referencing foreign keys across different users - oracle

I'm referencing a key that is part of another user:
Example:
create table emp
( name char(10)
, empid char(10)
, dob date
, phone char(14)
, primary key(empid)
, foreign key phone
references (user2.contacts)
);
where user2 holds a table which has phone as the primary key.
How can I achieve this?

USER2 needs to grant your user the REFERENCES privilege on their table.
grant references on contacts to user1
/
Note that, unlike other privileges, REFERENCES must always be granted directly. USER1 will not be able to create a foreign key if the privilege is granted through a role.
The syntax for foreign keys requires us to include the primary key (or at least a unique key) column in the reference:
create table emp
( name char(10)
, empid char(10)
, dob date
, phone char(14)
, constraint emp_pk primary key(empid)
, constraint emp_contact foreign key (phone)
references (user2.contacts.phone) );
It is good practice to explicity name our constraints, as the system-generated names are a pain to work with.

it should be like this:
create table emp
( name char(10)
, empid char(10)
, dob date
, phone char(14)
, primary key(empid)
, foreign key phone
references user2.contacts)
);

Related

ORA-02267: incompatible with the data type or collation of the referenced column

create table doctor
(
name varchar2(20)
, d_id varchar2(20)
, address varchar2(50)
, phone_number number(10)
, qualification varchar2(20)
, gender varchar2(20)
, constraint pk_doctor primary key(d_id)
)
;
create table room
(
room_id varchar2(5)
, room_type varchar2(20)
, constraint pk_room primary key(room_id)
)
;
create table patient
(
p_id varchar2(10)
, p_name varchar2(20)
, p_age number(3)
, p_gender varchar2(10)
, address varchar2(50)
, date_admission date
, phone_number number(10)
, room_id varchar2(5)
, constraint pk_patient primary key(p_id)
, constraint fk_p1 foreign key(room_id) references room
)
;
create table bill
(
bill_no varchar2(10)
, bill_date date
, p_id varchar2(10)
, p_name varchar2(20)
, p_age number(3)
, p_gender varchar2(10)
, date_admission date
, date_discharge date
, room_charges number(10)
, pathology_fees number(10)
, d_fees number(10)
, miscellaneous number(10)
, total_amount number(10)
, constraint pk_bill primary key(bill_no)
, constraint fk_b1 foreign key(p_id) references patient
, constraint fk_b2 foreign key(p_name) references patient
, constraint fk_b3 foreign key(p_age) references patient
, constraint fk_b4 foreign key(p_gender) references patient
, constraint fk_b5 foreign key(date_admission) references patient
)
;
Error starting at line : 15 in command -
create table bill
(
bill_no varchar2(10)
, bill_date date
, p_id varchar2(10)
, p_name varchar2(20)
, p_age number(3)
, p_gender varchar2(10)
, date_admission date
, date_discharge date
, room_charges number(10)
, pathology_fees number(10)
, d_fees number(10)
, miscellaneous number(10)
, total_amount number(10)
, constraint pk_bill primary key(bill_no)
, constraint fk_b1 foreign key(p_id) references patient
, constraint fk_b2 foreign key(p_name) references patient
, constraint fk_b3 foreign key(p_age) references patient
, constraint fk_b4 foreign key(p_gender) references patient
, constraint fk_b5 foreign key(date_admission) references patient
)
Error report -
ORA-02267: column type incompatible with referenced column type
02267. 00000 - "column type incompatible with referenced column type"
*Cause: The data type or collation of the referencing column was
incompatible with the data type or collation of the referenced
column.
*Action: Select a compatible data type for the referencing column.
Also, the collation of a character column in a foreign key must
match the collation of the corresponding column in the primary
key.
I understand what the error is trying to tell but my datatypes are same in both tables but still, it is showing the error??
Please tell me where I made a mistake. I would be very grateful.
From the documentation:
If you identify only the parent table or view and omit the column name, then the foreign key automatically references the primary key of the parent table or view.
So you're effectively actually doing:
, constraint fk_b1 foreign key(p_id) references patient
, constraint fk_b2 foreign key(p_name) references patient(p_id)
, constraint fk_b3 foreign key(p_age) references patient(p_id)
, constraint fk_b4 foreign key(p_gender) references patient(p_id)
, constraint fk_b5 foreign key(date_admission) references patient(p_id)
which obviously isn't what you want, and explains the error you get.
You need to specify the matching non-PK columns:
, constraint fk_b1 foreign key(p_id) references patient
, constraint fk_b2 foreign key(p_name) references patient(p_name)
, constraint fk_b3 foreign key(p_age) references patient(p_age)
, constraint fk_b4 foreign key(p_gender) references patient(p_gender)
, constraint fk_b5 foreign key(date_admission) references patient(date_admission)
However, this will not get
ORA-02270: no matching unique or primary key for this column-list
because those four columns aren't suitable targets; none can really be unique, at least safely. And it wouldn't allow data to change - name and gender could, but age will, for example. These may be recording the patient's status on admission though, so then those wouldn't change, other than to correct mistakes.
It doesn't really make sense to duplicate/denormalise the data. You can just have the PK reference and join to the main table as needed to get the other information.
db<>fiddle
Splitting patient into a table to identify an individual (with date of birth rather than age) and a separate table that records each stay for that patient - with room and admission/discharge dates, for example - might make more sense. Only recording the discharge date on the bill seems odd.
The problem you're facing comes from bad data model which is needed for some rework.
The "bill" table contains p_name, p_age, p_gender, and date_admission columns. Same columns exist in the "patient" table, so you don't need them in the "bill". Just remove the columns from the "bill" definition and it'll be fine.
Whenever you need to get patient's name for the bill, you can always get it using p_id:
select p.p_name, p.p_age
from patient p
join bill b
on b.p_id, p.p_id
where b.bill_no = 'some_bill_no'
And keeping patient's age will lead to an engineering overhead because you'll have to update this field constantly once per year. I'd suggest you to store patient's birth_date and calculate his/her age when needed.
Of course when it is not a homework with gives table structure

Create table as select statement primary key in oracle

Is it possible to specify which is the primary key on creating table as select statement? My aim is to include the declaration of primary key on the create table not modifying the table after the creation.
CREATE TABLE suppliers
AS (SELECT company_id, address, city, state, zip
FROM companies
WHERE company_id < 5000);
Yes, it's possible. You would need to specify columns explicitly:
CREATE TABLE suppliers (
company_id primary key,
address,
city,
state,
zip
)
AS
SELECT company_id, address, city, state, zip
FROM companies
WHERE company_id < 5000;
Here is a demo
Note: in this case primary key constraint will be given a system-generated name. If you want it to have a custom name you'd have to execute alter table suppliers add constraint <<custom constraint name>> primary key(<<primary_key_column_name>>) after executing(without primary key specified) CREATE TABLE suppliers.. DDL statement.
Yes, it's possible.You can try referring below example.
create table student (rollno ,student_name,score , constraint pk_student primary key(rollno,student_name))
as
select empno,ename,sal
from emp;
You can create Suppliers table explicitly and if any column have primary key in companies table, then copy that structure from companies table.
Or
Create a same column defining primary key that you want and copy that column from companies table!

Create table field with foreign key constraint

I want to create a table department:
COLUMN NAME DATATYPE SIZE CONSTRAINT
dept_id number 4 Primary key
prod_id number 4 Foreign key
I tried this:
CREATE TABLE Department(
dept_id number(4) primary key,
prod_id number(4) foreign key);
It shows error. How can I add a foreign key constraint to this table?
A foreign key defines a relationship between your table DEPARTMENT and another table with a primary key. It means, you cannot create a row in DEPARTMENT with a PROD_ID of 1234 unless there is a pre-existing row in the designated parent table with a value of 1234 as its primary key.
So do you have such an existing parent table? if so you need to include its name in the foreign key definition. Otherwise you must create it.
Let's say the parent table is PRODUCT:
create table product (
prod_id number(4) primary key
, name varchar2(32) not null
);
Then you can create DEPARTMENT with a foreign key like this:
CREATE TABLE Department(
dept_id number(4) primary key,
prod_id references PRODUCT );
Yep, that's all the syntax you need: it automatically creates a column PROD_ID with the same datatype and precision as the primary key column of the referenced table. More verbose syntax is available. Read the Oracle SQL documentation to find out more.
I assume that the other table is named other_table_name and that it contains a primary key named prod_id.
CREATE Department (
dept_id number(4) primary key,
prod_id number(4) REFERENCES other_table_name (prod_id)
);
or a different syntax
CREATE Department (
dept_id number(4) primary key,
prod_id number(4)
...
CONSTRAINT fk_prod_id
FOREIGN KEY (prod_id)
REFERENCES other_table_name (prod_id)
);

Can a unique key ( not a primary key) be a foreign key to other table?

I have two table students and studentsprofilepic
'username' from students is unique key of the table
it is referenced as foreign key for the 'studentsprofilepic' table
the DDL for the tables are
CREATE TABLE students (
id NUMBER,
username VARCHAR2(30),
password VARCHAR2(30),
firstname VARCHAR2(30),
lastname VARCHAR2(40),
email VARCHAR2(300),
dob VARCHAR2(20),
alt_email VARCHAR2(300),
street_address VARCHAR2(50),
address_2 VARCHAR2(50),
city VARCHAR2(30),
state VARCHAR2(30),
zip VARCHAR2(10),
country VARCHAR2(60),
telephone VARCHAR2(10),
CONSTRAINT student_id_pk PRIMARY KEY (id),
CONSTRAINT student_username_uk UNIQUE (username)
);
CREATE TABLE studentsprofilepic (
id NUMBER,
photo_id NUMBER,
photo BLOB,
PRIMARY KEY (photo_id),
FOREIGN KEY (username) REFERENCES students (username)
);
YES, The foreign key column establishes a direct relationship with a primary key or unique key column (referenced key) usually in another table:
CREATE TABLE BOOK(
BNAME VARCHAR2(10)NOT NULL UNIQUE,
BTYPE VARCHAR2(10));
CREATE TABLE BOOKS_AUTH(
A_ID INT NOT NULL,
BNAME_REF VARCHAR2(10) NOT NULL,
FOREIGN KEY (BNAME_REF) REFERENCES BOOK (BNAME));
SQLFIDDLE DEMO
Yes, why not. It is possible to reference a UNIQUE constraint in a FOREIGN KEY.
You could have a Primary key and an Unique key, and you would like to validate both.
Yes, you can reference a column (or columns) governed by either a primary key constraint or a unique constraint.
The problem with your table "studentsprofilepic" is that your foreign key tries to use the column "studentsprofilepic"."username", but that column doesn't exist.
create table studentsprofilepic(
id number,
photo_id number,
photo blob,
-- Add the "username" column.
username varchar2(30) not null,
primary key (photo_id),
foreign key (username) references students (username)
);
Also, ask yourself "What's the point of "studentsprofilepic"."id"?" It's not the primary key. It's not a foreign key. It doesn't seem to serve any purpose besides letting you say, "Hey, my table has a column named 'id'!" That's a questionable feature.
Think about adding more not null constraints.

missing right parenthesis problems

i didn't know what the problem is. First this one works fine in sql
create table Department
(Department_Id number(8) PRIMARY KEY ,
Dept_Name varchar(20),
Location varchar(20));
but the second one says missing right parenthesis ora-00907
create table Instructor(Instructor_ID number(8) PRIMARY KEY ,
Department_Id number(8) FOREIGN KEY REFERENCES Department(Department_Id) ,
Ins_name varchar2(20) ,
Position varchar(20) ,
email_Id varchar (40),
Contact_No number(10),
Date_Of_Joining date);
Leave out the FOREIGN KEY part:
Department_Id number(8) REFERENCES Department(Department_Id) ,
See Oracle FAQs for an example. The FOREIGN KEY keywords are for out-of-line constraints, which go after the column definitions.

Resources