¿Hay alguna otra forma de crear restricciones durante la creación de la tabla SQL?

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 ¿declaración?

preguntado el 01 de febrero de 12 a las 03:02

Thank you for the edit. The question looks nicer :) -

1 Respuestas

Puedes hacer esto con un create schema declaración:

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)
);

respondido 27 mar '17, 07:03

I have been looking for something like this! Thank you! This is great! - rofans91

Wow, I have never seen that statement before. Excellent! - ruakh

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.