基本的Sql表

时间:2015-09-23 05:23:50

标签: sql oracle insert

嘿我正在设计一个虚构的银行数据库,我无法让表格和表格之间的关系发挥作用。 具体来说,我试图在a2_bankbranch和a2_loan

之间创建与外键的关系

我的代码在

下面
CREATE TABLE a2_bank (
routing_code   VARCHAR(200)  PRIMARY KEY,
name          VARCHAR(200)  NOT NULL,
address       VARCHAR(200)  NOT NULL
);

INSERT INTO a2_bank VALUES
( '123456','ASB', '3 gladstone rd');
INSERT INTO a2_bank VALUES
( '123556','BNZ', '5 gladstone rd');
INSERT INTO a2_bank VALUES
( '12456','KIWIBANK', '3 gladstone rd');


CREATE TABLE a2_bankbranch (
name           VARCHAR(200)  NOT NULL,
branch_num     VARCHAR(200)  NOT NULL,
address        VARCHAR(200)  NOT NULL,
routing_code   VARCHAR(200)  NOT NULL,
total_loan     NUMBER(38),
FOREIGN KEY(routing_code) REFERENCES a2_bank(routing_code),
PRIMARY KEY(branch_num, routing_code)
);

INSERT INTO a2_bankbranch VALUES
( 'ASB', '5', '3 gladstone rd', '123456', '');
INSERT INTO a2_bankbranch VALUES
( 'ASB', '4', '28 stevee rd', '123456', '');

CREATE TABLE a2_loan (
loan_num       CHAR(10)  PRIMARY KEY,
type           VARCHAR(200)  NOT NULL,
amount         NUMBER(38)  NOT NULL,
contract_date  DATE          NOT NULL,
branch_num     VARCHAR(200) NOT NULL,
routing_code   VARCHAR(200) NOT NULL,
FOREIGN KEY(routing_code) REFERENCES a2_bank(routing_code),
FOREIGN KEY(branch_num) REFERENCES a2_bankbranch(branch_num)
);

INSERT INTO a2_loan VALUES
( '323', 'Mortgage', '2000000', TO_DATE('11-03-1994', 'DD-MM-YYYY'), '5', '123456' );
INSERT INTO a2_loan VALUES
( '33', 'Car', '2000', TO_DATE('12-08-1994', 'DD-MM-YYYY'), '5', '123456' );
INSERT INTO a2_loan VALUES
( '3243', 'Pesonal', '875', TO_DATE('14-06-1994', 'DD-MM-YYYY'), '5', '123456' );
INSERT INTO a2_loan VALUES
( '6', 'Mortgage', '400500', TO_DATE('11-06-1994', 'DD-MM-YYYY'), '5', '123456'  );

CREATE TABLE a2_account (
acc_num       CHAR(10)  PRIMARY KEY,
type           VARCHAR(20)  NOT NULL,
balance         VARCHAR(10)  NOT NULL
);
INSERT INTO a2_account VALUES
( '2539267332', 'Savings', '20');
INSERT INTO a2_account VALUES
( '8237893378', 'Cash', '300');
INSERT INTO a2_account VALUES
( '2378723936', 'Cheque', '75');
INSERT INTO a2_account VALUES
( '2378723937', 'Savings', '175');


CREATE TABLE a2_customer (
ird_num         CHAR(8)  PRIMARY KEY,
name            VARCHAR(200)  NOT NULL,
address         VARCHAR(200)  NOT NULL,
phone           VARCHAR(20)
);
INSERT INTO a2_customer VALUES
( '25362672',  'Stan Yel', '5 Wanna way', '02010201');
INSERT INTO a2_customer VALUES
( '83783783', 'Cam Birch', '34 Trada st', '02302020202');
INSERT INTO a2_customer VALUES
( '23723367', 'Jeff King', '5 Queens st', '38982383');
INSERT INTO a2_customer VALUES
( '54637822',  'John Smith', '24 Queen st', '38922383');


CREATE TABLE a2_accr (
ird_num                CHAR(8)  NOT NULL ,
account_num            CHAR(10)  NOT NULL,
FOREIGN KEY(ird_num) REFERENCES a2_customer(ird_num),
FOREIGN KEY(account_num) REFERENCES a2_account(acc_num)
);
INSERT INTO a2_accr VALUES
( '25362672', '2539267332');
INSERT INTO a2_accr VALUES
( '83783783', '8237893378');
INSERT INTO a2_accr VALUES
( '83783783', '2378723937');

CREATE TABLE a2_loanr (
ird_num                CHAR(8)  NOT NULL ,
loan_num            CHAR(10)  NOT NULL,
FOREIGN KEY(ird_num) REFERENCES a2_customer(ird_num),
FOREIGN KEY(loan_num) REFERENCES a2_loan(loan_num)
);
INSERT INTO a2_loanr VALUES
( '54637822', '323');
INSERT INTO a2_loanr VALUES
( '23723367', '33');

COMMIT;

我认为我的外键在a2_loan中有错,但我不确定。

我在a2_loan和a2_loanr表中遇到插入错误

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

您错误地引用了FOREIGN KEY

routingcode列名称更改为routing_code以与其他人保持一致:

CREATE TABLE a2_bank (
    routing_code   VARCHAR(200)  PRIMARY KEY,
    name          VARCHAR(200)  NOT NULL,
    address       VARCHAR(200)  NOT NULL
 );