0

Hey I am designing a fictional database of a bank and I am having trouble getting all the tables and relationships between the tables to work. Specifically I am trying to create relationships with foreign keys between a2_bankbranch and a2_loan

My code is below

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;

I think that I have my foreign keys wrong in a2_loan but I am not sure.

I am getting insert errors in a2_loan and a2_loanr tables

Any help would be appreciated

Val
  • 47
  • 5
  • what queery want to execute give an example – Dibyendu Konar Sep 23 '15 at 05:27
  • I just want to create the tables but I am getting insert errors such as: INSERT INTO a2_loan VALUES * ERROR at line 1: ORA-00942: table or view does not exist – Val Sep 23 '15 at 05:28
  • and you are not being able to insert as you are inserting value for ird_num CHAR(8) NOT NULL , loan_num CHAR(10) NOT NULL, but not inserting for the foreign keys. – Dibyendu Konar Sep 23 '15 at 05:28
  • Is it possible for you to show me how to fix this? – Val Sep 23 '15 at 05:31

1 Answers1

2

You incorrectly reference the FOREIGN KEY

Change the routingcode column name to routing_code to be consistent with the others :

CREATE TABLE a2_bank (
    routing_code   VARCHAR(200)  PRIMARY KEY,
    name          VARCHAR(200)  NOT NULL,
    address       VARCHAR(200)  NOT NULL
 );
dstudeba
  • 8,878
  • 3
  • 32
  • 41
  • I am now getting: FOREIGN KEY(branch_num) REFERENCES a2_bankbranch(branch_num) * ERROR at line 9: ORA-02270: no matching unique or primary key for this column-list – Val Sep 23 '15 at 05:43
  • You need to make `branch_num` the `PRIMARY KEY` in `a2_bankbranch`, please see: http://stackoverflow.com/questions/10802212/oracle-ora-02270-no-matching-unique-or-primary-key-for-this-column-list-erro – dstudeba Sep 23 '15 at 05:51