How to create dynamic table in mySql - mysql

I want to know how to create dynamic table in mySql . I have used dynamic table in Sqlserver 2008 but i am new to mySql . Is it possible ?
Eg: In Sql server i have created Dynamic Customer Table.
DECLARE #tblCustomer as table(
[ ] bit
,Sl# int
,custID int
,CustCode varchar(max)
,Customer nvarchar(max)
,Authorized bit
,RCount int)
SELECT * FROM #tblCustomer
Please Help

#sqlstmt = 'whatever sql';
Prepare st from #sqlstmt;
Execute #st;
Deallocate prepare #st;
Place the CREATE TABLE statement in #sqlstmt and you are good to go !
The table is a real one. You would have to drop the table afterwards.

Pretty easy to do:
CREATE TABLE AS
SELECT * FROM tblCustomer
It will take the existing field types from the schema where possible..

Related

Search Specific Range of Tables mySQL

I have a question that is plaguing me. I have a mysql database that is being used for historical data. Basically every year the system writes a new table in the database housing the time data.
so it is like this:
tables:
year_2017
year_2018
year_2019
and in each table there is data.
What I would like to do is build a query that will search through these specific tables.
Now there are other tables in there I don't want to search through. I only want the year_* tables.
Basically I would want SELECT * FROM year_*
I know you could do SELECT * FROM year_2017, year_2018, year_2019 but the problem is that every year there is going to be a new table so I would have to edit the query every year and I don't want to do that.
Any help would be great.
From the information_schema.tables, find the names of the tables that contain the year_ and then place the table names in a temporary table. Then fetch each table with a data loop.
CREATE PROCEDURE myproc()
BEGIN
CREATE TEMPORARY TABLE temp(
tablename varchar(100)
);
INSERT INTO temp
SELECT t.TABLE_NAME
FROM information_schema.tables t
WHERE t.TABLE_NAME LIKE '%year_%';
WHILE (Select Count(*) From temp) > 0 DO
SET #tablename= (Select tablename From temp limit 1);
SET #sql= concat("select * from ", #tablename);
prepare exec from #sql;
execute exec;
Delete From temp Where tablename=#tablename;
END WHILE;
END;
CALL myproc();
demo in db<>fiddle
You can use a variable to store the table name. But you'd need to come up with an automation to fill the table name variables.
mysql prepared statements
/* MYSQL */
SET #table = 't1';
SET #s = CONCAT('SELECT * FROM ', #table);
PREPARE stmt3 FROM #s;
EXECUTE stmt3;

MySQL create a dynamic table name in a Join statement

long time user, first time poster.
I have 2 tables;
a1_watchlists {id(PK),name,date}
a1_watchlist {id(PK),watchlists_id(FK(a1_watchlists.id)),company_name,asx_code,date}
I also have 2000 other tables that have been created with the name 'asx_'+[asx_code] (where asx_code is pulled from another table)
this table looks like;
asx_[asx_code] {date(PK),open,high,low,close,volume}
I want to select all from a1_watchlists and a1_watchlist and then select the latest date from the asx_[asx_code] table using the value from a1_watchlist.asx_code to generate the [asx_code] part of the table name.
The problem I have is that I want to use the value from a1_watchlist.asx_code as the table name prepending the string 'asx_' to this first.
Closest I have been able to get is;
DECLARE #TableName VARCHAR(100)
SELECT *
FROM a1_watchlist AS wl
JOIN a1_watchlists AS wls
ON wls.id = wl.watchlists_id
SET #TableName = 'asx_' + wl.asx_code
INNER JOIN (SELECT MAX(date),open,high,low,close,volume,amount_change,percent_change FROM #TableName)
This currently give the error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE #TableName VARCHAR(100)
SELECT *
FROM a1_watchlist AS wl
' at line 1
The expected colums I need in the final result would be:
wl.id,wl.watchlists_id,wl.company_name,wl.asx_code,asx_[asx_code].date,asx_[asx_code].open,asx_[asx_code].high,asx_[asx_code].low,asx_[asx_code].close,asx_[asx_code].volume
Let me know if you require more information.
I'm not going to speak to what to do in the case where you have 2000+ tables that start with asx+ some code... (i live in a town with multiple bridges) or even whether what you're doing is the best way to get where you want to go. BUT, it does look like you're attempting to concatenate things together and create a dynamic statement. If that sounds right, then I'd recommend you look into prepared statements. Like the following. Hope this helps.
DELIMITER $$
DROP PROCEDURE IF EXISTS prRetrieveAllFromTable$$
CREATE PROCEDURE prRetrieveAllFromTable(tableName VARCHAR(64))
BEGIN
SET #s = CONCAT('SELECT * FROM ',tableName );
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
CALL prRetrieveAllFromTable('calendar');
http://dev.mysql.com/doc/refman/5.5/en/sql-syntax-prepared-statements.html
How To have Dynamic SQL in MySQL Stored Procedure

How to create a table in mysql with a random name?

I have a procedure that update a table. However, I need to back up the table each time prior updating it. So the only way I can backup the table via procedure is by create a table and inserting all the information from one table to another.
So What I need to do is create a table name with a random value to distinguish the different tables. The ideal solution will be "New_Table_Name_TIMESTAMP" so append timestamp to a string.
My Question is how to create a table with a timestamp added to it's name
New_table_name_201412301044
I have tried the following
DECLARE new_table varchar(100) DEFAULT '';
SET new_table = CONCAT('WORKFLOW_BU_', client_id_to_update, '_', unix_timestamp() );
CREATE TABLE data_import.new_table LIKE development.inventory_engine;
INSERT INTO data_import.new_table
SELECT * FROM development.inventory_engine;
but it create a table name called "new_table" and not the variable
Thanks
You're going to have to use so-called dynamic SQL, also known as "server side prepared statements" to do this. Ordinary SQL prohibits the use of variables for the names of tables or columns.
See here.
http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
sounds like a bad idea but you could do it with PREPARE statement FROM #sql and EXECUTE statement like this sqlFiddle
sample table
CREATE table yourTable(id int auto_increment primary key,value varchar(50));
INSERT into yourtable(value) values ('test1'),('test2'),('test3'),('test4');
create a table with a time_stamp ending (in the example I am only recording down to the HOUR, you can add _%i_%s to the formatting if you want it down to the seconds.
SET #TimeStamp = DATE_FORMAT(NOW(),'%Y_%m_%d_%H');
SET #sql = CONCAT('CREATE table yourTable',#TimeStamp,'(id int auto_increment primary key,value varchar(50))');
PREPARE statement FROM #sql;
EXECUTE statement;
SET #sql = CONCAT('INSERT INTO yourTable',#TimeStamp,'(id,value) SELECT id,value FROM yourTable');
PREPARE statement FROM #sql;
EXECUTE statement;

Using dynamic sql to populate a temp table

I know this isn't ideal, but I would like to know if it is possible to populate a temp table based on dynamic sql?
A very similar example to what I want to achieve is shown in this answer as either
SELECT into #T1 execute ('execute ' + #SQLString )
or
INSERT into #T1 execute ('execute ' + #SQLString )
I couldn't get either to work. It seems from the edit that the first option was wrong, so for the second option I've tried something like;
DECLARE #SQLString VARCHAR (2000) = 'SELECT * FROM INFORMATION_SCHEMA.COLUMNS'
INSERT into #MyTempTable execute (#SQLString )
Any ideas are much appreciated.
Edit:
In an attempt to clarify what I am trying to do without being too localised, I explain as briefly as I can below.
I have data in a staging area of my database that contains tables with dynamic names and a dynamic number of columns. However, a few of the column names are the same for each table. Rather than construct everything in dynamic sql, I would like to be able to simply extract the known columns into a temp table (or table variable, CTE, derived table or whatever) and act on that.
So given a table as so;
CREATE TABLE SomeParticularNameThatCantBeKnownToAStoredProc (
[1] AS VARCHAR(100),
[2] AS VARCHAR(100),
... -- Could be any number of these columns
[Id] AS INT,
[KnownCol] AS VARCHAR(100),
[KnownCol2] AS VARCHAR(100),
....
[DboId] AS INT
)
I'd like to be able to perform the necessary operations to allow my to process this data without having to do it all in dynamic sql. I was hoping to be able to do something like;
DECLARE #TableName AS VARCHAR(1000) = 'SomeParticularNameThatCantBeKnownToAStoredProc'
SELECT [Id], [KnownCol], [KnownCol2], [DboId]
INTO #KnownName
FROM #TableName -- I know this isn't possible, but this is what I'd like to do
This would then allow me to perform SQL statements against a consistent #KnownName. Some of the other operations I need to do are quite lengthy such as using the data to relate to other existing tables, copying data from the staging table(s) to their dbo schema equivalents and matching the DboId against the staging table Id using MERGE with OUTPUT INTO as described here, and so on and so forth.
If you can think of any other way I can limit the amount of dynamic SQL I need to write given the fact that the table name is dynamic then please let me know.
Assuming #MyTempTable already exists:
DECLARE #sql NVARCHAR(MAX);
SET #sql = N'SELECT * FROM INFORMATION_SCHEMA.COLUMNS;';
INSERT #MyTempTable EXEC sp_executesql #SQLString;
Otherwise please clarify what you are trying to do. If the table isn't already created, you can do everything inside of dynamic SQL, e.g.:
DECLARE #sql NVARCHAR(MAX);
SET #sql = N'SELECT * INTO #MyTempTable FROM INFORMATION_SCHEMA.COLUMNS;
SELECT * FROM #MyTempTable;';
EXEC sp_executesql #sql;

Duplicating a MySQL table, indices, and data

How do I copy or clone or duplicate the data, structure,
and indices of a MySQL table to a new one?
This is what I've found so far.
This will copy the data and the structure,
but not the indices:
create table {new_table} select * from {old_table};
This will copy the structure and indices,
but not the data:
create table {new_table} like {old_table};
To copy with indexes and triggers do these 2 queries:
CREATE TABLE new_table LIKE old_table;
INSERT INTO new_table SELECT * FROM old_table;
To copy just structure and data use this one:
CREATE TABLE new_table AS SELECT * FROM old_table;
I've asked this before:
Copy a MySQL table including indexes
Apart from the solution above, you can use AS to make it in one line.
CREATE TABLE tbl_new AS SELECT * FROM tbl_old;
MySQL way:
CREATE TABLE recipes_new LIKE production.recipes;
INSERT recipes_new SELECT * FROM production.recipes;
Go to phpMyAdmin and select your original table then select "Operations" tab in the "Copy table to (database.table)" area. Select the database where you want to copy and add a name for your new table.
I found the same situation and the approach which I took was as follows:
Execute SHOW CREATE TABLE <table name to clone> : This will give you the Create Table syntax for the table which you want to clone
Run the CREATE TABLE query by changing the table name to clone the table.
This will create exact replica of the table which you want to clone along with indexes. The only thing which you then need is to rename the indexes (if required).
The better way to duplicate a table is using only DDL statement. In this way, independently from the number of records in the table, you can perform the duplication instantly.
My purpose is:
DROP TABLE IF EXISTS table_name_OLD;
CREATE TABLE table_name_NEW LIKE table_name;
RENAME TABLE table_name TO table_name_OLD;
RENAME TABLE table_name _NEW TO table_name;
This avoids the INSERT AS SELECT statement that, in case of table with a lot of records can take time to be executed.
I suggest also to create a PLSQL procedure as the following example:
DELIMITER //
CREATE PROCEDURE backup_table(tbl_name varchar(255))
BEGIN
-- DROP TABLE IF EXISTS GLS_DEVICES_OLD;
SET #query = concat('DROP TABLE IF EXISTS ',tbl_name,'_OLD');
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- CREATE TABLE GLS_DEVICES_NEW LIKE GLS_DEVICES;
SET #query = concat('CREATE TABLE ',tbl_name,'_NEW LIKE ',tbl_name);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- RENAME TABLE GLS_DEVICES TO GLS_DEVICES_OLD;
SET #query = concat('RENAME TABLE ',tbl_name,' TO ',tbl_name,'_OLD');
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
-- RENAME TABLE GLS_DEVICES_NEW TO GLS_DEVICES;
SET #query = concat('RENAME TABLE ',tbl_name,'_NEW TO ',tbl_name);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END//
DELIMITER ;
Have a nice day!
Alex
To duplicate a table and its structure without data from a different a database use this. On the new database sql type
CREATE TABLE currentdatabase.tablename LIKE olddatabase.tablename
Worths to mention the importance of checking if the table already exists before
trying to duplicate it:
CREATE TABLE IF NOT EXISTS new_table LIKE old_table;
INSERT new_table
SELECT * FROM old_table;
As was said by the previous answers this will copy structure, data and
all the dependent objects of the table.
See MySql Tutorial:
Expanding on this answer one could use a stored procedure:
CALL duplicate_table('tableName');
Which will result in a duplicate table called tableName_20181022235959 If called when
SELECT NOW();
results:
2018-10-22 23:59:59
Implementation
DELIMITER $$
CREATE PROCEDURE duplicate_table(IN tableName VARCHAR(255))
BEGIN
DECLARE schemaName VARCHAR(255) DEFAULT SCHEMA();
DECLARE today VARCHAR(14) DEFAULT REPLACE(REPLACE(REPLACE(NOW(), '-', ''), ' ', ''), ':', ''); -- update # year 10000
DECLARE backupTableName VARCHAR(255) DEFAULT CONCAT(tableName, '_', today);
IF fn_table_exists(schemaName, tableName)
THEN
CALL statement(CONCAT('CREATE TABLE IF NOT EXISTS ', backupTableName,' LIKE ', tableName));
CALL statement(CONCAT('INSERT INTO ', backupTableName,' SELECT * FROM ', tableName));
CALL statement(CONCAT('CHECKSUM TABLE ', backupTableName,', ', tableName));
ELSE
SELECT CONCAT('ERROR: Table "', tableName, '" does not exist in the schema "', schemaName, '".') AS ErrorMessage;
END IF;
END $$
DELIMITER ;
DELIMITER $$
CREATE FUNCTION fn_table_exists(schemaName VARCHAR(255), tableName VARCHAR(255))
RETURNS TINYINT(1)
BEGIN
DECLARE totalTablesCount INT DEFAULT (
SELECT COUNT(*)
FROM information_schema.TABLES
WHERE (TABLE_SCHEMA COLLATE utf8_general_ci = schemaName COLLATE utf8_general_ci)
AND (TABLE_NAME COLLATE utf8_general_ci = tableName COLLATE utf8_general_ci)
);
RETURN IF(
totalTablesCount > 0,
TRUE,
FALSE
);
END $$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE statement(IN dynamic_statement TEXT)
BEGIN
SET #dynamic_statement := dynamic_statement;
PREPARE prepared_statement FROM #dynamic_statement;
EXECUTE prepared_statement;
DEALLOCATE PREPARE prepared_statement;
END $$
DELIMITER ;
After I tried the solution above, I come up with my own way.
My solution a little manual and needs DBMS.
First, export the data.
Second, open the export data.
Third, replace old table name with new table name.
Fourth, change all the trigger name in the data (I use MySQL and it show error when I don't change trigger name).
Fifth, import your edited SQL data to the database.
To create table structure only use this below code :
CREATE TABLE new_table LIKE current_table;
To copy data from table to another use this below code :
INSERT INTO new_table SELECT * FROM current_table;
Simple Cloning:
it create a table from another table without taking into account any column attributes and indexes.
CREATE TABLE new_table SELECT * FROM original_table;
Shallow Cloning:
This will only create an empty table base on the structure of the original table
CREATE TABLE new_table LIKE original_table;
The following command would create an empty table base on the original table.
CREATE TABLE adminUsers LIKE users;
Deep Cloning:
This means the new table gets to have all the attributes of each column and indexes of the existing table. This quite useful if you want to maintain the indexes and attributes of the existing table.
CREATE TABLE new_table LIKE original_table;
INSERT INTO new_table SELECT * FROM original_table;
https://towardsdatascience.com/how-to-clone-tables-in-sql-dd29586ec89c
// To copy specific column data use this one:
CREATE TABLE ut_axis_existrec LIKE ut_karvy_annexure; // To create new table
INSERT INTO ut_axis_existrec
(funding_ac,micr_no, warrant_no,
amount,invname,mfundcode,funding_dt,status,remarks1,amc_remark,created_at)
SELECT
t1.funding_ac,
t1.micr_no,
t1.warrant_no,
t1.amount,
t1.invname,
t1.mfund_code,
t1.funding_dt,
t1.status,
t1.remarks1,
t1.created_at
from ut_axis_karvy
inner join
ut_axis_karvy_master as t2
on t1.micr_no = t2.micr_no;
Try this :
`CREATE TABLE new-table (id INT(11) auto_increment primary key) SELECT old-table.name, old-table.group, old-table.floor, old-table.age from old-table;`
I selected 4 columns from old-table and made a new table.
FOR MySQL
CREATE TABLE newtable LIKE oldtable ;
INSERT newtable SELECT * FROM oldtable ;
FOR MSSQL
Use MyDatabase:
Select * into newCustomersTable from oldCustomersTable;
This SQL is used for copying tables, here the contents of oldCustomersTable will be copied to newCustomersTable.
Make sure the newCustomersTable does not exist in the database.