创建银行数据库(database 第5版)

文章目录

  • 一、建立数据库
    • 1.六个表的内容
    • 2.表内部关系的说明
      • (1)创表分先后
      • (2)六个表:
      • (3)内容说明
    • 3.建立表的代码
      • (1)建立数据库
      • (2)表branch
      • (3)表account
      • (4)customer表
      • (5)depositor表
      • (6)loan表
      • (7)borrower表
  • 附录:一键建库


一、建立数据库

1.六个表的内容

第2章2-3 branch表

创建银行数据库(database 第5版)_第1张图片

第2章图2-1 account表

创建银行数据库(database 第5版)_第2张图片

第2章图2-4 customer表

书上的customer表:
创建银行数据库(database 第5版)_第3张图片
实际的customer表:因为borrower表有一个Jackson,而borrower中有customer的外码,所以要插入一个Jackson到customer表中。
创建银行数据库(database 第5版)_第4张图片

第2章图2-5 depositor表

创建银行数据库(database 第5版)_第5张图片

第2章图2-6 loan表

创建银行数据库(database 第5版)_第6张图片

第2章图2-7 borrower表

创建银行数据库(database 第5版)_第7张图片

2.表内部关系的说明

(1)创表分先后

先后指A表的外码就是引用B表的主码,所以得先创建B表

创建银行数据库(database 第5版)_第8张图片

  • branch表中branch_name是主码
  • accout表中account_number是主码,branch_name是branch表中的外码
  • customer表中customer_name是主码
  • depositor表中customer_name是customer表中的外码,account_number是account表中的外码
  • loan表中loan_number是主码,branch_name是branch表中的外码
  • borrower表中customer_name是customer表中的外码,loan_number是loan表中的外码

(2)六个表:

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)

(3)内容说明

账户

account表示存款表,balance表示存款
depositor表示存款人表,有了存款人的名字customer_name

借款

loan表示贷款表:amount表示贷款金额。
borrower表示贷款人表,有了贷款人的名字customer_name

客户表

customer_name表示客户的名字,这些包括借钱的和存钱的。

3.建立表的代码

(1)建立数据库

create database bank;
use bank;

(2)表branch

创建

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);

(3)表account

创建

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);

(4)customer表

创建

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');

(5)depositor表

创建

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');

(6)loan表

创建

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);

(7)borrower表

创建

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');

你可能感兴趣的:(数据库MySQL)