Greeting to all smart people around here !!
I have faced a weird interview question regarding SQL.
Qn . If I have 100 tables in Database. I want to fetch common records from Each table.
For example, location is common field in 100 tables. I want to fetch location field from all the tables without mentioning each table name in my SQL query.
Is there any way to do it?
If any possibilities let me know...
get list of tables from db metadata, and then query with each:
Statement stmt = conn.createStatement();
ResultSet locationRs = null;
DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
locationRs = stmt.executeQuery("SELECT location from "+ rs.getString(3));
System.out.println(locationRs.getString(1));
}
In MSSQL Server you have INFORMATION_SCHEMA.COLUMNS table that contains the column names so you can use group by and having count some value you will get the column name after that you can use pivot to get the values of column names and carry on to it. You will get the ans.
For eg.
Select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS group BY COLUMN_NAME having count(COLUMN_NAME) > 2
By above query you will get the common column names
You can try this for any Number of Tables in a DB :
select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS group by COLUMN_NAME having count(COLUMN_NAME)=(select count(*) from INFORMATION_SCHEMA.TABLES)
My friend has found answer for my question..
To get common column from multiple tables,Use INFORMATION_SCHEMA.COLUMNS and common column name.
Query :
select *from information_schema.columns where column_name='column name'
Hope this will helpful !
I am assuming you already have connection and statemnt object's. Now try the below; it might work for you, if not make some adjustments with loops and conditions. Also, you need to have two ResultSet Objects ex: rs1 and rs2. DatabaseMetaData dbmd = con.getMetaData();
String table[] = {"TABLE"} `;
rs1 = dbmd.getTable(null, null, ".*" ,table);
while(rs1.next()){
String tableFrom = rs1.getString(3) ;
rs2 = dbmd.getColumns(null,null,tableFrom , ".*") ;
while(rs2.next()) {
String locColFrom = rs2.getString(3);
if(locColFrom .equalsIgnoreCase("location"))
stmt.executeQuery(select locColFrom from tableFrom ) ;
}
}
Here's an link to study [Oracle] (http://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getTables(java.lang.String,%20java.lang.String,%20java.lang.String,%20java.lang.String[]))
Related
I have a query using various joins, and I just need the list of columns which are returned by this query. I done it in java, by asking only one row with rownum=1 and getting column name for value.The problem is if there is no data returned by that query.
For ex.
select * from something
and if there is any data returning by this query then it will return col1,col2,col3.
But if there is no data returned by this query, then it will throw error.
What I need is
Is there any way that I can run
desc (select * from something)
or similar to get list of columns returned by query.
It can be in sql or JAVA. Both methods are acceptable.
In my application, user passes the query, and I can add wrapper to the query but I cant modify it totally.
The flow of application is
query from user -> execute by java and get one row -> return list of columns in the result set.
you can use ResultSetMetaData of resultset
for example :
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();
int countOfColumns = rsmd.getColumnCount();
for(int i = 1; i <= countOfColumns ; i++ )
System.out.println(rsmd.getColumnName(i));
you could maybe convert your query to a view, you can then see the columns in the view by querying user_tab_columns
select * from user_tab_columns
The Oracle equivalent for information_schema.COLUMNS is USER_TAB_COLS for tables owned by the current user, ALL_TAB_COLS or DBA_TAB_COLS for tables owned by all users.
Tablespace is not equivalent to a schema, neither do you have to provide the tablespace name.
Providing the schema/username would be of use if you want to query ALL_TAB_COLS or DBA_TAB_COLS for columns OF tables owned by a specific user. in your case, I'd imagine the query would look something like:
String sqlStr= "
SELECT column_name
FROM all_tab_cols
WHERE table_name = 'users'
AND owner = ' || +_db+ || '
AND column_name NOT IN ( 'password', 'version', 'id' )
"
Note that with this approach, you risk SQL injection.
actually i have googled a bit and i need corresponding SELECT command to following PostgreSQL shell command :
\dt schemaname.*
i managed to get all databases with following code :
Statement statement = (Statement) connection.createStatement();
ResultSet rs = statement
.executeQuery("SELECT datname FROM pg_database");
while (rs.next()) {
System.out.println("DB Name : " + rs.getString(1));
//i need another while here to list tables
//inside the selected database
}
i tried following statement, but no luck :
statement.executeQuery("SELECT table_schema,table_name FROM "
+ rs.getString(1)
+ " ORDER BY table_schema,table_name");
this is the error i am getting :
org.postgresql.util.PSQLException: ERROR: relation "template1" does not exist
Position: 37
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254)
at com.isiran.rayten.rg.db.bare.wrapper.PGWrap.main(PGWrap.java:64)
If you use psql -E, it'll echo the actual queries that get run when you type commands such as \dt:
denis=# \dt public.*
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','s','')
AND n.nspname !~ '^pg_toast'
AND n.nspname ~ '^(public)$'
ORDER BY 1,2;
**************************
These queries can then be simplified or amended per your specific use-case.
Use the DatabaseMetaData object to query information, eg getTables(...):
DatabaseMetaData dbmd = connection.getMetaData();
try (ResultSet tables = dbmd.getTables(null, null, "%", new String[] { "TABLE" })) {
while (tables.next()) {
System.out.println(tables.getString("TABLE_NAME"));
}
}
This will return all tables in the database, you may need to specify values for catalog and/or schemaPattern to get a more specific result.
To list all tables from database you have to read table pg_catalog.pg_tables
But unfortunately you have to be logged-in in database.
So in place where you wrote comments
//i need another while here to list tables
//inside the selected database
Before loop for tables you need to log-in in this database.
I use oracle 11g with hibernate. Hibernate is configured to validate schema on startup, however for some reason it can not find one table and fails.
I have a strange situation where java.sql.DatabaseMetaData does not see one table (getTables returns empty result set) when it sees other table from the same schema.
So I have following situation:
String[] TYPES = {"TABLE", "VIEW"};
DatabaseMetaData meta = ...
meta.getTables(null, "SCHEMA_NAME", "TABLE1", TYPES).next(); // true
meta.getTables(null, "SCHEMA_NAME", "TABLE2", TYPES).next(); // false
But following queries are successfully executed:
select * from schemaName.table1
select * from schemaName.table2
More I can query a dictionary table and see both these tables:
select * from user_tables where table_name in ('TABLE1', 'TABLE2')
What could be a reason?
DatabaseMetaData md = connection.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
System.out.println(rs.getString(3));
}
try the above code
The column 3 represents the Table_name
Click to know all the column numbers
query = "SELECT * FROM POST_COMMENT WHERE Post_date_time= ? AND Post_User= !;";
query = query.replace("?", "'"+post.getDatetime()+"'");
query = query.replace("!", Integer.toString(post.getPublisher().getID()));
PreparedStatement pstm = con.prepareStatement(query);
ResultSet rs = pstm.executeQuery();
The resulting query looks like this
SELECT * FROM POST_COMMENT WHERE Post_date_time= '2013-04-12 07:20:34.0' AND Post_User= 378;
Which works right in the MySQL prompt line but, when launched from prepared statement throws this error:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '07:20:34.0 AND Post_User=378' at line 1
But
pstm.setString(1, post.getDatetime());
pstm.setString(2, Integer.toString(post.getPublisher().getID()));
isn't working either.
Table definition
CREATE TABLE Post_Comment (
Comment_ID INTEGER(7) NOT NULL,
Post_date_time DATETIME NOT NULL,
Post_User INTEGER(7) NOT NULL,
PRIMARY KEY(Comment_ID, Post_date_time, Post_User),
CONSTRAINT Post_Comment_Post
FOREIGN KEY (Post_User, Post_date_time)
REFERENCES Post (User, date_time)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT Post_Comment_Comment
FOREIGN KEY (Comment_ID)
REFERENCES Commentary (ID)
ON DELETE CASCADE
ON UPDATE CASCADE
)
;
Please help.
Take a look at the Java tutorials. You're not supposed to replace() the fields using the String functions.
You're supposed to call the "set" methods of the PreparedStatement.
EDIT:
No, take a look at the available methods. And what types the database columns expect. If you want a date, use setDate(), if you want an Integer, then use setInt(). Example:
query = "SELECT * FROM POST_COMMENT WHERE Post_date_time = ? AND Post_User = ?;";
PreparedStatement pstm = con.prepareStatement(query);
pstm.setDate(1, post.getDatetime());
pstm.setInt(2, post.getPublisher().getID());
ResultSet rs = pstm.executeQuery();
Also note that the query uses ? for both placeholders, not !.
query = "SELECT * FROM POST_COMMENT WHERE Post_date_time= ? AND Post_User= ?;";
PreparedStatement pstm = con.prepareStatement(query);
pstm.setObject(1, post.getDatetime());
pstm.setObject(2, post.getPublisher().getID());
ResultSet rs = pstm.executeQuery();
i have two tables "Table1" with columns user_name,Password and course ID and another table "course" with columns course_id,course_name.I have used the following code to display the course ID from Table1 according to the user_name received from the login page.using ResultSet rs1.now i want to retrieve the course_name from the table "course" according to the course ID receieve from "Table1".for that in the second query pstmt2.setString(1, ); what parameter i should use to get the course_id value from the previous query
HttpSession sess=request.getSession();
String a=(String)sess.getAttribute("user");
String b=(String)sess.getAttribute("pass");
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:ggg");
Statement st = con.createStatement();
String query="select * from Table1 where user_name=?";
PreparedStatement pstmt=con.prepareStatement(query);
pstmt.setString(1,a);
ResultSet rs1=pstmt.executeQuery();
while(rs1.next())
out.println("<h3>COURSE ID: "+rs1.getString("course ID")+"<h3>");
String query2="SELECT * from course where course_id=?";
PreparedStatement pstmt2=con.prepareStatement(query2);
pstmt2.setString(1,);
ResultSet rs2=pstmt2.executeQuery();
while(rs2.next())
{
out.println("<h3>course name: "+rs2.getString("course_name")+"<h3>");
}
why do you go for two turns of database hit, even though you created one time connection object.
modify the query as below
SELECT * from course where course_id = (select course_id from Table1 where user_name=?);
from this query you noneed to give input of courseid also.
No need to hit database twice to get the results that you need. use the query
Select table1.course_id, course.course_name from table1, course where table1.course_id=course_id and table1.user_name=?
This should set the course_id parameter:
pstmt2.setString(1,rs1.getString("course_id"));
Or, as I see the "course_id" column may have a different name in "Table1":
pstmt2.setString(1,rs1.getString("course ID"));
As the other post mentioned there's no need to go to another set of query. Try this example query:
SELECT course.course_id, course.course_name
FROM table1 t1
INNER JOIN course c
ON t1.course_id = c.course_id
WHERE t1.user_name = ?;
Now if you insist your coding the parameter o your pstmt2.setString(1,); is:
pstmt2.setString(1,rs1.getString("course_id")); //or course ID defending on your column name