MySQL Search Within Database (Table Search) - mysql

Alright, I'm trying to write a query to display all the tables that contain a certain prefix. Something like what is displayed below (but is obviously incorrect)
SELECT TABLES LIKE chat_
So any table that has the chat prefix, would be displayed. I plan on formatting the output, so it's not going to be a raw output, and I also understand that "what idiot would display table names publicly", and security measures are being taken to prevent that "accidental" table drop (just trying to avoid a flame war). So, how is this accomplished?

You can also use regular expressions, which allows a little more flexibility (though a performance cost):
SHOW TABLES WHERE tables_in_db REGEXP 'chat.*';
In this example, replace db with the database name of concern.

SHOW TABLES LIKE 'chat_%';

You need to add "in some_db first" before where like below
SHOW TABLES in test_server_service where 'table' regexp 't_*';

Related

Lookups inside tables.. still 'evil' if I use bound columns? What is less evil?

What if I have it where I (in the design mode for my table, in a particular field) use the combo box to lookup the ID and the value and then make the value my bound column?
I understand I'm losing the ability to see relationships that I've made in this way on the relationships database tool. Am I losing anything else?
In other articles I've researched, they talk about queries not working correctly or the database being too rigid/hard to change after the fact. I don't necessarily see those problems.(although I'm still learning Access! =) )
When I build a Query to test if it returns the ID or the value, it returns the value, so what's the real problem here?
If I understand you correctly, you have a domain table that looks something like this:
DomainTable
-----------
DomainID
DomainValue
The rows of your table might look something like this:
1 Male
2 Female
3 Other
And you have another table where you want to include either the DomainID or the DomainValue.
Generally, you would use the DomainID. This allows you to make a change to the domain value without having to make the change in hundreds of places.
On the other hand, if you want changes to the domain values to not be reflected in your other tables, you would use the DomainValue. This is important when creating a log file, as one example.

How to search a Mysql table which contains 150+ columns?

I know this is a very poor database design,but still i want to know is there any way to search a keyword in all the columns in that table through Mysql query(not Php)?
I've searched in internet but didn't find any help regarding this? if nothing such possibilty is there,then i think dividing the table is only the option left
You cannot use wildcards and anything like that on column names. Either select all columns with *or name the ones you need.
You could use dynamic SQL to patch your query together but this will probably only give you more problems handling it.

MySql - Select * from 2 tables, but Prefix Table Names in the Resultset?

I'd like to select * from 2 tables, but have each table's column name be prefixed with a string, to avoid duplicate column name collissions.
For example, I'd like to have a view like so:
CREATE VIEW view_user_info as (
SELECT
u.*,
ux.*
FROM
user u,
user_ex ux
);
where the results all had each column prefixed with the name of the table:
e.g.
user_ID
user_EMAIL
user_ex_ID
user_ex_TITLE
user_ex_SIN
etc.
I've put a sql fiddle here that has the concept, but not the correct syntax of course (if it's even possible).
I'm using MySql, but would welcome generic solutions if they exist!
EDIT: I am aware that I could alias each of the fields, as mentioned in one of the comments. That's what I'm currently doing, but I find at the start of a project I keep having to sync up my tables and views as they change. I like the views to have everything in them from each table, and then I manually select out what I need. Kind of a lazy approach, but this would allow me to iterate quicker, and only optimize when it's needed.
I find at the start of a project I keep having to sync up my tables and views as they change.
Since the thing you're trying to do is not really supported by standard SQL, and you keep modifying database structures in development, I wonder if your best approach would be to write a little script that recreates that SELECT statement for you. Maybe wrap it in a method call in the development language of your choice?
Essentially you'd need to query INFORMATION_SCHEMA for the tables and columns of interest, probably via a join, and write the results out in SQL style.
Then just run the script every time you make database structural changes that are important to you, and watch your code magically keep up.

MYSQL / SQL 'Exclude' or 'Except'

I want to query everything, Like:
SELECT * FROM
but I want to exclude two columns because their not necessary, but there's too many columns I need to just type it all one by one. Is there an exclude keyword or except keyword or something in SQL or MYSQL?
No. It's better practice to type out all of the fields as opposed to SELECT * FROM ... anyway.
If you're going to be a programmer, you may as well get used to typing :)
Yes, you must type them all out.
Or, if there really are that many you can do it programatically.
SHOW COLUMNS FROM `tablename`
Will give you the list of columns. Use whatever language you are using to pull them out and build the query with them.
Of course, you have to make sure the column names are escaped (what if you have a column name called select?).

mysql don't return results if not from statement but from INDEX table or something

I think my question was a little confusing.....It confused me :)
Working on a media site as a take-over project and it has a custom CMS. The client wants the ability to activate/deactivate media....sort of like Wordpress's publish/unpublish feature.
Instead of digging through all the code looking for mysql queries (which I'm not opposed to), I was wondering if you can add a sort of INDEX to a table that won't let it return result rows if that rows "active" column = let's say 0.
Just trying to be lazy and learn something at the same time, heh.
I don't need examples of queries to make it happen, btw.
What you describe is called a "view". Here is a page describing how to create them in MySQL: http://dev.mysql.com/doc/refman/5.0/en/create-view.html. However, in most cases you will still have to alter your code to use the view instead of the table.
You can consider create a view (which contains active record only)
AND swap the view name to actual table name instead, so you can achieve the negative filtering without changing any of your source code.