“数据库系统实验”实验报告(8)

Bella ·
更新时间:2024-09-21
· 561 次阅读

实验目的

为了测试一个新的功能组件,我们需要构建一个简单的实验楼课程数据库。

右边是实验楼的服务器,请尝试在 MySQL 中创建一个名称为 shiyanlou 的数据库。服务器中的 MySQL 还没有启动,请注意 MySQL 的 root 账户默认密码为空。

首先在服务器中下载要导入的数据文件,数据文件是 csv 格式的,数据项之间使用逗号隔开。

下载命令:

wget http://labfile.oss.aliyuncs.com/contestlou2/loudatabase.zip
copy
解压后文件夹中有三个 csv 数据文件,每个数据文件的格式和描述如下:

shiyanlou_user.csv:1000名实验楼用户数据,包含两列,用户ID和用户名
shiyanlou_course.csv:10门实验楼课程数据,包含两列,课程ID和课程名
shiyanlou_usercourse.csv:100条用户课程学习记录,包含三列,用户ID,课程ID和学习时间(分钟)

我的步骤 下载数据并解压 wget http://labfile.oss.aliyuncs.com/contestlou2/loudatabase.zip unzip loudatabase.zip 复制数据

此过程是为了避免数据导入时出现权限不足等错误(error1290)
可以尝试参考https://www.jianshu.com/p/129f29d37811

sudo cp /home/shiyanlou/loudatabase/shiyanlou_usercourse.csv /var/lib/mysql-files sudo cp /home/shiyanlou/loudatabase/shiyanlou_course.csv /var/lib/mysql-files/ sudo cp /home/shiyanlou/loudatabase/shiyanlou_user.csv /var/lib/mysql-files/ 打开mysql sudo service mysql start mysql -u root 新建数据库 create database shiyanlou; use shiyanlou 新建表 create table `user`( `id` int primary key, `name` varchar(32) not null )charset=utf8; create table `course`( `id` int primary key, `name` varchar(32) not null )charset=utf8; create table `usercourse`( `id` int(10) primary key not null auto_increment, `user_id` int not null, `course_id` int not null, `study_time` int not null )charset=utf8;

在建表的时候,使用了utf8,目的是为了避免中文编码造成的误差,
在写表名的时候使用`,为了避免一个error
在写usercourse这个表示,将id进行了自动增序编码的过程,避免之后读入数据是出现error1261
图片描述

设置外键连接表 Alter table usercourse add foreign key(`user_id`) references `user`(`id`); Alter table usercourse add foreign key(`course_id`) references `course`(`id`); 设置用户名和密码 create user 'shiyanlou'@'localhost' identified by 'shiyanlou'; 设置读写权利 grant all privileges on shiyanlou.* to 'shiyanlou'@'localhost';

图片描述

导入数据 load data infile '/var/lib/mysql-files/shiyanlou_usercourse.csv' into table usercourse FIELDS TERMINATED BY ','(user_id,course_id,study_time); load data infile '/var/lib/mysql-files/shiyanlou_user.csv' into table user character set utf8 fields terminated by ','enclosed by '"'; load data infile '/var/lib/mysql-files/shiyanlou_course.csv' into table course character set utf8 fields terminated by ','enclosed by '"';

图片描述

实验小结

实验结束,通过此次实验,自己手写了许多error,从谷歌、百度上见到了许多大佬的解决方法,自己得到了提升.

Greyson: 原创文章 15获赞 12访问量 493 关注 私信 展开阅读全文
作者:Greyson:



实验报告 数据 系统 数据库系统 数据库

需要 登录 后方可回复, 如果你还没有账号请 注册新账号