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);
外鍵含義:
如果公共關鍵字在一個關系中是主關鍵字,那麼這個公共關鍵字被稱為另一個關系的外鍵。由此可見,外鍵表示了兩個關系之間的相關聯系。以另一個關系的外鍵作主關鍵字的表被稱為主表,具有此外鍵的表被稱為主表的從表。外鍵又稱作外關鍵字。