第2章2-3 branch表
第2章图2-1 account表
第2章图2-4 customer表
书上的customer表:
实际的customer表:因为borrower表有一个Jackson,而borrower中有customer的外码,所以要插入一个Jackson到customer表中。
第2章图2-5 depositor表
第2章图2-6 loan表
第2章图2-7 borrower表
先后指A表的外码就是引用B表的主码,所以得先创建B表
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower (customer_name, loan_number)
账户
account表示存款表,balance表示存款
depositor表示存款人表,有了存款人的名字customer_name。
借款
loan表示贷款表:amount表示贷款金额。
borrower表示贷款人表,有了贷款人的名字customer_name。
客户表
customer_name表示客户的名字,这些包括借钱的和存钱的。
create database bank;
use bank;
创建
create table branch(
branch_name char(30) NOT NULL,
branch_city char(30),
assets decimal(12),
primary key(branch_name)
);
插入数据
insert into branch values
('Brighton','Brooklyn',7100000),
('Downtown','Brooklyn',9000000),
('Mianus','Horseneck',400000),
('North Town','Rye',3700000),
('Perryridge','Horseneck',1700000),
('Pownal','Bennington',300000),
('Redwood','Palo Alto',2100000),
('Round Hill','Horseneck',8000000);
创建
create table account (
account_number char(10),
branch_name char(30) NOT NULL,
balance decimal(12),
PRIMARY KEY (account_number),
FOREIGN KEY (branch_name) REFERENCES branch(branch_name),
CONSTRAINT chk_balance CHECK (balance >= 0 )
);
插入数据
insert into account values
('A-101','Downtown',500),
('A-102','Perryridge',400),
('A-201','Brighton',900),
('A-215','Mianus',700),
('A-217','Brighton',750),
('A-222','Redwood',700),
('A-305','Round Hill',350);
创建
create table customer(
customer_name char(30),
customer_street char(30),
customer_city char(30),
primary key(customer_name)
);
插入数据
insert into customer values
('Adams','Spring','Pittsfield'),
('Brooks','Senator','Brooklyn'),
('Curry','North','Rye'),
('Glenn','Sand Hill','Woodside'),
('Green','Walnut','Stamford'),
('Hayes','Main','Harrison'),
('Johnson','Alma','Palo Alto'),
('Jones','Main','Harrison'),
('Lindsay','Park','Pittsfield'),
('Smith','North','Rye'),
('Turner','Putnam','Stamford'),
('Williams','Nassau','Princeton');
创建
create table depositor(
customer_name char(30),
account_number char(10),
FOREIGN KEY (customer_name) REFERENCES customer(customer_name),
FOREIGN KEY (account_number) REFERENCES account(account_number)
);
插入数据
insert into depositor values
('Hayes','A-102'),
('Johnson','A-101'),
('Johnson','A-201'),
('Jones','A-217'),
('Lindsay','A-222'),
('Smith','A-215'),
('Turner','A-305');
创建
create table loan(
loan_number char(30),
branch_name char(30),
amount int,
primary key(loan_number),
FOREIGN KEY (branch_name) REFERENCES branch(branch_name)
);
插入数据
insert into loan values
('L-11','Round Hill',900),
('L-14','Downtown',1500),
('L-15','Perryridge',1500),
('L-16','Perryridge',1300),
('L-17','Downtown',1000),
('L-23','Redwood',2000),
('L-93','Mianus',500);
创建
create table borrower(
customer_name char(30),
loan_number char(30),
FOREIGN KEY (loan_number) REFERENCES loan(loan_number),
FOREIGN KEY (customer_name) REFERENCES customer(customer_name)
);
插入数据
insert into borrower values
('Adams','L-16'),
('Curry','L-93'),
('Hayes','L-15'),
('Jones','L-17'),
('Smith','L-11'),
('Smith','L-23'),
('Williams','L-17');
insert into customer values
('Jackson',null,null);
insert into borrower values
('Jackson','L-14');
create database bank;
use bank;
create table branch(
branch_name char(30) NOT NULL,
branch_city char(30),
assets decimal(12),
primary key(branch_name)
);
insert into branch values
('Brighton','Brooklyn',7100000),
('Downtown','Brooklyn',9000000),
('Mianus','Horseneck',400000),
('North Town','Rye',3700000),
('Perryridge','Horseneck',1700000),
('Pownal','Bennington',300000),
('Redwood','Palo Alto',2100000),
('Round Hill','Horseneck',8000000);
create table account (
account_number char(10),
branch_name char(30) NOT NULL,
balance decimal(12),
PRIMARY KEY (account_number),
FOREIGN KEY (branch_name) REFERENCES branch(branch_name),
CONSTRAINT chk_balance CHECK (balance >= 0 )
);
insert into account values
('A-101','Downtown',500),
('A-102','Perryridge',400),
('A-201','Brighton',900),
('A-215','Mianus',700),
('A-217','Brighton',750),
('A-222','Redwood',700),
('A-305','Round Hill',350);
create table customer(
customer_name char(30),
customer_street char(30),
customer_city char(30),
primary key(customer_name)
);
insert into customer values
('Adams','Spring','Pittsfield'),
('Brooks','Senator','Brooklyn'),
('Curry','North','Rye'),
('Glenn','Sand Hill','Woodside'),
('Green','Walnut','Stamford'),
('Hayes','Main','Harrison'),
('Johnson','Alma','Palo Alto'),
('Jones','Main','Harrison'),
('Lindsay','Park','Pittsfield'),
('Smith','North','Rye'),
('Turner','Putnam','Stamford'),
('Williams','Nassau','Princeton');
create table depositor(
customer_name char(30),
account_number char(10),
FOREIGN KEY (customer_name) REFERENCES customer(customer_name),
FOREIGN KEY (account_number) REFERENCES account(account_number)
);
insert into depositor values
('Hayes','A-102'),
('Johnson','A-101'),
('Johnson','A-201'),
('Jones','A-217'),
('Lindsay','A-222'),
('Smith','A-215'),
('Turner','A-305');
create table loan(
loan_number char(30),
branch_name char(30),
amount int,
primary key(loan_number),
FOREIGN KEY (branch_name) REFERENCES branch(branch_name)
);
insert into loan values
('L-11','Round Hill',900),
('L-14','Downtown',1500),
('L-15','Perryridge',1500),
('L-16','Perryridge',1300),
('L-17','Downtown',1000),
('L-23','Redwood',2000),
('L-93','Mianus',500);
create table borrower(
customer_name char(30),
loan_number char(30),
FOREIGN KEY (loan_number) REFERENCES loan(loan_number),
FOREIGN KEY (customer_name) REFERENCES customer(customer_name)
);
insert into borrower values
('Adams','L-16'),
('Curry','L-93'),
('Hayes','L-15'),
('Jones','L-17'),
('Smith','L-11'),
('Smith','L-23'),
('Williams','L-17');
insert into customer values
('Jackson',null,null);
insert into borrower values
('Jackson','L-14');