i'm sure there is such question at stackoverflow, but i just c't find it :(
I have 2 databases with same data ("developer" database and "production" database).
"production" database is "Live" database - sitve visitors see this data
"developer" database is database where i create new functions at my local server.
I have situation when i add to "developer" database some new tables and some new fields in old tables.
And now i have to copy this new created fields and tables to "production" database (but only structure, data should not be copied and no data at "production" database must be changed).
UPD: Maybe there is solution where i can make database structure dump from developing database and when i import it to production database, it automatically add all new fields from all tables
What functions should i use?
Thanks.
You want alter table: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
To alter an existing table:
alter table tablename add column newcolumn tinyint(1) default 1 AFTER othercolumn
To create a new table:
CREATE TABLE `newtablename` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`newcolumn` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
to copy ONLY the structure you can use like:
jcho360> create table t3 like t2;
Query OK, 0 rows affected (0.05 sec).
If you want to copy the content after create the structure you can use :
INSERT INTO t3 SELECT * FROM t2;
if you want to copy with data you can use (without structure, I mean, PK, FK, etc)
mysql> create table t4 as select * from t1;
of course you are able to use backup to restore tables too
if you want to add new column you can use
alter table tablename add column newcolumn vartype;
if you want to use a table from another database you can use queries calling first the database and later the name, like
select * from database.tablename;
remember the user need permission to the other db too.
Related
I am able to create a table manually in SQL Server by checking columns from MySQl table. And then able to move data from MySQL to SQL Server table.
Example: MySQL table = Employee
Describe Employee;
Output
Field, Type, Null, Key, Default, Extra
EmpId int(10) NO PRI 0
Name varchar(100) YES
Age int(10) YES 18
EmailId varchar(100) NO
Using this I am creating same table in SQL Server
Drop table MsSQLdb..Employee;
CREATE TABLE Employee (
EmpId int NOT NULL PRIMARY KEY DEFAULT 0,
Name varchar(100),
Age int de,
Name varchar(100) NOT NULL DEFAULT 18
);
Now using OpenQuery to copy the data from MySQL to SQL Server :
select EmpId,Name,Age,EmailId into MsSQLdb..Employee
from
OPENQUERY(LinkedServer, 'SELECT EmpId,Name,Age,EmailId FROM mySQL_db.Employee')
But, daily my mySQL_db.Employee table gets more columns or less. So daily I need to manually Map this columns in the above queries to redo the task. Is there any dynamic way for this task?
I have around 40-tables and each table have around 30+ columns. so looking for any a dynamic way.
You can go for SELECT * INTO for loading data into tables. More on SELECT
I assume that you are fine for DROP & RECREATE and simple scenario of loading data. You might not get right datatype as datatype is automatically decided by SQL Server based on initial set of rows.
DROP TABLE dbo.Employee;
SELECT * INTO dbo.Employee
from
OPENQUERY(LinkedServer, 'SELECT * FROM mySQL_db.Employee')
Indexes, constraints, and triggers defined in the source table are not
transferred to the new table, nor can they be specified in the
SELECT...INTO statement. If these objects are required, you can create
them after executing the SELECT...INTO statement.
I'm currently trying to find a way to separate the mysql export function (using phpmyadmin and/or shell)
Following situation:
I have two databases which were the same at some state but over time only one got updated. So for example:
- database1 has table "users" with columns "UID" and "username"
- database2 has table "users" with columns "UID" "username" and "status"
Now i want to export database2 and import it to database1. database2 contains data I DONT need anymore. But database1 contains important data. So I need something that gives me the possibility to somehow "merge" these databases without data loss on database1 and without merging data from database2.
The merge should look if the table exists, if not -> create it
The merge should look if the columns exist in the table, if not -> create it
What I have tried so far:
I've first tried it with the inbuilt PHPmyadmin export function (advanced options). This would give me the following result:
CREATE TABLE IF NOT EXISTS `users` (
`UID` int(11) NOT NULL,
`username` varchar(25) NOT NULL,
`status` int(2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
When I import this, of course, nothing special will happen since the table already exists and so the 'status' column does not get created.
On my research I found a possibility to export a database separated from table creation using shell using the command (blacked out database name and user/pass):
mysqldump -t --insert-ignore --skip-opt -u USER -p PASSWORD -h 127.0.0.1 database > database.sql
But this gives me an insert option with data (which I dont need)
INSERT IGNORE INTO `users` VALUES (1,"bla",1);
Is there any different possibility?
Sure I know I could write it by hand to alter the columns into the table but it sure has to be automatic since its not only the table I named in the example.
To be 100% clear what I'm trying to achieve, a pseudo handwritten sql script:
CREATE TABLE IF NOT EXISTS 'users';
For every column -> IF NOT EXISTS column in 'users' -> ALTER TABLE 'users' ADD column def;
You can use MySQLWorkBench as well. Look video instruction
I'm trying to create a data warehouse.
Is it possible to populate a table in db1, from data in db2.
For example
Corporate Database Table Route
CREATE TABLE ROUTE (
RouteID INTEGER(4) PRIMARY KEY,
RouteName VARCHAR (50) NOT NULL,
BoardingStop VARCHAR (50) NOT NULL,
AlightingStop VARCHAR (50) NOT NULL
);
Insert Information
INSERT INTO `ROUTE` (`RouteID`,`RouteName`,`BoardingStop`,`AlightingStop`)
VALUES (1,"ab","B","C")
Data warehouse table dimRoute
CREATE TABLE DimROUTE (
RouteID INTEGER(4),
RouteName VARCHAR (50) NOT NULL,
BoardingStop VARCHAR (50) NOT NULL,
AlightingStop VARCHAR (50) NOT NULL,
PRIMARY KEY(RouteID)
);
Populate the above table with data from the first table.
You can copy from one table into another table with INSERT INTO ... SELECT. See docs here: http://dev.mysql.com/doc/refman/5.7/en/insert-select.html
You can copy between tables in different databases on the same MySQL instance, provided you have privileges to both database. Just use databasename.tablename syntax:
INSERT INTO warehouse.DimRoute
SELECT * FROM corporate.Route;
If the databases are hosted on different MySQL instances, you can dump data from the corporate instance and import to the data warehouse instance using mysqldump. Since your table is named differently in the data warehouse, this is a little bit tricky.
You could restore the data to its original table name, and then rename the table:
$ mysqldump --host=corporate corp_dbname ROUTE > route-dump.sql
$ mysql --host=datawarehouse dw_dbname < route-dump.sql
$ mysql --host=datawarehouse -e "RENAME TABLE ROUTE TO DimROUTE" dw_dbname
(I'm leaving out user/password options for brevity, but I suggest you use the config file for those.)
You just need a couple of queries to clone a table (with its indexes and keys) then populate it with the records:
CREATE TABLE DimROUTE LIKE ROUTE;
INSERT DimROUTE SELECT * FROM ROUTE;
Demo SQL Fiddle
Yes, you can. The technique you want is called Extract, Transform and Load (ETL). There are a number of tools you can use, which will help you automate and organise the process. Or you can roll your own solution.
It is quite common for reporting databases to be feed by other databases in this fashion.
I have created a MySQL table with the following code from a tutorial, I just put it into the MySQL console:
CREATE TABLE `test`.`name` (
`nameid` INT NOT NULL AUTO_INCREMENT ,
`firstname` VARCHAR(45) NULL ,
`lastname` VARCHAR(45) NULL ,
PRIMARY KEY (`nameid`)
);
INSERT INTO `test`.`name`
(`firstname`,`lastname`)
VALUES
("TheBig","Monster"),
("Guy","Smiley"),
("Big","Bird"),
("Oscar","Grouch"),
("Alastair","Cookie");
With the python script given in the tutorial I am able to print each item.
But when it comes to erase the table, I have tried all possible combinations of:
DROP TABLE `test`
DROP TABLE test
DROP TABLE `test`.`name`
etc, but none will erase it. Moreover, If I use the initial table creation script again, it will add the entries to the table, so I know its a stored table within MySQL but cannot access it, delete it, or list it with:
SHOW DATABASES
or
SHOW TABLES
It is my first afternoon with MySQL, but don't know how to go further if the commands in the MySQL help are not working!
So the summary question is:
How to delete / access the table created with the script?
drop table test.name;
this should work
Is there a way to copy a column's structure from an already populated table to a new table which is empty? I'm only asking about copying the structure without the data
Example:
We have a table
CREATE TABLE `animals` (
`animal` varchar(11) NOT NULL,
`food` varchar(11) NOT NULL,
PRIMARY KEY (`animal`)
) ENGINE=InnoDB
INSERT INTO `animals` (`animal`, `food`) VALUES
('cat', 'chips'),
('dog', 'bones'),
('shark', 'ppl');
And a new table called predators for which I want to make just one column but with the same data type as the animals' column type.
Is there a way to combine the SHOW COLUMNS/FIELDS with the CREATE TABLE or create the table with a column that has some kind of type like VARCHAR(17) and then ALTER CHANGE it to the same type as the animal column?
I know this is a simple question but i haven't had any luck finding the answer to it
If you wish to copy the data:
INSERT INTO newTable (col1, col2)
SELECT col1, col2 FROM otherTable
If you wish to copy the table structure:
Use LIKE to create an empty table based on the definition of another table, including any column attributes and indexes defined in the original table:
CREATE TABLE new_tbl LIKE orig_tbl;
The copy is created using the same version of the table storage format as the original table. The SELECT privilege is required on the original table.
Documentation
If you want to copy the structure and the data:
CREATE TABLE animals2 AS
SELECT *
FROM animals ;
And if you want to copy the structure (but not all columns) without data:
CREATE TABLE animals2 AS
SELECT animal -- only the columns you want
FROM animals
WHERE FALSE; -- and no data