I want to make an alias(as in bash) for the following MySQL query:
SHOW COLUMNS FROM table WHERE Field != 'col_name' AND Field != 'col_name';
I read something about views but it seems that I need a SELECT query to use them.
I want to type only something like: showcols in the MySQL prompt and in the background the above query to be executed, is that possible?
PS: I cannot use DESCRIBE because of the length of some enum fields in the table.
You can replace the show columns with a select from the information_schema database.
SELECT column_name FROM INFORMATION_SCHEMA.`columns`
WHERE column_name not IN ('col1','col2');
Now you can create a view based on this select:
CREATE VIEW as SELECT ......
Related
how we can check if a column exists on Table in MySQL without using Stored Procedure. MySQL v3.23 which won't support writing Store Procedure.
v3.23 ?? If You know the table name and column name then try describe tablename or show create tablename if you know only column name select * from information schema.columns where column_name = columnname. Show tables should show all tables then manually select column name from the listed tables.
But this version is so ancient I have no idea if any of these will work
Try this, counting the columns in your table using the information_schema.COLUMNS.
SELECT COUNT(*) FROM information_schema.`COLUMNS`
WHERE table_schema = 'your_database_name'
AND table_name='your_table_name'
AND column_name='your_column_name';
The INFORMATION_SCHEMA COLUMNS Tabletable provides information about columns in tables.
Link
In MySQL, how to change all columns names of all tables to remove the string "_euro" from columns names?
I just could find a way to search tables having some columns containing "_euro" in their names:
SELECT DISTINCT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE "%_euro"
AND TABLE_SCHEMA='my_database'
For example, for the column named price_total_euro I want to rename it as price_total
Create a script with the following SQL:
SELECT CONCAT("ALTER TABLE ",TABLE_SCHEMA,".",TABLE_NAME," RENAME COLUMN ",COLUMN_NAME," TO ",REPLACE(COLUMN_NAME,"_euro",""),"; ")
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE "%_euro"
output will be, multiple lines which look like this:
ALTER TABLE test.t1_euro RENAME COLUMN t1_euro TO t1;
Review the script, and execute it on your database (after making a backup....)
put your select into a stored procedure and make a cursor and loop on all and do the alter operation
you can find example for a procedure here : https://stackoverflow.com/questions/15786240/mysql-create-stored-procedure-syntax-with-delimiter#:~:text=Getting%20started%20with%20stored%20procedure%20syntax%20in%20MySQL%3A,Why%20didn%27t%20this%20work%3F%20...%20More%20items...%20
if your using python with MySQL Connector you could place the column headers into a list and then run it through a for loop?
for name in table_names:
if name.endswith("_euro"):
new_name = name.replace("_euro", "")
I want to search a single string or word from MYSQL database that contains more than 50 table.I do not know what is the column or table where this string may be located.is there any command or function that return table and column name that contain the desired string.please help
You can get a list of all the tables and the columns available in a database using the below query.
SELECT * FROM INFORMATION_SCHEMA.COLUMNS;
Using the values get from TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, you can get the matched table name and column name using the below query.
SELECT * FROM table_name WHERE column_name LIKE "%your_string%";
I've been using the following query to replace certain data:
UPDATE wx3_t1 SET umo = REPLACE(umo, 'stringbefore', 'string after');
wx3_t1 = Table
umo = Column
I am looking for a way to update across the entire database, without needing to put the table or the column into the query.
Something as simple as just REPLACE('stringbefore', 'string after')
I realize that doing it this way is really aggressive but that's fine.
You can create a simple script that query information_schema.COLUMNS to get list of all your columns by table:
select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME from information_schema.COLUMNS;
Then you have to iterate on the result to play your UPDATE query.
If MySQL were rationally designed, I would be able to select from a describe table operation's results.
But that doesn't seem to work. So how can I get a list of a table's field names?
Take a look at the INFORMATION_SCHEMA COLUMNS table.
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'YourTableName'
MySql DDL: "show fields in myTableName"
Edit - as requested from comment:
DDL (Data Definition Language):
In the MySql Administrator, open a Query Browser window and select your database. Then enter the command above. Results will provide:
Field (fieldname)
Type Null
(allowed, yes or no)
Key
Default (value)
Extra *(auto_increment, etc.)*
Executing this statement in .Net can result in a DataTable with all of these values. If you only require one of the fields, then you need to use the more explicit DDL described by #Joe Stefanelli.
Perhaps this
select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='my_table' and TABLE_SCHEMA='my_database';