the sql statement used to create a table? - mysql

what is the query that outputs the sql statement used to create a table?

You should check it in the output window of the mySQL workbench. It shows the query you executed along with many other useful information like the success or failure result of the query execution, time taken, affected row count etc. I've marked the last query I executed in red box.

Related

How to execute multiple SQL queries at same time

I want to know If we can execute multiple queries at the same time in MYSQL/SQL. Let me explain a case scenario to elaborate my statement further.
Let's Assume, We have to create and load two tables.create table tbl1(col,col,col,col...); Insert Into tbl1 (val,val,val,val...) and other query as create table tbl2(col,col,col,col...); Insert Into tbl2 (val,val,val,val...). Now, When I execute the statement the flow will be
Create Table1
Insert Into Table1
Create Table2
Insert Into Table2
Is there any method We can use to minimize these 4 steps into a single step? Similar to the functionality of threads that run in parrallel.
You can use two different instances of SSMS or may be different tabs within SSMS.
Other solution to run 2 queries at the same time with a maintenance plan. Here is the link for more details
You can chain multiple queries by using the ";", see here for further details: How to run multiple SQL queries?.
In your setup, 1. needs to be executed before 2. (same is true for 3. before 4.) because you cannot insert data into a database that did not exist. So running these 4 queries in parallel is not possible. However, running 1+2 and 3+4 in parallel is possible.

MySQL Query not giving expected result

In my XAMPP server's database, table named report exist which consist several column with there fixed set of values (Ok, Not Ok, Done, Not Done, Not Available). For some column like, column 'battery' , when I fire query in XAMPP sql as "SELECT * FROM Report WHERE battery='yes'"; it returns 0 set of result(no records) even when there are several records in which battery value is 'yes'. It's also not working for selecting value 'no';
I think my query is correct but the output is wrong. Please help with solution for this. Also I am not making any spelling mistake related to database, table and column name.
SELECT * FROM report WHERE battery='yes';
SELECT * FROM report WHERE battery='no';
output : MySQL returned an empty result set (i.e. zero rows). (Query took 0.0044 seconds.)

Update query does not return any values

When I try to create a simple update query with Subtraction or anything else in the same table, it doesn't return me any value, could it be something to do with formating?
This is SQL code: UPDATE Sales SET Sales.GrossProfit = [SubTot]-[Cost];
It doesn't return any value.
I have copied syntax from different databases (from colleagues) where the same type of query worked. Could it be something to do with my general Access settings?
UPDATE Sales SET Sales.GrossProfit = [SubTot]-[Cost];
I expect the query to update the GrossProfit column with the calculation "SubTot - Cost"
Strictly speaking, an UPDATE query does not "return" any columns. It only changes data in existing rows, but by itself does not return those changes back to you. (It is more accurately called an Update statement since it does not return data.)
For instance, if you were to execute the UPDATE statement in VBA code, you would not get a RecordSet of columns like with a SELECT query. To get the results after executing the UPDATE statement would require that you execute a separate SELECT query.
However, I assume that you are viewing the Update query in the Access query designer. Correct?
In that case, when you click on the View button (Datasheet View), Access tries to be smart & useful about the UPDATE and converts it temporarily into a SELECT query so that you can visualize the existing data in the columns. The Datasheet View does not actually run the UPDATE statement, nor does it show a preview of what will happen when you do run it.
While in Design View or SQL View for an Update query (or for Delete, Make Table, and Append queries), there is a Run button with a red exclamation point (on the Design ribbon/toolbar). Clicking that button will actually execute the query that changes the table values. Although it will not show you results row by row, you should get a pop-up telling you how many rows were changed due to the statement. (You may also get confirmation prompts that the statement is about to change data in the table.)

Select Into/Insert Into SQL Server query duplicates

Sorry for asking this question, but I am a beginner in SQL, my colleague at work build a view, which I need as datasource for a report, however since this view is based on several other views it takes like 45 minutes to execute the query. This is way to long. Therefore I created a table from that view, initial execution time is the same, but once in place it executes in seconds.
In Microsoft SQL Server 2014 I used the following query:
select *
into [dbo].[MAT_v_demnew_daily_am_all_data]
from [dbo].[v_demnew_daily_am]
This works fine, but since the view is updated daily I also need to refresh the table everyday. When I now execute the above mentioned query I get the message that the table already exists.
That's why I tried to use 'insert' in this case:
insert into [dbo].[MAT_v_demnew_daily_am_all_data]
select *
from [dbo].[v_demnew_daily_am]
Here I have the problem that it not only inserts the additional data but also the already existing data, so in the end I have duplicates.
As a workaround I now manually delete the [dbo].MAT_v_demnew_daily_am_all_data] table and then execute the select * into query.
Now I am looking for an easier solution, is it possible to having the table deleted by query and in the same query create a new one by select * into or is it possible to only insert new data from the view to the table so that I don't get duplicates.
Moreover, is it possible to have such SQL statement being executed automatically on a daily basis, maybe by .bat file and windows task scheduler?
I know that the source of all problems is the View and that we should improve that, but looking for a short term solution first.
Thanks so much.
Mathias
Try this:
IF OBJECT_ID('dbo.MAT_v_demnew_daily_am_all_data', 'U') IS NOT NULL
DROP TABLE dbo.MAT_v_demnew_daily_am_all_data
SELECT INTO dbo.MAT_v_demnew_daily_am_all_data FROM dbo.v_demnew_daily_am
This query is reusable on a daily basis.
You can create one stored procedure including this query.
Then you only need to execute the stored procedure.
Updated
Before you create the stored procedure, please check if you have the permission.
Then try:
create procedure [procedure_name]
as
IF OBJECT_ID('dbo.MAT_v_demnew_daily_am_all_data', 'U') IS NOT NULL
DROP TABLE dbo.MAT_v_demnew_daily_am_all_data
SELECT INTO dbo.MAT_v_demnew_daily_am_all_data FROM dbo.v_demnew_daily_am;
After you create it:
EXEC [procedure_name];

Unable to do an INSERT INTO using a SELECT where the SELECT is `information_schema`.`COLUMNS`

This one really stumps me. I'm trying to create a MySQL query that I can populate information into a columns table to then export into SQLite. I can populate just about anything I want, except when the SELECT statement comes from information_schema.COLUMNS.
Here's a simple example to test. I created a table called TestTbl in a schema called and gave it 2 rows. The first is an AUTO INCREMENT INT id row, and the 2nd is a varchar 100 row called Namedb. I then run the following query.
INSERT INTO `tblinfoetc`.`testtbl` (Namedb)
SELECT
`COLUMNS`.`TABLE_SCHEMA` AS `Namedb`
FROM
`information_schema`.`COLUMNS`;
My message after doing this is:
Affected rows: 0
Time: 0.000s
If I run this without the INSERT INTO line, it returns the records without a hitch. Any ideas?
This is more of a workaround suggestion than a real solution:
Selecting from information_schema.columns works fine, so try doing the operation in two steps. First, copy the data using a client interface then insert it into the destination table from the client.
I have encountered the same problem on a windows machine running mysql 5.6.13. The query used to work with mysql 5.5. It might be related to this other issue