當前位置:首頁 » 編程語言 » sql添加外鍵約束

sql添加外鍵約束

發布時間: 2022-11-04 21:24:31

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

    最後我們就可以在左側看見外鍵約束關系了,如下圖所示

熱點內容
linux下的程序開發 發布:2025-05-19 18:55:02 瀏覽:925
該文件夾未包含 發布:2025-05-19 18:54:17 瀏覽:194
安卓拳皇對戰用哪個平台 發布:2025-05-19 18:42:39 瀏覽:530
華為暢玩5怎麼取消鎖屏密碼 發布:2025-05-19 18:42:38 瀏覽:582
linuxrm文件夾 發布:2025-05-19 18:40:25 瀏覽:972
譚浩強c語言錯誤 發布:2025-05-19 18:39:33 瀏覽:951
安卓和蘋果用流量哪個劃算 發布:2025-05-19 18:38:04 瀏覽:408
安卓手機怎麼設定背景牆 發布:2025-05-19 18:29:40 瀏覽:1001
androidstudio斷點調試 發布:2025-05-19 18:20:23 瀏覽:766
abaqus如何配置證書 發布:2025-05-19 18:19:38 瀏覽:583