当前位置:首页 » 编程语言 » sql主外键主键不允许更新
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql主外键主键不允许更新

发布时间: 2022-12-19 21:16:05

1. 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的主键和外键就是起约束作用。

2. sql 主键和外键数据同步吗

假如一个表中有主键, 还有连接另外一个或多个表的外键..
如果这个表更新数据,一般外键连接的表是不会更新数据的.
除非你自己设置了数据同步更新,比如说利用触发器.存储过程等等同步数据,这样的话就就可以同步了.
所以如果你什么都没有设置,它是不会主动同步更新的!

3. SQL表主键可不可以修改update

ifobject_id('primarytbl')isnotnull
droptableprimarytbl
go

--建主表
createtableprimarytbl
(
IDintprimarykey,--主键
aaint,
bbint,
ccint
)
go

ifobject_id('foreigntbl')isnotnull
droptableforeigntbl
go

--建外表
createtableforeigntbl
(
IDintprimarykey,--主键
aaint
(ID)--建立外键
onupdatecascade,--更新级联
ddint,
eeint
)
go

--插入主表数据
insertintoprimarytbl
select1,1,2,3unionall
select2,2,3,4unionall
select3,3,4,5unionall
select4,4,5,6unionall
select5,5,6,7unionall
select6,6,7,8
go

--插入外表数据
insertintoforeigntbl
select1,1,2,2unionall
select2,1,3,3unionall
select3,2,4,4unionall
select4,2,4,4unionall
select5,2,5,5unionall
select6,3,6,6unionall
select7,4,7,7
go

--显示主外表信息
select*
fromprimarytbl

select*
fromforeigntbl
go
--primarytbl
/*
IDaabbcc
--------------------------------------------
1123
2234
3345
4456
5567
6678
--foreigntbl
IDaaddee
--------------------------------------------
1122
2133
3244
4244
5255
6366
7477
*/

--更新主表主键
updateprimarytbl
setID=8
whereID=1
go

--结果
select*
fromprimarytbl

select*
fromforeigntbl
go

/*
--primarytbl
IDaabbcc
--------------------------------------------
2234
3345
4456
5567
6678
8123
--foreigntbl
IDaaddee
--------------------------------------------
1822
2833
3244
4244
5255
6366
7477
*/

droptableforeigntbl
droptableprimarytbl

4. 数据库的两个表通过主键和外键相关联如果修改其中表的数据会不会自动修改另一个表的数据啊

现有俩表A,B,A表有主键,B表建立外键关联到A表
修改A表,若修改之后的结果是B表外键字段的值在A表中找不到了,则会报错,不允许进行此修改,其他情况可以任意修改。
修改B表,必须保证修改后B表外键字段的值依然能在A表中找到,否则会报错。
俩表自己的修改,只会影响自己表的数据,对其他表无影响。

5. sql主键和外键的区别

主键就是当前表的id,表示唯一字段,方便定位索引查询……
外键就是其他表的主键ID在当前表中是外键,用来做关联关系。
create table A(
aid primary key identity(1) not null, 主键
aname varchar(50),
bid 外键(忘了这单词怎么写了)

create table B(
bid primary key identity(1) not null, 主键
bsubject varchar(100)
)
数据:A表 1,张三 ,1 B表: 1,数学
2,李四,1 2,英语
3,张三,2
4,王五,2
这样子B表跟A表就有关联关系了。我们查询或者索引的时候可以按ID来找人对吧,
也可以, 1,张三,数字
2,李四,数字
3,张三,英语
4,王五,英语

6. sql添加外主键约束为什么不更新修改

--我也是初学者,共同研究下,你最好一段一段执行,要不就没意义了
use school
drop table teacher1
drop table student1
go
create table teacher1
(
t_id int primary key,
t_name nvarchar(10) not null
)
go
insert into teacher1 values(1347,'张三')
insert into teacher1 values(2680,'李四')
insert into teacher1 values(6379,'王五')
go
create table student1
(
s_id int primary key,
t_id int
)
go
alter table student1
add constraint FK foreign key(t_id) references teacher1(t_id) on update cascade
insert into student1 values(1,2680)
insert into student1 values(2,6379)
insert into student1 values(3,1347)
insert into student1 values(4,6379)
insert into student1 values(5,1347)
--两个表建好了,大概按照你的意思,不知道是不是这种情况
go
select * from student1;
select * from teacher1; --查询所有数据
go
--在student1表(外键表)中加入一组数据
insert into student1 values(6,1234) --失败了,因为外键表中外键的值必须是引用主键表中关联的主键的值
--也就是外键约束,或称参照完整性
--外键约束是约束外键的数据的取值的
insert into teacher1 values(1234,'童川') --然后在teacher1表(主键表)中加一组数据
--成功,外键约束对主键表被引用的主键无影响
insert into student1 values(6,1234) --成功,验证了失败的原因
go
select * from student1;
select * from teacher1; --再次查询所有数据
--on update cascade 这个东西我也没学过,网络了一下,大概明白了
go
update teacher1 set t_id = t_id + 10000
where t_name in('童川') --成功
update student1 set t_id = t_id + 10000 --提示与外键约束发生冲突
go
select * from student1;
select * from teacher1; --看到了么?学生表中引用'童川'老师的数据的一列数据中t_id的值也随之改变
--我想这就是on update cascade的作用
--主键的值改变后,引用它的外键的值也随之改变
--最后顺便一提,童川是我的室友,恶搞一下他,嘿嘿···

7. SQL2005 设置了主键,能用update更新主键吗

如果id这列没有重复值的话可以修改
如果要修改的值已经存在了
就不可以修改了
当然这个是在2000下
2005下我还没有实践过~~

8. sql中存在主外键关系时,能更新主键列的数据吗

能更新的前提是外键表的列中没有主键列对应的值时。否则是无法更新成功的,会报错