データベース - サブクエリ、ビュー、取引

サブクエリ

サブクエリ:他のSQLクエリの条件として、SQLクエリ結果

増加

#insert into 表 select 子查询

検索

# select fron 表 where() in (select 子查询)[表不能与delete表相同]
'''查每个学校身高最高的那个人的全部信息'''

1)#子查询的sql select school,max(height) from student group by school; #先查询出每个学校的最大身高 +--------+-------------+ | school | max(height) | +--------+-------------+ | 北大 | 183.1 | | 复旦 | 183.3 | | 清华 | 185.9 | +--------+-------------+ 2)#查 select * from student where (school,height) in (select school,max(height) from student group by school); +----+------+--------+--------+--------+--------+--------+ | id | name | gender | height | area | port | school | +----+------+--------+--------+--------+--------+--------+ | 2 | bbb | 男 | 183.1 | 上海 | 青浦 | 北大 | | 3 | ccc | 男 | 183.3 | 北京 | 朝阳 | 复旦 | | 4 | ddd | 男 | 185.9 | 广东 | 广州 | 清华 | +----+------+--------+--------+--------+--------+--------+
# 将子查询转换为一张表

1)# 创建一个存子查询数据的一张表
create table school_height(
school_name varchar(64),
max_height varchar(64)); 2)#将子查询的数据增加到school_height表中 insert into school_height select school,max(height) from student group by school; +-------------+------------+ | school_name | max_height | +-------------+------------+ | 北大 | 183.1 | | 复旦 | 183.3 | | 清华 | 185.9 | +-------------+------------+ 3)#需求(同样也会实现上面的需求) select name,school_name,height from student join school_height on student.school = school_height.school_name and student.height = school_height.max_height; +------+-------------+--------+ | name | school_name | height | +------+-------------+--------+ | bbb | 北大 | 183.1 | | ccc | 复旦 | 183.3 | | ddd | 清华 | 185.9 | +------+-------------+--------+

変更

# update 表 set 字段=值 where 字段 in (select 子查询)[表不能与delete表相同]

'''每个部门最大身高加一'''
update school_height set max_height=max_height+1;
+-------------+------------+
| school_name | max_height |
+-------------+------------+
| 北大        | 184.1      |
| 复旦        | 184.3 | | 清华 | 186.9 | +-------------+------------+ '''给school_height表额外增加一个新学校''' insert into school_height values ('南开', 188); +-------------+------------+ | school_name | max_height | +-------------+------------+ | 北大 | 184.1 | | 复旦 | 184.3 | | 清华 | 186.9 | | 南开 | 188 | +-------------+------------+ '''更改''' update school_height set max_height=max_height+1 where school_name in (select distinct school from student); +-------------+------------+ | school_name | max_height | +-------------+------------+ | 北大 | 185.1 | | 复旦 | 185.3 | | 清华 | 187.9 | | 南开 | 188 | +-------------+------------+ #You can't specify target table 'school_height' for update in FROM clause #报错,update更新的表 与 子查询select的表 相同 update school_height set max_height=max_height+1 where school_name in (select distinct school_name from school_height);

削除

#delete from 表 where() in (select 子查询)[表不能与delete表相同]

delete from school_height where school_name in (select distinct school from student); +-------------+------------+ | school_name | max_height | +-------------+------------+ | 南开 | 188 | +-------------+------------+ #错误: delete删除的表 与 子查询select的表 相同 delete from school_height where school_name in (select distinct school_name from school_height);

変更された条件の第二に、範囲:すべてと任意の

すべて:条件のすべてを満たすために

全ての場合よりも小さくなければならない場合、給与<全て(3、6、9)=>給与

どの:ライン上のいずれかの条件を満たしています

給与<任意(3、6、9)=>給料限りケース未満であるように

eg:
select * from student where height < all(select height from student where id<4);
#先找到id<4的学生的身高,分别为 173.1,183.1,183.3,之后再拿身高作为参照物,找出满足所有满足身高条件(身高小于173.1)的同学
+----+------+--------+--------+--------+--------------+--------+
| id | name | gender | height | area | port | school | +----+------+--------+--------+--------+--------------+--------+ | 5 | eee | 女 | 168 | 山东 | 烟台 | 北大 | | 6 | fff | 女 | 165 | 新疆 | 乌鲁木齐 | 北大 | | 8 | hhh | 女 | 166.1 | 广州 | 广东 | 复旦 | | 13 | ppp | 女 | 155 | 江苏 | 连云港 | 清华 | +----+------+--------+--------+--------+--------------+--------+

第三に、ビュー:ビュー

CRUD操作は、(実際に動作テーブルに、本質的に)実際のテーブルビューに直接マッピングすることができます

  • ビューは、メモリの一時テーブルに格納されています
  • 依存関係ビューのSELECT文を作成します。
  • 検索を変更するには、ビュー・サポート・データの追加および削除
  • ビューは、テーブルの視野への変更を許可していません。
  • 作成、更新、および削除をサポートするだけでなく、ビューもサポートされています
'''创建视图'''
'''create view 视图名[(别名们)] as select 语句'''

create view 视图名[(别名们)] as select 语句;
eg:
create view v1 as select 
school,max(height) 
from student group by school; +--------+-------------+ | school | max(height) | +--------+-------------+ | 北大 | 183.1 | | 复旦 | 183.3 | | 清华 | 185.9 | +--------+-------------+ '''创建或替换视图''' ''' create or replace 视图名[(别名们)] as select 语句;''' create or replace view v1(school_name, max_height) as select school, max(height) from student group by school; +-------------+------------+ | school_name | max_height | +-------------+------------+ | 北大 | 183.1 | | 复旦 | 183.3 | | 清华 | 185.9 | +-------------+------------+ ''' alter 视图名[(别名们)] as select 语句;''' alter view v1(name, height) as select school, max(height) from student group by school; +--------+--------+ | name | height | +--------+--------+ | 北大 | 183.1 | | 复旦 | 183.3 | | 清华 | 185.9 | +--------+--------+ '''删除视图''' drop view 视图名 eg:drop view v1; '''视图可以作为正常表完成连表查询''' eg:create or replace view v2 as select name,gender,height from student; select * from v2; +------+--------+--------+ | name | gender | height | +------+--------+--------+ | aaa | 女 | 173.1 | | bbb | 男 | 183.1 | | ccc | 男 | 183.3 | | ddd | 男 | 185.9 | | eee | 女 | 168 | | fff | 女 | 165 | .........

CRUDビュー

CRUDの完全なビュー、基本的に削除変更は、実際の手術台であります

#先创建一个视图
create or replace view v2 as select id,name,gender,height from student;

#改 update v2 set height=height+1 where id=1; #两个表都改变了 select * from v2; +----+------+--------+--------+ | id | name | gender | height | +----+------+--------+--------+ | 1 | aaa | 女 | 174.1 | select * from student; +----+------+--------+--------+-----------+--------------+--------+ | id | name | gender | height | area | port | school | +----+------+--------+--------+-----------+--------------+--------+ | 1 | aaa | 女 | 174.1 | 甘肃 | 张掖 | 清华 | #删 deleate from v2 where id = 1; #增 create or replace view v3 as select * from student; insert into v2 values(18,'wwb','男','183','上海','青浦','北大'); #操作视图会影响真实表,反之也会影响 update sutdent set height = height+1 whereid= 1;

第四に、トランザクション

通常、いくつかの企業が複数のSQL参加を要求し、関与のSQLの実行は、我々は全体のトランザクションを呼び出し全体を形成します(トランザクションが実行されたSQL文が複数含まれています)

4つのプロパティ4.1総務

  • 原子性:すべてのは、一回の成功を実行しなければならない、あなたは同時に、任意の失敗または成功しばらく持っていずれか失敗することはできません
  • 一貫性:データの整合性トランザクションの前と後では一貫していなければなりません
  • 単離:同時実行をサポートするが、同時に行うことができ、データは混沌ではありません
  • 耐久性:トランザクションがコミットされると、彼はデータへのデータの変更が永続的な障害がある場合、データベースは、それが影響を提出しないだろう
create table bank(
id int primary key auto_increment,
name varchar(16), money decimal(65,2)); insert into bank(name,money) values('yjy',10),('wwb',20); #假设出现以下执行情况 update bank set money = money-1 where name = 'yjy'; update bank set money = money+1 where name = 'wwb'; '''没有事务支持下,yjy的钱就丢了''' #这种执行是错误的,因为ypp不存在,yjy的银行账户钱转丢了 update bank set money = money-1 where name = 'yjy'; update bank set money = money+1 where name = 'ypp'; '''将2条sql看做事务处理''' #开启事务 begin; update bank set money = money-1 where name = 'yjy'; update bank set money = money+1 where name = 'ypp'; #确认无误,提交事务 commit; #确认有误,回滚事务 rollback;

おすすめ

転載: www.cnblogs.com/lulingjie/p/11656068.html
おすすめ