高級(視圖 索引)

-- 視圖--


-- 視圖可以隱藏真正的表結構,對於比較重要的數據(如銀行),只讓別人訪問視圖,沒有權限使用真正的表


-- 創建視圖

create view v_stu_score_course asselect stu.name,stu.class,sc.score,cs.name as a from students stuINNER JOIN scores sc on stu.studentNo=sc.studentNoINNER JOIN courses cs on cs.courseNo=sc.courseNo

-- 使用視圖

select * from v_stu_score_course

--事務--

圖片

提交commit;

兩個命令行客戶端 左邊客戶端 右邊客戶端

1、左邊客戶端:查詢學生信息
select * from students;
2、右邊客戶端:開啓事務,插入數據begin;insert into students(studentNo,name) values ('013','abc');3、右邊客戶端:查詢數據,此時有新增的數據
select * from students;
4、左邊客戶端:查詢數據,發現並沒有新增的數據
select * from students;
5、右邊客戶端:完成提交
commit; 6、左邊客戶端:查詢,發現有新增的數據select * from students;

回滾rollback

兩個命令行客戶端 左邊客戶端 右邊客戶端1、左邊客戶端:查詢學生信息select * from students;
2、右邊客戶端:開啓事務,插入數據begin;insert into students(studentNo,name) values ('014','aaa');3、右邊客戶端:查詢數據,此時有新增的數據select * from students;4、左邊客戶端:查詢數據,發現並沒有新增的數據select * from students;5、右邊客戶端:回滾rollback;6、左邊客戶端:查詢,發現沒有新增的數據

--索引--

圖片


  • 查看索引

show index from 表名;


  • 創建索引

方式一:建表時創建索引

create table create_index(

id int primary key,

name varchar(10) unique,

age int,

key (age)

);

創建表時,對於主鍵和unique字段,自動創建索引


方式二:對於已經存在的表,添加索引
如果指定字段是字符串,需要指定長度,建議長度與定義字段時的長度一致字段類型如果不是字符串,可以不填寫長度部分create index 索引名稱 on 表名(字段名稱(長度))例:create index age_index on create_index(age);create index name_index on create_index(name(10));
  • 刪除索引:

    drop index 索引名稱 on 表名;

  • 分析查詢

    explain

    select * from test_index where title='test10000'


實操:

創建測試表testindexcreate table test_index(title varchar(10));
創建存儲過程
create procedure proc_test()在navicat查詢中,執行下面sql語句;(創建test_index表,並添加100000條記錄,命名爲title(i))begin
declare i int default 0;
while i<100000 do
insert into test_index(title) values(concat('test',i));
set i=i+1;
end while;
end
 調用存儲過程,向表中添加數據
call proc_test() 開啓運行時間監測:
set profiling=1;查找第1萬條數據test10000select * from test_index where title='test10000';查看執行的時間:show profiles;結果:時間長;
爲表title_index的title列創建索引:create index title_indexontest_index(title(10));
執行查詢語句:select * from test_index where title='test10000';
再次查看執行的時間
show profiles;
結果:時間短;(索引的作用)






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章