設(shè)置外鍵,navicat外鍵怎么設(shè)置

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供城北網(wǎng)站建設(shè)、城北做網(wǎng)站、城北網(wǎng)站設(shè)計、城北網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、城北企業(yè)網(wǎng)站模板建站服務(wù),10余年城北做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

本文目錄一覽

1,navicat外鍵怎么設(shè)置

打開我的navicat,然后找到我的teacher表,選中它,然后點擊菜單欄上  在彈出的對話框中找到“Foreign Keys”,然后單機。

2,SQL如何設(shè)置外鍵

可以在創(chuàng)建表的時候創(chuàng)建,也可以在創(chuàng)建表之后創(chuàng)建。創(chuàng)建表時創(chuàng)建:create table student(id int primary key,name char(4),dept char(9)sex char(4))create table grade(id int ,grade intconstraint id_fk foreign key (id) references student (id))或創(chuàng)建了兩表之后再建alter table gradeadd constraint id_fk foreign key (id) references student (id)呵呵,希望能幫助你。

首先在booktype表中定義主鍵:booktypeidcreate table booktype (booktypeid varchar(20) primary key,typename varchar(20));create table book (bookid int primary key, bookname varchar(20),booktypeid varchar(20) ,constraint fk foreign key(booktypeid) references booktype(booktypeid));

3,如何設(shè)置外鍵約束

create table a ( a_id int primary key, ##主鍵 a_name varchar(2))create table b( b_id int , b_name varchar(2))##添加外鍵alter table b add constraint fk_b_a foreign key b_id references a(a_id)

create table t1(A1 int primary key)create table t2(B1 int,B2 int)--對t2表的B2創(chuàng)建外鍵alter table t2 add constraint FK_B2_t1A1 foreign key(B2) references t1(A1)--注意:能作為一個表的外鍵關(guān)聯(lián)字段(t1.A1)這個字段必須是主鍵或有唯一約束的(t1的A1必須是主鍵或者unique)

create table t1(A1 int primary key)create table t2(B1 int,B2 int)--對t2表的B2創(chuàng)建外鍵(關(guān)聯(lián)字段t1表的A1字段)alter table t2 add constraint FK_B2_t1A1 foreign key(B2) references t1(A1)--注意:能作為一個表的外鍵關(guān)聯(lián)字段(t1.A1)這個字段必須是主鍵或有唯一約束的(t1的A1必須是主鍵或者unique)

4,怎么給聯(lián)合主鍵設(shè)置外鍵啊

我正好有一個例子和你說的一樣,請參考:CREATE TABLE [dbo].[ContractorMobileService]( [ContractorID] [int] NOT NULL, [MobileServiceID] [int] NOT NULL, [Charge] [money] NOT NULL, CONSTRAINT [PK_ContractorMobileService] PRIMARY KEY CLUSTERED ( [ContractorID] ASC, [MobileServiceID] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOALTER TABLE [dbo].[ContractorMobileService] WITH NOCHECK ADD CONSTRAINT [FK_ContractorMobileService_Contractor] FOREIGN KEY([ContractorID])REFERENCES [dbo].[Contractor] ([ID])GOALTER TABLE [dbo].[ContractorMobileService] CHECK CONSTRAINT [FK_ContractorMobileService_Contractor]GOALTER TABLE [dbo].[ContractorMobileService] WITH NOCHECK ADD CONSTRAINT [FK_ContractorMobileService_MobileService] FOREIGN KEY([MobileServiceID])REFERENCES [dbo].[MobileService] ([ID])GOALTER TABLE [dbo].[ContractorMobileService] CHECK CONSTRAINT [FK_ContractorMobileService_MobileService]GO

語句的我知道用references就行 可是可視化的操作可以做到么?

GUI界面,先右鍵表B,表C,選擇設(shè)計,然后選擇表B字段b1設(shè)置主鍵,表C字段c1設(shè)置主鍵,然后保存關(guān)閉。右鍵表A,選擇設(shè)計,按住shift然后選擇a1,a2設(shè)置為聯(lián)合主鍵,然后右鍵a1列,選擇關(guān)系,添加后右面點擊表和列規(guī)范,彈出界面,左邊選擇主鍵表主鍵列,右面選擇本表字段a1即可。a2同樣設(shè)置。

5,如何創(chuàng)建外鍵是本身表的主鍵的外鍵

create table NewsType(id INT PRIMARY KEY ,parentId int references NewsType(id))其他字段你自己添吧 比如插入測試數(shù)據(jù)insert into NewsType values (1,1);--這個執(zhí)行沒問題insert into NewsType values (2,3);--會報錯

可以用數(shù)據(jù)庫工具創(chuàng)建。具體方法如下:create table TabA (AF1 string ,AF2 int null,AF3 int null)create table TabB (BF1 string ,BF2 int )//創(chuàng)建constraint PK_AF1 primary key (AF1)constraint TabB primary key (BF1),constraint FK_Tab1_TAB2 foreign key (BF2)references TabA(AF1)具體例子如下:create table VAS_POSSTOR_DETAIL ( TEAMNO VARCHAR2(10) not null, POS_NO VARCHAR2(3) not null, ITEM_CODE VARCHAR2(10) not null, SORT_NO VARCHAR2(5) not null, INIT_QUANTITY NUMBER, STO_QUANTITY NUMBER, ORD_QUANTITY NUMBER, DEL_PRICE NUMBER(10,4), constraint PK_VAS_POSSTOR_DETAIL primary key (TEAMNO, POS_NO, ITEM_CODE), constraint FK_VAS_POSS_REFERENCE_VAS_POS foreign key (TEAMNO, POS_NO) references VAS_POSSTORAGE (TEAMNO, POS_NO)

??創(chuàng)建表的語法-創(chuàng)建表格語法:create table 表名(???? 字段名1?? 字段類型(長度) 是否為空,???? 字段名2?? 字段類型???????????? 是否為空);-增加主鍵alter table 表名 add constraint 主鍵名 primary key (字段名1);-增加外鍵:alter table 表名???? add constraint 外鍵名 foreign key (字段名1)?????????? references 關(guān)聯(lián)表 (字段名2);在建立表格時就指定主鍵和外鍵create table t_stu?? (

網(wǎng)頁名稱:設(shè)置外鍵,navicat外鍵怎么設(shè)置
網(wǎng)址分享:http://bm7419.com/article48/ehidhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、網(wǎng)站排名、營銷型網(wǎng)站建設(shè)建站公司、軟件開發(fā)App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)