Find table from querying column name in oracle SQL developer - php

I want to query a database for a column name to find the table without knowing the table name (there are a huge number of tables)
I am working with a huge database at the moment and trying to find where, based on CFML code and the URL of the problematic page, a website is retrieving information from. I am new to ColdFusion however I can see that the URL contains a few ID references with column names. Some I have been able to find easily and others, I have no idea as the column names are quite vague e.g. 'NTASKID'.
My plan is to try to query the database using the column name but Google has presented me queries which require knowing the table name which I do not have. I guess those examples may have been more useful for denormalised tables(?).
If someone knows such a query it would be great. Additionally, could someone please comment on whether I am going down the right avenue of investigation here?

The query that worked was
SELECT * FROM ALL_TAB_COLUMNS
WHERE COLUMN_NAME LIKE '%TASK%'
AND owner = 'database_name';
It was the 'owner' part I did not previously know about. Everyone who answered certainly opened avenues for further Googling.
Cheers

If you need to find table name then 'juergen d' answered your question. If you nee to find column names then use user_tab_columns/all_tab_columns to select column names where table='YOUR_TABLE' - must be in upper case as well as column names.

The IDE itself has a feature for this, View > Find DB Obect
here's a 20 second video showing it, in action

Related

MySQL 5.7.9 fulltext research in multiple tables with Sphinx on WAMP

I I have to do a research in multiple MySQL tables for my internship.
In fact, I do this for a web phone directory. I have a form text input to enter the research.
I tried to use the MATCH/AGAINST syntax but it appears to be wrong.
My query is actually that one :
SELECT U_ID, 'users'
from users
where match ([columns that I want to search in]) AGAINST ([The text inside my search field])
UNION
SELECT S_ID, 'service'
from service
where match ([columns that I want to search in]) AGAINST ([The text inside my search field])
This problem is the following : With this type of search, I must send the variable in many MATCH so I can't have a relevant result (because of the multiple against elements). The perfect solution could be to replace the 'UNION' by an 'INTER' but It would be too easy.
I don't know if it will be usefull, but I use PDO to send my query with PhP
I tried to search solutions but I couldn't find one for me :
https://dev.mysql.com/doc/refman/5.5/en/fulltext-search.html
Using Full-Text Search in SQL Server 2008 across multiple tables, columns
MySQL fulltext search on multiple tables with different fields
Then I tried to use Sphinx but the documentation is complicated to me and I couldn't understand it (http://sphinxsearch.com/docs/current.html).
Can someone help me to find the query that I need or can you give me a link of a very clear and simple Sphinx tutorial on Windows (I have read the IBM one) ?
Edit :
I wanted to illustrate my problem with the set theory (inter mean intersection).
For example, when I type "John accounting department" in my form input, I want to display all users named John, but only if they belong to the accounting department.
With my actual search, I will have the id of all the departments named : "John" or "accounting" or "department" and all the id for the people named "John" or accounting" or department".
That's my actual problem.
I think a materialized view would be the easiest way to solve this in MySQL.
They not strictly a feature of mysql (ie mysql cant maintain the view automatically, its not really a view) its a normal table, that just happens to be a copy of other table(s) data.
Can actually be created quite easily...
CREATE TABLE my_search_view (fulltext(U_Name,S_Name))
SELECT U_ID, U_Name, S_ID, S_Name
FROM user
INNER JOIN service USING (S_ID);
(If you update the source tables, just delete the table, and recreate it! Although can use triggers if want a more dynamic solution, but depending size of data it possibly not worth the effort)
Now you can just run a simply query against this table (it has a full-text index)
SELECT *
FROM my_search_view
WHERE MATCH (U_Name,S_Name) AGAINST ('John accounting department')

PHP/MySQL join with CSV array

I have a tricky (well for me it is) question about getting info from 2 different tables.
I have an array, like this:
$abc=$_COOKIE['cookie'];
$comma_separated = implode(",", $abc);
Now, the array will be a CSV list of ID's that need to match ID's from the "Specials" table. In the "Specials" table there is a column called Contract Name. This Contract Name column needs to match a column of the same name, each unique, in the Products table, and display the information contained therein.
My currenty MySQL query looks like this:
$query= "SELECT specials.id,
specials.product_name,
specials.contract_name,
specials.included_value AS inc_val,
specials.image_url,
specials.contract_monthly,
specials.outlet,
products.package,
products.bundled,
products.included_value
FROM specials,
product
WHERE `id` IN ('.$comma_separated.')
AND specials.contract_name = `products.package`";
What happens is... that nothing happens. I've tried wrapping my brain around some of the JOIN tutorials but no luck.
So basically I'd like to display a list of current specials, along with the package info, which is contained in a different table. I've tried wrapping my brain around some of the JOIN tutorials but no luck.
To my knowledge I'm crap at explaining things properly, so please do shout if I can shed any more light on this conundrum.
Thanks! :)
To even start to get this to work, your $query string is going to have to be a valid query. Ordinarily, to troubleshoot this kind of problem, you do echo $query; somewhere along the way to see if it is valid. You then might even paste the query's textinto a standalone MySQL client (phpMyAdmin, or maybe HeidiSQL or something) to see what you get.
Looking at this line:
WHERE `id` IN ('.$comma_separated.') /* wrong! */
it looks like it needs to read
WHERE `id` IN (".$comma_separated.")
because you're using double quotes to surround your fragments of SQL text. Also, you might want to use
WHERE `specials`.`id` IN (".$comma_separated.")
just because some other tables might contain id columns and then your search clause will be ambiguous.

PostgreSQL Query and PHP Assistance

Directly under this small intro here you'll see the layout of the database tables that I'm working with and then you'll see the details of my question. Please provide as much guidance as possible. I am still learning PHP and SQL and I really do appreciate your help as I get the hang of this.
Table One ('bue') --
chp_cd
rgn_no
bgu_cd
work_state
Table Two ('chapterassociation') --
chp_cd
rgn_no
bgu_cd
work_state
Database Type: PostgreSQL
I'm trying to do the following with these two tables, and I think it's a JOIN that I have to do but I'm not all that familiar with it and I'm trying to learn. I've created a query thus far to select a set of data from these tables so that the query isn't run on the entire database. Now with the data selected, I'm trying to do the following...
First and foremost, 'work_state' of table one ('bue') should be checked against 'work_state' of table two ('chapterassociation'). Once a match is found, 'bgu_cd' of table one ('bue') should be matched against 'bgu_cd' of table two ('chapterassociation'). When both matches are found, it will always point to a unique row within the second table ('chapterassociation'). Using that unique row within the second table ('chapterassociation'), the values of 'rgn_no' and 'chp_cd' should be UPDATED within the first table ('bue') to match the values within the second table ('chapterassociation').
I know this is probably asking a lot, but if someone could help me to construct a query to do this, it'd be wonderful! I really do want to learn, as I don't wish to be ignorant to this forever. Though I'm not sure if I completely understand how the JOIN and comparison here would work.
If I'm correct, I'll have to put this into seperate queries which will then be in PHP. So for example, it'll probably be a few IF ELSE statements that end with the final result of the final query, which updates the values from table two to table one.
A JOIN will do both level of matching for you...
bue
INNER JOIN
chapterassociation
ON bue.work_state = chapterassociation.work_state
AND bue.bgu_cd = chapterassociation.bgu_cd
The actual algorithm is determined by PostreSQL. It could be a merge, use hashes, etc, and depends on indexes and other statistics about the data. But you don't need to worry about that directly, SQL abstracts that away for you.
Then you just need a mechanism to write the data from one table to the other...
UPDATE
bue
SET
rgn_no = chapterassociation.rgn_no,
chp_cd = chapterassociation.chp_cd
FROM
chapterassociation
WHERE bue.work_state = chapterassociation.work_state
AND bue.bgu_cd = chapterassociation.bgu_cd

Need help querying across two databases (first time trying to do this)

I have two databases I need to work with, db_site and db_forum (these are generic names, FYI).
db_site has a table called main-news, which has a forumurl field which holds a forum thread id and a views field which holds the current pageviews for the article entry in the database. db_forum has a table called forum_threads which has a tid field and a replies field.
I have two things I need to do, one using just the replies and another using the replies and the views. I assume once the former is figured out the latter won't be much more than adding some extra parts, so I'm concerned with the former for the time being.
Not sure how I should approach this since the two tables are in different databases. The login I'm using has access to both of them (AFAIK), so that isn't the problem, it's more of the syntax involved. Would what I'm looking to do be something like this, perhaps?
SELECT
db_forum.forum_threads.replies AS replies
FROM
`db_forum.forum_threads` AS f,
`db_site.main-news` AS s
WHERE
f.tid = s.forumurl
That's a rough guess, from what I can find online abut doing this type of query. Any help is appreciated. :)
First of all, you should indent your SQL code properly. That long line was almost unreadable.
SELECT
db_forum.forum_threads.replies AS replies
FROM
`db_forum.forum_threads` AS f,
`db_site.main-news` AS s
WHERE
f.tid = s.forumurl
Then, make use of your table aliases "f" and "s". You introduced them, so you have to use them:
SELECT
f.replies AS replies
FROM
`db_forum.forum_threads` AS f,
`db_site.main-news` AS s
WHERE
f.tid = s.forumurl
Finally, you should remove the unnecessary quoting:
SELECT
f.replies AS replies
FROM
db_forum.forum_threads AS f,
db_site.main-news AS s
WHERE
f.tid = s.forumurl
If the names of the fields are indicative of their function, then f.tid refers to an identity column while s.forumurl does not. Normally the s.formurl in this case would be a foreign key. Just a guess.

hiding model data based on id's existance in another table

I've got a somewhat complicated question for you cakephp experts.
Basically, I have created a db table called "locations". Every month I will get this table sent to me in csv format from a client. Unfortunately, instead of updating this table, I will have to empty it and reimport all of the records. Unfortunately, I cannot alter this table at all.
Functionality wise, users will have the ability to look at a display of these records, and be able to choose to hide certain ones. This "hidden" attribute must be persistent and survive the month to month purging of all records.
I had all of this working yesterday. What I did was, create a separate table called location_properties (columns were: id(int), location_id(foreign key), is_hidden(boolean)). When showing these records, it would simply check to see if "is_hidden==true".
This was all well and good(AND WORKING!), but then my boss kind of gummed up the works. He told me to delete the "is_hidden" column from the table because it would be more efficient. That I should be able to simply check for the existence of the location_id to hide or show it.
It doesn't appear to be quite that simple. Anyone know how I can pull this off? I've tried everything I can think of.
Your boss is wrong.
It's more efficient to add your column, than it is too delete and re-import the locations every month.
Did he say it was less efficient, or did you do an actual benchmark to see if its harms performance too much?
At first glance I see 2 solutions:
1) add a condition array('Location.id' => 'NOT NULL')
2) change join type to right join
I hope this helps

Categories