准备工作,新建名为students的数据,三张表分别是student,courses,stu_cou,并创建外键约束,级联删除更新,插入数据。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/*创建数据库*/ create database if not EXISTS students character set utf8 collate utf8_general_ci; /*创建表*/ use students; create table if not EXISTS student ( stuID int(5) not null primary key, stuName varchar(50) not null, stuSex CHAR(10), stuAge smallint ); CREATE TABLE if not EXISTS courses( couID int not null primary key auto_increment COMMENT '学号', couName varchar(50) not null DEFAULT('大学英语'), couHours smallint UNSIGNED COMMENT '学时', couCredit float DEFAULT(2) COMMENT '学分' )ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_0900_ai_ci; CREATE TABLE if not EXISTS stu_cou( ID int not null primary key auto_increment, stuID int(5) not null COMMENT '学号', couID int not null COMMENT '课程编号', time timestamp not null DEFAULT(now()) ); /*添加外键约束*/ alter table stu_cou add CONSTRAINT fk_stuid foreign key(stuID) REFERENCES student(stuID) ON UPDATE CASCADE ON DELETE CASCADE ; alter table stu_cou add CONSTRAINT fk_couid foreign key(couID) REFERENCES courses(couID) ON UPDATE CASCADE ON DELETE CASCADE ; /*插入数据*/ insert into student(stuID,stuName,stuSex,stuAge) values(1001,'张三','男',19),(1002,'李四','男',18),(1003,'王五','男',18),(1004,'黄丽丽','女',18),(1005,'李晓辉','女',19),(1006,'张敏','女',18); insert into student VALUES(1007,'五条人','男',20),(1008,'胡五伍','女',19); insert into courses(couID,couName,couHours,couCredit) values(50,'大学英语',64,2),(60,'计算机基础',78,2.5),(70,'Java程序设计',108,6),(80,'数据库应用',48,2.5); insert into stu_cou(stuID,couID) values(1001,50),(1001,60),(1001,70),(1001,80),(1002,50),(1002,60),(1002,70),(1002,80),(1003,50),(1003,60),(1003,70),(1003,80),(1004,50),(1004,60),(1004,70),(1004,80),(1005,50),(1005,60),(1005,70),(1005,80),(1006,50),(1006,60),(1006,70),(1006,80); alter table stu_cou add COLUMN grade FLOAT null; UPDATE stu_cou set grade=(SELECT FLOOR(50 +RAND() * 50)); alter table student add COLUMN stuColleage varchar(100) null; update student set stuColleage='大数据学院' where stuID BETWEEN 1001 and 1003; update student set stuColleage='物流学院' where stuID BETWEEN 1004 and 1006; update student set stuColleage='康养学院' where stuID BETWEEN 1007 and 1008; alter table courses add COLUMN couColleage varchar(50) null; update courses set couColleage='通识教育学院' where couName='大学英语'; update courses set couColleage='通识教育学院' where couName='计算机基础'; update courses set couColleage='大数据学院' where couName='Java程序设计'; update courses set couColleage='大数据学院' where couName='数据库应用'; insert into courses values(90,'大学体育',56,1.5,'通识教育学院'),(100,'Android程序设计',92,5,'大数据学院'),(101,'大学物理',48,2,'通识教育学院'); insert into stu_cou(stuID,couID,grade) values(1007,50,86),(1007,60,71),(1008,70,56),(1008,80,63); insert into student VALUES(1009,'曾小小','男',17,'物流学院'),(1010,'项XXX','女',21,'大数据学院');View Code
1.视图的语法
create view 视图名称 [列名1,列名2,...] as 查询语句 [with check option];
其中,AS后的查询可以是任意具体的数据库系统支持的SELECT语句。语句WITH CHECK OPTION表示通过视图进行更新操作时要保证更新的数据满足子查询的条件表达式。
组成视图的列名要么省略,要么全部指定。如果省略,则视图的列名就由子查询中的列名组成。
(1)在下列情况下,必须指定视图列名:
- 子查询的某个目标列是聚合函数或列表达式;
- 多表连接时出现同名列作为视图的列;
- 需要在视图中指定新列名替代子查询列名。
例1:创建一个名为“查询所有大数据学院学生”的视图
create view `查询所有大数据学院学生` as select * from student where stuColleage='大数据学院' with check option;
例2:创建一个名为“查询学生表中男女同学的人数”的视图
create view `查询学生表中男女同学的人数`(`性别`,`人数`) as select stuSex,count(stuID) as '人数' from student GROUP BY stuSex;
标签:50,视图,courses,student,MySQL,null,stuID From: https://www.cnblogs.com/YorkZhangYang/p/16893594.html