creating database from batch file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • konsu

    #1

    creating database from batch file

    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 programmaticall y, 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('tabl ename') and
    objectproperty( id, 'isusertable') = 1)
    drop table tablename

    thanks for any help
    konstantin


  • Mr. Clean

    #2
    Re: creating database from batch file

    In article <EfCdnfzXs-aW0a3dRVn-sA@kallback.com >, konsu@hotmail.c om
    says...[color=blue]
    > 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 programmaticall y, 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('tabl ename') and
    > objectproperty( id, 'isusertable') = 1)
    > drop table tablename
    >
    > thanks for any help
    > konstantin
    >
    >
    >[/color]
    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\my sql < dp.sql


    Comment

    • Mr. Clean

      #3
      Re: creating database from batch file

      In article <EfCdnfzXs-aW0a3dRVn-sA@kallback.com >, konsu@hotmail.c om
      says...[color=blue]
      > 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 programmaticall y, 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('tabl ename') and
      > objectproperty( id, 'isusertable') = 1)
      > drop table tablename
      >
      > thanks for any help
      > konstantin
      >
      >
      >[/color]
      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\my sql < dp.sql


      Comment

      • Mr. Clean

        #4
        Re: creating database from batch file

        In article <EfCdnfzXs-aW0a3dRVn-sA@kallback.com >, konsu@hotmail.c om
        says...[color=blue]
        > 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 programmaticall y, 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('tabl ename') and
        > objectproperty( id, 'isusertable') = 1)
        > drop table tablename
        >
        > thanks for any help
        > konstantin
        >
        >
        >[/color]
        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\my sql < dp.sql


        Comment

        Working...