表创建derby sql jpa

时间:2013-11-27 19:43:37

标签: sql jpa derby

大家好,我正在寻找一些帮助

我有

create table usertable (
        username varchar(128) NOT NULL CONSTRAINT USER_PK PRIMARY KEY ,
        password varchar(128) NOT NULL
);


create table grouptable(
        username varchar(128) NOT NULL,
        groupid  varchar(128) NOT NULL,
        CONSTRAINT GROUP_PK PRIMARY KEY(username, groupid),
        CONSTRAINT USER_FK FOREIGN KEY(username) REFERENCES usertable(username)
            ON DELETE CASCADE ON UPDATE RESTRICT
    );

要将用户名和密码存储到数据库中,我要问的是如何添加另一个名为address的表,并将其与多对一实现中的usertable链接,因为一个用户可能有多个地址,我该怎么做呢?

谢谢你们!

修改

这会有用吗?

create table usertable (
        username varchar(128) NOT NULL CONSTRAINT USER_PK PRIMARY KEY ,
        password varchar(128) NOT NULL,
firstaname varchar(30) NOT NULL,
surname varchar(30) NOT NULL,
gender varchar(6) NOT NULL,
homenumber varchar(128) NOT NULL,
mobilenumber varchar(128) NOT NULL,
dob date NOT NULL,
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)
);


create table grouptable(
        username varchar(128) NOT NULL,
        groupid  varchar(128) NOT NULL,
        CONSTRAINT GROUP_PK PRIMARY KEY(username, groupid),
        CONSTRAINT USER_FK FOREIGN KEY(username) REFERENCES usertable(username)
            ON DELETE CASCADE ON UPDATE RESTRICT
    );

create table address(
username varchar(128) NOT NULL,
                addressline1 varchar(128) NOT NULL,
                addressline2 varchar(128) NOT NULL,
                addressline3 varchar(128) NOT NULL,
                country varchar(128) NOT NULL,
                postcode varchar(9) NOT NULL,
CONSTRAINT USER2_FK FOREIGN KEY(username) REFERENCES usertable(username)
                     );

编辑2:

create table usertable (
        username varchar(128) NOT NULL CONSTRAINT USER_PK PRIMARY KEY,
        password varchar(128) NOT NULL,
firstaname varchar(30) NOT NULL,
surname varchar(30) NOT NULL,
gender varchar(6) NOT NULL,
homenumber varchar(128) NOT NULL,
mobilenumber varchar(128) NOT NULL,
dob date NOT NULL,
id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)
);


create table grouptable(
        username varchar(128) NOT NULL,
        groupid  varchar(128) NOT NULL,
        CONSTRAINT GROUP_PK PRIMARY KEY(username, groupid),
        CONSTRAINT USER_FK FOREIGN KEY(username) REFERENCES usertable(username)
            ON DELETE CASCADE ON UPDATE RESTRICT
    );

create table address(
ID NUMERIC(3) not null primary key,
                 username varchar(128) NOT NULL,
                addressline1 varchar(128) NOT NULL,
                addressline2 varchar(128) NOT NULL,
                addressline3 varchar(128) NOT NULL,
                country varchar(128) NOT NULL,
                postcode varchar(9) NOT NULL,
                CONSTRAINT USER2_FK FOREIGN KEY(username) REFERENCES usertable(username)
                     );

0 个答案:

没有答案