A. 在sql中如何创建外键约束
可以用创建关系图的方式进行约束,步骤如下
企业管理器中打开数据库,新建关系图,选出自己所要的几张表,然后将对应的外键用鼠标连接到另一张表的主键上就行了
ps
环境
sql2000
B. SQL如何建立外键请教高手了
数据库mysql
建立外键的前提:
本表的列必须与外键类型相同(外键必须是外表主键)。
外键作用:
使两张表形成关联,外键只能引用外表中的列的值!
指定主键关键字:
foreign
key(列名)
引用外键关键字:
references
<外键表名>(外键列名)
事件触发限制:
on
delete和on
update
,
可设参数cascade(跟随外键改动),
restrict(限制外表中的外键改动),set
Null(设空值),set
Default(设默认值),[默认]no
action
例如:
outTable表
主键
id
类型
int
创建含有外键的表:
create
table
temp(
id
int,
name
char(20),
foreign
key(id)
references
outTable(id)
on
delete
cascade
on
update
cascade);
说明:把id列
设为外键
参照外表outTable的id列
当外键的值删除
本表中对应的列筛除
当外键的值改变
本表中对应的列值改变。
自己实践
才能完全了解外键的作用
关键是:事件触发限制的作用
C. plsql里如何追加外键
你查一下Oracle追加外键的SQL是什么,完后你打开数据连接,写SQL就可以了啊
图形化的话,在表的属性里面添加也行
望采纳
D. sql创建外键语句
1、创建测试主表(班级表test_class),
create table test_class(class_id number, class_name varchar2(20));
E. 怎么在SQL中设置外键
sql ce表中建立外键约束的语法:CREATE TABLE DetectTable(UserID integer,StartTime datetime not null,EndTime datetime not null,MassName nvarchar(10), foreign key (UserID) references UserTable(UserID)),其中,UserID为UserTable表中的主键。
也可以在创建数据库关系图直接拖
在数据库关系图上右键-->新建关系图-->添加表
然后直接用鼠标拖字段连接就可以建立外键约束了
F. 怎么在SQL中设置外键
sql ce表中建立外键约束的语法:CREATE TABLE DetectTable(UserID integer,StartTime datetime not null,EndTime datetime not null,MassName nvarchar(10), foreign key (UserID) references UserTable(UserID)),其中,UserID为UserTable表中的主键。
也可以在创建数据库关系图直接拖
在数据库关系图上右键--新建关系图--添加表
然后直接用鼠标拖字段连接就可以建立外键约束了
G. 怎么在SQL中设置外键
sql ce表中建立外键约束的语法:CREATE TABLE DetectTable(UserID integer,StartTime datetime not null,EndTime datetime not null,MassName nvarchar(10), foreign key (UserID) references UserTable(UserID)),其中,UserID为UserTable表中的主键。 也可以在创建数据库关系图直接拖 在数据库关系图上右键--新建关系图--添加表 然后直接用鼠标拖字段连接就可以建立外键约束了
H. sql怎么设置外键
sql
server中建立外键约束有3中方式:enterprise
manager中,tables,design
table,设置table的properties,可以建立constraint,
reference
key;enterprise
manager中,diagrams,
new
diagrams,建立两个表的关系;直接用transact
sql语句。
1、三个方法都需要先建立数据表。
1)创建表author
:
create
table
[dbo].[author]
(
[id]
[bigint]
not
null
,
[authorname]
[char]
(10)
null
,
[address]
[char]
(480)
null
,
[introction]
[ntext]
null
)
2)创建表mybbs:
reate
table
[dbo].[mybbs]
(
[id]
[bigint]
identity
(1,
1)
not
null
,
[authorid]
[bigint]
not
null
,
[title]
[char]
(40)
null
,
[date_of_created]
[datetime]
null
,
[abstract]
[char]
(480)
null
,
[content]
[ntext]
null
)
2、设置表mybbs中的authorid为外键,参照author表的id字段,直接使用transact
sql语句,过程如下:
1)增加表mybbs(authorid)的外键约束fk_mybbs_author,表mybbs中的authorid受表author中的主键id约束:
begin
transaction
alter
table
dbo.mybbs
add
constraint
fk_mybbs_author
foreign
key
(authorid)
references
dbo.author([id])
on
update
cascade
on
delete
cascade
2)删除外键约束fk_mybbs_author:
--alter
table
dbo.mybbs
drop
constraint
fk_mybbs_author
--rollback
commit
transaction
上面on
update
cascade,on
delete
cascade两个选项,指明以后author表的id字段有delete,update操作时,mybbs表中的id也会被级联删除或更新。如果没有选中,是不可以对author表中已被mybbs表关联的id进行update或者delete操作的。
拓展资料:
SQL的主键和外键的作用:
1、插入非空值时,如果主键表中没有这个值,则不能插入。
2、更新时,不能改为主键表中没有的值。
3、删除主键表记录时,你可以在建外键时选定外键记录一起级联删除还是拒绝删除。
4、更新主键记录时,同样有级联更新和拒绝执行的选择。
简而言之,SQL的主键和外键就是起约束作用。
I. sql怎么设置外键
sql server中建立外键约束有3中方式:enterprise manager中,tables,design table,设置table的properties,可以建立constraint, reference key;enterprise manager中,diagrams, new diagrams,建立两个表的关系;直接用transact sql语句。
1、三个方法都需要先建立数据表。
1)创建表author :
create table [dbo].[author] (
[id] [bigint] not null ,
[authorname] [char] (10) null ,
[address] [char] (480) null ,
[introction] [ntext] null
)
2)创建表mybbs:
reate table [dbo].[mybbs] (
[id] [bigint] identity (1, 1) not null ,
[authorid] [bigint] not null ,
[title] [char] (40) null ,
[date_of_created] [datetime] null ,
[abstract] [char] (480) null ,
[content] [ntext] null
)
2、设置表mybbs中的authorid为外键,参照author表的id字段,直接使用transact sql语句,过程如下:
1)增加表mybbs(authorid)的外键约束fk_mybbs_author,表mybbs中的authorid受表author中的主键id约束:
begin transaction
alter table dbo.mybbs add constraint fk_mybbs_author
foreign key (authorid)
references dbo.author([id]) on update cascade on delete cascade
2)删除外键约束fk_mybbs_author:
--alter table dbo.mybbs drop constraint fk_mybbs_author
--rollback
commit transaction
上面on update cascade,on delete cascade两个选项,指明以后author表的id字段有delete,update操作时,mybbs表中的id也会被级联删除或更新。如果没有选中,是不可以对author表中已被mybbs表关联的id进行update或者delete操作的。
拓展资料:
SQL的主键和外键的作用:
1、插入非空值时,如果主键表中没有这个值,则不能插入。
2、更新时,不能改为主键表中没有的值。
3、删除主键表记录时,你可以在建外键时选定外键记录一起级联删除还是拒绝删除。
4、更新主键记录时,同样有级联更新和拒绝执行的选择。
简而言之,SQL的主键和外键就是起约束作用。
J. sql server 中 在已经有的数据表中,如何添加一列外键
可以先添加字段,然后再在字段上建立外键,分以下两步:
如表名为sc,其中添加一个字段为sid,是student表中sid的外键,可用以下语句:
1、
alter table sc add sid varchar(20);
2、
alter table sc add constraint fk_sid foreign key (sid) references student(sid);
外键含义:
如果公共关键字在一个关系中是主关键字,那么这个公共关键字被称为另一个关系的外键。由此可见,外键表示了两个关系之间的相关联系。以另一个关系的外键作主关键字的表被称为主表,具有此外键的表被称为主表的从表。外键又称作外关键字。