• MySQL数据库索引练习


    1.学生表:Student (Sno, Sname, Ssex , Sage, Sdept)
    学号,姓名,性别,年龄,所在系 Sno为主键
    课程表:Course (Cno, Cname,)
    课程号,课程名 Cno为主键
    学生选课表:SC (Sno, Cno, Score)
    学号,课程号,成绩 Sno,Cno为主键

    1.用SQL语句创建学生表student,定义主键,姓名不能重名,性别只能输入男或女,所在系的默认值是 “计算机”。

    create table Student(
         Sno int primary key auto_increment,
         Sname varchar(100) NOT NULL unique,
         Ssex varchar(20) check (Ssex='男' or Ssex='女') NOT NULL,
         Sage int NOT NULL,
         Sdept varchar(100) default '计算机' NOT NULL
         );
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.修改student 表中年龄(age)字段属性,数据类型由int 改变为smallint。

    alter table Student modify Sage smallint;
    
    • 1

    3.为SC表建立按学号(sno)和课程号(cno)组合的升序的主键索引,索引名为SC_INDEX 。

    创建Course表
    create table Course(
         Cno int primary key NOT NULL,
         Cname varchar(100) NOT NULL
         );
    创建SC表
    create table SC(
         Sno int NOT NULL,
         Cno int primary key NOT NULL,
         Score int NOT NULL
         );
    
    create unique index SC_INDEX on SC(Sno asc, Cno asc);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4.创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。

     create view stu_info as select Student.Sname as '姓名', Student.Ssex as '性别', Course.Cname as '课程名', SC.Score as '成绩' from Student, Course, SC;
    
    • 1

    2.创建学生信息表:学号(主键),姓名,年龄, 班级(外键关联到班级表的班级编号)
    班级表:班级编号(主键), 班级名称

    create table Student(
    	Sno int primary key,
    	Sname char,
    	Sage int,
    	C_name char
    	);
    
    create table Class(
    	Cno int primary key,
    	Cname char,
    	foreign key Cno references C_name
    	);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.创建一张表(id, data): 创建一个存储过程,要求可以调用存储过程向表中插入200条,2000条,20000条
    提示调用存储过程:call procedure_name(参数)

    create table Data(
    	id int primary key,
    	data char
    	);
    
    • 1
    • 2
    • 3
    • 4

    4.题3中相同的表:创建一个函数,要求可以调用函数向表中插入200条,2000条,20000条
    提示调用函数:select function_name(参数)

  • 相关阅读:
    CF603E Pastoral Oddities
    如何高效实现 MySQL 与 elasticsearch 的数据同步
    【Node.js】path 模块进行路径处理
    GitHub平台 Bookget操作
    你见过哪些实用到爆的 Java 代码技巧?
    Jenkins部署springboot项目至远程服务器
    剑指 Offer II 079+080+081+082
    Jquery 获取当前时间日期
    超写实虚拟人制作模拟真人般交流互动
    fisco Java-sdk 快速入门案例
  • 原文地址:https://blog.csdn.net/weixin_54986292/article/details/133525818