>create table userinfo f2
(id varchar2(10) primary key,
username varchar2(20),
typeid new varchar2(10)
constraint fk typeid new foreign key(typeid new)references typeinfo(typeid) on delete cascade):

创建表时设置外键约束
create table userinfo_f2
(id varchar2(10) primary key,
username varchar2(20),
typeid_new varchar2(10),
constraint fk_typeid_new foreign key(typeid_new)references typeinfo(typeid));
使用接连删除创建的外键约束
create table userinfo_f3
(id varchar2(10) primary key,
username varchar2(20),
typeid_new varchar2(10),
constraint fk_typeid_new1 foreign key(typeid_new)references typeinfo(typeid) on delete cascade);
FOREIGN KEY

--在创建表时,定义完所有字段,设置外键约束
-- CONSTRAINT constraint_name FOREIGN KEY(column_name) REFERENCES table_name(column_name)[ON DELETE CASCADE]; 语法
--constraint_name 外键名字
-- FOREIGN KEY(column_name) ,column_name 这个是从表的字段
--table_name(column_name) 主表的名字和字段名
--[ON DELETE CASCADE] 级联删除,主表的哪一行数据删除,从表的使用了主表的那一行数据,也会被删除
--示例
CREATE TABLE xcx_info (
id varchar2(10) primary key,
username varchar2(20),
type_new varchar2(10),
CONSTRAINT fk_type_new FOREIGN KEY (type_new) REFERENCES typeinnfo (typeid) ON DELETE CASCADE
);
2)在创建表时设置外键约束(表级)
CONSTRAINT constraint_name FOREIGN KEY (column_name) REFERENCES table_name(column_name)[ON DELETE CASCADE]
例:
create table userinfo_f2
(
id varchar2(10) primary key,
username varchar2(20),
typeid_new varchar2(10),
constraint fk_typeid_new foreign key(typeid_new) references typeinfo(typeid)
);
// 级联删除:
create table userinfo_f3
(
id varchar2(10) primary key,
username varchar2(20),
typeid_new varchar2(10),
constraint fk_typeid_new1 foreign key(typeid_new) references typeinfo(typeid) on delete cascade
);
使用级联删除的外键约束表创建
实例创建外键约束
创建表是设置外键约束,级联删除
创建表时设置外键约束(下)
CONSTRAINT constraint_name FOREIGN KEY(column_name) REFERENCES table_name(column_name) [ON DELETE CASCADE]
-----------------------------------------
create table userinfo_f2(
id varchar2(10) primary key,
username varchar2(20),
typeid_new varchar2(10),
constraint fk_typeid_new foreign key(typeid_new)references typeinfo(typeid));
----------------------------------------
create table userinfo_f3
(id varchar2(10) primary key,
username varchar2(20),
typeid_new varchar2(10),
constraint fk_typeid_new1 foreign key(typeid_new)references typeinfo(typeid) on delete cascade);
一脸蒙蔽,就记得连锁删除这一条
表级设置外键
语法:如图,该语句放置在所有字段定义完之后,其中的可选语句ON DELETE CASCADE为级联操作,当主表中删除一条记录时,从表中对应该主键的记录也会被删除。
注意:这里约束的名字也是需要唯一的。
举例:


表级设置外键
语法:如图,该语句放置在所有字段定义完之后,其中的可选语句ON DELETE CASCADE为级联操作,当主表中删除一条记录时,从表中对应该主键的记录也会被删除。
注意:这里约束的名字也是需要唯一的。
举例:


create table userinfo_f3 (id varchar2(10) primary key,username varchar2(20),typeid_new varchar2(10),constraint fk_typeid_new1 foreign key(typeid_new) references typeinfo(typeid) on delete cascade);级联删时创建表的外键约束
create table userinfo_f2 (id varchar2(10) primary key,username varchar2(20),typeid_new varchar2(10), constraint fk_typeid_new foreign key (typeid_new) references typeinfo(typeid));创建表时设置外键约束
在创建表时设置外键约束
CONSTRAINT constraint_name FOREIGN KEY(column_name) REFERENCES table_name(column_name)[ON DELETE CASCADE]
表级外键约束:
之前的约束是列级约束;
列:
使用及联删除创建:
在创建表时设置外键约束
外键约束对应主表的删除有三种处理模式,no action,set null,cascade。
create table tabname( usertypeid varchar2(20), constraint fk_typeid foreign key(usertypeid) references table2(typeid) on delete cascade); --table2表中的行被删除后,引用此行typeid 的 tabname 表中的数据也会被删除。
创建表时设置外键约束
constraint constraint_name foreign key(column_name) references
table_name(column_name) [on delete cascade];
[on delete cascade]是级联删除,如果主表中该条记录被删除,那么在从表中使用了这条记录的值也会被删除
创建表时创建表级约束:
创建表的最后添加:
constraint fk_[table_column] foreign key(table_column) references [pri_table](pri_table_column) on delete cascas;
外键2
constraint constraint_name foreign key (column_name) references table_name1(column_name1) [on delete cascade]