mysql8学习笔记③数据库和表的创建等常用操作

数据库对象命名规则

为表和列选择合适的名字

1.所有数据库对象名称必须使用小写字母可选用下划线分隔

2.所有数据库对象名称定义禁止使用mysql保留关键字

3.数据库对象的命名要做到见名知意,并且最好不要超过32个字

4.临时库表必须以tmp为前缀并以日期为后缀

5.用户备份的表,表必须以bak为前缀并且以日期为后缀

6.所有存储相同数据的列名和列类型必须一致

创建数据库、表

create database imc_db;

use imc_db;

use imc_db;

# 课程主表
create table imc_course (
    course_id int unsigned auto_increment comment '课程ID',
    title varchar(20) not null default '' comment '课程主标题',
    title_desc varchar(50) not null default '' comment '课程副标题',
    type_id smallint unsigned not null default 0 comment '课程方向ID',
    class_id smallint unsigned not null default 0 comment '课程分类ID',
    level_id smallint unsigned not null default 0 comment '课程难度ID',
    online_time datetime not null default current_timestamp comment '课程上线时间',
    study_cnt int unsigned not null default 0 comment '学习人数',
    course_time time not null default '0:00' comment '课程时长',
    intro varchar(200) not null default '' comment '课程简介',
    info varchar(200) not null default '' comment '学习须知',
    harvest varchar(200) not null default '' comment '课程收获',
    user_id int unsigned not null default 0 comment '讲师ID',
    main_pic varchar(200) not null default '' comment '课程主图片',
    content_score decimal(3,1) not null default 0.0 comment '内容评分',
    level_score decimal(3,1) not null default 0.0 comment '简单易懂',
    logic_score decimal(3,1) not null default 0.0 comment '逻辑清晰',
    score decimal(3,1) not null default 0.0 comment '综合评分',
    primary key(course_id),
    unique key udx_title (title)
)comment '课程主表';



# 课程章节
create table imc_chapter(
    chapter_id int unsigned auto_increment not null comment '章节ID',
    course_id int unsigned not null default 0 comment '课程ID',
    chapter_name varchar(50) not null default '' comment '章节名称',
    chapter_info varchar(200) not null default '' comment '章节说明',
    chanpter_no tinyint(2) unsigned  zerofill not null default 0 comment '章节编号',
    primary key(chapter_id),
    unique key udx_courseid_chaptername (course_id,chapter_name)
)comment '课程章节';

# 课程小节表
create table imc_subsection(
    sub_id int unsigned auto_increment not null comment '小节ID',
    chapter_id int unsigned not null default 0 comment '章节ID',
    course_id int unsigned not null default 0 comment '课程ID',
    sub_name varchar(50) not null default '' comment '小节名称',
    sub_url varchar(200) not null default '' comment '小节URL',
    video_type enum('avi', 'mp4', 'mpeg') not null default 'mp4' comment '视频格式',
    sub_time time not null default '0:00' comment '小节时长',
    chapter_no tinyint(2) unsigned zerofill not null default 0 comment '章节编号',
    primary key(sub_id),
    unique key udx_chapterid_courseid_subname (chapter_id,course_id,sub_name)
)comment '课程小节表';

# 课程分类表
create table imc_class (
    class_id smallint unsigned auto_increment not null comment '课程分类ID',
    class_name varchar(10) not null default '' comment '分类名称',
    add_time timestamp not null default current_timestamp comment '添加时间',
    primary key (class_id)
)comment '课程分类';

create table imc_level(
    level_id smallint unsigned auto_increment not null comment '课程难度ID',
    level_name varchar(10) not null default '' comment '课程难度名称',
    add_time timestamp not null default current_timestamp comment '添加时间',
    primary key (level_id)
)comment '课程难度表';


# 用户表
create table imc_user(
    user_id int unsigned auto_increment not null comment '用户ID',
    user_nick varchar(20) not null default '慕课网' comment '用户昵称',
    user_pwd char(32) not null default '' comment '密码',
    sex char(2) not null default '' comment '性别',
    province varchar(20) not null default '' comment '性别',
    city varchar(20) not null default '' comment '',
    position varchar(10) not null default '未知' comment '经验值',
    mem varchar(100) not null default '' comment '说明',
    exp_cnt mediumint unsigned default 0 comment '经验值',
    score int unsigned not null default 0 comment '积分',
    follow_cnt int unsigned not null default 0 comment '关注人数',
    fans_cnt int unsigned not null default 0 comment '粉丝人数',
    is_teachar tinyint unsigned not null default 0 comment '讲师标识,0:普通用户,1:将是用户',
    reg_time datetime not null default current_timestamp comment '注册时间',
    user_status tinyint unsigned not null default 1 comment '用户状态 1:正常 0:冻结',
    primary key(user_id),
    unique key udx_usernick(user_nick)
)comment '用户表';

# 问答评论表
create table imc_question(
    quest_id int unsigned auto_increment not null comment '评论',
    user_id int unsigned not null default 0  comment '用户ID',
    course_id int unsigned not null default 0  comment '课程ID',
    chapter_id int unsigned not null default 0  comment '章节ID',
    sub_id int unsigned not null default 0  comment '小节ID',
    replyid int unsigned not null default 0  comment '父评论ID',
    quest_title varchar(50) not null default '' comment '评论标题',
    quest_content text comment '评论内容',
    quest_type enum('问答', '评论') not null default '评论' comment '评论类型',
    view_cnt int unsigned not null default 0 comment '浏览量',
    add_time datetime not null default current_timestamp comment '发布时间',
    primary key(quest_id)
) comment '问答评论表';


# 用户笔记表
create table imc_note(
    note_id int unsigned auto_increment not null comment '笔记ID',
    user_id int unsigned not null default 0  comment '用户ID',
    course_id int unsigned not null default 0  comment '课程ID', 
    chapter_id int unsigned not null default 0  comment '章节ID',
    sub_id int unsigned not null default 0  comment '小节ID',
    note_title varchar(50) not null default '' comment '笔记标题',
    note_content text comment '评论内容',
    add_time datetime not null default current_timestamp comment '发布时间',
    primary key(note_id)
) comment '笔记表';

# 课程评价表
create table imc_classvalue(
    value_id int unsigned auto_increment not null comment '评价ID',
    user_id int unsigned not null default 0  comment '用户ID',
    course_id int unsigned not null default 0  comment '课程ID', 
    content_score decimal(3,1) not null default 0.0 comment '内容评分',
    level_score decimal(3,1) not null default 0.0 comment '简单易懂',
    logic_score decimal(3,1) not null default 0.0 comment '逻辑清晰',
    score decimal(3,1) not null default 0.0 comment '综合评分',
    add_time datetime not null default current_timestamp comment '发布时间',
    primary key(value_id)
)comment '课程评价表';


CREATE TABLE `imc_type` (
  `type_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT COMMENT '课程方向ID',
  `type_name` varchar(10) NOT NULL DEFAULT '' COMMENT '课程方向名称',
  `add_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '填加时间',
  PRIMARY KEY (`type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='课程方向表';

插入测试数据

LOCK TABLES `imc_class` WRITE;

INSERT INTO `imc_class` VALUES (1,'MySQL','2019-01-24 08:57:26'),(2,'Redis','2019-01-24 08:57:26'),(3,'MongoDB','2019-01-24 08:57:26'),(4,'安全测试','2019-01-24 08:57:26'),(5,'Oracle','2019-01-24 08:57:26'),(6,'SQL Server','2019-01-24 08:57:26'),(7,'Hbase','2019-01-24 08:57:26'),(8,'大数据','2019-01-24 08:57:26'),(9,'HadoopDock','2019-01-24 08:57:26'),(10,'Docker','2019-01-24 08:57:26'),(11,'运维','2019-01-24 08:57:26'),(12,'Linux','2019-01-24 08:57:26'),(13,'自动化运维','2019-01-24 08:57:26');

UNLOCK TABLES;

posted @ 2020-07-23 17:42  reblue520  阅读(578)  评论(7编辑  收藏  举报