Find table details in Oracle - oracle

I am having TEST schema and it has many tables.
I want to find table details like
Table name, column name, column datatype, column length, column default value, column allow null, column comment
I am using Oracle database, please guide me how to do this.
In SQL developer I am able to find these details for individual table but I want to get this for tables where table name starts with A,B or C (this can be any alphabet character)

If you are logged in as a DBA user, you can use:
SELECT *
FROM dba_tab_columns
WHERE OWNER = 'TEST'
AND SUBSTR(TABLE_NAME, 1, 1) IN ('A', 'B', 'C', 'a', 'b', 'c');
Or you can query the all_tab_columns data dictionary view or, if you are logged in as the TEST user:
SELECT *
FROM user_tab_columns
WHERE SUBSTR(TABLE_NAME, 1, 1) IN ('A', 'B', 'C', 'a', 'b', 'c');

select * from ALL_TAB_COLUMNS
where
OWNER = 'TEST'
and SUBSTR(TABLE_NAME, 1, 1) IN ('A', 'B', 'C', 'a', 'b', 'c');
this will give you column names and info of all tables accessible to the current user
https://docs.oracle.com/cd/B19306_01/server.102/b14237/statviews_2094.htm

Related

Why am i not able to insert multiple values at a time by this code?

I have created the table successfully but not able to insert multiple values by this code. What is the mistake. Oracle Code
CREATE TABLE sales (
customer_id VARCHAR2(4)
,order_date DATE
,product_id VARCHAR2(5)
);
INSERT INTO sales
VALUES (
'A'
,'01-JAN-2021'
,'2'
);(
'A'
,'07-JAN-2021'
,'2'
);(
'A'
,'10-JAN-2021'
,'3'
);(
'A'
,'11-JAN-2021'
,'3'
);(
'A'
,'11-JAN-2021'
,'3'
);(
'B'
,'01-JAN-2021'
,'2'
);(
'B'
,'02-JAN-2021'
,'2'
);(
'B'
,'04-JAN-2021'
,'1'
);(
'B'
,'11-JAN-2021'
,'1'
);(
'B'
,'16-JAN-2021'
,'3'
);(
'B'
,'01-FEB-2021'
,'3'
);(
'C'
,'01-JAN-2021'
,'3'
);(
'C'
,'01-JAN-2021'
,'3'
);(
'C'
,'07-JAN-2021'
,'3'
);
Why? because that's invalid syntax in Oracle.
One option is this:
SQL> INSERT INTO sales VALUES ('A', DATE '2021-01-01', '2');
1 row created.
SQL> INSERT INTO sales VALUES ('A', DATE '2021-01-07', '2');
1 row created.
SQL> INSERT INTO sales VALUES ('A', DATE '2021-01-10', '3');
1 row created.
etc.
Note that you shouldn't insert strings into date datatype column; I used date literal; you could use TO_DATE with appropriate format model.
Another option is e.g.
SQL> INSERT ALL
2 INTO sales VALUES ('A', DATE '2021-01-01', '2')
3 INTO sales VALUES ('A', DATE '2021-01-07', '2')
4 SELECT * FROM DUAL;
2 rows created.
or
SQL> INSERT INTO sales
2 SELECT 'A', DATE '2021-01-01', '2' FROM DUAL
3 UNION ALL
4 SELECT 'A', DATE '2021-01-07', '2' FROM DUAL;
2 rows created.

Laravel Eloquent/DB: Multiple sum fields in grouped select

Is there any possibility to do the following simple sql-select in Laravel?
SELECT a,b,c,d, SUM(e) as e, SUM(f) as f, SUM(g) as g FROM my_table GROUP BY a,b,c,d
You could use the DB class for that:
DB::table('my_table')
->select('a', 'b', 'c', 'd', DB::raw('SUM(e) as e'), DB::raw('SUM(f) as f'), DB::raw('SUM(g) as g'))
->groupBy('a', 'b', 'c', 'd')
->get()
Documentation

How do i select a coma seperated list of values into a single column

I have a string of comma separated values "'a', 'b', 'c', 'd'".
select 'a', 'b', 'c', 'd' from dual;
How do i select these into a temp table all in a single column rather than a column per value? So my select would return output like this:
value
a
b
c
d
Note: Doing the normal multiple selects and unions is not allowed due to the comma separated values as the input.
I guess you could do something like this, taking the assumption that you can qualify your columns:
SQL> select val from
( select 'a' as c1, 'b' as c2, 'c' as c3, 'd' as c4 from dual ) unpivot include
nulls ( val for col in ( c1,c2,c3,c4 ) );
V
-
a
b
c
d
In this option, it does not matter what values you have in 'a' , 'b' , 'c' 'd' , as basically unpivot is transposing columns into rows.
Hope it helps
You can use hierarchy query as follows:
SQL> with YOUR_DATA as (select q'#'a', 'b', 'c', 'd'#' as str from dual)
2 -- Your query starts from here
3 SELECT TRIM('''' FROM TRIM(REGEXP_SUBSTR(STR,'[^,]+',1,LEVEL))) AS RESULT
4 FROM YOUR_DATA CONNECT BY
5 LEVEL <= 1 + REGEXP_COUNT(STR,',');
RESULT
------------------
a
b
c
d
SQL>
If you have version 12c or later, and if your data has no weird characters, then:
with input(x) as (select q'#'a', 'b', 'c', 'd'#' from dual)
select x_out from input,
json_table(
'['||replace(x, '''', '"')||']', '$[*]'
columns x_out path '$'
);
X_OUT
a
b
c
d

Traverse a Tree With Oracle. Each Node Has A Property

I have a simple hierarchy structure in a table (Using Oracle Database 11g)
A
|-B
|-B1
|-C
|-D
Each node has a property associated with it (Y or N).
Traversing from the root node (parent), I want to get the first node in each branch that has the property Y.
For example:
A
|-B (N)
|-B1 (Y)
|--B2 (N)
|-C (Y)
|-C1 (Y)
|--C2 (N)
|-D (Y)
This should return B1, C and D.
Please give some ideas on how this can be done (code needs to be optimized for time).
There's probably a fancier way than using sys_connect_by_path, but I find it very easy to understand.
create table hier_test (id varchar2(2), parent_id varchar2(2), prop varchar2(1));
insert into hier_test values ('A', null, 'N');
insert into hier_test values ('B', 'A', 'N');
insert into hier_test values ('C', 'A', 'Y');
insert into hier_test values ('D', 'A', 'Y');
insert into hier_test values ('B1', 'B', 'Y');
insert into hier_test values ('B2', 'B1', 'N');
insert into hier_test values ('C1', 'C', 'Y');
insert into hier_test values ('C2', 'C1', 'N');
select id from (
select id, prop, sys_connect_by_path(prop, '>') as priors
from hier_test
connect by parent_id = prior id
start with id = 'A'
)
where prop = 'Y' and priors not like '%Y>%';
Edit: sqlfiddle

Bug on select with count and case

I have a test which uses H2 as an in memory database. The following table, data and query produces a single row in MySQL with the expected values, but not in H2:
CREATE TABLE redirects (
site_id INTEGER NOT NULL,
company_id INTEGER NOT NULL,
type CHAR(1) NOT NULL,
capture TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'm', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 2, 'm', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'h', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'h', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 2, 'h', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'a', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'a', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 1, 'a', CURRENT_TIMESTAMP);
INSERT INTO redirects (site_id, company_id, type, capture) VALUES (1, 2, 'a', CURRENT_TIMESTAMP);
SELECT
COALESCE(site_id, 1) site_id,
COALESCE(company_id, -1) company_id,
COUNT(CASE WHEN type = 'm' THEN 1 END) views,
COUNT(CASE WHEN type IN ('h', 'a') THEN 1 END) clicks
FROM
redirects
WHERE
(site_id = 1 AND company_id = 3 AND capture > TIMESTAMP '2017-04-24 00:00:00.0');
The result in MySQL is the following:
+---------+------------+-------+--------+
| site_id | company_id | views | clicks |
+---------+------------+-------+--------+
| 1 | -1 | 0 | 0 |
+---------+------------+-------+--------+
However, in H2 (version 1.4.194) the result is:
SITE_ID COMPANY_ID VIEWS CLICKS
1 2 0 0

Resources