成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供城北網(wǎng)站建設(shè)、城北做網(wǎng)站、城北網(wǎng)站設(shè)計(jì)、城北網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、城北企業(yè)網(wǎng)站模板建站服務(wù),10余年城北做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
打開我的navicat,然后找到我的teacher表,選中它,然后點(diǎn)擊菜單欄上 在彈出的對話框中找到“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 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)--注意:能作為一個(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)--對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)
我正好有一個(gè)例子和你說的一樣,請參考: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è)計(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è)置。
create table NewsType(id INT PRIMARY KEY ,parentId int references NewsType(id))其他字段你自己添吧 比如插入測試數(shù)據(jù)insert into NewsType values (1,1);--這個(gè)執(zhí)行沒問題insert into NewsType values (2,3);--會報(bào)錯(cuò)
可以用數(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);在建立表格時(shí)就指定主鍵和外鍵create table t_stu?? (