Reference a foreign key to a different type - sql

Is it possible to do this in PL/SQL:
CREATE TABLE ROOM_TYPE
(
ROOM_TYPE_ID NUMBER NOT NULL PRIMARY KEY,
REF_SERVICE_PROVIDER NUMBER NOT NULL,
ROOM_TYPE_NAME1 VARCHAR2(100) NOT NULL,
ROOM_TYPE_NAME2 VARCHAR2(100),
REF_SERVICE_ID NUMBER(15,3),
ROOM_AC NUMBER(1),
ROOM_BATH_ROOM NUMBER(1),
ROOM_CHOICE_OF_MEAL NUMBER(1),
CONSTRAINT fk_ref_service_provider
FOREIGN KEY (REF_SERVICE_PROVIDER)
REFERENCES PROVIDER(PROVIDER_ID),
CONSTRAINT fk_ref_service_id
FOREIGN KEY (REF_SERVICE_ID)
REFERENCES MEDICAL_SERVICE(SERVICE_ID)
);
While the REF_SERVICE_ID of type number(15,3) is a foreign key in this table which refers to a primary key SERVICE_ID of type number.
The question is, can I refer to a primary key of type number while the foreign key is number(15,3) type !?

Yes you can, but there is a big chance getting following error:
ORA-01438: value larger than specified precision allowed for this column
For eg:
CREATE TABLE A ( ID NUMBER NOT NULL PRIMARY KEY);
CREATE TABLE b
(
id NUMBER(15,3),
val CHAR(1),
CONSTRAINT b_pk FOREIGN KEY (ID) REFERENCES a(id)
);
INSERT INTO A VALUES(456);
INSERT INTO B VALUES( 456, 'X'); --inserts successfully
INSERT INTO A VALUES(12345678901234567890);
INSERT INTO B VALUES( 12345678901234567890, 'X'); --errors

Related

Column exist but program don t see? [duplicate]

This question already has answers here:
SQLite Foreign Key
(5 answers)
Closed 1 year ago.
I created a 3 tables but in the last one shows errors
CREATE TABLE Student
(
St_Id char(7) PRIMARY KEY,
St_Fname varchar(15) NOT NULL,
St_Lname varchar(20) NOT NULL,
St_DOB date
)
CREATE TABLE Course
(
Course_code char(5) PRIMARY KEY,
Course_title varchar(30) NOT NULL,
Course_credit INTEGER NOT NULL
)
CREATE TABLE Registration
(
Reg_no INTEGER PRIMARY KEY AUTOINCREMENT ,
FOREIGN KEY (St_Id) REFERENCES Student(St_Id),
FOREIGN KEY (Course_code) REFERENCES Course(Course_code),
Mark_obtaines INTEGER
)
and error is
Execution finished with errors. Result: unknown column "St_Id" in
foreign key definition At line 1: CREATE TABLE Registration ( Reg_no
INTEGER PRIMARY KEY AUTOINCREMENT , FOREIGN KEY (St_Id) REFERENCES
Student(St_Id),
In order to define a foreign key, you need to define the column first -- and the types need to match the type in the referenced table:
CREATE TABLE Registration (
Reg_no INTEGER PRIMARY KEY AUTOINCREMENT,
St_Id CHAR(7),
Course_Code CHAR(5),
FOREIGN KEY (St_Id) REFERENCES Student(St_Id),
FOREIGN KEY (Course_code) REFERENCES Course(Course_code),
Mark_obtaines INTEGER
);
Then the FOREIGN KEY declaration provides more information about the column.

SQL ORA-02291: integrity constraint violated - parent key not found

I have encountered some problem with SQL foreign key.
Here are my table and insert SQL.
create table passenger_card2
(
phone char(20) primary key,
name char(20)
);
create table card
(
card_num char(20) primary key,
balance number(10,2),
cvn char(20)
);
create table passenger_card1
(
sin integer primary key,
user_id char(20) not null unique,
phone char(20),
card_num char(20) unique,
foreign key(phone) references passenger_card2,
foreign key (card_num) references card
);
And here are my INSERT statements:
INSERT INTO PASSENGER_CARD2 VALUES ( '111222333' , 'Ace');
INSERT INTO CARD VALUES ( '1000' , '100.1' , '110');
INSERT INTO PASSENGER_CARD1 VALUES ('100', 'aaaa', '111222333', '1000');
However, I get an error when I tried to insert PASSENGER_CARD1 data:
SQL ORA-02291: integrity constraint violated - parent key not found
I am not sure why my foreign key is wrong?
I am not sure if this is going to be right but you should make the table 2 first before you create the first table. The database is confused because it wouldnt make sense telling them that theres gonna be a foreign key in the second table but the table isnt created. Run the code for the second table first and then run the code for the first table.

How do I fix a ORA-00902: invalid datatype creating a table

I am creating a table in a command SQL sections into a script already populated I have created several tables already but in this one I get a message saying
ORA-00902: invalid datatype
CREATE TABLE Weapons
(
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2 (10),
CONSTRAINT pk_Weapons PRIMARY_KEY(id),
CONSTRAINT fk_Weapons_company
FOREIGN_KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo
FOREIGN_KEY(ammo_id) REFERENCES Ammo(id)
);
In the CONSTRAINT, it should be FOREIGN KEY and not FOREIGN_KEY. Also it should be PRIMARY KEY, not PRIMARY_KEY.
There is no underscore required as per syntax. So the query will be:
CREATE TABLE Weapons (
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2(10),
CONSTRAINT pk_Weapons PRIMARY KEY(id),
CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES Ammo(id)
);
About Foreign Keys: https://www.techonthenet.com/oracle/foreign_keys/foreign_keys.php
About Primary Keys: https://www.techonthenet.com/oracle/primary_keys.php
Here's a working example. I've created the AMMO table (whose description you didn't post, so I used only the ID column so that the foreign key constraint wouldn't fail). Pay attention to comments I wrote within the code.
SQL> create table ammo
2 ( id VARCHAR2(10),
3 CONSTRAINT pk_ammo PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Company
2 ( id VARCHAR(3),
3 name VARCHAR(30), --> switch from CHAR to VARCHAR2
4 CONSTRAINT pk_Company PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Weapons
2 ( id NUMBER(4),
3 name VARCHAR2(30),
4 damage NUMBER(4),
5 company_id VARCHAR2(3), --> should match COMPANY.ID datatype
6 ammo_id VARCHAR2(10), --> should match AMMO.ID datatype
7 CONSTRAINT pk_Weapons PRIMARY KEY(id),
8 CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
9 CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES ammo(id) );
Table created.
SQL>
In referential integrity constraint, you should match datatypes of the foreign and primary key columns. There's no sense in having a VARCHAR2(10) in the detail table which points to a VARCHAR2(3) column in the master table; you won't be able to put anything longer than 3 characters into the detail table's column anyway (foreign key constraint won't let you).

foreign keys: number of columns not equal to referenced columns

I'm getting an error from oracle that says "number of referencing columns must match referenced columns."
I want my column recorded_on in the table measurement to reference recorded_on in the table called sample
The column Recorded on in the Sample table must be part of a composite key together with Scientist_Num
The error is coming from
FOREIGN KEY (Recorded_On) REFERENCES Sample(Scientist, Recorded_On, Site_ID)
CREATE TABLE Sample (
Scientist_Num varchar2(5) not null,
Recorded_On date not null,
Site_ID varchar2(4) not null,
Comments clob,
Primary key (Scientist_Num, Recorded_On),
FOREIGN KEY (Scientist_Num) REFERENCES Scientist(Scientist_Num),
FOREIGN KEY (Site_ID) REFERENCES Site(Site_ID)
);
CREATE TABLE Measurement (
Site_ID varchar2(4) not null,
Recorded_On date not null,
Name varchar2(10) not null,
Value varchar2(10),
Outlier_Indicator varchar2(10),
Primary key (Site_ID, Recorded_On, Name),
FOREIGN KEY (Site_ID) REFERENCES Sample(Site_ID),
FOREIGN KEY (Recorded_On) REFERENCES Sample(Scientist, Recorded_On, Site_ID)
);
The Scientist_Num and Recorded_On columns must be in a composite key together.
The answer to my problem and an explanation of what went wrong would be greatly appreciated.
You can create virtual column in sample table:
Recorded_virtual varchar2(5) [GENERATED ALWAYS] AS
(Scientist||Recorded_On||Site_ID) [VIRTUAL]
And create reference to this column:
CONSTRAINT fk_column
FOREIGN KEY (Recorded_On)
REFERENCES Sample(Recorded_virtual )
Foreign key references need to match the primary keys in number and type. So I think you intend:
CREATE TABLE Measurement (
Site_ID varchar2(4) not null,
Scientist_Num varchar2(5) not null,
----^ added for foreign key reference
Recorded_On date not null,
Name varchar2(10) not null,
Value varchar2(10),
Outlier_Indicator varchar2(10),
Primary key (Site_ID, Recorded_On, Name),
FOREIGN KEY (Site_ID) REFERENCES Site(Site_ID),
-------------------------------------^ Presumably you intend the site table
FOREIGN KEY (Scientist_Num, Recorded_On) REFERENCES
Sample(Scientist_Num, Recorded_On)
-----------------^ two columns, both need to already be defined
);
I suspect there are other issues with your data model, but this should fix the syntax error. If you want further help, then ask another question.

ORACLE - Cannot insert a NULL value to a NON-Primary Key

I Have searched the web and various forums but I cannot figure out why this won't work. My Database is made up from the following Tables:
CREATE TABLE CUSTOMER(
custid Number(4),
cfirstname varchar2(30),
csurname varchar2(20) NOT NULL,
billingaddr varchar2(30),
cgender varchar2(1),
CONSTRAINT custpk PRIMARY KEY (custid),
CONSTRAINT genderconst CHECK(cgender in ('M','F','m','f'))
);
CREATE TABLE PRODUCT(
prodid Number(4),
prodname varchar2(30),
currentprice Number(6,2),
CONSTRAINT cprice_chk CHECK(currentprice >= 0 AND currentprice <=5000 ),
CONSTRAINT prodpk PRIMARY KEY (prodid),
CONSTRAINT pricepos CHECK((currentprice >= 0))
);
CREATE TABLE SALESPERSON(
spid Number(4),
spfirstname varchar2(30),
spsurname varchar2(30),
spgender varchar2(1),
CONSTRAINT salespk PRIMARY KEY (spid)
);
CREATE TABLE SHOPORDER(
ordid Number(4),
deliveryaddress varchar2(30),
custid Number(4) NOT NULL,
spid Number(4) NOT NULL,
CONSTRAINT orderpk PRIMARY KEY (ordid),
CONSTRAINT orderfk1 FOREIGN KEY (custid) REFERENCES CUSTOMER(custid),
CONSTRAINT orderfk2 FOREIGN KEY (spid) REFERENCES SALESPERSON(spid)
);
CREATE TABLE ORDERLINE(
qtysold Number(4),
qtydelivered Number(4),
saleprice Number (6,2),
ordid Number(4) NOT NULL,
prodid Number(4) NOT NULL,
CONSTRAINT qty_chk CHECK (qtydelivered >= 0 AND qtydelivered <=99),
CONSTRAINT price_chk CHECK(saleprice >= 0 AND saleprice <=5000 ),
CONSTRAINT linefk1 FOREIGN KEY (ordid) REFERENCES SHOPORDER(ordid),
CONSTRAINT linefk2 FOREIGN KEY (prodid) REFERENCES PRODUCT(prodid)
);
And I am using an insert statement to insert the following:
INSERT INTO SHOPORDER(ordid, deliveryaddress, spid)
VALUES (41, NULL, 23);
Whether I use '' or NULL it gives me the error:
ORA-01400: cannot insert NULL into ("S9710647"."SHOPORDER"."CUSTID");
My issue that I have not set deliveryaddress as a Primary key nor is it a Foreign key or contain any NOT NULL CoNSTRAINTS.
Is there a factor that I am missing here? The majority of forums have had people with problems relating to constraints. I cannot see any conflicting constraints.
Cheers
You're only inserting the columns ordid, deliveryaddress and spid into SHOPORDER which means the others will probably default to NULL.
However, you've declared custId as NOT NULL so that's not allowed. You can actually tell what the complaint is by looking at the error message:
ORA-01400: cannot insert NULL into ("S9710647"."SHOPORDER"."CUSTID");
^^^^^^
It's clearly having troubles with the CUSTID column there and you know you haven't explicitly set that, so it must be the default value causing you grief.
You can fix it by either inserting a specific value in to that column as well, or by giving a non-NULL default value to it, though you'll have to ensure the default exists in the CUSTOMER table lest the orderfk1 foreign key constraint will fail.
The problem is that this:
INSERT INTO SHOPORDER(ordid, deliveryaddress, spid)
VALUES (41, NULL, 23);
uses the default values for all columns that you don't specify an explicit value for, so it's equivalent to this:
INSERT INTO SHOPORDER(ordid, deliveryaddress, custid, spid)
VALUES (41, NULL, NULL, 23);
which violates the NOT NULL constraint on custid.
CREATE TABLE SHOPORDER(
ordid Number(4),
deliveryaddress varchar2(30),
custid Number(4) NOT NULL,
spid Number(4) NOT NULL,
CONSTRAINT orderpk PRIMARY KEY (ordid),
CONSTRAINT orderfk1 FOREIGN KEY (custid) REFERENCES CUSTOMER(custid),
CONSTRAINT orderfk2 FOREIGN KEY (spid) REFERENCES SALESPERSON(spid)
);
INSERT INTO SHOPORDER(ordid, deliveryaddress, spid)
VALUES (41, NULL, 23);
Your problem is with custid which is defines as NOT NULL. You do not specify a value for it, hence your attempt to set it to NULL.