當前位置:首頁 » 編程語言 » 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中存在主外鍵關系時,能更新主鍵列的數據嗎

能更新的前提是外鍵表的列中沒有主鍵列對應的值時。否則是無法更新成功的,會報錯