mysql 第四次作业

新建数据库

mysql> create database mydb15_indexstu;
Query OK, 1 row affected (0.01 sec)

mysql> use mydb15_indexstu;
Database changed

新建表student  course sc

mysql> create table student(sno int primary key auto_increment,sname varchar(30) not null unique, ssex varchar(2) check (ssex='男' or ssex='女') not null, sage int not null, sdept varchar(10) default '计算机' not null);
Query OK, 0 rows affected (0.04 sec)

mysql> desc student;
+-------+-------------+------+-----+-----------+----------------+
| Field | Type        | Null | Key | Default   | Extra          |
+-------+-------------+------+-----+-----------+----------------+
| sno   | int         | NO   | PRI | NULL      | auto_increment |
| sname | varchar(30) | NO   | UNI | NULL      |                |
| ssex  | varchar(2)  | NO   |     | NULL      |                |
| sage  | int         | NO   |     | NULL      |                |
| sdept | varchar(10) | NO   |     | 计算机    |                |
+-------+-------------+------+-----+-----------+----------------+
5 rows in set (0.00 sec)

mysql> create table course(cno int primary key not null, cname varchar(20) not null);
Query OK, 0 rows affected (0.01 sec)

mysql> desc course;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| cno   | int         | NO   | PRI | NULL    |       |
| cname | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> create table sc(sno int not null, cno varchar(10) primary key not null, score int not null);
Query OK, 0 rows affected (0.01 sec)

mysql> desc sc;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sno   | int         | NO   |     | NULL    |       |
| cno   | varchar(10) | NO   | PRI | NULL    |       |
| score | int         | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

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

mysql> alter table student modify  sage smallint;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc student;
+-------+-------------+------+-----+-----------+----------------+
| Field | Type        | Null | Key | Default   | Extra          |
+-------+-------------+------+-----+-----------+----------------+
| sno   | int         | NO   | PRI | NULL      | auto_increment |
| sname | varchar(30) | NO   | UNI | NULL      |                |
| ssex  | varchar(2)  | NO   |     | NULL      |                |
| sage  | smallint    | YES  |     | NULL      |                |
| sdept | varchar(10) | NO   |     | 计算机    |                |
+-------+-------------+------+-----+-----------+----------------+
5 rows in set (0.00 sec)

2.为Course表中Cno 课程号字段设置索引,并查看索引

mysql> create index cno_index on course(cno);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table course \G
*************************** 1. row ***************************
       Table: course
Create Table: CREATE TABLE `course` (
  `cno` int NOT NULL,
  `cname` varchar(20) NOT NULL,
  PRIMARY KEY (`cno`),
  KEY `cno_index` (`cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

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

mysql> create unique index sc_index on sc(sno , cno asc);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

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

mysql> create view stu_info as select sname,ssex,cname,score from student,course,sc;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from stu_info;
Empty set (0.00 sec)

5.删除所有索引

mysql> drop  index cno_index on course;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> drop  index sc_index on sc;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
开放大学mysql实验训练4作业是一个提供给学生进行实践的作业,旨在帮助学生通过实际操作加深对MySQL数据库的理解和掌握。 在这个实验训练中,学生将会学习如何创建和操作数据库中的表格。这包括创建表格、定义表格的结构、插入数据、更新数据、删除数据、查询数据等等。在完成这些任务的过程中,学生会学习到如何使用SQL语句进行数据库操作。 通过完成这个作业,学生将会掌握以下几个方面的内容: 1. 数据库的创建与删除:学生将会学习如何使用SQL语句创建和删除数据库。这是学生在进行数据库操作前的第一步。 2. 表格的创建与删除:学生将会学习如何使用SQL语句创建和删除表格。表格是存储数据的基本单位,学生将会学习如何定义表格的结构以及设置表格的属性。 3. 数据的插入与更新:学生将会学习如何使用SQL语句向表格中插入数据以及更新已有的数据。这是学生在真实应用中经常需要进行的操作。 4. 数据的删除与查询:学生将会学习如何使用SQL语句删除表格中的数据以及查询表格中的数据。这是学生在进行数据管理和数据分析时需要掌握的基本技能。 通过完成这个作业,学生将会加深对于MySQL数据库的理解和掌握,并能够独立进行数据库的创建、表格的创建、数据的插入和查询等操作。这将为学生今后从事与数据库相关的工作打下坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值