Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
825 views
in Technique[技术] by (71.8m points)

oracle - Is there any other way to create constraints during SQL table creation?

Assume I have this following SQL statements, using Oracle:

drop table department cascade constraints;
drop table facultyStaff cascade constraints;
drop table student cascade constraints;
drop table campusClub cascade constraints;
drop table studentClub cascade constraints;

create table department 
(   code      varchar2(3) primary key,
    name      varchar2(40)  not null,
    chair     varchar2(11));

create table facultyStaff 
(   staffID     varchar2(5) primary key,
    dob     date,
    firstName   varchar2(20),
    lastName    varchar2(20),
    rank        varchar2(10),
    deptCode    varchar2(3),
    constraint rankValue check (rank in ('Assistant', 'Associate', 'Full', 'Emeritus')),
    constraint facultyDeptFk foreign key (deptCode) references department (code));

create table student 
(   studentId   varchar2(5) primary key,
    dob     date ,
    firstName   varchar2(20),
    lastName    varchar2(20),
    status      varchar(10),
    major       varchar(3),
constraint statusValue check (status in ('Freshman', 'Sophomore', 'Junior', 'Senior',  'Graduate')),
    constraint studentMajorFk foreign key (major) references department (code));

alter table department 
    add constraint departmentChairFk foreign key(chair) references facultyStaff(staffId) on delete set null; 

Since there is a recursive reference between department and faculty for the chair relationship, the foreign key constraint on chair in department cannot be defined until the faculty table is defined. The constraint must be added after faculty is defined using the alter table statement.

Are there any other ways to do this, to somehow automate the constraint creation thus eliminating the alter table statement?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this with a create schema statement:

create schema authorization [schema name]
create table department 
(   code      varchar2(3) primary key,
    name      varchar2(40)  not null,
    chair     varchar2(11),
    constraint departmentChairFk foreign key(chair) references facultyStaff(staffId) on delete set null
)
create table facultyStaff 
(   staffID     varchar2(5) primary key,
    dob     date,
    firstName   varchar2(20),
    lastName    varchar2(20),
    rank        varchar2(10),
    deptCode    varchar2(3),
    constraint rankValue check (rank in ('Assistant', 'Associate', 'Full', 'Emeritus')),
    constraint facultyDeptFk foreign key (deptCode) references department (code)
)
create table student 
(   studentId   varchar2(5) primary key,
    dob     date ,
    firstName   varchar2(20),
    lastName    varchar2(20),
    status      varchar(10),
    major       varchar(3),
constraint statusValue check (status in ('Freshman', 'Sophomore', 'Junior', 'Senior',  'Graduate')),
    constraint studentMajorFk foreign key (major) references department (code)
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...