【資料庫MySQL】練習---檢視
阿新 • • 發佈:2021-02-04
步驟1:建立學生表stu,插入3條記錄
#建立stu表
CREATE TABLE stu
(
s_id INT PRIMARY KEY,
s_name VARCHAR(20),
addr VARCHAR(50),
tel VARCHAR(50)
);
#stu表中插入資料
INSERT INTO stu
VALUES(1,'XiaoWang','Henan','0371-12345678'),
(2,'XiaoLi','Hebei','13889072345'),
(3,'XiaoTian','Henan','0371-12345670');
步驟2:建立報名表sign,插入3條記錄
#建立sign表 CREATE TABLE sign ( s_id INT PRIMARY KEY, s_name VARCHAR(20), s_sch VARCHAR(50), s_sign_sch VARCHAR(50) ); #表sign中插入資料 INSERT INTO sign VALUES(1,'XiaoWang','Middle School1','Peking University'), (2,'XiaoLi','Middle School2','Tsinghua University'), (3,'XiaoTian','Middle School3','Tsinghua University');
步驟3:建立成績表stu_mark,插入3條記錄
CREATE TABLE stu_mark (s_id INT PRIMARY KEY ,s_name VARCHAR(20) ,mark int );
INSERT INTO stu_mark VALUES(1,'XiaoWang',80),(2,'XiaoLi',71),(3,'XiaoTian',70);
步驟4:建立考上Peking University的學生的檢視
create view Peki_stu(學號,姓名,成績,大學) as select a.s_id,a.s_name,mark,s_sign_sch from sign a inner join stu_mark b on a.s_id=b.s_id where s_sign_sch='Peking University';
步驟5:建立考上Tsinghua University的學生的檢視
create view Tsing_stu(學號,姓名,成績,大學) as
select a.s_id,a.s_name,mark,s_sign_sch from sign a inner join stu_mark b
on a.s_id=b.s_id
where s_sign_sch='Tsinghua University';
步驟6:XiaoTian的成績在錄入的時候錄入錯誤多錄了50分,對其錄入成績進行
update stu_mark
set mark=mark-50
where stu_mark.s_name='XiaoTian';
步驟7:檢視更新過後檢視和表的情況
步驟8:檢視檢視的建立資訊
#檢視北大學生資訊
mysql> show create view Peki_stu \G
*************************** 1. row ***************************
View: Peki_stu
Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`skip-grants user`@`skip-grants host` SQL SECURITY DEFINER VIEW `Peki_stu` AS select `a`.`s_id` AS `學號`,`a`.`s_name` AS `姓名`,`b`.`mark` AS `成績`,`a`.`s_sign_sch` AS `大學` from (`sign` `a` join `stu_mark` `b` on((`a`.`s_id` = `b`.`s_id`))) where (`a`.`s_sign_sch` = 'Peking University')
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
#檢視清華學生資訊
mysql> show create view Tsing_stu \G
*************************** 1. row ***************************
View: Tsing_stu
Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`skip-grants user`@`skip-grants host` SQL SECURITY DEFINER VIEW `Tsing_stu` AS select `a`.`s_id` AS `學號`,`a`.`s_name` AS `姓名`,`b`.`mark` AS `成績`,`a`.`s_sign_sch` AS `大學` from (`sign` `a` join `stu_mark` `b` on((`a`.`s_id` = `b`.`s_id`))) where (`a`.`s_sign_sch` = 'Tsinghua University')
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
步驟9:刪除建立的檢視
完成!!!