sql创建外键语句

sql创建外键语句,第1张

1、创建测试主表(班级表test_class),

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

2、创建测试子表(学生表test_student),

create table test_student(stu_id number, stu_name varchar2(200), class_id number)

3、主表(班级表test_class)添加唯一主键,

alter table TEST_CLASS

add constraint pk_class_id primary key (CLASS_ID)

4、子表(学生表test_student)创建外键,

alter table TEST_STUDENT

add constraint fk_class_id foreign key (CLASS_ID)

references test_class (CLASS_ID)

为数据库表添加外键方法(以SqlSever2005数据库为例):

1、新建两张表,一张为主表,一张为副表。主表里的外键就是连接到副表的主键。

2、首先在主表中添加一个外键字段,如下:

3、在主表上右击,选择关系,如下:

4、点击“添加”。

5、再点击上图中标出的位置,如下:

6、图中标出的“主键表”就选择上面的副表。如下:

7、主键表底下的就选择副表的主键,如下:

8、外键表底下的就选择刚才新建的外键字段。如下:

9、再点击“确定”。

10、点击“是”,即可完成外键的添加。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11902179.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存