1. 程式人生 > 實用技巧 >mysql8學習筆記③資料庫和表的建立等常用操作

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;