2014-01-05 67 views
3

我正在为我的数据库使用MySQL 5.1,并通过Java程序(JBDC)发送命令。 是否有MySQL创建或更改表的命令? 比方说,我有一个下表:MySQL创建或更改表

+----------+----------+ 
| column_a | column_b | 
+----------+----------+ 
| value_a | value_b | 
+----------+----------+ 

现在我想用一个命令,这将增加一列“column_c”如果不存在了。 这将导致:

+----------+----------+----------+ 
| column_a | column_b | column_c | 
+----------+----------+----------+ 
| value_a | value_b |   | 
+----------+----------+----------+ 

如果表不存在,它会创建一个新的与指定的列:

+----------+----------+----------+ 
| column_a | column_b | column_c | 
+----------+----------+----------+ 

最后,如果表中列有该间没有在命令中没有指定,它会离开他们未触动

+0

就没有什么'ALTER TABLE IF'我知道,也许你应该看看像“liquibase”或“flyway”这样的工具 – 2014-01-05 16:13:22

+0

我不认为使用这些程序对于单个命令来说是必要的,@RC ..是否没有其他方式? – Limeth

+0

检查数据库元数据,并在需要时创建列 – 2014-01-05 16:54:05

回答

1

这里是在Java代码中创建一个表中调用的咖啡:

/*Making the connection*/ 
try {//if any statements within the try block cause problems, rather than the program failing 
     //an exception is thrown which will be caught in the catch block 
     con = DriverManager.getConnection(url, "your username", "your password"); 

     //create a statement object 
     stmt = con.createStatement(); 
     //supply the statement object with a string to execute       
     stmt.executeUpdate("create table COFFEES (COF_NAME varchar(32), " + 
          "SUP_ID int, PRICE double, SALES int, TOTAL int, " + 
          "primary key(COF_NAME))"); 

     //close the statement and connection 
     stmt.close(); 
     con.close(); 

    } catch(SQLException ex) { 
     System.err.println("SQLException: " + ex.getMessage()); 
    } 

说明: - 在这个例子中,Java程序与位于服务器上的数据库进行交互,所以我们必须首先我们设置服务器所在位置的URL并登录用户名和密码,您可能不会使用与我使用的方法相同的方法。 - 这些需要在你的java程序的顶部声明:

String url = "jdbc:mysql://www.yoururlexample.co.uk"; 
Connection con;  
Statement stmt 

希望这会有所帮助,那么你将能够将数据插入到数据库和执行查询。

编辑:

这可以在executeUpdate的语句中使用,如果你想如果没有名为“COFFEES”的存在是为了创建一个表:

create table if not exists COFFEES 
+0

我想你错过了这个问题,我要求命令在MySQL中使用,而不是用于Java连接。 _是否有创建或更改表的命令?_ – Limeth

+0

嗯,我不完全确定,但不应该添加这些来做你想要的东西吗?: stmt.executeUpdate(CREATE TABLE IF NOT EXISTS .... ); stmt.executeUpdate(ALTER TABLE .....); – escull638

0
/*Making the connection*/ 
try { 
     //an exception is thrown which will be caught in the catch block 
     con = DriverManager.getConnection("jdbc:mysql:exaple.com", "username", "pass"); 

     //create a statement object 
     stmt = con.createStatement(); 
     //supply the statement object with a string to execute       
     stmt.executeUpdate("ALTER TABLE table_name ADD column_name datatype"); 

     //close the statement and connection 
     stmt.close(); 
     con.close(); 

    } catch(SQLException ex) { 
     System.err.println("SQLException: " + ex.getMessage()); 
    } 

我认为这将为你工作。

+0

我想你错过了这个问题,我要求命令在MySQL中使用,而不是用于Java连接。_是否有用于创建或更改表格的命令?_ – Limeth

+0

yes命令是executeUpadate。 ALTER TABLE table_name ADD column_name datatype我希望这可以为你工作 –

+0

问题是,如果表不存在,它不会创建它。另外,您的示例中没有办法检测缺失的列。 – Limeth

0

类似的东西可能是一个解决方案(这需要在表格中至少一个记录工作):

package com.stackoverflow.so20935793; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

public class App { 

    // complete this: 
    private static final String JDBC_URL = "....."; 
    private static final String JDBC_USER = "....."; 
    private static final String JDBC_PASS = "....."; 

    public static void main(final String[] args) { 
     Connection con = null; 
     PreparedStatement stm = null; 
     ResultSet rs = null; 

     try { 
      con = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASS); 
      stm = con.prepareStatement("SELECT * FROM your_table LIMIT 1"); 
      rs = stm.executeQuery(); 
      if (rs.next() && rs.getMetaData().getColumnCount() == 2) { 
       // add your new column 
      } 
     } catch (final SQLException ex) { 
      // handle exception 
     } finally { 
      closeQuietly(rs); 
      closeQuietly(stm); 
      closeQuietly(con); 
     } 
    } 

    private static void closeQuietly(final AutoCloseable what) { 
     if (what == null) { 
      return; 
     } 

     try { 
      what.close(); 
     } catch (final Exception ex) { 
      // ignore 
     } 
    } 
} 

(未测试)