MySQL武林秘籍,学废必过考试
Posted IT邦德
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL武林秘籍,学废必过考试相关的知识,希望对你有一定的参考价值。
前言
最近有很多大学僧网友以苦于逃课没有学习mysql,到处看视频网站学习,没有体系化的只是体系,将曾经的Mysql实战案例分享给网友,相互学习,考试必过1.建表语句
注:讲以下建表create_db.sql,导入Mysql数据库
mysqldump -uroot -p >d:/create_db.sql
-- MySQL dump 10.13 Distrib 5.7.21, for Win64 (x86_64)
--
-- Host: localhost Database: db_school
-- ------------------------------------------------------
-- Server version 5.7.21-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Current Database: `db_school`
--
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `db_school` /*!40100 DEFAULT CHARACTER SET gb2312 */;
USE `db_school`;
--
-- Table structure for table `tb_class`
--
DROP TABLE IF EXISTS `tb_class`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tb_class` (
`classNo` char(6) NOT NULL,
`className` varchar(20) NOT NULL,
`department` varchar(20) DEFAULT NULL,
`grade` enum('1','2','3','4') DEFAULT NULL,
`classNum` tinyint(4) DEFAULT NULL,
PRIMARY KEY (`classNo`),
UNIQUE KEY `uq_class` (`className`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `tb_class`
--
LOCK TABLES `tb_class` WRITE;
/*!40000 ALTER TABLE `tb_class` DISABLE KEYS */;
INSERT INTO `tb_class` VALUES ('AC1301','会计13-1班','会计学院','1',35),('AC1302','会计13-2班','会计学院','1',35),('CS1401','计算机14-1班','计算机学院','2',35),('IS1301','信息系统13-1班','信息学院','1',NULL),('IS1401','信息系统14-1班','信息学院',NULL,30);
/*!40000 ALTER TABLE `tb_class` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `tb_course`
--
DROP TABLE IF EXISTS `tb_course`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tb_course` (
`courseNo` char(6) NOT NULL,
`courseName` varchar(20) NOT NULL,
`credit` decimal(3,1) NOT NULL,
`courseHour` tinyint(2) NOT NULL,
`term` tinyint(1) DEFAULT NULL,
`priorCourse` char(6) DEFAULT NULL,
PRIMARY KEY (`courseNo`),
UNIQUE KEY `courseName` (`courseName`),
UNIQUE KEY `uqidx_courseName` (`courseName`(3)),
KEY `fk_course` (`priorCourse`),
CONSTRAINT `fk_course` FOREIGN KEY (`priorCourse`) REFERENCES `tb_course` (`courseNo`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `tb_course`
--
LOCK TABLES `tb_course` WRITE;
/*!40000 ALTER TABLE `tb_course` DISABLE KEYS */;
INSERT INTO `tb_course` VALUES ('11003','管理学',2.0,32,2,NULL),('11005','会计学',3.0,48,3,NULL),('21001','计算机基础',3.0,48,1,NULL),('21002','OFFICE高级应用',3.0,48,2,'21001'),('21004','程序设计',4.0,64,2,'21001'),('21005','数据库',4.0,64,5,'21004'),('21006','操作系统',4.0,64,5,'21001'),('31001','管理信息系统',3.0,48,3,'21004'),('31002','信息系统-分析与设计',2.0,32,4,'31001'),('31005','项目管理',3.0,48,5,'31001');
/*!40000 ALTER TABLE `tb_course` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `tb_score`
--
DROP TABLE IF EXISTS `tb_score`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tb_score` (
`studentNo` char(10) NOT NULL,
`courseNo` char(6) NOT NULL,
`score` decimal(4,1) NOT NULL,
PRIMARY KEY (`studentNo`,`courseNo`),
KEY `idx_stuNo_courNo` (`courseNo`,`studentNo`),
KEY `idx_courseNo` (`courseNo`),
CONSTRAINT `fk_score_courNo` FOREIGN KEY (`courseNo`) REFERENCES `tb_course` (`courseNo`),
CONSTRAINT `fk_score_stuNo` FOREIGN KEY (`studentNo`) REFERENCES `tb_student` (`studentNo`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `tb_score`
--
LOCK TABLES `tb_score` WRITE;
/*!40000 ALTER TABLE `tb_score` DISABLE KEYS */;
INSERT INTO `tb_score` VALUES ('2013110101','11003',90.0),('2013110101','21001',86.0),('2013110103','11003',89.0),('2013110103','21001',88.0),('2013110201','11003',78.0),('2013110201','21001',92.0),('2013110202','11003',82.0),('2013110202','21001',85.0),('2013310101','21004',83.0),('2013310101','31002',68.0),('2013310103','21004',80.0),('2013310103','31002',76.0),('2014210101','11003',80.0),('2014210101','11005',75.0),('2014210101','21001',60.0),('2014210101','21002',93.0),('2014210101','21004',89.0),('2014210101','21005',55.0),('2014210101','21006',80.0),('2014210101','31001',68.0),('2014210101','31002',77.0),('2014210101','31005',85.0),('2014210102','21002',95.0),('2014210102','21004',88.0),('2014310101','21001',79.0),('2014310101','21004',80.0),('2014310102','21001',91.0),('2014310102','21004',87.0);
/*!40000 ALTER TABLE `tb_score` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `tb_student`
--
DROP TABLE IF EXISTS `tb_student`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `tb_student` (
`studentNo` char(10) NOT NULL,
`studentName` varchar(10) NOT NULL,
`sex` char(2) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`native` varchar(20) DEFAULT NULL,
`nation` varchar(20) DEFAULT '汉',
`classNo` char(6) DEFAULT NULL,
PRIMARY KEY (`studentNo`),
KEY `fk_student` (`classNo`),
CONSTRAINT `fk_student` FOREIGN KEY (`classNo`) REFERENCES `tb_class` (`classNo`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `tb_student`
--
LOCK TABLES `tb_student` WRITE;
/*!40000 ALTER TABLE `tb_student` DISABLE KEYS */;
INSERT INTO `tb_student` VALUES ('2013110101','张晓勇','男','1997-12-11','山西','汉','AC1301'),('2013110103','王一敏','女','1996-03-25','河北','汉','AC1301'),('2013110201','江山','女','1996-09-17','内蒙','锡伯','AC1302'),('2013110202','李明','男','1996-01-14','广西','壮','AC1302'),('2013310101','黄菊','女','1995-09-30','北京','汉','IS1301'),('2013310102','林海','男','1996-01-18','北京','满','IS1301'),('2013310103','吴昊','男','1995-11-18','河北','汉','IS1301'),('2014210101','黄涛','男','1997-04-03','湖南','侗','CS1401'),('2014210102','郭志坚','男','1997-02-21','上海','汉','CS1401'),('2014210103','王玲','女','1998-02-21','安徽','汉','CS1401'),('2014310101','王林','男','1996-10-09','河南','汉','IS1401'),('2014310102','李怡然','女','1996-12-31','辽宁','汉','IS1401'),('2015310103','李彤','男',NULL,NULL,'傣','IS1401');
/*!40000 ALTER TABLE `tb_student` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2018-11-15 23:42:32
2.字符集设置
2.1 在MySQL中创建一个名为db_school的数据库,要求设置默认字符集为GB2312,
字符集的校对规则为gb2312_chinese_ci。
show collation
show collation where `Charset`='GB2312' and `Collation` like '%ci';
CREATE DATABASE db_school CHARACTER SET GB2312 COLLATE gb2312_chinese_ci;
show databases;
--查看当前数据库字符集
show VARIABLES like 'character%';
--设置数据库字符集
alter database db_school character set GBK;
2.2 查看当前用户可查看的数据库列表
3. 修改已有数据库db_school的默认字符集和校对规则分别为GBK, GBK_chinese_ci
--同时设置字符集合校对规则
alter database db_school DEFAULT CHARACTER SET GBK COLLATE gbk_chinese_ci;
show collation where `Charset`='GBK' and `Collation` like '%ci';
3.表的创建、修改、删除
##创建表
create table if not exists tb_student (
studentNo CHAR(10) not NULL primary key comment '学号',
studentName VARCHAR(10) NOT null comment '姓名',
sex CHAR(2) comment '性别',
birthday date comment '出生日期',
native VARCHAR(20) comment '籍贯',
nation VARCHAR(10) DEFAULT '汉' comment '民族',
classNo CHAR(6) comment '所属班级'
) ENGINE=InnoDB comment '学生表';
desc tb_student;
show columns from tb_student;
3.1 用命令show tables查看当前数据库中的所有表
3.2 .将tb_student的表结构复制到tb_student2,并向tb_student2中添加一个INT型字段id,
要求其不能为NULL,取值唯一且自动增加,并将该字段添加到表的第一个字段
create table tb_student2 select * from tb_student where 1=3;
alter table tb_student2 add id int
Alter table tb_student2 add primary key(id); -先创建主键
ALTER TABLE tb_student2 MODIFY id int not null AUTO_INCREMENT; --自增
3.3 .向tb_student表中添加一个varchar(16)类型的字段department,用于描述学生所在院系,要求设置其默认值为“城市学院”,并将该字段添加到原表nation之后
alter table tb_student add department varchar(16)
DEFAULT '城市学院' comment '院系' after nation;
3.4.将tb_student中的字段birthday重命名为age,并将其数据类型更改为TINYINT,
允许其为NULL,默认值为18。
用DESC 查看tb_student
alter table tb_student change birthday age TINYINT DEFAULT 18; --给字段重命名
3.5.将tb_student表中的字段department的默认值删除
ALTER TABLE tb_student ALTER COLUMN department DROP DEFAULT;
3.6.将tb_student表中的字段department的默认值改为’环化学院’
ALTER TABLE tb_student ALTER COLUMN department SET DEFAULT '环化学院';
3.7.将tb_student表中的字段department的数据类型更改为varchar(20),
取值不允许为空,并将此字段移至字段studentName之后。
用desc tb_student命令查看结果
ALTER TABLE tb_student MODIFY department varchar(20) not null after studentName;
3.8 .删除数据表tb_student2中的字段id
ALTER TABLE tb_student2 MODIFY id int not null; //删除自增长
Alter table tb_student2 drop primary key;//删除主建
ALTER TABLE tb_student2 DROP id;
3.9.使用RENAME [TO]子句将数据库db_school中的数据表tb_student2
重新命名为backup_tb_student
alter table tb_student2 rename to backup_tb_student;
3.10.使用RENAME TABLE语句将数据库db_school中的表
backup_tb_student再重新命名为tb_student2
RENAME TABLE backup_tb_student TO tb_student2,new_table TO old_table;
drop table tb_student,tb_student2;
4.数据完整性约束
4.1 重新按照表1创建tb_student数据表,要求以表级完整性约束方式定义主键,
并指定主键约束名为pk_student。
CREATE TABLE tb_student (
studentNo CHAR(10) NOT NULL,
studentName VARCHAR(10) NOT NULL,
sex CHAR(2),
birthday DATE,
native VARCHAR(20),
nation VARCHAR(20) default '汉',
classNo CHAR(6),
constraint pk_student primary key(studentNo)
) engine=InnoDB default charset=gb2312;
4.2 在数据库db_school中,按照表2的结构创建tb_class。要求:使用InnoDB存储引擎,gb2312字符集,主键约束为列级完整性约束,唯一约束为表级完整性约束其约束名为uq_class
CREATE TABLE tb_class (
classNo CHAR(6) PRIMARY KEY NOT NULL,
className VARCHAR(20) NOT NULL,
department VARCHAR(20),
grade ENUM('1','2','3','4'),
classNum TINYINT,
constraint uq_class unique(className)
) engine=InnoDB default charset=gb2312;
4.3 首先删除数据表tb_student,按照表1重新创建tb_student,在创建的同时建立tb_student到tb_class的外键约束(两个表相同含义的属性是classNo,因此classNo是tb_student的外键),约束名为fk_student,并定义相应的参照动作,更新操作为级联(cascade),删除操作为限制(restrict),数据表引擎为InnoDB,字符集为gb2312
drop table tb_student;
CREATE TABLE tb_student (
studentNo CHAR(10) NOT NULL,
studentName VARCHAR(10) NOT NULL,
sex CHAR(2),
birthday DATE,
native VARCHAR(20),
nation VARCHAR(20) default '汉',
classNo CHAR(6),
constraint fk_student FOREIGN KEY (classNo)
references tb_class(classNo) on delete restrict on update cascade
) engine=InnoDB default charset=gb2312;
4.4 在数据库db_school中按照表3创建tb_course表,
要求:外键名字为fk_course,引擎为InnoDB,默认字符集为gb2312。
CREATE TABLE tb_course (
courseNo CHAR(6) NOT NULL primary key comment '课程号',
courseName VARCHAR(20) unique not NULL comment '课程名',
credit DECIMAL(3,1) not NULL comment '学分',
courseHour TINYINT(2) not NULL comment '课时数',
term TINYINT(2) comment '开课学期',
priorCourse CHAR(6) comment '先修课程',
constraint fk_course FOREIGN KEY(priorCourse) REFERENCES tb_course(courseNo)
) engine=InnoDB default charset=gb2312;
4.5 在数据库db_school中定义数据表tb_score,
表结构如表4所示, 引擎为InnoDB,默认字符集为gb2312。
CREATE TABLE tb_score(
CREATE TABLE tb_score(
studentNo CHAR(10) NOT NULL comment '学号',
courseNo CHAR(6) NOT NULL comment '课程号',
credit DECIMAL(4,1) not NULL comment '成绩',
constraint fk_score_stuNo FOREIGN KEY(studentNo) REFERENCES tb_student(studentNo),
constraint fk_score_courNo FOREIGN KEY(courseNo) REFERENCES tb_course(courseNo),
constraint pk_score PRIMARY KEY(studentNo,courseNo)
) engine=InnoDB default charset=gb2312;
注:外键约束对应的主键(在表里是主键才可以)
5.更新完整性约束条件
5.1 删除在表tb_score中定义的外键约束fk_score_stuNo
alter table tb_score drop foreign key fk_score_stuNo;
5.2 .删除在表tb_student中定义的主键约束。
Alter table tb_student drop primary key;
5.3 添加主键约束,用alter table语句在tb_student对studentNo重新添加主键。
Alter table tb_student add primary key(studentNo);
5.4.添加外键约束,用alter table语句在tb_score表对studentNo重新添加外键,
对应的主键为tb_student表的studentNo,外键名称为fk_score_stuNo。
ALTER TABLE tb_score ADD CONSTRAINT
fk_score_stuNo FOREIGN KEY(studentNo) REFERENCES tb_student(studentNo);
6.索引的创建
6.1.创建新表的同时创建普通索引:要求按照实验一第5题表1的结构创建tb_student1表,
要求在创建的同时在studentName字段上建立普通索引,索引名为idx_studentName。
create table if not exists tb_student1 (
studentNo CHAR(10) not NULL primary key comment 以上是关于MySQL武林秘籍,学废必过考试的主要内容,如果未能解决你的问题,请参考以下文章