当前位置: 首页 > news >正文

mysql索引--实例

学生表:Student (Sno, Sname, Ssex , Sage, Sdept)
学号,姓名,性别,年龄,所在系 Sno为主键
课程表:Course (Cno, Cname,)
课程号,课程名 Cno为主键
学生选课表:SC (Sno, Cno, Score)
学号,课程号,成绩   Sno和Con为主键
1.SQL语句创建学生表student,定义主键,姓名不能重名,性别只能输入男或女,所在系的默认值是 计算机
2.修改student 表中年龄(age)字段属性,数据类型由int 改变为smallint
3.SC表建立按学号(sno)和课程号(cno)组合的升序的主键索引,索引名SC_INDEX 
4.创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。

创建表 

#student
>create table student(Sno int primary key,-> Sname char(20) unique, -> Ssex char(4) check(Ssex in ('男','女')), -> Sage int, -> Sdept char(20) default '计算机');#course
>cretae table course(-> Cno int primary key,-> Cname char(20));#sc
>create table SC(-> sno int(10),-> cno int(10), -> score int(10),-> primary key (sno,cno),-> foreign key(sno) references Student(sno),# 外键约束-> foreign key(cno) references Course(cno));一个表可以有多个主键;
>create table sc( Sno int, Cno int, Score int);
Query OK, 0 rows affected (0.00 sec)mysql8.0 [HH]>desc sc;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| Sno   | int  | YES  |     | NULL    |       |
| Cno   | int  | YES  |     | NULL    |       |
| Score | int  | YES  |     | NULL    |       |
+-------+------+------+-----+---------+-------+
3 rows in set (0.00 sec)mysql8.0 [HH]>alter table sc add primary key (Sno,Cno);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql8.0 [HH]>desc sc;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| Sno   | int  | NO   | PRI | NULL    |       |
| Cno   | int  | NO   | PRI | NULL    |       |
| Score | int  | YES  |     | NULL    |       |
+-------+------+------+-----+---------+-------+
2.修改student 表中年龄(age)字段属性,数据类型由int 改变为smallint
>alter table student modify Sage smallint;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql8.0 [HH]>desc student;
+-------+----------+------+-----+-----------+-------+
| Field | Type     | Null | Key | Default   | Extra |
+-------+----------+------+-----+-----------+-------+
| Sno   | int      | NO   | PRI | NULL      |       |
| Sname | char(20) | YES  | UNI | NULL      |       |
| Ssex  | char(4)  | YES  |     | NULL      |       |
| Sage  | smallint | YES  |     | NULL      |       |
| Sdept | char(20) | YES  |     | 计算机    |       |
+-------+----------+------+-----+-----------+-------+
5 rows in set (0.01 sec)

3.SC表建立按学号(sno)和课程号(cno)组合的升序的主键索引,索引名SC_INDEX
#一个表可以有多个主键:在原表没有有主键的基础上才可以添加两个主键,如果已有主键先删除
>create table sc( Sno int, Cno int, Score int);
Query OK, 0 rows affected (0.00 sec)mysql8.0 [HH]>desc sc;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| Sno   | int  | YES  |     | NULL    |       |
| Cno   | int  | YES  |     | NULL    |       |
| Score | int  | YES  |     | NULL    |       |
+-------+------+------+-----+---------+-------+
3 rows in set (0.00 sec)mysql8.0 [HH]>alter table sc add primary key (Sno,Cno);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql8.0 [HH]>desc sc;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| Sno   | int  | NO   | PRI | NULL    |       |
| Cno   | int  | NO   | PRI | NULL    |       |
| Score | int  | YES  |     | NULL    |       |
+-------+------+------+-----+---------+-------+
>create index SC_INDX on sc(sno asc,cno asc);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

4.创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
>CREATE VIEW stu_info as select student.Sname,student.Ssex,course.Cno,sc.score
from student,sc,course where student.Sno=scc.sno and sc.cno=course.Cno;

http://www.lryc.cn/news/8051.html

相关文章:

  • 浅聊一下,可中断锁(ReentrantLock)
  • 关于Arcgis林业数据处理的62个常用技巧
  • 一些NLP术语
  • Session详解,学习 Session对象一篇文章就够了
  • Java——不同的子序列
  • Git 基本操作之Git GUI界面和git命令行如何选择
  • Python编程 动态爱心
  • JavaScript :基础语法
  • buu [AFCTF2018]Single 1
  • Linux C++ 200行完成线程池类
  • C语言指针剖析(初阶) 最详细!
  • AcWing语法基础课笔记 第三章 C++中的循环结构
  • A simple freeD tracking protocol implementation written in golang
  • 简约精美电商小程序【源码好优多】
  • 全网详解 .npmrc 配置文件:比如.npmrc的优先级、命令行,如何配置.npmrc以及npm常用命令等
  • 从0开始学python -31
  • Jenkins的使用教程
  • 1.Maven的坐标和依赖
  • Jenkins 笔记
  • Python和Java语言,哪个更适合做自动化测试?
  • 互联网的路由选择协议
  • 接口幂等性处理
  • 数字孪生智慧机场:透视数字化时代下的航空运营
  • SpringBoot 文件上传后查看404的问题和解决404后需要访问两次才能查看的问题
  • 定时任务使用总结
  • Jira和Confluence Server版终止支持倒计时365天,企业应对策略汇总
  • GEE学习笔记九十一:栅格影像叠置分析
  • linux系统编程入门
  • JS代码安全防护常见的方式
  • PHP(13)HTTP协议