mysqlベース(接続クエリー、トランザクション、ビュー、ストレージ・プロシージャ、関数)
mysqlベース
接続クエリー
2020-4-20
全従業員の名前、番号、部門名を表示
90番従業員のjobを問い合わせるidと90番部門のlocation_id
ボーナスのあるすべての従業員の情報を検索
各職種、部門名、最低賃金の照会
国ごとの部門数が2より大きい国番号を検索
指定した従業員の名前と従業員番号、および対応する管理者の名前と従業員番号を問い合せます.
古典的なケース
2020-4-21
1.最低賃金の従業員情報の照会:last_name, salary
2.平均給与が最も低い部門情報の照会
3.平均給与が最も低い部門情報とその部門の平均給与を照会する
4.平均賃金が最も高いjob情報の照会
5.平均賃金が会社の平均賃金より高い部門情報の照会
6.会社内のすべてのmanagerの詳細を問い合わせる
7.各部門の中で最も高い給料の中で最も低いその部門の最低給料はいくらですか
8.平均給与が最も高い部門のmanagerの詳細を照会する:last_name, department_id, email, salary方法1 方法2 取引
隔離レベル READ UNCOMMITED:汚れた読みが現れ、繰り返してはいけない、幻読み READ COMMITED:汚読を避け、繰り返し不可、幻読 REPETABLE READ:汚読と重複不可を避け、幻読 が現れる SERIAUZABLE:汚い読みを避け、繰り返してはいけない、幻読みだが、効率は低い 共通コマンド
独立性レベルの表示:
たとえば、2つのトランザクションを同時に実行し、同じテーブルを処理します.テーブルの内容を変更してもトランザクションがコミットされていない場合、別のトランザクションでテーブルの内容を表示すると、データが変更されます.これがダーティリードです.
繰り返し不可
ダーティ・リードと似ていますが、別のトランザクションがコミットされた後に表示されます.ダーティ・リードは、別のトランザクションで変更すると、現在のトランザクションのテーブル・データが変更されます.
まぼろし読み
現在のテーブルでデータのすべてのローを変更すると、別のトランザクションが1つのローのデータを追加しています.この場合、現在のテーブル処理には1つのローが追加されます.これが幻読みです.
表示
ビュー作成ケース
1.ビューemp_の作成v 1、電話番号を調べて「011」で始まる従業員の名前、給料、メールボックスを要求します.
2.ビューemp_の作成v 2、部門の最高賃金が12000を超える部門情報の照会を要求する
ビュー変更アクション修正 削除 追加 6種類の更新不可ビュー
1.次のキーワードを含むsql文、distinct、groupby、having、union、union all
3.定数ビュー
4.selectにサブクエリを含める
5.join更新できないビュー6.fromは更新できないビュー7である.where文のサブクエリはfrom文のテーブルを使用します
truncateとdelectのトランザクションの違い
truncateはロールバックできません
delectはロールバック可能
ビューテスト問題
1.次のフィールドでテーブルBookテーブルを作成する
bid整形、プライマリキーbname文字型、一意キーの設定が要求され、空price浮動小数点型ではなく、デフォルト値10 btypeIdタイプ番号が要求され、bookTypeテーブルを参照するidフィールドが要求される
既知のbookTypeテーブル(作成しない)は、id name 2のフィールドで、トランザクションを開いてテーブルに1行のデータを挿入し、終了します.
3.クエリー価格が100より大きい書名とタイプ名を実現するビューを作成する
4.ビューを修正し、検索価格が90-120の間の書名と価格を実現する
5.先ほど作成したビューを削除
ストアド・プロシージャ・ケース
一.ストレージ・プロシージャの作成入力ユーザー名とパスワードをadminテーブルに挿入
二.ストレージ・プロシージャまたは関数を作成して女神番号を入力し、女神名と女神電話を返します.
三.ストレージ・プロシージャまたは関数を作成して、2つの女神の誕生日を実現し、サイズを返します.
四.ストレージ・プロシージャまたは関数の作成は、xx年xx月xx日にフォーマットされ、戻ってくる日付を実装します.
五.ストレージ・プロシージャまたは関数を作成して女神名を入力し、女神AND男神フォーマットの文字列を返します.
六.ストレージ・プロシージャまたは関数を作成し、受信したエントリ数と開始インデックスに基づいてbeautyテーブルのレコードをクエリーします.
関数の例
1.2つのfloatを渡す関数を作成し、両者の和を返す
接続クエリー
2020-4-20
全従業員の名前、番号、部門名を表示
select e.last_name, d.department_id, d.department_name
from departments d, employees e
where e.department_id = d.department_id;
90番従業員のjobを問い合わせるidと90番部門のlocation_id
select job_id, location_id
from employees e, departments d
where e.department_id = d.department_id
and e.department_id = 90
ボーナスのあるすべての従業員の情報を検索
select e.last_name, d.department_name, l.location_id, l.city
from employees e, departments d, locations l
where e.department_id = d.department_id
and d.location_id = l.location_id
and e.commission_pct is not null
各職種、部門名、最低賃金の照会
select department_name, job_title, min(salary)
from departments d, jobs j, employees e
where d.department_id = e.department_id
and e.job_id = j.job_id
group by department_name, job_title
国ごとの部門数が2より大きい国番号を検索
select count(*) , country_id
from departments d, locations l
where d.location_id = l.location_id
group by country_id
having >2
指定した従業員の名前と従業員番号、および対応する管理者の名前と従業員番号を問い合せます.
select e.last_name employees, e.employee_id "Emp#", m.last_name manager, m.employee_id "Mgr#"
from employees e, employees m
where e.manager_id = m.employee_id
古典的なケース
2020-4-21
1.最低賃金の従業員情報の照会:last_name, salary
select last_name, salary
from employees e
where e.salary = (
select min(salary)
from employees
)
2.平均給与が最も低い部門情報の照会
select e.department_id
from employees e
group by e.employee_id
order by avg(salary)
limit 1
3.平均給与が最も低い部門情報とその部門の平均給与を照会する
select d.*, ag
from departments d
join (
select avg(salary) ag, department_id
from employees
group by department_id
order by avg(salary)
limit 1
) ag_dep
on ag_dep.department_id = d.department_id
4.平均賃金が最も高いjob情報の照会
select avg(salary), job_id
from employees
group by employee_id
order by avg(salary) desc
limit 1
5.平均賃金が会社の平均賃金より高い部門情報の照会
select avg(salary), department_id
from employees
group by department_id
having avg(salary) > (
select avg(salary)
from employees
)
6.会社内のすべてのmanagerの詳細を問い合わせる
select
*
from
employees
where employee_id = any(
select distinct manager_id
from employees
where manager_id is not null
)
7.各部門の中で最も高い給料の中で最も低いその部門の最低給料はいくらですか
select min(salary), department_id
from employees
where department_id = (
select department_id
from employees
group by department_id
order by max(salary)
limit 1
)
8.平均給与が最も高い部門のmanagerの詳細を照会する:last_name, department_id, email, salary
select
last_name, department_id, email, salary
from
employees
where employee_id = any(
select manager_id
from employees
where department_id = (
select department_id
from employees
group by employee_id
order by avg(salary) desc
limit 1
)
)
select
last_name, e.department_id, email, salary
from
employees e
inner join
departments d
on d.manager_id = e.employee_id
where d.department_id = (
select department_id
from employees
group by employee_id
order by avg(salary) desc
limit 1
)
隔離レベル
独立性レベルの表示:
select @@tx_ioslation
独立性レベルの変更:set session|global transaction ioslation level
汚読たとえば、2つのトランザクションを同時に実行し、同じテーブルを処理します.テーブルの内容を変更してもトランザクションがコミットされていない場合、別のトランザクションでテーブルの内容を表示すると、データが変更されます.これがダーティリードです.
繰り返し不可
ダーティ・リードと似ていますが、別のトランザクションがコミットされた後に表示されます.ダーティ・リードは、別のトランザクションで変更すると、現在のトランザクションのテーブル・データが変更されます.
まぼろし読み
現在のテーブルでデータのすべてのローを変更すると、別のトランザクションが1つのローのデータを追加しています.この場合、現在のテーブル処理には1つのローが追加されます.これが幻読みです.
表示
ビュー作成ケース
1.ビューemp_の作成v 1、電話番号を調べて「011」で始まる従業員の名前、給料、メールボックスを要求します.
create or replace view emp_v2
as
select last_name, salary, email
from employees
where phone_number like '011%'
2.ビューemp_の作成v 2、部門の最高賃金が12000を超える部門情報の照会を要求する
create or replace view emp_v3
as
select max(salary), department_id
from employees
group by department_id
having max(salary)>12000
select d.*
from departments d
inner join emp_v3 e
on d.department_id = e.department_id
ビュー変更アクション
updata set
delect from
insert into value()
1.次のキーワードを含むsql文、distinct、groupby、having、union、union all
3.定数ビュー
create or replace view emp_v
as
select " " as name
4.selectにサブクエリを含める
create or replace view emp_v
as
select (select last_name from employees)
5.join更新できないビュー6.fromは更新できないビュー7である.where文のサブクエリはfrom文のテーブルを使用します
create or replace view emp_v
as
select e.*
from employees
where employees_id=(
select manager_id
from employees
where manager_id is not null
)
truncateとdelectのトランザクションの違い
truncateはロールバックできません
delectはロールバック可能
ビューテスト問題
1.次のフィールドでテーブルBookテーブルを作成する
bid整形、プライマリキーbname文字型、一意キーの設定が要求され、空price浮動小数点型ではなく、デフォルト値10 btypeIdタイプ番号が要求され、bookTypeテーブルを参照するidフィールドが要求される
create table Book(
bid int primary key,
bname varchar(20) unique not null,
price float default 10,
btypeld int,
foreign key(btypeld) references bookType(id)
)
既知のbookTypeテーブル(作成しない)は、id name 2のフィールドで、トランザクションを開いてテーブルに1行のデータを挿入し、終了します.
set autocommit = 0
inset into book(bid, bname, price, btypeId)
values(...)
commit|rockback;
3.クエリー価格が100より大きい書名とタイプ名を実現するビューを作成する
create or replace view myv1
as
select bname, name
from book b
inner join
bookType bt
on bt.id = b.btypeId
where price>100
4.ビューを修正し、検索価格が90-120の間の書名と価格を実現する
create or replace view myv1
as
select bname, price
from book
where price between 90 and 120
5.先ほど作成したビューを削除
drop view myv1
ストアド・プロシージャ・ケース
一.ストレージ・プロシージャの作成入力ユーザー名とパスワードをadminテーブルに挿入
delimiter $
create procedure my_pro1(in username varchar(20), in password int)
begin
insert into admin(admin.username, admin.password)
values(username, password);
end $
set names gbk$
call my_pro1("..", "..")
二.ストレージ・プロシージャまたは関数を作成して女神番号を入力し、女神名と女神電話を返します.
delimiter $
create procedure my_pro2(in id int, out name varchar(20), out phone varchar(20))
begin
select b.name, b.phone into name, phone
from beauty b
where b.id = id;
end $
call my_pro2(1, @name, @phone)
三.ストレージ・プロシージャまたは関数を作成して、2つの女神の誕生日を実現し、サイズを返します.
delimiter $
create procedure my_pro3(in birth1 datetime, in birth2 datetime, out result int)
begin
select datediff(birth1, birth2) into result;
end $
四.ストレージ・プロシージャまたは関数の作成は、xx年xx月xx日にフォーマットされ、戻ってくる日付を実装します.
delimiter $
create procedure my_pro4(in mydate datetime, out strDate varchar(20))
begin
select date_format(mydate, "%y %m %d ") into strDate;
end $
五.ストレージ・プロシージャまたは関数を作成して女神名を入力し、女神AND男神フォーマットの文字列を返します.
delimiter $
create procedure my_pro5(in name varchar(20), out cpname varchar(50))
begin
select concat(b.name,"AND", ifnull(bo.boyname, "null")) into cpname
from boys bo
right join
beauty b
on bo.id = b.boyfriend_id
where b.name = name;
end $
六.ストレージ・プロシージャまたは関数を作成し、受信したエントリ数と開始インデックスに基づいてbeautyテーブルのレコードをクエリーします.
delimiter $
create procedure my_pro6(in size int, in startindex int)
begin
select * from beauty limit startindex, size;
end $
関数の例
1.2つのfloatを渡す関数を作成し、両者の和を返す
delimiter $
create function my_fun1(num1 float, num2 float) returns float
begin
declare sum float default 0;
set sum=num1+num2;
return sum;
end $
select my_fun1(1,3)