在Oracle中設置外鍵可以通過以下幾種方法:
1、使用CREATE TABLE語句創建表時定義外鍵約束:
```sql
CREATE TABLE table1 (
id NUMBER PRIMARY KEY,
name VARCHAR2(50)
);
CREATE TABLE table2 (
id NUMBER PRIMARY KEY,
table1_id NUMBER,
CONSTRAINT fk_table1_id FOREIGN KEY (table1_id) REFERENCES table1(id)
);
```
2、使用ALTER TABLE語句添加外鍵約束:
```sql
ALTER TABLE table2
ADD CONSTRAINT fk_table1_id FOREIGN KEY (table1_id) REFERENCES table1(id);
```
3、使用ALTER TABLE語句修改外鍵約束:
```sql
ALTER TABLE table2
MODIFY CONSTRAINT fk_table1_id FOREIGN KEY (table1_id) REFERENCES table1(id);
```
4、使用ALTER TABLE語句刪除外鍵約束:
```sql
ALTER TABLE table2
DROP CONSTRAINT fk_table1_id;
```
5、在創建表時使用外鍵約束選項:
```sql
CREATE TABLE table2 (
id NUMBER PRIMARY KEY,
table1_id NUMBER CONSTRAINT fk_table1_id REFERENCES table1(id)
);
```
6、使用關鍵字FOREIGN KEY定義外鍵:
```sql
CREATE TABLE table2 (
id NUMBER PRIMARY KEY,
table1_id NUMBER,
FOREIGN KEY (table1_id) REFERENCES table1(id)
);
```