MySQL—视图(十)

一、数据准备

-- 数据准备
-- 1 创建教师表
CREATE TABLE teacher(
tea_num VARCHAR(10) PRIMARY KEY COMMENT "教师编号",-- 主键
name VARCHAR(30) COMMENT "姓名",
gender CHAR(2) COMMENT "性别",
age INT COMMENT "年龄",
department VARCHAR(30) COMMENT "部门",
institute VARCHAR(50) COMMENT "学院",
salary DECIMAL(10,2) COMMENT "薪水",
hire_date date COMMENT "入职时间"
);

-- 1.1 插入数据
-- 教师号为100001,姓名为旗木卡卡西,性别男,年龄30,科室为综合科,就职学院为火影学院,薪水2000,入职日期为1988-12-21
INSERT INTO teacher
VALUES("100001","旗木卡卡西","男",30,"综合科","火影学院",2000.56,"1988-12-21");
-- 教师号为100002,姓名为迈特凯,性别男,年龄31,科室为体术科,就职学院为火影学院,薪水2100,入职日期为1987-10-21
INSERT INTO teacher
VALUES("100002","迈特凯","男",31,"体术科","火影学院",2100.58,"1987-10-21");
-- 教师号为100003,姓名为孙悟空,性别男,年龄100,科室为猴科,就职学院为西游学院,薪水1500,入职日期为1966-11-01
INSERT INTO teacher
VALUES("100003","孙悟空","男",100,"猴科","西游学院",1500.123,"1966-11-01");
-- 教师号为100004,姓名为马基,性别男,年龄42,科室为风科,就职学院为风影学院,薪水3000,入职日期为1975-04-21
INSERT INTO teacher
VALUES("100004","马基","男",42,"风科","风影学院",3000.222,"1975-04-21");

二、实操练习

-- 1 创建视图
CREATE VIEW teacher_v 
AS SELECT tea_num,name,gender,age,department,institute,hire_date
FROM teacher;

-- 查看创建的视图
SHOW TABLES;
-- 查看创建的视图详情
SELECT *
FROM teacher_v;

-- 2 修改视图
ALTER VIEW teacher_v 
AS SELECT tea_num,name,gender,department,institute,hire_date
FROM teacher;

-- 3 删除视图
DROP VIEW teacher_v;

-- 4 查看视图的语法 (\G 在Navicat上无效)
SHOW CREATE VIEW teacher_v \G;
SELECT * FROM teacher_v;

你可能感兴趣的:(MySQL,mysql,数据库)