Java JDBC:更改表 [英] Java JDBC: Altering table
问题描述
我希望对该表进行以下更改:
I wish to make the following alterations to this table:
添加:状态列(varchar(20))日期(时间戳)列
Add: Column for state (varchar(20)) column for date (timestamp)
我不确定该怎么做
String createTable = "Create table aircraft ("
+"aircraftNumber int,"
+"airLineCompany varchar(20),"
+"departureAirport varchar (20),"
+"fuelPercentage int,"
+"passengerCount int);";
推荐答案
To add columns in a database table, you use the ALTER TABLE
statement:
ALTER TABLE aircraft
ADD COLUMN state VARCHAR(20),
ADD COLUMN dateCreated DATETIME
请注意,由于两个原因,我不使用 date
作为字段的名称:1)在大多数数据库引擎中,它是保留关键字; 2)表字段名必须为易于理解的是,名为 date 的字段对当前模型完全没有任何价值,该日期是:创建日期,最后更新日期,飞机到达日期,飞机事故发生日期?
Note that I don't use date
as name for your field for two reasons: 1) It is a reserved keyword in most database engines, 2) A table field name must be easy to understand for readers, A field named date adds no value at all to the current model, which date: date of creation, date of last update, date of aircraft arrival, date of aircraft accident?.
然后,在Java中,使用 Statement#execute
以在JDBC中执行任何DDL语句.
Then, in Java, use Statement#execute
to perform any DDL statement in JDBC.
总结一下:
String sql = "ALTER TABLE aircraft "
+ "ADD COLUMN state VARCHAR(20), "
+ "ADD COLUMN dateCreated DATETIME";
Connection con = ...
Statement stmt = con.createStatement(sql);
stmt.execute();
这篇关于Java JDBC:更改表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!