MySQL: How to Create A Table from A Table Schema File? - mysql

I am using MySQL to capture snapshots of my data everyday, so I need to create multiple tables with the same columns.
my_foobar_table_20170125
my_foobar_table_20170126
my_foobar_table_20170127
What's the easiest way to create the tables? Would it be with a table schema file? Or, with a create table query?
I am leaning toward using a table schema file if that is possible. As it seems cleaner than a sql query to create the table.
I have googled around, and surprisingly there is no clear answer on this. In fact, it's not even obvious what exactly is a "table schema file", or how to generate this from mysql workbench, or use the schema file to create the table.

Definitely create a table .sql file and load it into MySQL then log in. Manually making it each time is a ton of work and this allows you to also customize each table if needed, and to use drop database and to refresh easily as well.
CREATE DATABASE databasename;
USE database;
CREATE TABLE tableofthings (
ID INT NOT NULL AUTO_INCREMENT,
otherdatastring VARCHAR(50)...
PRIMARY KEY (ID)
);
...then put other tables below...

Related

Is there any way to import database table schema only

I have two databases. Now I'm trying to import all table schema only from first database to second database. I already exported all table structure from first database using phpmyadmin. But I can't import the table structures in the second database. phpmyadmin says
#1050 - Table 'XXXXXX' already exists
How can I import only the table structure correctly?
Note: Both databases had same table and all table had same structure. I have changed some table structures in the first database that I can't remember right now. Now I need to merge both table structure only. Also both database contains different data set and I can't afford any data loss from both databases.
Before executing any command I would recommend taking full database backup, otherwise you may lost a few days of sleep.
Using ALTER command
Use ALTER command to modify table structure. Here's sql that adds age not nullable age field to users table.
ALTER TABLE users ADD age int(11) not null
Re-creating table
I wouldn't recommend this method because you'll have data loss. Drop old table then create with new schema.
DROP TABLE mytable;
CREATE TABLE mytable (
...
);
Or if you want to keep data you can:
Duplicate or rename table to different name.
Create a new table with new schema.
Copy data from old table: INSERT INTO newtable SELECT * FROM oldtable
Renaming tables might cause relationship issues. I would recommend using ALTER command as much as possible. You can also take a look at scheme migration(aka: code first migration, database migration).
The main Issue is merging the tables. To identify the differences between the two tables I use a small software called SQL Power Architect.

How can I create a copy of a table in Redshift WITHOUT DATA but with all the table schema (like compressions and sort keys etc.)

Is there a way to create a copy of a table in redshift without data? However, I want all the table schema (like compression, data types for each column sort keys and primary keys) to remain the same.
Check LIKE option for CREATE TABLE statement in the docs: CREATE TABLE
It doesn't preserve primary key but it duplicates columns and sort/dist keys like this:
CREATE TABLE new_table (LIKE existing_table);
This is the best you can get unless you keep the original DDL statement somewhere and just re-execute it with a new table name.

working with temporary tables in Joomla 2.5

I am trying to use MySql temporary tables with my joomla site.
the problem is that whenever I query about the content of the table I get an empty result (except when using select statement in the same function where I create the table).
My questions are:
everytime I use $db = JFactory::getDBO() - do I create a new DB connection?
if so - how can I use temporary files?
if not - why don't I get the data of the temp table?
How can I create a temporary table that will remain until the user logs out?
You cannot use temporary tables like this, read the docs on temporary tables in MySQL here. The table will be deleted on each session, which will probably be each page load, possibly sooner, depending on how this is handled throughout Joomla.
If you want your temporary table to persist, you need to create normal table, not a temporary table...

Copying data from old table to new table in SQL (ORACLE)

I'm aware that copying data will only copy the data and no constraints, I'll lose my pk and fk etc.
But what if i were to copy the data back into a table with primary keys and foreign. because i want to remodel my db on workbench but don't want to lose the data i have imput into my tables so i was thinking of making a copy deleting the original remodelling the db and forward engineering and copying the data back into the table will this work?
Not sure if I gut your question right but from what it looks you only need a new table with the data to mass with. have you tried using CREATE TABLE AS SELECT?
http://dev.mysql.com/doc/refman/5.0/en/create-table-select.html
CREATE TABLE t AS SELECT * from old_t

copy table from one database to another, maintaining table's structure and renaming it too

I want to copy a table (say tbl_1) from one database (say source_db) to another database (say target_db), with following things in consideration:
Structure of table should be preserved, including primary key and
auto-increment key
While creating a copy of tbl_1, I need to rename it to cpy_tbl_1
How it is possible using query?
P.S. I know there will be many similar questions like mine, but I have those special considerations too.
CREATE TABLE db_target.cloned_table
SELECT *
FROM db_source.source_table;
With the previous sentence the table will be created with just the fields and it's types, but no keys, constraints, engine will be set. You can specify them manually in the same sentence like this:
CREATE TABLE db_target.cloned_table (
a INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (a), KEY(b)
) ENGINE=MyISAM
SELECT b,c FROM db_source.source_table;
mysql create table doc
you can attain this as follows;
First create the target_db
mysql > create database target_db;
then use it
mysql > use target_db;
then create the structure of the tb1 with name cpy_tbl_1
which is done as
mysql> create table cpy_tbl_1 like source_db.tb1;
then just copy the data.
mysql > insert into cpy_tbl_1 select * from source_db.tb1;
and check results
if you are able to use shell scripting then mysqldump is a powerful and flexible option... especially if you can use a little sed to do search and replaces.
simple version:
mysqldump $SOURCE $TABLE | mysql $TARGET