sql添加外键约束
A. 怎么添加外键约束
sql server中建立外键约束有3中方式:
1.Enterprise Manager中,Tables,Design Table,设置Table的properties,
可以建立constraint, reference key;
2.Enterprise Manager中,Diagrams, new Diagrams,建立两个表的关系。
3.直接用transact sql语句。
下面讲解一下用SQL添加外键约束的实例:
一个SQL创建外键的例子:
/**//*建库,名为student_info*/
create database student_info
/**//*使用student_info*/
use student_info
go
/**//*建student表,其中s_id为主键*/
create table student
(
s_id int identity(1,1) primary key,
s_name varchar(20) not null,
s_age int
)
go
/**//*建test表,其中test_no为主键*/
create table test
(
test_no int identity(1,1) primary key,
test_name varchar(30),
nax_marks int not null default(0),
min_marks int not null default(0)
)
go
/**//*建marks表,其中s_id和test_no为外建,分别映射student表中的s_id和test表中的test_no*/
create table marks
(
s_id int not null,
test_no int not null,
marks int not null default(0),
primary key(s_id,test_no),
foreign key(s_id) references student(s_id),
foreign key(test_no) references test(test_no)
)
go
参考资料: http://www.studyofnet.com/news/93.html
希望以上的回答能够帮到你!
B. 怎么添加外键约束
sql server中建立外键约束有3中方式: 1.Enterprise Manager中,Tables,Design Table,设置Table的properties, 可以建立constraint, reference key; 2.Enterprise Manager中,Diagrams, new Diagrams,建立两个表的关系。 3.直接用transact sql语句。 下面讲解一下用SQL添加外键约束的实例: 一个SQL创建外键的例子: /**//*建库,名为student_info*/ create database student_info /**//*使用student_info*/ use student_info go /**//*建student表,其中s_id为主键*/ create table student ( s_id int identity(1,1) primary key, s_name varchar(20) not null, s_age int ) go /**//*建test表,其中test_no为主键*/ create table test ( test_no int identity(1,1) primary key, test_name varchar(30), nax_marks int not null default(0), min_marks int not null default(0) ) go /**//*建marks表,其中s_id和test_no为外建,分别映射student表中的s_id和test表中的test_no*/ create table marks ( s_id int not null, test_no int not null, marks int not null default(0), primary key(s_id,test_no), foreign key(s_id) references student(s_id), foreign key(test_no) references test(test_no) ) go 参考资料: http://www.studyofnet.com/news/93.html 希望以上的回答能够帮到你!
C. sql语句添加外键约束。
外键关系通俗来讲就是将两个表关联起来用的
以学生和班级来举例子
新建一个学生表student
新建一个班级表 grade
然后每个学生,都对应一个班级,比如学生A是X班,学生B也是X班,就没必要在学生表里面存储重复的班级名称,因此需要一个班级表
只要存储一个班级表的id,就可以记录该学生班级的所有详细信息了
关联起来显示就是:
select * from student, grade where student.gradeId = grade.id
通过外键关联,显示学生信息和班级信息的所有数据,并根据外键进行一一匹配
D. 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的主键和外键就是起约束作用。
E. sql server如何添加外键
我们使用sql server创建数据表的时候,经常需要建立表之间的外键约束关系,那么如何添加外键呢?下面我给大家分享一下。
工具/材料
sql server
首先我们先来建立两个表,如下图所示,班级表和年级表
然后右键单击班级表,在弹出的菜单中选择关系选项,如下图所示
接下来在弹出的表和关系界面中设置外键对应字段,如下图所示
最后我们就可以在左侧看见外键约束关系了,如下图所示
F. 怎么在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的主键和外键就是起约束作用。
G. 在SQL中如何创建外键约束
添加外键
,alter
table
b
语法:alter
table
表名
add
constraint
外键约束名
foreign
key(列名)
references
引用外键表(列名)
如:
alter table stu_pkfk_sc
add constraint fk_s
foreign key (sno)
references stu_pkfk_s(sno)--cc是外键约束名,不能重复,也不能是int类型(如1,2,3)
add
constraint
cc
--b表里的需要约束的字段(id)
foreign
key
(id)
--a表后的(id)可省略
references
a
(id)
H. SQL中如何为表添加外键约束
-〉INDEX
(category_id),
->
FOREIGN
KEY
(category_id)
REFERENCES
categories
(category_id),
->
CONSTRAINT
fk_member
FOREIGN
KEY
(member_id)
REFERENCES
members
(member_id),
->
PRIMARY
KEY(article_id)
范例中的添加外键约束就是这种形式
如果是概念的问题,直接参考书籍,一般添加外键约束,主要在创建表中.
I. sql中怎样创建外键约束
添加外键
,alter
table
B
语法:alter
table
表名
add
constraint
外键约束名
foreign
key(列名)
references
引用外键表(列名)
如:
alter table Stu_PkFk_Sc
add constraint Fk_s
foreign key (sno)
references Stu_PkFk_S(sno)--cc是外键约束名,不能重复,也不能是int类型(如1,2,3)
add
constraint
cc
--B表里的需要约束的字段(id)
foreign
key
(id)
--A表后的(id)可省略
references
A
(id)
J. sql server如何添加外键
我们使用sql server创建数据表的时候,经常需要建立表之间的外键约束关系,那么如何添加外键呢?下面我给大家分享一下。
工具/材料
sql server
- 01
首先我们先来建立两个表,如下图所示,班级表和年级表
- 02
然后右键单击班级表,在弹出的菜单中选择关系选项,如下图所示
- 03
接下来在弹出的表和关系界面中设置外键对应字段,如下图所示
- 04
最后我们就可以在左侧看见外键约束关系了,如下图所示