retrieve source code create a mysql table - mysql

I got a DB in MYSQL (that I haven't created), I do not have the code that was used for it.
I want to know what was the code used to create one of the tables in the DB , is there an option to do so? I need to create the same table but on diffrent data..
Thanks alot!
P

In MySQL Workbench you can display the DDL for any DB object. Just right click on it in the schema tree on either Copy to Clipboard or Send to SQL Editor and Create Statement:

This is a late answer, but since I don't see any reference to it, I'll suggest you to perform a dump of your database. Every decent DBMS has now a tool to do it. With MySQL, from command line, this would be :
mysqldump -u <username> <database_name> > yourfile.sql
This performs a complete dump in SQL format of your base, enabling you to recreate it elsewhere. No need for any special tool to do it when you need to. Just pass the content of the file to the regular MySQL's client.
If you want to get only the database schema without any data, just pass "--no-data" option.
mysqldump --no-data -u <username> <database_name> > yourfile.sql
You'll now be able to recreate a brand new, virgin database, having all attributes and special features of the previous one, without the data.

Related

mysqlsh to dump and load full schema

I want to use mysqlsh to do the following:
Dump the FULL schema of a given database (not just tables, but functions, triggers, everything related to this database schema, same as mysqldump -R DATABASE > DATABASE.sql)
Load this full schema into a brand new database I just created (similar to mysql --database=NEWDATABASE < DATABASE.sql)
When I run mysqlsh --execute 'util.dumpTables("DATABASE", [], "SQL/DATABASE", {all:true});', it of course just dumps the tables, and this can easily be imported into a brand new database with this command mysqlsh --database=NEWDATABASE --execute 'util.loadDump("SQL/DATABASE", {schema: 'NEWDATABASE', ignoreVersion:true,resetProgress:true});. The problem is it is missing the functions and stored procedures.
So then I tried mysqlsh --execute 'util.dumpSchemas(["DATABASE"], "DATABASE");', and then load it into a new DB with mysqlsh --database=NEWDATABASE --execute 'util.loadDump("DATABASE", {dryRun: true, ignoreVersion:true});', but I instantly notice that it is trying to load into the original database, not my new database. So how do I load it into a NEW database, one with a totally different name?
In case you are wondering, I am trying to learn how to maximize mysqlsh for my use case. So the old mysqldump is not an option in this case.
I think you will just have to edit the .sql file(s) with a text editor before you try to load it.
This tool is really for dumping schemas and importing them to a different MySQL instance, but leaving the schema names unchanged.

What parameters does MySQL Workbench pass to mysqldump?

I need to write a script to automate MySQL backup of a database. So to determine what I will need, I go into MySQL Workbench, select the Schema, select Data Export, set a couple of controls (at the moment: Export to Self-Contained File & Include Create Schema) and Start Export.
Export Progress shows me command-line:
Running: mysqldump --defaults-file="/tmp/tmpTbhnzh/extraparams.cnf" --user=*** --host=*** --protocol=tcp --port=3306 --default-character-set=utf8 --skip-triggers "<schema-name>"
I need to know what is in that temporary "defaults file" if I'm to replicate whatever it is that MySQL Workbench passes to mysqldump. But the backup completes so quickly and deletes the file that I can't even copy it, of course!
Is there a way I can know just what arguments Workbench is passing to mysqldump so I can know I'm generating a good, robust script? (To be clear: I'm sure I can look up the mysqldump documentation to find arguments corresponding to whatever UI items I fill in explicitly, but I'm wondering what other "goodies" MySQL Workbench might know about and put in the parameters file.)
A bit of digging about in the python scripts (there's one called wb_admin_export.py) and the answer is....not very exciting... it's your password.
It also includes ignore-tables if there are any to ignore.

Importing Data and Schema to MySQL Workbench

I'm trying to learn SQL and I downloaded a database to practice. I downloaded two files of extension .sql, one is the schema and the other one the actual data. I've also installed MySQL Workbench. I've been googling and I've been trying things to solve this but I don't understand Workbench and I can't load the database.
Where do I import the schema and the data in order to try queries ?
Any help would be really appreciated.
This is simple in Workbench, and I'll use the freely available sakila database as an example. Feel free to apply this to your situation:
Download "sakila" from here: http://dev.mysql.com/doc/index-other.html
Extract it somewhere, in my case, onto the Desktop into a new sakila-db/ directory
Open Workbench
In the Schema Navigator, right-click an empty area and choose "Create Schema"
In the schema creation wizard, name it "sakila", keep the defaults, hit "Apply", finish wizard
Go to "File" -> "Run SQL Script..."
Choose "sakila-schema.sql", make sure "Default Schema Name" is blank or select "sakila", execute
Go to "File" -> "Run SQL Script..."
Choose "sakila-data.sql", execute
Click the "refresh" icon in the Workbench Schema Navigator (or restart Workbench)
Now, use the populated sakila database :)
Steps (4) and (5) are optional in this case (as executing sakila-schema.sql creates the schema), but the idea is worth mentioning.
Here's how it would look when loading th script into the SQL IDE:
The accepted answer is from 4 years ago, so I thought I'd give an update as in MySQL Workbench 6.3 the procedure is a bit different.
You have to select the menu item Server -> Data Import -> Import from Self-Contained File and select the SQL file containing the database you want to import.
In Default Target Schema, select the database you want to import the SQL dump to, or create a new empty database via New...
Then click on Start Import.
You could use mysql console from terminal. Login through the user id and pass. Then create a Database from the following command is the .sql file does not have one such command to create so.
Create database db-name
use db-name;
SOURCE xyz.sql;
Source command would load the the content from xyz.sql to your database created. This would be reflected later in workbench indeed.
Its very easy on Linux platform just follow below mentioned steps, After downloading zip file of sakila-db, extract it. Now you will have two files, one is sakila-schema.sql and other one is sakila-data.sql.
Open terminal
Enter command mysql -u root -p < sakila-schema.sql
Enter command mysql -u root -p < sakila-data.sql
Now enter command mysql -u root -p and enter your password, now you have entered into mysql system with default database.
To use sakila database, use this command use sakila;
To see tables in sakila-db, use show tables command
Please take care that extracted files are present in home directory else provide the absolute path of these files in all above commands.

Importing MySQL to Postgres. Permission issues

I have MySql and Postgres databases. I have been working on Mysql DB which is populated with my data. Now for me to use heroku, I need to port it to Postgres. These are the steps I followed:
I exported data from my Mysql DB by simple dump command:
mysqldump -u [uname] -p[pass] db_name > db_backup.sql
I logged into my Postgres
sudo su postgres
Now when I try to import the sql into Postgres, it does not have access to db_backup.sql. I changed the permissions for all users and made the dump file read/write to all but still I cannot import the sql.
My question is what is the correct way to duplicate (both schema and data) from Mysql to Postgres. Also why am I not able to access the dump file even after changing the permissions? And if I have a dump from Mysql what are the chances that it runs into the issues while running it on Postgres (I do not have any procedural stuff in my Mysql. Just creation of tables and dumping data into those tables.)?
Thanks!
P.S. I am on Mac-Mavericks if that matters
While the primary part of the question was answered by #wildplasser I thought I would put the entire answer for people looking at porting MySQL data to Postgres.
After trying out multiple solutions, the easiest and quite smooth solution was this: https://github.com/lanyrd/mysql-postgresql-converter
This worked quite smoothly. But just one problem- it does not port any of Mysql sequences to Postgres. This means if you have auto-increment primary ids, you will have to change your Postgres schema separately and create serial sequences after the porting is done. Apart from that, it was quite smooth.
To talk about the permission issue, logging in as Postgres user and trying to access dump created by original user failed, the right way to do it was stay logged in as original user and use postgres user only for DB operation by using -U postgresuser command.
E.g.: psql -U postgres databasename < data_base_dump
While for many this must be the obvious way of doing it, I must admit it was one of those eureka moments for me :)

Creating database with same structure but different name

i have a database with name "DB_General" i want to create separate data base for each user
according to his name like"DB_User1". i want to use the same structure for "DB_User1" as
given in "DB_General". is there any way to do this, i am using MySQL database in JSP.
make a backup of DB_General eg using mysqldump, remove data if needed and restore it under different database name like DB_User_1
however idea of creating separate database for each user seems to be far way from wise
Run the following programmatically from a shell:
mysqladmin --user=foo --password=bar create DB_User1
mysqldump --user=foo --password=bar --no-data DB_general | mysql --user=foo --password=bar DB_User1