"Create table as select" does not preserve not null - oracle

I am trying to use the "Create Table As Select" feature from Oracle to do a fast update. The problem I am seeing is that the "Null" field is not being preserved.
I defined the following table:
create table mytable(
accountname varchar2(40) not null,
username varchar2(40)
);
When I do a raw CTAS, the NOT NULL on account is preserved:
create table ctamytable as select * from mytable;
describe ctamytable;
Name Null Type
----------- -------- ------------
ACCOUNTNAME NOT NULL VARCHAR2(40)
USERNAME VARCHAR2(40)
However, when I do a replace on accountname, the NOT NULL is not preserved.
create table ctamytable as
select replace(accountname, 'foo', 'foo2') accountname,
username
from mytable;
describe ctamytable;
Name Null Type
----------- ---- -------------
ACCOUNTNAME VARCHAR2(160)
USERNAME VARCHAR2(40)
Notice that the accountname field no longer has a null, and the varchar2 field went from 40 to 160 characters. Has anyone seen this before?

This is because you are no longer selecting ACCOUNTNAME, which has a column definition and meta-data. Rather you are selecting a STRING, the result of the replace function, which doesn't have any meta-data. This is a different data type entirely.
A (potentially) better way that might work is to create the table using a query with the original columns, but with a WHERE clause that guarantees 0 rows.
Then you can insert in to the table normally with your actual SELECT.
By having query of 0 rows, you'll still get the column meta-data, so the table should be created, but no rows will be inserted. Make sure you make your WHERE clause something fast, like WHERE primary_key = -999999, some number you know would never exist.

Another option here is to define the columns when you call the CREATE TABLE AS SELECT. It is possible to list the column names and include constraints while excluding the data types.
An example is shown below:
create table ctamytable (
accountname not null,
username
)
as
select
replace(accountname, 'foo', 'foo2') accountname,
username
from mytable;
Be aware that although this syntax is valid, you cannot include the data type. Also, explicitly declaring all the columns somewhat defeats the purpose of using CREATE TABLE AS SELECT.

Related

Insert records into collection type Oracle

I want to inserted into my collection some SQL records, but I can not. I'm a beginner. Can you help me ?
This is my tables :
CREATE TYPE article_type AS OBJECT (idA CHAR(10), nomA CHAR(10), prixA CHAR(10) )
CREATE TYPE facture_type AS OBJECT (idF CHAR(10), dateFact DATE)
CREATE TYPE ens_collection_fact AS OBJECT (refFact facture_type, refArticle article_type)
CREATE TYPE collection_fact AS TABLE OF ens_collection_fact
CREATE TYPE client_type AS OBJECT (idC NUMBER, nomC CHAR(10),adresse CHAR(10), Compose collection_fact )
CREATE TABLE Article OF article_type
CREATE TABLE Facture OF facture_type
CREATE TABLE Client OF client_type (PRIMARY KEY(idC)) NESTED TABLE Compose STORE AS temp
This is my query that I want to insert, but I have an error from the Oracle : ORA-02315
INSERT INTO ECOLER.CLIENT VALUES
(100, 'Jules Verne', '1', Collection_fact(Ens_collection_fact(reffact('A','2002-12-10'), ens_collection_fact(refarticle('D','E','F'))) ))
Thank in advance
reffact and refarticle are identifiers for objects within other objects, not types; you need to refer to the actual types. You also need to supply both values for each Ens_collection_fact attribute for the default constructor; you can pass null if you only want one or the other:
INSERT INTO CLIENT VALUES
(100, 'Jules Verne', '1',
Collection_fact(
Ens_collection_fact(facture_type('A',date '2002-12-10'), null),
Ens_collection_fact(null, article_type('D','E','F'))
)
)
Also notice that I've added the date keyword so it's providing an actual date literal rather than a string, which would be converted - if you're lucky - with your session NLS settings.
This will still error because 'Jules Verne' is 11 characters and you've defined the name attribute as 10 characters/bytes, but it will work with a shorter string literal.
db<>fiddle

Automatically inserting data into a table using a procedure

I would like to ask you a rather easy question but I cannot get my head around it as I am a beginner in SQL.
My task is: Enter initial data into BankStats2 by inserting rows into BankStats2 that
contain the branch names together with how many loans are in the Loan
table for that branch name.
desc BankStats2
Name Null? Type
----------------------------------------- -------- ----------------------------
BRANCHNAME NOT NULL VARCHAR2(20)
NUMBEROFLOANS NUMBER(38)
desc Loan
Name Null? Type
----------------------------------------- -------- ----------------------------
CUSTOMERNAME CHAR(20)
BRANCHNAME CHAR(20)
AMOUNT NUMBER(38)
LOANNUMBER NOT NULL NUMBER(38)
select branchName,count(customerName) from Loan group by branchName;
BRANCHNAME COUNT(CUSTOMERNAME)
-------------------- -------------------
Yorkshire 3
RoyalBank 1
Midlands 3
Basically, I would like to insert this information in the BankStats2 table and the way I thought of doing it is by creating a procedure which I will show below.
CREATE OR REPLACE PROCEDURE PopulateBankStats AS
CURSOR someLoanRows IS
SELECT branchName,COUNT(customerName) FROM loan GROUP BY branchName;
aBranchNameRow loan.branchName%TYPE;
numberOfLoans INT;
BEGIN
OPEN someLoanRows;
LOOP
FETCH someLoanRows INTO aBranchNameRow, numberOfLoans;
INSERT INTO BankStats2 VALUES (aBranchNameRow,numberOfLoans);
EXIT WHEN someLoanRows%NOTFOUND;
END LOOP;
CLOSE someLoanRows;
END;
/
But executing it give me the following error:
ERROR at line 1:
ORA-00001: unique constraint (N0757934.SYS_C0034405) violated
ORA-06512: at "N0757934.POPULATEBANKSTATS", line 10
ORA-06512: at line 1
Any help would be greatly appreciated. Thank you for your time!
This insert fails: INSERT INTO BankStats2 VALUES (aBranchNameRow,numberOfLoans); due to the error: ORA-00001: unique constraint (N0757934.SYS_C0034405) violated
This means that there is an unique constraint created on some of the columns of the table BankStats2.
In order to find which column has unique constraint, run this query:
select * from USER_IND_COLUMNS where index_name = 'SYS_C0034405';
Your procedure is trying to insert a record with a value of this column which already is existing in the table.
Have a look on the INSERT statement.
What your procedure is doing is exactly this insert statement:
INSERT INTO BankStats2 (BRANCHNAME,NUMBEROFLOANS)
SELECT branchName,COUNT(customerName) FROM loan GROUP BY branchName;
It is always preferable to use SQL statement (if possible) instead of the PL/SQL cursor loop logik - search Tom Kyte's "row by row - slow by slow" for an explantion.
Even if you want to use a procedure at all cost - use this INSERT in the preocedure.
Your exception means that you try to insert a value of the column BRANCHNAME that already exists in the table BankStats2.
This could be by an accident or a systematic problem.
If it is an accident, simple clean the data, i.e. DELETE the row(s) with the corresponding keys from the BankStats2 table.
This query returns the values existing in both tables
select BRANCHNAME from BankStats2
intersect
select branchName FROM loan;
If you want to systematically avoid inserting the duplicated row, add this logik in your INSERT statement:
INSERT INTO BankStats2 (BRANCHNAME,NUMBEROFLOANS)
SELECT branchName,COUNT(customerName)
FROM loan
WHERE branchName IS NOT NULL
and branchName NOT IN (select BRANCHNAME from BankStats2)
GROUP BY branchName;
Note that the SELECT excludes the row with the value that already exists in the target table - using NOT IN (subquery).
Note also that I'm approaching your next possible problem. The column BRANCHNAME is non nullable in BankStats2, but is nullable (i.e. may contain NULL) in loan, so you would fail to insert the row with NULL to the table BankStats2. Therefore I exclude those rows with the branchName IS NOT NULL predicate.
If you want to process the existing keys with an UPDATE logik, check the MERGE statement.

Oracle Database Can set Constraint for Upper Case Values?

Is there anyway that we can set a constraint in database table level to have upper or lower case values for certain columns? When we create a table, we can set NOT NULL to avoid having null values on a column. Same way, can we do that for either uppercase or lower case?
You can do that using a check constraint:
create table foo
(
only_lower varchar(20) not null check (lower(only_lower) = only_lower),
only_upper varchar(20) not null check (upper(only_upper) = only_upper)
);
I had almost same case, tried with check constraint, but if the user is not mentioning it as UPPER() or LOWER() it gives error so I took TRIGGER route as below code.
--creating table
create table user_name (
first_name varchar2(50),
last_name varchar2(50));
--creating trigger
CREATE OR REPLACE TRIGGER TRG_USER_NAME_IU
BEFORE INSERT OR UPDATE ON USER_NAME
FOR EACH ROW
BEGIN
:NEW.FIRST_NAME := UPPER(:NEW.FIRST_NAME);
:NEW.LAST_NAME := UPPER(:NEW.LAST_NAME);
END;
/
Can test and share feedback or comments

Substitution variable seems to fail in a query to describe an oracle table

I am trying to describe a table without using the DESCRIBE command but I want to combine the query with a substitution variable. Assuming I have the following table:
--DROP TABLE customers CASCADE CONSTRAINTS PURGE;
CREATE TABLE customers
( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50)
);
Following the posts here and here but adding a substitution variable, I have the following:
ACCEPT myv CHAR PROMPT 'Enter a table name: '
SELECT
column_name AS "Name",
nullable AS "Null?",
concat(concat(concat(data_type,'('),data_length),')') AS "Type"
FROM user_tab_columns
WHERE table_name = '&myv';
This returns a blank table with the appropriate column names. It doesn't matter if I entered the table name in the input prompt as CUSTOMERS or customers. However, desc customers yields:
Name Null Type
------------- -------- ------------
CUSTOMER_ID NOT NULL NUMBER(10)
CUSTOMER_NAME NOT NULL VARCHAR2(50)
CITY VARCHAR2(50)
Any idea how I can get substitution variable to work here? Thanks.
I got it to work with a bind variable. Not exactly sure what is going on because #kordirko said the query worked for him as is. Anyway, to get it to work for me (I'm using SQL Developer Version 4.0.3.16), I used bind variable, like so:
SELECT
column_name "Name",
nullable "Null?",
concat(concat(concat(data_type,'('),data_length),')') AS "Type"
FROM user_tab_columns
WHERE table_name = :myv;
I then entered CUSTOMERS into the value field of the Enter Binds window and the query executed fine. If anybody knows why substitution variable failed but bind variable did not, that will certainly add to the discussion.

Sequence with variable

In SQL we will be having a sequence. But it should be appended to a variable like this
M1,M2,M3,M4....
Any way of doing this ?
Consider having the prefix stored in a separate column in the table, e.g.:
CREATE TABLE mytable (
idprefix VARCHAR2(1) NOT NULL,
id NUMBER NOT NULL,
CONSTRAINT mypk PRIMARY KEY (idprefix, id)
);
In the application, or in a view, you can concatenate the values together. Or, in 11g you can create a virtual column that concatenates them.
I give it 99% odds that someone will say "we want to search for ID 12345 regardless of the prefix" and this design means you can have a nice index lookup instead of a "LIKE '%12345'".
select 'M' || my_sequence.nextval from dual;

Resources