MySQL数据库实际运用(一)

实验目的

(1) 掌握系统数据类型的特点和功能
(2) 掌握创建、修改表结构的方法
(3) 掌握数据添加的方法

实验预习与准备

(1) MYSQL中创建数据库的命令
(2) 表结构的创建、修改和删除
(3) MYSQL导入导出表数据的方式
(4) 添加数据的SQL命令

实验内容及步骤

(1) 利用SQL命令创建自己的的数据库 (smallfish)

create database smallfish;

(2) 使用命令行方式利用SQL语句在第一步创建的数据库中按下列要求创建数据表

  • student 表
列名类型是否允许为空字段说明
snochar(10)not null学号
snamechar(10)not null姓名
sexenum(‘男’,‘女’) vnotnull性别
birthdatenot null出生日期
sourcevarchar(16)null生源地
clnochar(10)null班级号
phonechar(11)null电话
creditsmallintnull学分
picturevarchar(30)null照片(存放地址)
remarktextnull字段说明
pwdchar(6)not null密码
use smallfish;   # 进入数据库
# 建立student表
mysql> create table student(
    -> sno char(10) not null,
    -> sname char(10) not null,
    -> sex enum("男","女") not null,
    -> birth date not null,
    -> source varchar(16),
    -> clno char(10),
    -> phone char(11),
    -> credit smallint,
    -> picture varchar(30),
    -> remark text,
    -> pwd char(6) not null);
  • course表(课程表)
列名类型是否允许为空字段说明
cnochar(6)not null课程号
cnamevarchar(16)not null课程名
credittinyintnot null学分
hoursmallintnot null学时
termtinyintnot null开课学期(取值范围1-8)
mysql> create table course(
    -> cno char(6) not null,
    -> cname varchar(16) not null,
    -> credit tinyint not null,
    -> hour smallint not null,
    -> term tinyint not null);
  • score表(成绩表)
列名类型是否允许为空字段说明
snochar(10)not null学生编号
cnochar(6)not null课程编号
scoredecimal(4,1)null期末成绩
mysql> create table score(
    -> sno char(10) not null,
    -> cno char(6) not null,
    -> score decimal);
  • teacher表(教师表)
列名类型是否允许为空字段说明
tno vchar(4)not null教师号
tnamechar(10)not null姓名
pwdchar(6)not null密码
sexenum(‘男’,‘女’)not null性别
telchar(11)null电话
departmentvarchar(10)not null院系名称
typechar(1)not null身份(0:管理员;1:教师)
remarktextnull字段说明
mysql> create table teacher(
    -> tno char(6) not null,
    -> tname char(10) not null,
    -> pwd char(6) not null,
    -> sex enum("男","女"),
    -> tel char(11),
    -> department varchar(10) not null,
    -> type char(1) not null,
    -> remark text);
  • class表(班级表)
列名类型是否允许为空字段说明
clnochar(8)not null班级编号
clnamevarchar(16)not null班级名称
departmentvarchar(12)not null院系名称
mysql> create table class(
    -> clno char(8) not null,
    -> clname varchar(16) not null,
    -> department varchar(12) not null);
  • course_class表(教师授课表)
列名类型是否允许为空字段说明
列名类型是否允许为空字段说明
tnochar(4)not null教师编号
clnochar(8)not null班级编号
cnochar(6)not null课程编号
mysql> create table course_class(
    -> tno char(4) not null,
    -> clno char(8) not null,
    -> cno char(6) not null);

(3) 利用SQL语句修改表的结构

a) 修改student表中clno属性的数据类型为varchar(8),并且不允许为空

alter table student modify clno varchar(8);

b) 将student表中的phone属性名修改为tel,类型不变

alter table student rename column phone to tel;

c) 为student表增加point属性(入学成绩),数据类型为smallint,允许为空,并将属性至于tel属性之后,credit属性之前

alter table student add point smallint null after tel;

d) 为student表增加email属性,数据类型为varchar(30),允许为空,并将属性至于picture属性之后,remark属性之前

alter table student add email varchar(30) null after picture;

e) 为score表增加usual属性(平时成绩),数据类型为decimal(4,1),允许为空

alter table score add usual decimal(4,1);

f) 删除student表中的pwd属性

alter table student drop column pwd;

(4) 利用MYSQL的数据导入/导出功能将jwgl数据库中的相应表中数据导入到自己创建的数据表中

load data infile 'D:/MySQL Data/class.txt' into table class;
load data infile 'D:/MySQL Data/course.txt' into table course;
load data infile 'D:/MySQL Data/course_class.txt' into table course_class;
load data infile 'D:/MySQL Data/score.txt' into table score;
load data infile 'D:/MySQL Data/teacher.txt' into table teacher;
load data infile 'D:/MySQL Data/student.txt' into table student;

(5)向表中插入新的信息

向student表中插入自己的个人信息

mysql> insert into student
    -> (sno,sname,sex,birth,clno,tel,point,email)
    -> values('202015741','小鱼干','男','2020-01-01','12920301','12345678999',600,'xiaoyvgan@qq.com');

向score表中插入一条记录,学生学号为:0922221326,课程编号为:010003,其余属性取空值

mysql> insert into score(sno,cno)
    -> values('0922221326','010003');
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼干儿♛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值