- Apache Derby 教程
- Apache Derby - 首頁
- Apache Derby - 簡介
- Apache Derby - 部署模式
- Apache Derby - 環境設定
- Apache Derby - 工具
- Apache Derby - 語法
- Apache Derby - 資料型別
- Apache Derby - 建立表
- Apache Derby - 刪除表
- Apache Derby - 插入資料
- Apache Derby - 檢索資料
- Apache Derby - 更新資料
- Apache Derby - 刪除資料
- Apache Derby - WHERE 子句
- Apache Derby - GROUP BY 子句
- Apache Derby - ORDER BY 子句
- Apache Derby - HAVING 子句
- 修改表語句
- Apache Derby - Derby 索引
- Apache Derby - 儲存過程
- Apache Derby - 模式
- Apache Derby - 觸發器
- Apache Derby 有用資源
- Apache Derby - 快速指南
- Apache Derby - 有用資源
- Apache Derby - 討論
Apache Derby - 修改表語句
ALTER TABLE 語句允許您修改現有表。使用它您可以執行以下操作:
新增列,新增約束
刪除列,刪除約束
更改表的行級鎖定
假設我們建立了一個名為 Employees 的表,如下所示:
ij> CREATE TABLE Employees ( Id INT NOT NULL GENERATED ALWAYS AS IDENTITY, Name VARCHAR(255), Salary INT NOT NULL, Location VARCHAR(255), PRIMARY KEY (Id) );
並且,使用 insert 語句插入了四條記錄,如下所示:
ij> INSERT INTO Employees (Name, Salary, Location) VALUES
('Amit', 30000, 'Hyderabad'),
('Kalyan', 40000, 'Vishakhapatnam'),
('Renuka', 50000, 'Delhi'),
('Archana', 15000, 'Mumbai');
向表中新增列
以下是使用 ALTER 語句向表中新增列的語法。
ALTER TABLE table_name ADD COLUMN column_name column_type;
示例
使用 ALTER 語句,我們嘗試新增一個名為 Age 的新列,型別為整數。
ALTER TABLE Employees ADD COLUMN Age INT; 0 rows inserted/updated/deleted
新增另一個名為 Phone_No 的列,型別為整數。
ALTER TABLE Employees ADD COLUMN Phone_No BIGINT; 0 rows inserted/updated/deleted
DESCRIBE 命令透過列出列及其詳細資訊來描述指定的表(如果表存在)。如果您 DESCRIBE Employees 表,您可以觀察到新新增的列,如下所示:
ij> DESCRIBE Employees; COLUMN_NAME |TYPE_NAME|DEC&|NUM&|COLUM&|COLUMN_DEF|CHAR_OCTE&|IS_NULL& ------------------------------------------------------------------------------ ID |INTEGER |0 |10 |10 |AUTOINCRE&|NULL |NO NAME |VARCHAR |NULL|NULL |255 |NULL |510 |YES SALARY |INTEGER |0 |10 |10 |NULL |NULL |NO LOCATION |VARCHAR |NULL|NULL |255 |NULL |510 |YES AGE |INTEGER |0 |10 |10 |NULL |NULL |YES PHONE_NO |INTEGER |0 |10 |10 |NULL |NULL |YES 6 rows selected
向表中新增約束
以下是使用 ALTER 語句向表的列新增約束的語法。
ALTER TABLE table_name ADD CONSTRAINT constraint_name constraint (column_name);
其中constraint可以是 NOT NULL、NULL、PRIMARY KEY、UNIQUE、FOREIGN KEY、CHECK。
示例
使用 ALTER 語句,我們嘗試向 Phone_No 列新增UNIQUE約束。
ij> ALTER TABLE Employees ADD CONSTRAINT New_Constraint UNIQUE(Phone_No); 0 rows inserted/updated/deleted
一旦您向列添加了 UNIQUE 約束,它就不能對兩行具有相同的值,即每個員工的電話號碼必須唯一。
如果您嘗試新增兩列具有相同電話號碼的列,您將收到如下所示的異常。
ij> INSERT INTO Employees (Name, Salary, Location, Age, Phone_No) VALUES
('Amit', 30000, 'Hyderabad', 30, 9848022338);
1 row inserted/updated/deleted
ij> INSERT INTO Employees (Name, Salary, Location, Age, Phone_No) VALUES
('Sumit', 35000, 'Chennai', 25, 9848022338);
ERROR 23505: The statement was aborted because it would have caused a duplicate
key value in a unique or primary key constraint or unique index identified by
'NEW_CONSTRAINT' defined on 'EMPLOYEES'.
從表中刪除約束
以下是刪除列約束的語法:
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
示例
以下查詢刪除上面建立的 Phone_No 列上的約束名稱 New_Constraint。
ij> ALTER TABLE Employees DROP CONSTRAINT New_Constraint; 0 rows inserted/updated/deleted
由於我們已刪除了 Phone_No 列上的 UNIQUE 約束,因此您可以新增具有相同電話號碼的列。
ij> INSERT INTO Employees (Name, Salary, Location, Age, Phone_No) VALUES
('Sumit', 35000, 'Chennai', 25, 9848022338);
1 row inserted/updated/deleted
您可以透過以下方式驗證表的內容:select * from Employees 如下所示:
ID |NAME |SALARY |LOCATION |AGE |PHONE_NO ------------------------------------------------------------------------- 1 |Amit |30000 |Hyderabad|30 |9848022338 2 |Sumit |35000 |Chennai |25 |9848022338 2 rows selected
從表中刪除列
以下是刪除列的語法。
ALTER TABLE table_name DROP COLUMN column_name;
示例
以下查詢刪除名為age of the employee的列:
ij> ALTER TABLE Employees DROP COLUMN Age; 0 rows inserted/updated/deleted
如果您描述該表,您只能看到 4 列。
ij> DESCRIBE Employees; COLUMN_NAME |TYPE_NAME|DEC&|NUM&|COLUM&|COLUMN_DEF |CHAR_OCTE&|IS_NULL& ------------------------------------------------------------------------------ ID |INTEGER |0 |10 |10 |AUTOINCRE& |NULL |NO NAME |VARCHAR |NULL|NULL|255 |NULL |510 |YES SALARY |INTEGER |0 |10 |10 |NULL |NULL |NO LOCATION |VARCHAR |NULL|NULL|255 |NULL |510 |YES PHONE_NO |BIGINT |0 |10 |19 |NULL |NULL |YES
使用 JDBC 程式修改表
以下是使用 ALTER 查詢修改表的 JDBC 程式:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class AlterTableExample {
public static void main(String args[]) throws Exception {
//Registering the driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
//Getting the Connection object
String URL = "jdbc:derby:sampleDB;create=true";
Connection conn = DriverManager.getConnection(URL);
//Creating the Statement object
Statement stmt = conn.createStatement();
//Executing the query
String createQuery = "CREATE TABLE Employees( "
+ "Id INT NOT NULL GENERATED ALWAYS AS IDENTITY, "
+ "Name VARCHAR(255), "
+ "Salary INT NOT NULL, "
+ "Location VARCHAR(255), "
+ "PRIMARY KEY (Id))";
stmt.execute(createQuery);
System.out.println("Table created");
System.out.println(" ");
//Executing the query
String insertQuery = "INSERT INTO Employees("
+ "Name, Salary, Location) VALUES "
+ "('Amit', 30000, 'Hyderabad'), "
+ "('Kalyan', 40000, 'Vishakhapatnam'), "
+ "('Renuka', 50000, 'Delhi'), "
+ "('Archana', 15000, 'Mumbai'), "
+ "('Trupti', 45000, 'Kochin')";
stmt.execute(insertQuery);
System.out.println("Values inserted");
System.out.println(" ");
//Executing the query
String selectQuery = "SELECT * FROM Employees";
ResultSet rs = stmt.executeQuery(selectQuery);
System.out.println("Contents of the table after inserting the table");
while(rs.next()) {
System.out.println("Id: "+rs.getString("Id"));
System.out.println("Name: "+rs.getString("Name"));
System.out.println("Salary: "+rs.getString("Salary"));
System.out.println("Location: "+rs.getString("Location"));
}
System.out.println(" ");
//Altering the table
stmt.execute("ALTER TABLE Employees ADD COLUMN Age INT");
stmt.execute("ALTER TABLE Employees ADD COLUMN Phone_No BigINT");
stmt.execute("ALTER TABLE Employees " + "ADD CONSTRAINT New_Constraint UNIQUE(Phone_No)");
stmt.execute("INSERT INTO Employees "
+ "(Name, Salary, Location, Age, Phone_No) "
+ "VALUES ('Amit', 30000, 'Hyderabad', 30, 9848022338)");
ResultSet alterResult = stmt.executeQuery("Select * from Employees");
System.out.println("Contents of the table after altering "
+ "the table and inserting values to it: ");
while(alterResult.next()) {
System.out.println("Id: "+alterResult.getString("Id"));
System.out.println("Name: "+alterResult.getString("Name"));
System.out.println("Salary: "+alterResult.getString("Salary"));
System.out.println("Location: "+alterResult.getString("Location"));
System.out.println("Age: "+alterResult.getString("Age"));
System.out.println("Phone_No: "+alterResult.getString("Phone_No"));
}
}
}
輸出
執行上述程式後,將生成以下輸出:
Table created Values inserted Contents of the table after inserting the table Id: 1 Name: Amit Salary: 30000 Location: Hyderabad Id: 2 Name: Kalyan Salary: 40000 Location: Vishakhapatnam Id: 3 Name: Renuka Salary: 50000 Location: Delhi Id: 4 Name: Archana Salary: 15000 Location: Mumbai Id: 5 Name: Trupti Salary: 45000 Location: Kochin Contents of the table after altering the table and inserting values to it: Id: 1 Name: Amit Salary: 30000 Location: Hyderabad Age: null Phone_No: null Id: 2 Name: Kalyan Salary: 40000 Location: Vishakhapatnam Age: null Phone_No: null Id: 3 Name: Renuka Salary: 50000 Location: Delhi Age: null Phone_No: null Id: 4 Name: Archana Salary: 15000 Location: Mumbai Age: null Phone_No: null Id: 5 Name: Trupti Salary: 45000 Location: Kochin Age: null Phone_No: null Id: 6 Name: Amit Salary: 30000 Location: Hyderabad Age: 30 Phone_No: 9848022338
廣告