how to create a new table from `describe` statement in sql? - mysql

I want to create a new table with rows of the column names. Essentially, I want to put the output of a decribe statement into a new table, so that I can use a where clause to only extract certain column names. I'm using sparksql.
How do I do this? Thanks.

No need to create an additional table just use either SHOW COLUMNS command instead of describe with filter, or query information_schema.columns table with a select.

Related

Create a table and specify data types according to an existing query

Let's say I have a table like this
this table is the result of a query from another larger table stored in my database
All I want is to create a table like this one above and specify for each column a custom format and store it into my database
I know that I could do create table mytab as select ... etc
however i don't know how to specify the column formats that I want in mysql
could you please help ?
If you have the query sql, you should be able to do a select into to store the results in a table. Add a LIMIT clause to just store one row. You could then do SHOW CREATE TABLE tablename (from this SO answer) to get the SQL for creating the table. It would be up to you to figure out what your primary key should be.
Assuming with column formats you mean data types: Use CAST to cast to the desired data type.
create new_table as
select
cast( a.metrique as varchar(100) ) as metrique,
cast( b.nombre_de_lignes as int ) as cote_de_lignes, ...
from ...
You may specify columns properties completely or partially. Like there is no SELECT part, and you simply create empty table.
I.e. like
CREATE TABLE table_name ({any definitions allowed in table creation query:
columns specifications, indices, constraints, FKs, etc.})
SELECT ...
In this form each output column in SELECT must have alias which matches according column name defined in CREATE TABLE part. If alias is absent in the structure then a column with the name==alias will be added to the table definition with dynamically formed properties.

Replace column names, table names and other structures using a query

I want to search the structure of my MySQL db and replace structures that contain or equal "xyz*"... column names and table names are the main thing, but I'd like to search all structures.
Then, I want to replace "xyz*" with "abcdefg".
I doubt one can do this with one query. What you can do is create ALTER statements. I would write a small program (in the language of your choice) and
first I would get the table names like this:
select table_name from information_schema.tables;
The result set has to be checked in a loop, if there is a table named "xyz*" then create an ALTER statement:
RENAME TABLE tbl_name TO new_tbl_name
For the colums, you would have to get each Table again, then all colums for each table:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tbl_name';
the again a loop to change the columns name, this time using ALTER TABLE.
see also:
https://dev.mysql.com/doc/refman/5.1/en/alter-table.html
https://dev.mysql.com/doc/refman/5.0/en/rename-table.html

how to alter multiple tables using regex

I want to alter multiple tables using regex,
ALTER tablename DROP INDEX feed
the following are the table names
wp_1_ai1ec_event_feeds
wp_12_ai1ec_event_feeds
wp_14_ai1ec_event_feeds
wp_19_ai1ec_event_feeds
and so on..
I tried using regex in place of tablename
alter wp_[0-9]+_ai1ec_event_feeds drop index feed
but I get syntax error. How to achieve this?
You can fetch the table names using SHOW TABLES, then do the ALTER queries for each of these tables.
To fetch the table names, you can use the simple LIKE matching:
SHOW TABLES LIKE 'wp\_%\_ai1ec\_event\_feeds';
This will work as long as there are no tables like wp_letters_ai1ec_event_feeds.

INSERT ... SELECT to same table

I have a table that has a number of columns. For each row, I'd like to select three columns (PAR_BOOK, PAR_PAGE, PAR_LINE) and concatenate the contents of those three columns into a new fourth column (APN).
So, if PAR_BOOK=0108, PAR_PAGE=291 and PAR_LINE=07, APN should be 010829107
Make sense?
But, I'm unsure of what query I should use to do this. I need the results stored back in the same table as it needs to be ultimately exported out as a csv to work with the program that's going to map the data.
Assuming your fourth column is already in the table, you would use the following update query:
UPDATE YourTable
SET APN = CONCAT(PAR_BOOK, PAR_PAGE, PAR_LINE)
If your fourth column is not present in the table yet, you should use the ALTER TABLE statement to add it first before running the UPDATE statement:
ALTER TABLE YourTable
ADD APN VARCHAR(256) NULL
Inserting into the same table with INSERT INTO ... SELECT ... is no problem at all. MySQL holds the selected rows in a temporary table.

List of temporary table columns (MySQL)

I need to get list columns of some temporary table (MyISAM) in MySQL in view like number column - name column.
I need to know number of column with specific name. In advance, I can't know what is the number of column - I'm using dynamic sql with some variables to create temporary table.
I can not use show columns... because I can't work with results of this function. I can't use INFORMATION_SCHEMA.COLUMNS because it does not contains columns of temporary table.
Some ideas?
Thanks in advance!