Add new field to multiple table at once? - mysql

I want to add same field to multiple tables. Can I do this with single query in MySql?

I don't think you can do this with a single query, as it requires the ALTER TABLE syntax which only supports one table per query (although you could add/modify multiple columns in the same table).
You have to do one ALTER TABLE query per table to be modified.

Related

Compare a list of records to a table in database and list their timestamp

I've a list of records (in excel) which I want to lookup in SQL table to find their entry date in table.
For example I've name of 200 customers in an excel sheet which are also in my SQL table but there are many others as well. I want to compare those users in table and find their date of joining the table i.e the date they were added to the table.
Do you have any column such as "customer_id"? If not, create it in the tables as it will make it easier for you to select and join tables. You can use the alter table statement to add the new column. After adding the columns, join the tables and use the select statement to find the required record.
You must have a key to bind those data. Any other way can be dangerous linking these informations. Maybe, the way is adapt your application or excel to provide this bind between the data.

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

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.

mySQL: duplicating multiple records via temporary table, how to preserve autoincrement index?

I wish to duplicate a selection of records in a mySQL table.
The pk of the table is an autoincremented int.
I want to do this with one set of mysql queries (for performance reasons).
It seems like the fastest way to do this is to put the results of the selection into a temporary table,
make any changes needed, and reinsert the records back to the original table, like this:
CREATE TEMPORARY TABLE temp1234 ENGINE=MEMORY SELECT * FROM a_table WHERE column='my selection';
# do updates in temp1234; (altering FK's mainly)
INSERT INTO a_table SELECT * FROM temp1234;
But when I try to do this i get an error for duplicate PKs.
Now, I realise that I could alter the INSERT with SELECT query to exclude the pk/ID column, but as I am proceduraly generating these queries across multiple tables for a large data copying function, i want to avoid having to supply column names.
What is the best way around this problem?

Select count from each table from a list stored on a table

I have a table tbls with a field name containing names of tables.
I'm trying to form a statement to get the number of rows of each of this tables.
Should I use a stored procedure or is there a simpler way to do it?
There is a thread that talks about how you can Get record counts for all tables in MySQL database. Using the result, you can constraint the result set to tables specified in the tbls table.

phpMyAdmin wants to add multiple indices as one multi-column index

I'm creating tables using phpMyAdmin and want to define two different columns as indices. I'm not trying to create a multi-column index but phpMyAdmin creates them as such. Are there any possible issues with that? The fields don't relate to each other directly and both fields will not be used in WHERE clauses simultaneously.
Consider:
ALTER TABLE `documents` ADD INDEX (`offer_number`, `contract_number`);
And:
ALTER TABLE `documents` ADD INDEX (`offer_number`);
ALTER TABLE `documents` ADD INDEX (`contract_number`);
What's the difference?
MySQL can only make use of an index if the first column(s) of the index match the columns used in the query. In other words, if you perform a query where an index on contract_number could be used, the composite index won't be used since contract_number is not the first column in that key. The composite index could be used for a query where offer_number is used, however.
http://dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html
http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html
Given what you say about these fields, they should not be a part of one multi column index.
If you want to create single column indexes on PhpMyAdmin, you need to create them one at a time.