racle添加 主键的四种方法:
列级,表级建立主键
drop table constraint_test;
1.create table constraint_test
( name_id number not null constraint cons_name_id primarykey,
old number )
2.create table constraint_test
( name_id number primary key,
old number )
drop table constraint_test;
3.create table constraint_test
(
name_id number not null,
old number ,
constraint cons_name_id primary key ( name_id)
);
drop table constraint_test;
4.create table constraint_test
(
name_id number not null,
old number
);
alter table constraint_test add constraint cons_name_id primary key( name_id );
外键
drop table course ;
drop table students ;
create table students
(code number ,
name varchar2(10),
country varchar2(30)
);
alter table students add constraint pk_st_cod primary key (code);
insert into studentsvalues(0001,'zhangsan','shanghai');
insert into studentsvalues(0002,'lisi','beijing');
insert into studentsvalues(0003,'wangwu','guangzhou');
create table course
(id number,
code number,
name varchar2(10),
subject varchar2(30)
);
alter table course add constraint pk_co_idprimary key ( id);
alter table course add constraint fk_co_stforeign key ( code) references students(code);
alter table students add foreign key pk_co_id onid;
inser into course(id,code,name,subject)values(1,001,'zhangsan','yuwen');
inser into course(id,code,name,subject)values(2,001,'zhangsan','shuxue');
|