oracle 如何创建表外键

2025年03月23日 05:16
有4个网友回答
网友(1):

--使用表级约束
CREATE TABLE table_name
(column_1 datatype ,
column_2 datatype ,
...
CONSTRAINT fk_column
FOREIGN KEY (column_1, column_i, ... column_n)
REFERENCES parent_table (column_1, column_i, ... column_n)
);

--使用列级约束
CREATE TABLE table_name
(column_1 datatype ,
column_2 datatype CONSTRAINT fk_column REFERENCES parent_table (column_name),
...
);

网友(2):

alter table XXXXX add constraint [foreignKey name] FOREIGN KEY ([column_1,column_2....]) FEFERENCES [parent_table name] ([column_1,column_2....]) using index tablespace XXXXX

注意 parent_table里指定的列必须是主键

网友(3):

alter table t_stu add constraint fk_name foreign key (kch) references t_kc(kcid) ;
这句话的意思是在学生表(t_stu)的课程号上建个外外键,引用课程表(t_kc)的课程主键课程的id(kcid)

网友(4):

create table tbname
(
id number ,
cd number,
name varchar2(20),
primary key(id,cd),
foreign key(cd) references 你的外表(cd)
);714439306希望有所提示,有空到365testing,测评网,51cto进一步交流!