msql 复杂练习
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 复杂练习的更多相关文章
- 免费数据库(SQLite、Berkeley DB、PostgreSQL、MySQL、Firebird、mSQL、MSDE、DB2 Express-C、Oracle XE)
SQLite数据库是中小站点CMS的最佳选择 SQLite 是一个类似Access的轻量级数据库系统,但是更小.更快.容量更大,并发更高.为什么说 SQLite 最适合做 CMS (内容管理系统)呢? ...
- TDSQL“相似查询工具MSQL+”入选VLDB论文
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云数据库 TencentDB发表于云+社区专栏 作者介绍:王晓宇,腾讯数据库TDSQL团队成员,目前参与TDSQL数据库内核研发工 ...
- msql_createdb: 建立一个新的 mSQL 数据库。
mcrypt_ecb: 使用 ECB 将资料加/解密. mcrypt_get_block_size: 取得编码方式的区块大小. mcrypt_get_cipher_name: 取得编码方式的名称. m ...
- Msql浅析-基础命令(二)
篇幅简介 一.Msql数据类型 1.整型 tinyint, 占 1字节 ,有符号: -128~127,无符号位 :0~255 smallint, 占 2字节 ,有符号: -32768~32767无符 ...
- Msql入门实战之下
前面一章主要解说了mysql的select的使用方法.将select的大部分使用方法进行分别解说.本章主要解说Msql约束表的建立,以及存储过程的实现,附带其它介绍.临时就算入门了,Mysql索引之后 ...
- springMVC+springJDBC+Msql注解模式
最近基于Spring4.X以上的版本写了一个springMVC+springJDBC+Msql注解模式的一个项目,之中也遇到过很多问题 ,为了防止以后遇到同样问题现记录一下知识点以及详细配置. 首先我 ...
- 【msql】关于redo 和 undo log
InnoDB 有两块非常重要的日志,一个是undo log,另外一个是redo log,前者用来保证事务的原子性以及InnoDB的MVCC,后者用来保证事务的持久性.和大多数关系型数据库一样,Inno ...
- msql数据迁移,myisam及innoDB
直接迁移数据库文件. 一.MySQL数据库文件介绍 MySQL的每个数据库都对应存放在一个与数据库同名的文件夹中,MySQL数据库文件包括MySQL所建数据库文件和MySQL所用存储引擎创建的数据库文 ...
- msql,触发器无事物回滚,插入之前满足条件再插入
很少写mysql的触发器和存储过程,由于需要需要做一个很小的判断,要用到触发器,要达到的效果就是,插入之前判断是否满足条件如果不满足就不插入 如果用sqlserver 或者orcale 就很简单,按s ...
- msql 实现sequence功能增强
create table sequence ( seq_name VARCHAR(50) NOT NULL COMMENT '序列名称', min_val ...
随机推荐
- UCML JS函数说明
UCML JS函数说明1.调用父窗体函数 window.openerWindow.函数名 2.公用JS存放位置 BPObject\Model\Rule\initvalue.js 3.弹窗JS var ...
- ethereum/EIPs-1102 Opt-in provider access metamask不再默认直接连入网页
eip title author discussions-to status type category created 1102 Opt-in provider access Paul Boucho ...
- Qt5.9一个简单的多线程实例(类QThread)(第一种方法)
Qt开启多线程,主要用到类QThread.有两种方法,第一种用一个类继承QThread,然后重新改写虚函数run().当要开启新线程时,只需要实例该类,然后调用函数start(),就可以开启一条多线程 ...
- 201904Online Human Action Recognition Based on Incremental Learning of Weighted Covariance Descriptors
论文标题:Online Human Action Recognition Based on Incremental Learning of Weighted Covariance Descriptor ...
- TerraExplorer Add-ons 和TEZ使用说明
1. 概述 Skyline提供了丰富的API,用户可以根据不同实际需求,进行二次开发封装,实现各种功能的扩展和定制.当我们开发了各种功能页面或者应用程序后,如何将它们快速嵌入到TerraExpl ...
- python 知识
def action_cancel_sale_order(self,cr,uid,ids,context=None): self.message_post(cr, uid, ids, body=u&q ...
- 4.5《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)—第四章小结
本章相关重要命令总结在Table 6. 命令 描述 示例 mkdir <name> 创建某目录 $ mkdir foo pwd 显示当前所在目录 $ pwd cd <dir> ...
- Luogu4640 BJWC2008 王之财宝 容斥、Lucas
传送门 题意:有$N$种物品,其中$T$个物品有限定数量$B_i$,其他则没有限定.问从中取出不超过$M$个物品的方案数,对质数$P$取模.$N,M \leq 10^9 , T \leq 15 , P ...
- 计算2个时间之间经过多少Ticks
Ticks是一个周期,存储的是一百纳秒,换算为秒,一千万分之一秒.我们需要计算2个时间之间,经过多少Ticks,可以使用下面的方法来实现,使用2个时间相减. 得到结果为正数,是使用较晚的时间减去较早的 ...
- React + js-xlsx构建Excel文件上传预览功能
首先要准备react开发环境以及js-xlsx插件 1. 此处省略安装react安装步骤 2.下载js-xlsx插件 yarn add xlsx 或者 npm install xlsx 在项目中引入 ...