Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy database structure without data in mysql (with empty tables)

Is there any way to copy database structure without data in MySQL, so the new database will be the same as it is copied from, but with empty tables.

After getting some suggestions I tried the command, but I am getting syntax error, my username = root and password = nothing. I guess the default one. I am trying following command,

mysqldump -u root -p -d xyz_db | mysql -u root -p -Dnew_db 

what I am missing or misplacing in command?

like image 459
Pramod Avatar asked Jan 01 '13 04:01

Pramod


People also ask

How do you create a copy of a table without the data in MySQL?

If you need to duplicate the table structure, but not its data, it is better to use the CREATE TABLE ... LIKE statement. In case you need to copy the table structure including primary keys, foreign keys and constraints, run the SHOW CREATE TABLE ...


2 Answers

mysqldump -u user -ppass -d olddb | mysql -u user -ppass -D newdb 

The new database must already exist. The -d flag in the mysqldump command prevents copying of data.

There's no space between the flag -p and the password.

like image 91
Raab Avatar answered Sep 27 '22 20:09

Raab


You can take backup using mysqldump and restore with mysql using commandline.

For backup database

$ mysqldump -u root-pPassword -P3309 --routines --no-data testdb > "d:\dbwithnodata.sql" 

For restoration of database

$ mysql -u root-pPassword -P3309 newdb < "d:\dbwithnodata.sql" 
like image 40
Saharsh Shah Avatar answered Sep 27 '22 20:09

Saharsh Shah