是否需要为DDL添加约束?

时间:2017-04-30 13:21:06

标签: sql computer-science ddl

create table Country(
    code char(3)        PRIMARY KEY,
    name varchar2(100)  NOT NULL
 );

create table Member(
    firstname varchar(100)  NOT NULL,
    lastname varchar(100)   NOT NULL,
    title varchar(100)      NOT NULL,
    member_id INTEGER   PRIMARY KEY,
    Country_code char(3)    NOT NULL REFERENCES Country(code)
                            ON DELETE CASCADE
 ON UPDATE CASCADE
 );

create table Athlete(
   id integer           NOT NULL REFERENCES Member(member_id)
                    ON DELETE NO ACTION
                    ON UPDATE CASCADE

);

create table Official(
   id integer           NOT NULL REFERENCES Member(member_id)
ON DELETE NO ACTION
                    ON UPDATE CASCADE
);

create table Staff(
   id integer           NOT NULL REFERENCES Member(member_id)
                ON DELETE NO ACTION
                    ON UPDATE CASCADE                   
);

Create table Books(
   when timestamp       NOT NULL,
   member_id integer        NOT NULL REFERENCES Member(member_id),

   start_time timestamp NOT NULL REFERENCES Journey(start_time),
   start_date date      NOT NULL REFERENCES Journey(start_date),
   byStaff integer      NOT NULL REFERENCES Staff(id)
);


create table Journey(
    start_time timestamp    PRIMARY KEY,
    start_date date     PRIMARY KEY,
    member_id integer       PRIMARY KEY REFERENCES Member(member_id),
    nbooked integer     NOT NULL,
    departure varchar(100)  NOT NULL REFERENCES Place(name),
    arrival varchar(100)    NOT NULL REFERENCES Place(name),
    code varchar(100)       NOT NULL REFERENCES Vehicle(code)
 );

create table Vehicle(
    code varchar(100)       PRIMARY KEY,
    capacity varchar(100)   NOT NULL
 );

create table Place(
    name varchar(50)        PRIMARY KEY,
    address varchar(100)    NOT NULL,
    longitude float     NOT NULL,
    latitude float`     NOT NULL
 );

create table SportVenue(
   name varchar(50)         NOT NULL REFERENCES Place(name)
);

create table Accommodation(
   name varchar(50)         NOT NULL REFERENCES Place(name)
);

create table Event(
    name varchar(100)       PRIMARY KEY,
    result_type varchar(100)    NOT NULL,
    time timestamp      NOT NULL,
    date date           NOT NULL,
    sport_name varchar(50)  NOT NULL REFERENCES Sport(name),    
);

对于上面的DDL,是否需要为DDL添加约束?如果需要修改,应该实施哪些约束?我在编写DDL时非常困惑...我们很少见,如果我们不知道怎么样,如果你想知道如何看待它们的话 感谢。

1 个答案:

答案 0 :(得分:0)

约束也称验证应该从您的应用程序的所有方面实现。前端,后端和您的数据库。否则,您将无法完成整体数据完整性。所以,你的问题的答案是肯定的。详细信息取决于您将使用的数据的性质。