https://blog.csdn.net/xiao__oaix/article/details/78122294

customer表branch 表account 表

depositor 表loan 表borrower表

CREATE TABLE branch
(branch_name VARCHAR(20) NOT NULL,
branch_city VARCHAR(20),
assets INT,
CONSTRAINT PRIMARY KEY(branch_name)
);

CREATE TABLE account
(account_number INT NOT NULL AUTO_INCREMENT,
branch_name VARCHAR(20),
balance INT,
CONSTRAINT PRIMARY KEY(account_number),
CONSTRAINT FOREIGN KEY(branch_name)REFERENCES branch(branch_name) ON DELETE CASCADE
);

CREATE TABLE depositor
(customer_name VARCHAR(20),
account_number INT,
CONSTRAINT FOREIGN KEY(account_number) REFERENCES account(account_number) ON DELETE CASCADE
);
CREATE TABLE customer
(customer_name VARCHAR(20),
customer_street VARCHAR(20),
customer_city VARCHAR(20),
CONSTRAINT PRIMARY KEY(customer_name)
);

CREATE TABLE loan
(loan_number INT,
branch_name VARCHAR(20),
amount INT,
CONSTRAINT FOREIGN KEY(branch_name) REFERENCES branch(branch_name) ON DELETE CASCADE,
CONSTRAINT PRIMARY KEY(loan_number)
);

CREATE TABLE borrower
(customer_name VARCHAR(20),
loan_number INT,
CONSTRAINT FOREIGN KEY(customer_name) REFERENCES customer(customer_name) ON DELETE CASCADE,
CONSTRAINT FOREIGN KEY(loan_number) REFERENCES loan(loan_number) ON DELETE CASCADE
);

INSERT INTO customer VALUES('Adams','Spring','Pittsfield');
INSERT INTO customer VALUES('Brooks','Senator','Brooklyn');
INSERT INTO customer VALUES('Curry','North','Rye');
INSERT INTO customer VALUES('Glenn','Sand Hill','Woodside');
INSERT INTO customer VALUES('Green','Walnut','Stamford');
INSERT INTO customer VALUES('Hayes','Main','Harrison');
INSERT INTO customer VALUES('Johnson','Alma','Palo Alto');
INSERT INTO customer VALUES('Jones','Main','Harrison');
INSERT INTO customer VALUES('Lindasy','Park','Pittsfield');
INSERT INTO customer VALUES('Smith','North','Rye');
INSERT INTO customer VALUES('Turner','Putnam','Stamford');
INSERT INTO customer VALUES('Willianms','Nassau','Princeton');

SELECT * FROM customer;

INSERT INTO branch VALUES('Brighton','Brooklyn',7100000);
INSERT INTO branch VALUES('Downtown','Brooklyn',9000000);
INSERT INTO branch VALUES('Mianus','Horseneck',400000);
INSERT INTO branch VALUES('North Town','Rye',3700000);
INSERT INTO branch VALUES('Perryridge','Horseneck',1700000);
INSERT INTO branch VALUES('Pownal','Bennington',300000);
INSERT INTO branch VALUES('Redwood','Palo Alto',2100000);
INSERT INTO branch VALUES('Round Hill','Horseneck',8000000);

SELECT * FROM branch;

INSERT INTO account VALUES(101,'Downtown',500);
INSERT INTO account VALUES(102,'Perryridge',400);
INSERT INTO account VALUES(201,'Brighton',900);
INSERT INTO account VALUES(215,'Mianus',700);
INSERT INTO account VALUES(217,'Brighton',750);
INSERT INTO account VALUES(222,'Redwood',700);
INSERT INTO account VALUES(305,'Round Hill',350);

SELECT * FROM account;

INSERT INTO depositor VALUES('Hayes',102);
INSERT INTO depositor VALUES('Johnson',101);
INSERT INTO depositor VALUES('Johnson',201);
INSERT INTO depositor VALUES('Jones',217);
INSERT INTO depositor VALUES('Lindsay',222);
INSERT INTO depositor VALUES('Smith',215);
INSERT INTO depositor VALUES('Turner',305);

SELECT * FROM depositor;

INSERT INTO loan VALUES(11,'Round Hill',900);
INSERT INTO loan VALUES(14,'Downtown',1500);
INSERT INTO loan VALUES(15,'Perryridge',1500);
INSERT INTO loan VALUES(16,'Perryridge',1300);
INSERT INTO loan VALUES(17,'Downtown',1000);
INSERT INTO loan VALUES(23,'Redwood',2000);
INSERT INTO loan VALUES(93,'Mianus',500);

SELECT * FROM loan;

INSERT INTO borrower VALUES('Adams',16);
INSERT INTO borrower VALUES('Curry',93);
INSERT INTO borrower VALUES('Hayes',15);
INSERT INTO borrower VALUES('Jacson',14);
INSERT INTO borrower VALUES('Jones',17);
INSERT INTO borrower VALUES('Smith',11);
INSERT INTO borrower VALUES('Smith',23);
INSERT INTO borrower VALUES('Williams',17);

SELECT * FROM borrower;

//通过中间表交集如下一图,默认是inner join

SELECT customer_name,loan.loan_number loan_id,amount
FROM loan JOIN borrower ON loan.loan_number = borrower.loan_number;

在customer表   查找customer_street中包含main字符段对应的customer_name

SELECT customer_name FROM customer
WHERE customer_street LIKE "%Main%";

(SELECT customer_name FROM depositor)
UNION
(SELECT customer_name FROM borrower);

联合查表并去重

见下图

求两个表交集去重,见下标

SELECT DISTINCT d.customer_name FROM depositor AS p
INNER JOIN borrower AS d ON p.customer_name=d.customer_name;

求平均值

SELECT AVG(balance) FROM account
WHERE branch_name = 'Perryridge' OR branch_name = 'Mianus';

select count(*) from customer;   //求总行数

求两个表相同键的值一共有几行

SELECT COUNT(*) FROM depositor,account
WHERE account.account_number = depositor.account_number;

分组

SELECT branch_name,COUNT(*) FROM depositor,account
WHERE account.account_number = depositor.account_number
GROUP BY branch_name;

SELECT branch_name, COUNT(*) FROM account GROUP BY branch_name;

SELECT branch_name FROM branch
WHERE branch_name != 'Brooklyn' AND
assets > SOME(SELECT assets FROM branch WHERE branch_city = 'Brooklyn');

msql 复杂练习的更多相关文章

  1. 免费数据库(SQLite、Berkeley DB、PostgreSQL、MySQL、Firebird、mSQL、MSDE、DB2 Express-C、Oracle XE)

    SQLite数据库是中小站点CMS的最佳选择 SQLite 是一个类似Access的轻量级数据库系统,但是更小.更快.容量更大,并发更高.为什么说 SQLite 最适合做 CMS (内容管理系统)呢? ...

  2. TDSQL“相似查询工具MSQL+”入选VLDB论文

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云数据库 TencentDB发表于云+社区专栏 作者介绍:王晓宇,腾讯数据库TDSQL团队成员,目前参与TDSQL数据库内核研发工 ...

  3. msql_createdb: 建立一个新的 mSQL 数据库。

    mcrypt_ecb: 使用 ECB 将资料加/解密. mcrypt_get_block_size: 取得编码方式的区块大小. mcrypt_get_cipher_name: 取得编码方式的名称. m ...

  4. Msql浅析-基础命令(二)

    篇幅简介 一.Msql数据类型 1.整型 tinyint,  占 1字节 ,有符号: -128~127,无符号位 :0~255 smallint, 占 2字节 ,有符号: -32768~32767无符 ...

  5. Msql入门实战之下

    前面一章主要解说了mysql的select的使用方法.将select的大部分使用方法进行分别解说.本章主要解说Msql约束表的建立,以及存储过程的实现,附带其它介绍.临时就算入门了,Mysql索引之后 ...

  6. springMVC+springJDBC+Msql注解模式

    最近基于Spring4.X以上的版本写了一个springMVC+springJDBC+Msql注解模式的一个项目,之中也遇到过很多问题 ,为了防止以后遇到同样问题现记录一下知识点以及详细配置. 首先我 ...

  7. 【msql】关于redo 和 undo log

    InnoDB 有两块非常重要的日志,一个是undo log,另外一个是redo log,前者用来保证事务的原子性以及InnoDB的MVCC,后者用来保证事务的持久性.和大多数关系型数据库一样,Inno ...

  8. msql数据迁移,myisam及innoDB

    直接迁移数据库文件. 一.MySQL数据库文件介绍 MySQL的每个数据库都对应存放在一个与数据库同名的文件夹中,MySQL数据库文件包括MySQL所建数据库文件和MySQL所用存储引擎创建的数据库文 ...

  9. msql,触发器无事物回滚,插入之前满足条件再插入

    很少写mysql的触发器和存储过程,由于需要需要做一个很小的判断,要用到触发器,要达到的效果就是,插入之前判断是否满足条件如果不满足就不插入 如果用sqlserver 或者orcale 就很简单,按s ...

  10. msql 实现sequence功能增强

    create table sequence (      seq_name        VARCHAR(50)  NOT NULL COMMENT '序列名称',    min_val        ...

随机推荐

  1. 转载 c# automapper 使用(一) https://www.cnblogs.com/caoyc/p/6367828.html

    一.最简单的用法 有两个类User和UserDto 1 public class User 2 { 3 public int Id { get; set; } 4 public string Name ...

  2. Linux系统学习之字符处理

    管道 管道是一种使用非常频繁的通信机制,我们可以使用管道符"|"来连接进程,由管道连接起来订单进程可以自动运行,如同有一个数据流一样,所以管道表现为输入输出重定向的一种方法,它可以 ...

  3. VMware虚拟机将英文改成中文的方法

    由于之前安装的虚拟机和老师要求的不同,我安装的是VMware,所以没有安装教程,没能修改系统语言,用了几次发现英文的不太方便,特别是出错的时候,看不懂系统的出错提示. 我从网上参考了https://b ...

  4. Django rest framework集成微博第三方登录

    Django restframework 集成第三方登录(微博.微信.QQ等) 友情链接 python-social-auth-app官方文档 微博开放者平台 QQ开放者平台 准备工作 1.注册微博开 ...

  5. Photoshop 基础七 位图 矢量图 栅格化

    矢量图(CorelDraw)不是像素组成的,放大不会失真,体积小,颜色比较单一.由直线.曲线构成,画一些直线.曲线.多边形.图标. 位图(Photoshop画的就是位图),又像素组成,放大失真,放的越 ...

  6. 【Codeforces Round 725】Canada Cup 2016

    模拟Canada Cup 2016,ABC三题,Rank1376 第三题卡住了 Codeforces 725 C 求出两个相同字符的位置,记为x和y. 然后考虑把相同的那个字符放在第一行的什么地方, ...

  7. ubuntu打开windows下txt文档乱码问题的解决

    昨天晚上安装了Ubuntu11.10,打开TXT文件的时候发现中文乱码问题,在网上查了一下,一些网友提供了下面的方法: “按Alt+F2,打开“运行应用程序”对话框,输入“gconf-editor”, ...

  8. Oracle 存储过程或函数传入的数值参数number

    在oralce中,如果存储过程需要接收含有数值类型的参数时,如何声明呢.如下: CREATE OR REPLACE PACKAGE GPS.PKG_MONTH_TARGET AS ---------- ...

  9. Mac安装使用MongoDB

    Mac 下安装 MongoDB 一般有两种方法,一种是通过源码安装,一种是直接使用 homebrew ,个人推荐使用 homebrew ,简单粗暴. 一.安装 homebrew : /usr/bin/ ...

  10. React-本地状态(state)

    在类组件中添加本地状态(state): 1.创建一个继承自 React.Component 类的 ES6 class 同名类: 2.添加一个 类构造函数(class constructor) 初始化 ...