Modifying column data type - oracle10g

I have two tables like Samp1(Parent Table) and Samp2 (Child table)
Parent Table :Samp1
column Datatype Constraint
----------------------------------------
Id Number(6) Primary Key
Child table :Samp2
column Datatype Constraint
----------------------------------------------
Id Number(6) Foriegn Key
Then how to modify column data type Number to Varchar2(10) both parent and child table at a time, can I?

You can't do this in one step. Assuming the tables have data, you'd probably need to do something like
Add a new column (e.g. id_varchar) to your parent table
Populate the data in this new column from the data in the existing column
Add that new column to the child table
Populate the data in this new column from the data in the existing column
Create a new foreign key constraint for the new column
Drop the existing foreign key constraint
Drop the existing primary key constraint
Drop the existing id columns from both tables
Rename the id_varchar column to id in each table
Create a new primary key constraint on the parent table
Normally, this would require some downtime since you generally don't want sessions modifying data while you're doing this. If you need to do this online, you could potentially use the dbms_redefinition package which would involve creating new copies of both tables.

Related

Postgresql Merging of two data

I have a inventory table with the following row data.
Row One -> Barcode : SH9025H36SP23,
Row Two -> Barcode : SH9025H36SP23N1
Both are referred as foreign key in other tables.
Row One is old barcode whereas Row Two is the new barcode.
How do i change Row One to Row Two without loosing any references ?
Thank you for taking your time to answer my question.
Simplified Table Structure for Inventory table
barcode varchar not null
name varchar not null
PRIMARY KEY (barcode)
Referenced by:
TABLE "act" CONSTRAINT "actkey" FOREIGN KEY (fkbarcode) REFERENCES inventory(barcode) ON UPDATE CASCADE ON DELETE RESTRICT
TABLE "do" CONSTRAINT "dokey" FOREIGN KEY (fkbarcode) REFERENCES inventory(barcode) ON UPDATE CASCADE ON DELETE RESTRICT
so on..

How to edit a record that results in a uniqueness violation and echo the change to child tables?

PostgreSQL 11.1
How can "Editing" of a record be transmitted to dependent records?
Summary of my issue:
The master table, disease, needs a unique constraint on the description column. This unique constraint is needed for foreign key ON UPDATE CASCADE to its children tables.
To allow for a temporary violation of the unique constraint, it must be made deferrable. BUT A DEFERABLE CONSTRAINT CAN NOT BE USED IN A FOREIGN KEY.
Here is the situation.
The database has 100+ tables (and keeps on growing).
Most all information has been normalized in that repeating groups of information have been delegated to their own table.
Following normalization, most tables are lists without duplication of records. Duplication of records within a table is not allowed.
All tables have a unique ID assigned to each record (in addition to a unique constraint placed on the record information).
Most tables are dependent on another table. The foreign keys reference the primary key of the table they are dependent on.
Most unique constraints involve a foreign key (which in turn references the primary key of the parent table).
So, assume the following schema:
CREATE TABLE phoenix.disease
(
recid integer NOT NULL DEFAULT nextval('disease_recid_seq'::regclass),
code text COLLATE pg_catalog."default",
description text COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT disease_pkey PRIMARY KEY (recid),
CONSTRAINT disease_code_unique UNIQUE (code)
DEFERRABLE,
CONSTRAINT disease_description_unique UNIQUE (description)
,
CONSTRAINT disease_description_check CHECK (description <> ''::text)
)
CREATE TABLE phoenix.dx
(
recid integer NOT NULL DEFAULT nextval('dx_recid_seq'::regclass),
disease_recid integer NOT NULL,
patient_recid integer NOT NULL,
CONSTRAINT pk_dx_recid PRIMARY KEY (recid),
CONSTRAINT dx_unique UNIQUE (tposted, patient_recid, disease_recid)
,
CONSTRAINT dx_disease_recid_fkey FOREIGN KEY (disease_recid)
REFERENCES phoenix.disease (recid) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT,
CONSTRAINT dx_patients FOREIGN KEY (patient_recid)
REFERENCES phoenix.patients (recid) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT
)
(Columns not involved in this question have been removed. :) )
There are many other children tables of disease with the same basic dependency on the disease table. Note that the primary key of the disease table is a foreign key to the dx table and that the dx table uses this foreign key in a unique constraint. Also note that the dx table is just one table of a long chain of table references. (That is the dx table also has its primary key referenced by other tables).
The problem: I wish to "edit" the contents of the parent disease record. By "edit", I mean:
change the data in the description column.
if the result of the change causes a duplication in the disease table, then one of the "duplicated" records will need to be deleted.
Herein lies my problem. There are many different tables that use the primary key of the disease table in their own unique constraint. If those tables ALSO have a foreign key reference to the duplicated record (in disease), then cascading the delete to those tables would be appropriate -- i.e., no duplication of records will occur.
However, if the child table does NOT have a reference to the "correct" record in the parent disease table, then simply deleting the record (by cascade) will result in loss of information.
Example:
Disease Table:
record 1: ID = 1 description = "ABC"
record 2: ID = 2 description = "DEF"
Dx Table:
record 5: ID = 5 refers to ID=1 of Disease Table.
Editing of record 1 in Disease table results in description becoming "DEF"
Disease Table:
record 1: ID = 1 "ABC" --> "DEF"
I have tried deferring the primary key of the disease table so as to allow the "correct" ID to be "cascaded" to the child tables. This causes the following errors:
A foreign key can not be dependent on a deferred column. "cannot use a deferrable unique constraint for referenced table "disease"
additionally, the parent table (disease) has no way of knowing ahead of time if its children already have a reference to the "correct" record so allowing deletion, or if the child needs to change its own column data to reflect the new "correct" id.
So, how can I allow a change in the parent table (disease) and notify the child tables to change their column values -- and delete within them selves should a duplicate record arise?
Lastly, I do not know today what future tables I will need. So I cannot "precode" into the parent table who its children are or will be.
Thank you for any help with this.

How to handle foreign key in postgresql

I am new to postgresql..I am creating a database which contains table “user” with columns “Name”,”Sum”,”id”(which is a serial primary key)
I want to input data only in columns Name and Sum since id is a serial PK
The column “id” is a foreign key in another table “account”, so when I am trying to input data in the table it is telling me I am violating the foreign key constraint
Here is the code:
INSERT INTO public.”user”(“Name”,”Sum”)
VALUES (‘Tasneem’,400);
It is telling me
Insert or update on table “user” violates foreign key constraint “user_Sum_fkey”
Key (Sum)=(400) is not present in table account

how to refer a column with integer data type foreign key in existing table with columns values

In my parent table (empid integer) is primary key. In my child table have some columns with values. I need to add a column (empid integer not null) in my child table ,i can't add a column.Because the child table have values. I need a add column with default value. If i add column with default 0.I need to have the 0 value in my parent table. So any other way to refer the parent table and set the default value. I'm using PostgreSQL.
Add the column first with a default value before declaring the foreign key constraint. Prefill it correctly, then create the foreign key constraint.
A bad way is to create a row with 0 empid in the parent table, then create the column in the child table with default value 0 while having the foreign key constraint, but you shouldn't need this, it's absurd.

postgres Foreign Key to only accept table names

I'm using postgres, and I want to log new changes in several of my tables. I want to have a column which will only take on values which are table names, so that I can have a column for table, column for id, and column for date that that value was updated.
I see that all the table names are in the table information_schema.tables. Is it possible to use that as a foreign key, or would that be inadvisable?