
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)
用命令创建主键与外键方法如下:
--用命令创建主键与外键.
--使用SQL语句创建主键约束可以在创建表(CreateTable)或修改表(AlterTable)时进行 创建。
--例创建表时
createtabletablename
(
idint(CONSTRAINTpk_id)PRIMARYKEY
)
--例修改表时
ALTERTABLEtablename --修改表
ADD
CONSTRAINTtb_PRIMARY
PRIMARYKEYCLUSTERED(列名)/*将你要设置为主键约束的列*/
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)