MySQL Create table with procedure analyze() - mysql

Procedure Analyse() suggests the optimal field for my columns. I want to create a new table with optimal field types starting from a table that I already have. At this time I'm running
SELECT * FROM mytable PROCEDURE ANALYSE();
Then I copy the report and manually I write the create statement. Is there a way to do that automatically? Is it more efficient to alter a table with new field types or create a new empty table with optimal field types and re-import data?

In truth you would not want to blindly accept the data types returned by this Analysis as you would / could never be sure what data types were "suggested". This Procedure returns "Suggested" optimal data types that "May" help reduce data storage requirements. The return values also depend on the data in the table you're selecting and could possibly change each time you run this query on new data.
Read more here on dev.mysql
But if you wanted to try something, I would start by building a Procedure of my own that could pass the returned data types recommended into a dynamically created DDL statement that you would need to check for possible incorrect datatypes and then execute the resulting DDL. It might take a little working out in terms of your code but you really should read more on Procedure Analyze()

Related

MySQL - Invalid GIS data provided to function st_polygonfromtext

I have a table in mysql with geometry data in one of the columns. The datatype is text and I need to save it as Polygon geometry.
I have tried a few solutions, but keep running into Invalid GIS data provided to function st_polygonfromtext. error.
Here's some data to work with and an example:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=78ac63e16ccb5b1e4012c21809cba5ff
Table has 25k rows, there are likely some bad geometries in there. When I attempt to update on a subset of rows, it seems to successfully work, like it did in the fiddle example. It fails when I attempt to update all 25k rows.
Someone suggested using wrapping the statements around TRY and CATCH. Detecting faulty geometry WKT and returning the faulty record
I am not too familiar with using them in MySQL or stored procedures either.
I need a spatial index on the table to be able to use spatial functions and filter queries by location.
Plan A: Create a new table and try to convert as you INSERT IGNORE INTO that table from your existing table. I don't know if this will apply the "IGNORE" to conversion failures. Also, you would end up with the "good" values. What do you want to do about the "bad" values?
Plan B: Write a loop in application code -- read one row, convert the varchar value, check for errors.

Getting SHOW CREATE TABLE from a view, as if it were a table

I have access to a remote database, and I would like to dump the schema and data of several views onto my local machine and load this into my local database as tables in a quick and easy way.
I lack the user privileges to run CREATE TABLE AS (SELECT * FROM target_view), otherwise this would be trivial to solve. In other words, I want to retrieve and recreate the "composite" schema of target_view as if it were a table.
I do not want the output of SHOW CREATE VIEW, as this only shows a complex SELECT statement with joins to various tables on remote I have limited ability to access. And a problem I'm seeing in MySQL 8.x is when I run SHOW CREATE TABLE on the view, this command simply acts as an alias of SHOW CREATE VIEW (which is reasonable).
Frustratingly, I can run DESCRIBE and see the schema of these views as they were tables. I really just need to convert this information into a CREATE TABLE statement without actually being able to run CREATE TABLE.
In case it weren't obvious, the key is to avoid manual reconstruction of these views' tabular schemas (as they may change in the future). I also want to avoid the solution of reverse engineering a generic table construction of 20-30 generic VARCHAR or TEXT columns from a CSV dump.
I don't know of any way to display the metadata of a result set in CREATE TABLE syntax.
What I would do given your circumstance is first create on your local MySQL instance the base table and the view, then you can use the CREATE TABLE AS SELECT ... syntax to produce a concrete table to match the metadata of the view result set.

Automated Data Import Stored Procedure From an Excel File

I have this Excel file:
Based on this data, I want to create a stored procedure that will identify the correct meter, if it exists, and perform either an insert or update to the monthly data.
Here is the MonthlyData table:
I really have no idea where to get started on this. Sorry about the tables, I am new here and I cannot post pictures yet. Please copy the tables and paste it in Excel.
Thank you
It's probably easiest to create an SSIS package for this if you're going to do this repeatedly.
First, create two tables:
myDataRaw
myDataCleaned
With myDataRaw, you truncate the table and then upload the Excel file into that table using a data upload object.
Create the stored procedure to work with the raw data. I would truncate the myDataCleaned table and then do a INSERT ... SELECT to it, making the WHERE clause specific to finding the account meters that you're looking for. If there are a lot, you can create another table to hold the specific account meters you want to import and use it in your WHERE clause.
I hope that helps get you started.
Have you considered using MERGE Query? I have no idea what 'meter' in this context mean, but if its something that can be checked in database itself, then MERGE query will be the best solution to your problem.
http://www.jooq.org/doc/2.6/manual/sql-building/sql-statements/merge-statement/

How do I store and iterate over a result set in a MySQL Stored Proc?

I'm relatively new to MySQL stored procs, so I was hoping someone could help me out here. I want to call my stored proc (maybe pass in one IN param) and have it do the following:
SELECT some data
Iterate over the records
Perform some operations on a few of the fields in each record including some INSERTs in other tables based on the data it finds.
My problem is that I don't know how to store the SELECT data set and iterate the records. I know how to declare and set stuff like int and text, but not full data sets. How do I do this?
Thanks
Look into MySql Cursors
http://dev.mysql.com/doc/refman/5.0/en/cursors.html

SELECT-ing data from stored procedures

I have a complicated SELECT query that filters on a time range, and I want this time range (start and end dates) to be specifiable using user-supplied parameters. So I can use a stored procedure to do this, and the return is a multiple-row result set. The problem I'm having is how to deal with this result set afterwards. I can't do something like:
SELECT * FROM (CALL stored_procedure(start_time, end_time))
even though the stored procedure is just a SELECT that takes parameters. Server-side prepared statement also don't work (and they're not persistent either).
Some suggest using temporary tables; the reason that's not an ideal solution is that 1) I don't want to specify the table schema and it seems that you have to, and 2) the lifetime of the temporary table would only be limited to a invocation of the query, it doesn't need to persist beyond that.
So to recap, I want something like a persistent prepared statement server-side, whose return is a result set that MySQL can manipulate as if it was a subquery. Any ideas? Thanks.
By the way, I'm using MySQL 5.0. I know it's a pretty old version, but this feature doesn't seem to exist in any more recent version. I'm not sure whether SELECT-ing from a stored procedure is possible in other SQL engines; switching is not an option at the moment, but I'd like to know whether it's possible anyway, in case we decide to switch in the future.
Selecting from functions is possible in other engines. For instance, Oracle allows you to write a function that returns a table of user defined type. You can define result sets in the function, fill them by using queries or even using a combination of selects and code. Eventually, the result set can be returned from the function, and you can continue to query on that by using:
select * from table(FunctionToBeCalls(parameters));
The only disadvantage, is that this result set is not indexed, so it might be slow if the function is used within a complex query.
In MySQL nothing like this is possible. There is no way to use a result set from a procedure directly in a select query. You can return single values from a function and you can use OUT or INOUT parameters to you procedure to return values from.
But entire result sets is not possible. Filling a temporary table within you procedure is the closest you will get.