創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、八步網(wǎng)絡(luò)推廣、微信小程序定制開發(fā)、八步網(wǎng)絡(luò)營(yíng)銷、八步企業(yè)策劃、八步品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供八步建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.rwnh.cn
打開我的navicat,然后找到我的teacher表,選中它,然后點(diǎn)擊菜單欄上 在彈出的對(duì)話框中找到“Foreign Keys”,然后單機(jī)。
可以在創(chuàng)建表的時(shí)候創(chuàng)建,也可以在創(chuàng)建表之后創(chuàng)建。創(chuàng)建表時(shí)創(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));
create table t1(A1 int primary key)create table t2(B1 int,B2 int)--對(duì)t2表的B2創(chuàng)建外鍵alter table t2 add constraint FK_B2_t1A1 foreign key(B2) references t1(A1)--注意:能作為一個(gè)表的外鍵關(guān)聯(lián)字段(t1.A1)這個(gè)字段必須是主鍵或有唯一約束的(t1的A1必須是主鍵或者unique)
create table t1(A1 int primary key)create table t2(B1 int,B2 int)--對(duì)t2表的B2創(chuàng)建外鍵(關(guān)聯(lián)字段t1表的A1字段)alter table t2 add constraint FK_B2_t1A1 foreign key(B2) references t1(A1)--注意:能作為一個(gè)表的外鍵關(guān)聯(lián)字段(t1.A1)這個(gè)字段必須是主鍵或有唯一約束的(t1的A1必須是主鍵或者unique)
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)
GUI界面,先右鍵表B,表C,選擇設(shè)計(jì),然后選擇表B字段b1設(shè)置主鍵,表C字段c1設(shè)置主鍵,然后保存關(guān)閉。右鍵表A,選擇設(shè)計(jì),按住shift然后選擇a1,a2設(shè)置為聯(lián)合主鍵,然后右鍵a1列,選擇關(guān)系,添加后右面點(diǎn)擊表和列規(guī)范,彈出界面,左邊選擇主鍵表主鍵列,右面選擇本表字段a1即可。a2同樣設(shè)置。
語(yǔ)句的我知道用references就行 可是可視化的操作可以做到么?
我正好有一個(gè)例子和你說(shuō)的一樣,請(qǐng)參考: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
create table NewsType(id INT PRIMARY KEY ,parentId int references NewsType(id))其他字段你自己添吧 比如插入測(cè)試數(shù)據(jù)insert into NewsType values (1,1);--這個(gè)執(zhí)行沒(méi)問(wèn)題insert into NewsType values (2,3);--會(huì)報(bào)錯(cuò)
可以用數(shù)據(jù)庫(kù)工具創(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)建表的語(yǔ)法-創(chuàng)建表格語(yǔ)法:create table 表名(???? 字段名1?? 字段類型(長(zhǎng)度) 是否為空,???? 字段名2?? 字段類型???????????? 是否為空);-增加主鍵alter table 表名 add constraint 主鍵名 primary key (字段名1);-增加外鍵:alter table 表名???? add constraint 外鍵名 foreign key (字段名1)?????????? references 關(guān)聯(lián)表 (字段名2);在建立表格時(shí)就指定主鍵和外鍵create table t_stu?? (
網(wǎng)站標(biāo)題:外鍵設(shè)置,navicat外鍵怎么設(shè)置
標(biāo)題URL:http://www.rwnh.cn/article30/iocoso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、網(wǎng)站設(shè)計(jì)、靜態(tài)網(wǎng)站、小程序開發(fā)、虛擬主機(jī)、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)