By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,304 Members | 3,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,304 IT Pros & Developers. It's quick & easy.

creating database from batch file

P: n/a
hello,

i would like to write a batch file with all sql commands necessary to create
a database, its tables, and populate them with initial data.

would the experts please help me with these problems:

1. how to programmatically, in sql script, check if a known database already
exists on the server, and drop it if it does?
2. similarly, how to check if a table exists and drop it?

something similar to MSSQL's:

if exists(select * from sysobjects where id = object_id('tablename') and
objectproperty(id, 'isusertable') = 1)
drop table tablename

thanks for any help
konstantin
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <Ef********************@kallback.com>, ko***@hotmail.com
says...
hello,

i would like to write a batch file with all sql commands necessary to create
a database, its tables, and populate them with initial data.

would the experts please help me with these problems:

1. how to programmatically, in sql script, check if a known database already
exists on the server, and drop it if it does?
2. similarly, how to check if a table exists and drop it?

something similar to MSSQL's:

if exists(select * from sysobjects where id = object_id('tablename') and
objectproperty(id, 'isusertable') = 1)
drop table tablename

thanks for any help
konstantin

Here is how to do it using the mySQL command-line tool:

Create a SQL script to create your database and your tables:

{named dp.sql}

#
# Table structure for table 'DelphiPages'
#
#
# Create Database named MB34
#
CREATE DATABASE IF NOT EXISTS MB34;
# MUST USE the database just CREATED!!!!
USE MB34;
DROP TABLE IF EXISTS DelphiPages;
CREATE TABLE DelphiPages (
RecNo int(10) unsigned NOT NULL auto_increment,
GMTTimestamp datetime default NULL,
Comments varchar(50) default NULL,
PRIMARY KEY (RecNo)
) TYPE=MyISAM;
Then create a batch file to run the sql through the
command-line tool:

{modify the path to your installation of mySQL}

@echo off
c:\mysql\bin\mysql < dp.sql
Jul 19 '05 #2

P: n/a
In article <Ef********************@kallback.com>, ko***@hotmail.com
says...
hello,

i would like to write a batch file with all sql commands necessary to create
a database, its tables, and populate them with initial data.

would the experts please help me with these problems:

1. how to programmatically, in sql script, check if a known database already
exists on the server, and drop it if it does?
2. similarly, how to check if a table exists and drop it?

something similar to MSSQL's:

if exists(select * from sysobjects where id = object_id('tablename') and
objectproperty(id, 'isusertable') = 1)
drop table tablename

thanks for any help
konstantin

Here is how to do it using the mySQL command-line tool:

Create a SQL script to create your database and your tables:

{named dp.sql}

#
# Table structure for table 'DelphiPages'
#
#
# Create Database named MB34
#
CREATE DATABASE IF NOT EXISTS MB34;
# MUST USE the database just CREATED!!!!
USE MB34;
DROP TABLE IF EXISTS DelphiPages;
CREATE TABLE DelphiPages (
RecNo int(10) unsigned NOT NULL auto_increment,
GMTTimestamp datetime default NULL,
Comments varchar(50) default NULL,
PRIMARY KEY (RecNo)
) TYPE=MyISAM;
Then create a batch file to run the sql through the
command-line tool:

{modify the path to your installation of mySQL}

@echo off
c:\mysql\bin\mysql < dp.sql
Jul 19 '05 #3

P: n/a
In article <Ef********************@kallback.com>, ko***@hotmail.com
says...
hello,

i would like to write a batch file with all sql commands necessary to create
a database, its tables, and populate them with initial data.

would the experts please help me with these problems:

1. how to programmatically, in sql script, check if a known database already
exists on the server, and drop it if it does?
2. similarly, how to check if a table exists and drop it?

something similar to MSSQL's:

if exists(select * from sysobjects where id = object_id('tablename') and
objectproperty(id, 'isusertable') = 1)
drop table tablename

thanks for any help
konstantin

Here is how to do it using the mySQL command-line tool:

Create a SQL script to create your database and your tables:

{named dp.sql}

#
# Table structure for table 'DelphiPages'
#
#
# Create Database named MB34
#
CREATE DATABASE IF NOT EXISTS MB34;
# MUST USE the database just CREATED!!!!
USE MB34;
DROP TABLE IF EXISTS DelphiPages;
CREATE TABLE DelphiPages (
RecNo int(10) unsigned NOT NULL auto_increment,
GMTTimestamp datetime default NULL,
Comments varchar(50) default NULL,
PRIMARY KEY (RecNo)
) TYPE=MyISAM;
Then create a batch file to run the sql through the
command-line tool:

{modify the path to your installation of mySQL}

@echo off
c:\mysql\bin\mysql < dp.sql
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.