how to get or display duplicate data in codeigniter - codeigniter

Can somebody share some code how to get or display duplicate data from my database using codeigniter.
[id]
[1]
[2]
[3]
[3]
[4]
thank you in advance.

Try this
SELECT column_name FROM table_name GROUP BY column_name HAVING COUNT(column_name) > 1;
You can do like this in codeigniter
$sql_select = 'SELECT column_name FROM table_name GROUP BY column_name HAVING COUNT(column_name) > 1';
$query = $this->db->query($this->sql_select);
$result = $query->result_array();

Related

Find a certain column name in all views in one select statement

I'm searching for a certain column name in all views. I know that the following works for all tables but don't know how to do it for all views:
What I use to find a column_name='SHORT_TITLE' in all tables but now need to search all views:
SELECT table_name, column_name FROM all_tab_columns WHERE column_name='SHORT_TITLE'
Thanks
Oracle:
Joining all_tab_columns with all_views
SELECT table_name, column_name
FROM all_tab_columns, all_views
WHERE all_tab_columns.table_name = all_views.view_name
AND column_name = ...
Result of table_name will only be views
SQL:
We can do this by joining information_schema.views and information_schema.columns
SELECT v.table_name, column_name FROM information_schema.views v JOIN information_schema.columns c
ON v. = c.table_schema
AND v.table_name = c.table_name
WHERE column_name = ...
v.table_name is the name of the view.
EDIT: I added Oracle because I just realized the all_tab_columns in your question

How to get Column list of Synonym in Oracle

I need to get column list of SYNONYMS in ORACLE. The table on which synonym is created is in other schema. Can anyone help me in this problem?
This will return a list of all synonyms in the db:
select * from DBA_SYNONYMS
order by synonym_name
This will return a list of synonyms with a 'like' name:
select * from DBA_SYNONYMS
where upper(synonym_name) like upper('%SYNONYM_NAME_HERE%')
order by synonym_name
Relevant column names in response:
SYNONYM_NAME
TABLE_NAME
Here is a query that I use to see synonyms and their targets.
You will need SELECT privileges on DBA_SYNONYMS and DBA_OBJECTS.
select decode(owner, 'PUBLIC', 'PUBLIC SYNONYM', 'SYNONYM') as objtype,
decode(owner, 'PUBLIC', '', owner) as objowner,
synonym_name as objname,
synonym_name || ' => ' ||
case
when db_link is null then '(' || (
select o1.object_type from dba_objects o1 where o1.owner = table_owner and o1.object_name = table_name and o1.object_type in ('VIEW','TABLE','SYNONYM','SEQUENCE','FUNCTION','PROCEDURE','PACKAGE','MATERIALIZED VIEW','JAVA CLASS','TYPE')
and not exists (select 1 from dba_objects o2 where o2.owner = o1.owner and o2.object_name = o1.object_name and o1.object_type = 'TABLE' and o2.object_type = 'MATERIALIZED VIEW')
) || ') ' || table_owner || '.' || table_name
else decode(table_owner, null, '', table_owner || '.') || table_name || decode(db_link, null, '', '#' || db_link)
end as objdesc
from dba_synonyms
where OWNER != 'PUBLIC'
order by 1,2,3,4
It's a bit wordy, but it makes nice output like this:
OBJTYPE OBJOWNER OBJNAME OBJDESC
------- ----------- -------------------- -----------------------------------------------------------------------------
SYNONYM SYSTEM PRODUCT_USER_PROFILE PRODUCT_USER_PROFILE => (TABLE) SYSTEM.SQLPLUS_PRODUCT_PROFILE
SYNONYM SYSTEM TAB TAB => (VIEW) SYS.TAB
This is actually part of a larger query I use for finding objects of various types, hence some of the redundant information.
I filtered out PUBLIC because that is one huge list.
it's never too late to respond
select * from all_tab_columns#&SYNONYM_DB_LINK
where upper(table_name) like '&TARGET_TABLE_NAME'
order by owner, table_name, column_id
In OWNER is the synonym owner
select * from all_synonyms
where table_owner=upper(:table_owner) and table_name=upper(:table_name)
What do you mean under "Column list"?
select * from all_tab_cols where table_name in
(select TABLE_NAME from all_synonyms where owner = <SCHEMA_NAME> )

show the schema of a table in oracle

The following mysql query returns the constraints and the default values along with column_name, is_null and other details -
mysql query - select TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, COLUMN_TYPE, COLUMN_KEY, EXTRA from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA = 'DB_NAME'
I want to write a similar query in Oracle, the following query returns data_type and is_null but doesn't return constraints and default values -
Oracle query - SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, NULLABLE FROM DBA_TAB_COLUMNS where owner = 'USERNAME'
How can I extract those information from an oracle table.
Note: I don't want to use describe table
Select tc.TABLE_NAME, tc.COLUMN_NAME, tc.DATA_TYPE, tc.NULLABLE, tc.DATA_DEFAULT,
con.cons
from DBA_TAB_COLUMNS tc
left join
( select listagg( cc.constraint_name, ',') within group (order by cc.constraint_name) cons,
table_name, owner , column_name
from DBA_CONS_COLUMNS cc
group by table_name, owner , column_name ) con
on con.table_name = tc.table_name and
con.owner = tc.owner and
con.column_name = tc.column_name
where tc.owner = 'USERNAME'
order by 1 ,2
There can be multiple constraints (or none) for each column. Because of that left join is used and listagg function to display all constraint in one column.
TABLE_NAME COLUMN_NAME DATA_TYPE NULLABLE DATA_DEFAULT CONS
AQ$_QUEUE_TABLES OBJNO NUMBER N AQ$_QUEUE_TABLES_PRIMARY,SYS_C001643
AQ$_QUEUE_TABLES SCHEMA VARCHAR2 N SYS_C001640
AQ$_QUEUE_TABLES SORT_COLS NUMBER N SYS_C001645
AQ$_QUEUE_TABLES TABLE_COMMENT VARCHAR2 Y
AQ$_QUEUE_TABLES TIMEZONE VARCHAR2 Y
You can still try to use this below snippet to get the requested details. Hope this helps.
Note : This is for indexed columns as I thought you might need this too
SELECT DISTINCT col.owner,
col.table_name,
col.DATA_TYPE,
Col.Column_Name,
DECODE(nullable,'Y','Yes','N','No') nullable,
high_value(col.table_name,col.column_name), -- This is own created function to deal with LONG datatype columns
Ind.Index_Name
FROM SYS.All_Tab_Cols col,
All_Ind_Columns ind
WHERE Col.Table_Name = Ind.Table_Name
AND Col.Column_Name = Ind.Column_Name(+)
AND Col.Table_Name = UPPER('<TABLE_NAME>')
AND Col.Owner = '<SCHEMA_NAME>';

How to get the Data Type of a Specific Column in H2 Database

I can get all column data by using the following query.
SHOW COLUMNS FROM table_name;
I want to get data of "column_name" only.
i tried the following query as well. but still didnt work.
SELECT data_type FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'table_name' AND COLUMN_NAME = 'column_name';
Your help is greatly appreciated.
Try this:
select type_name
from information_schema.columns
where table_name='Table_name' and column_name='columnName';
The right query for the latest H2 is:
show columns from table_name
You need to add a join with table INFORMATION_SCHEMA.TYPE_INFO , the correct query is:
SELECT i.type_name FROM INFORMATION_SCHEMA.COLUMNS c, INFORMATION_SCHEMA.TYPE_INFO i
WHERE c.data_type=i.data_type AND c.table_name = 'table_name' AND c.column_name = 'column_name';
For the sake of uniqueness you also want to extend selection by schema name:
SELECT TYPE_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE UPPER ( TABLE_SCHEMA ) = UPPER ( 'my_schemaname' )
AND UPPER ( TABLE_NAME ) = UPPER ( 'my_tablename' )
AND UPPER ( COLUMN_NAME ) = UPPER ( 'my_columnname' )

Comparing two tables for different columns PL SQL

I very new to PL SQL and I have encountered a problem. Hopefully its not too hard to solve and I'm just going about it all wrong.
My problem is this: I have two tables with a different amount of columns. I need to run a check to see what the different columns are and then add them to one of the tables.
For example:
Table 1 has 1 column called name.
Table 2 has 2 columns called name and id.
(name has the same data type in both tables)
In this case, I would need to run a script that will check table 1 and 2, see that table 1 is missing the 'id' column and then add it to table 1.
Is this possible?
so far I have this:
SELECT TABLE_NAME, COLUMN_NAME FROM user_tab_columns WHERE table_name = 'TEST_TBL' OR TABLE_NAME ='TEST_TBL1'
which returns the columns for both tables. I have looked everywhere on the internet with no luck at all. I have tried to do intersect and join but with no luck.
If anyone has any help or could point me in the right direction I would appreciate it so much!
To get the different columns
SELECT TABLE_NAME, COLUMN_NAME FROM user_tab_columns
where table_name = 'Table1' AND COLUMN_NAME NOT IN ( SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME='table2')
UNION
SELECT TABLE_NAME, COLUMN_NAME FROM user_tab_columns
where table_name = 'table2' AND COLUMN_NAME NOT IN ( SELECT COLUMN_NAME FROM USER_TAB_COLUMNS WHERE TABLE_NAME='table1');

Resources