當前位置:首頁 » 編程語言 » sql增加外鍵
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql增加外鍵

發布時間: 2023-01-29 17:13:08

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的主鍵和外鍵就是起約束作用。

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

Ⅲ 怎麼在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的主鍵和外鍵就是起約束作用。

Ⅳ sql用命令創建主鍵與外鍵。

1、為了方便大家理解,使用一個例子來幫助大家理解。意思大概就是通過引用表二中的欄位完成對表一欄位的約束。方法:

Ⅳ sql server如何添加外鍵

我們使用sql server創建數據表的時候,經常需要建立表之間的外鍵約束關系,那麼如何添加外鍵呢?下面我給大家分享一下。

工具/材料

sql server

  • 01

    首先我們先來建立兩個表,如下圖所示,班級表和年級表

  • 02

    然後右鍵單擊班級表,在彈出的菜單中選擇關系選項,如下圖所示

  • 03

    接下來在彈出的表和關系界面中設置外鍵對應欄位,如下圖所示

  • 04

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

Ⅵ sql 添加外鍵語句

資料庫表添加外鍵方法(以SqlSever2005資料庫為例):

1、新建兩張表,一張為主表,一張為副表。主表裡的外鍵就是連接到副表的主鍵。

Ⅶ SQL Server中建立外鍵的方法

首先我們打開資料庫表,找到要建立外鍵的表。並要確保要建立外鍵關系的列與主鍵表中的數據類型完全相同。

然後我們在要建立外鍵關系的表中,右擊關系,在外鍵關系對話框中,點擊左下角的添加,接著點擊【表和列規范】項的右側的小按鈕,在表和列對話框中,在主鍵表下方選擇外鍵列所在的表和該外鍵列。

接著在外鍵表下方選擇表中與主鍵表相對應就可以了,然後點擊確定回到外鍵關系表。

最後我們就已經成功建立了一個主外鍵關系。在外鍵表中隨意排列左右選擇關系就可以看到了。

Ⅷ SQL資料庫的、外鍵和查詢

增加外鍵

創建表的時候增加外鍵:在所有的表欄位之後,使用foreign key(外鍵欄位) references 外部表(主鍵欄位)

在新增表之後增加外鍵:修改表結構,使用alter table 表名 add [constraint 外鍵名字] foreign key(外鍵欄位) references 父表(主鍵欄位);

修改外鍵&刪除外鍵

alter table 表名 drop foreign key 外鍵名;

外鍵條件

外鍵要存在,首先必須保證表的存儲引擎是innodb

列類型必須與父表的主鍵類型一致

一張表中的外鍵名字不能重復

增加外鍵的欄位數據已經存在,必須保證數據與父表主鍵要求對應

外鍵約束

有三種約束模式

district:嚴格模式(默認的)

cascade:級聯模式

set null:置空模式

語法:foreign key(外鍵欄位) references 父表(主鍵欄位) on delete 模式 on update 模式;

聯合查詢

基本語法:

select 語句1

union [union 選項]

select 語句2……

union 選項

all:保留所有,不管重復

distinct:去重,默認的

子查詢(sub query)

按位置分類

from子查詢

where子查詢

exists子查詢

按結果分類

標量子查詢

列子查詢

行子查詢

表子查詢

子查詢

列子查詢

=any等價於in; -- 其中一個即可

any等價於some; -- 二者是一樣的

=all為全部

-- 創建外鍵

create table my_foreign1(

idint primary key auto_increment,

name varchar (20)not null comment

'學生姓名',

c_idint comment'班級id',

-- 增加外鍵

foreign key(c_id)references

my_class(id)

)charset utf8;

-- 創建表

create table my_foreign2(

idint primary key auto_increment,

name varchar (20)not null comment

'學生姓名',

c_idint comment'班級id'  -- 普通欄位

)charset utf8;

-- 增加外鍵

alter table my_foreign2add

-- 指定外鍵的名字

constraint student_class_1  -- 可以指定多個外鍵 但是名字不能相同

-- 指定外鍵的欄位

foreign key(c_id)

-- 引用父表主鍵

references my_class(id);

-- 刪除外鍵

alter table my_foreign1drop

foreign key my_foreign1_ibfk_1;  -- my_foreign1_ibfk_1 通過外鍵的名字來刪

-- 插入數據;外鍵欄位在父表不存在

insert into my_foreign2values (

null,'郭富城',4);  -- 沒有4號班級

insert  into my_foreign2values (

null,'項羽',1);

insert  into my_foreign2values (

null,'劉邦',2);

insert  into my_foreign2values (

null,'韓信',3);

-- 更新父表的記錄

update my_classset id=4 where id=1;  -- 失敗;id=1記錄已經被學生引用

update my_foreign2set c_id=2 where id=4;  -- 更新

update my_classset id=4 where id=3;  -- 可以;沒有學生引用此班級

-- mysql中添加外鍵約束遇到一下情況:

-- cannot add foreign key constraint

-- 出現這個問題的原因是,外鍵的使用:

-- 1. 外鍵欄位不能為該表的主鍵;

-- 2. 外鍵欄位參考欄位必須為參考表的主鍵

-- 插入數據

insert into my_foreign1values (

null,'馬超','3'

);

-- 增加外鍵

alter table my_foreign1add

foreign key(c_id)references

my_class(id);  -- 失敗;因為沒有3號班了

-- 創建外鍵,指定模式;刪除置空;更新級聯

create table my_foreign3(

idint primary key auto_increment,

name varchar (20)not null,

c_idint,

-- 增加外鍵

foreign key (c_id)

-- 引用表

references my_class(id)

-- 指定刪除模式

on delete set null

-- 指定更新模式

on update cascade

)charset utf8;

-- 插入數據

insert into my_foreign3values (

null,'劉備',1),

(null,'曹操',1),

(null,'孫權',1),

(null,'祝賀量',2),

(null,'周瑜',2);

-- 解除My_foreign2表的外鍵

alter table my_foreign2drop

foreign key student_class_1;

-- 更新父表主鍵

update my_classset id=3 where id=1;

-- 刪除父表主鍵

delete from  my_classwhere id=2;

-- 聯合查詢

select * from my_class

union  -- 默認去重

select * from my_class;

select * from my_class

union all  -- 不去重

select * from my_class;

select id,c_name,roomfrom my_class

union all  -- 不去重

select name,number,idfrom my_student;

-- 需求;男生升序;女生降序(年齡)

(select * from my_student

where sex='男'

order by ageasc limit9999999)

union

(select * from my_student

where sex='女'

order by agedesc limit9999999);

select * from my_studentwhere

c_id=(

-- 標量子查詢

select idfrom my_classwhere

c_name='python1903');-- id一定只有一個值(一行一列)

insert into my_classvalues (1,

'python1907','B407');

-- 列子查詢

select * from my_studentwhere

c_idin(select idfrom my_class);

-- any,some,all

select * from my_studentwhere

c_id=any(select idfrom my_class);

select * from my_studentwhere

c_id=some(select idfrom my_class);

select * from my_studentwhere

c_id=all(select idfrom my_class);

select * from my_studentwhere

c_id!=any(select idfrom my_class);  -- 所有結果(null除外)

select * from my_studentwhere

c_id!=some(select idfrom my_class);  -- 所有結果(null除外)

select * from my_studentwhere

c_id!=all(select idfrom my_class);  -- 所有2號班級(null除外)

select * from my_studentwhere

age=(select max(age)from

my_student)

and

height=(select max(height))from

my_student);

-- 行子查詢

select * from my_student

-- (age,height)稱之內為行元素

where (age,height)=(select max(

age),max(height)from my_student);

update my_studentset height=188

where name='王五';

select * from my_studentorder by

agedesc,heightdesc limit1;

select * from my_studentorder by

heightdesc;

-- 表子查詢

select * from my_studentgroup by

c_idorder by heightdesc;  -- 每個班選出第一個學生再按身高排序

select * from (select * from

my_studentorder by heightdesc)

as studentgroup by student.c_id;

Ⅸ sql中外鍵怎麼寫

sql中外鍵怎麼寫的方法。

如下參考:

1.創建測試表;創建表test_class(class_idvarchar2(10),class_namevarchar2(30));創建表test_student(student_idvarchar2(10),student_namevarchar2(30),class_idvarchar2(10));

Ⅹ sql創建外鍵語句

1、創建測試主表(班級表test_class),

create table test_class(class_id number, class_name varchar2(20));