How to Insert Blob into a table in Oracle 12c - sql

I have a table blobtest which was created with the following code:
create table blobtest(
id number,
file_data blob);
I want to insert data representing a file on my computer into this table. How may I do this?

Related

Create temporary table in Oracle

I'm trying to create temporary table in PL/SQL developer and insert some data, but it throws error:
ORA-00905
My code:
CREATE PRIVATE TEMPORARY TABLE my_temp_table (
id NUMBER,
description VARCHAR2(20)
);
CREATE PRIVATE TEMPORARY TABLE introduced only in Oracle 18:
Oracle 18c added private temporary tables, which are single-session in-memory objects.
In previous version, you can create global temporary table:
CREATE GLOBAL TEMPORARY TABLE my_temp_table (
id NUMBER,
description VARCHAR2(20)
);

How to create a table in PHPMyAdmin?

How do I (or rather, what do I write) a SQL CREATE TABLE command to create a table for Term as defined in the picture to create a table?
I am completely new to mhphpadmin (sql) and am learning this from scratch.
The general syntax is
Create Table Table_name (Column_name1 DataType, Column_name2 DataType,...)
You can create table as
CREATE TABLE Term (Term_Name VARCHAR(25), Term_StartDate DATE, TermEndDate DATE);

copy fields from one table to another in oracle toad

I have a Table A with 5 columns in DEV env and I want to copy 3 columns from that table into an existing (truncated) table in TEST env. How can i do this using Toad in Oracle database?
First create the database link. Following is the syntax :-
--Create database link
CREATE DATABASE LINK <dblinkname>
CONNECT TO <username> IDENTIFIED BY <password>
USING '(DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST=<hostname>)(PORT=<port_no>))
(CONNECT_DATA=(SERVICE_NAME=<servicename>)))';
Now i will demonstrate this by giving you one example
create table temp0
(
id number,
name varchar2(10)
);
--to be created in different environment
create table temp00
(
id number,
name varchar2(10)
);
insert into temp00 values(1,'roy');
insert into temp00 values(2,'nick');
Now,
insert into temp0(id) select(id) from temp00#dblinkname; --this will insert all the values of id in temp0 table from temp00 table
Now depending on your requirement, you can insert whatever columns you want by passing column names

Restoring a Truncated Table from a Backup

I am restoring the data of a truncated table in an Oracle Database from an exported csv file. However, I find that the primary key auto-increments and does not insert the actual values of the primary key constrained column from the backed up file.
I intend to do the following:
1. drop the primary key
2. import the table data
3. add primary key constraints on the required column
Is this a good approach? If not, what is recommended? Thanks.
EDIT: After more investigation, I observed there's a trigger to generate nextval on a sequence to be inserted into the primary key column. This is the source of the predicament. Hence, following the procedure above would not solve the problem. It lies in the trigger (and/or sequence) on the table. This is solved!
easier to use your .csv as an external table and then go
create table your_table_temp as select * from external table
examine the data in the new temp table to ensure you know what range of primary keys is present
do a merge into the new table
samples from here and here
CREATE TABLE countries_ext (
country_code VARCHAR2(5),
country_name VARCHAR2(50),
country_language VARCHAR2(50)
)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY ext_tab_data
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
(
country_code CHAR(5),
country_name CHAR(50),
country_language CHAR(50)
)
)
LOCATION ('Countries1.txt','Countries2.txt')
)
PARALLEL 5
REJECT LIMIT UNLIMITED;
and the merge
MERGE INTO employees e
USING hr_records h
ON (e.id = h.emp_id)
WHEN MATCHED THEN
UPDATE SET e.address = h.address
WHEN NOT MATCHED THEN
INSERT (id, address)
VALUES (h.emp_id, h.address);
Edit: after you have merged the data you can drop the temp table and the result is your previous table with the old data and the new data together
Edit you mention " During imports, the primary key column does not insert from the file, but auto-increments". This can only happen when there is a trigger on the table, likely, Before insert on each row. Disable the trigger and then do your import. Re-enable the trigger after committing your inserts.
I used the following procedure to solve it:
drop trigger trigger_name
Imported the table data into target table
drop sequence sequence_name
CREATE SEQUENCE SEQ_NAME INCREMENT BY 1 START WITH start_index_for_next_val MAXVALUE max_val MINVALUE 1 NOCYCLECACHE 20 NOORDER
CREATE OR REPLACE TRIGGER "schema_name"."trigger_name"
before insert on target_table
for each row
begin
select seq_name.nextval
into :new.unique_column_name
from dual;
end;

Oracle SQL to change column type from number to varchar2 while it contains data

I have a table (that contains data) in Oracle 11g and I need to use Oracle SQLPlus to do the following:
Target: change the type of column TEST1 in table UDA1 from number to varchar2.
Proposed method:
backup table
set column to null
change data type
restore values
The following didn't work.
create table temp_uda1 AS (select * from UDA1);
update UDA1 set TEST1 = null;
commit;
alter table UDA1 modify TEST1 varchar2(3);
insert into UDA1(TEST1)
select cast(TEST1 as varchar2(3)) from temp_uda1;
commit;
There is something to do with indexes (to preserve the order), right?
create table temp_uda1 (test1 integer);
insert into temp_uda1 values (1);
alter table temp_uda1 add (test1_new varchar2(3));
update temp_uda1
set test1_new = to_char(test1);
alter table temp_uda1 drop column test1 cascade constraints;
alter table temp_uda1 rename column test1_new to test1;
If there was an index on the column you need to re-create it.
Note that the update will fail if you have numbers in the old column that are greater than 999. If you do, you need to adjust the maximum value for the varchar column
Add new column as varchar2, copy data to this column, delete old column, rename new column as actual column name:
ALTER TABLE UDA1
ADD (TEST1_temp VARCHAR2(16));
update UDA1 set TEST1_temp = TEST1;
ALTER TABLE UDA1 DROP COLUMN TEST1;
ALTER TABLE UDA1
RENAME COLUMN TEST1_temp TO TEST1;
Look at Oracle's package DBMS_REDEFINE. With some luck you can do it online without downtime - if needed. Otherwise you can:
Add new VARCHAR2 column
Use update to copy NUMBER into VARCHAR2
Drop NUMBER column
Rename VARCHAR2 column
Here you go, this solution did not impact the existing NOT NULL or Primary key constraints. Here i am going to change the type of Primary key from Number to VARCHAR2(3), Here are the Steps on example table employee.
Take backup of table and Index, Constraints
created table employee_bkp
create table employee_bkp as select * from employee
commit;
Truncate the table to empty it
truncate table employee
Alter the table to change the type
ALTER TABLE employee MODIFY employee_id varchar2(30);
Copy the data back from backup table
insert into employee (select * from employee_bkp)
commit;
Verify