1.前言

回顾完pandas,紧接着也罢把mysql也回顾回顾吧。

2.mysql启动

打开cmd,输入:net start mysql80,其中80是之前你安装mysql时设置的服务器的名称,
如果报如下的错,就说明时cmd的权限不够,要打开cmd所在的目录,更改为管理员模式,并将更改后的cmd文件复制到c:/windows下,并重命名为cmdd,以后运行cmd就通过win+r然后输入cmdd就OK了,如果你用原来的cmd还是会报错哦。

启动成功为:

附带一句:这个mysql启动可以通过如下的按钮直接完成:

停止服务器:

net stop mysql80

停止与登录服务器账户密码后再退出的效果一样:

exit;

3.登录服务器

4.开始正式探索sql语句

对下面基本的语句不做过多的解释,只对比较难的做解释。

(1)show databases;

(2)use 某个数据库;

(3)show tables;

(4)create table student(name varchar(10));

其中的10表示的是最多10个汉字哦,再mysql中,一个汉字占用3个字节。

(5)desc student;或者describe student;

显示student表的配置结构。

(6)select * from student;

*表示没有具体的查询对象,返回所有的内容。

(7)insert into student values('张三');

(8)update student set name='王四';

(9)delete from student where name='王四';

(10)drop table student;

(11)create table student(name varchar(10), id int); alter table student add primary key(id);desc student;

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(10) | YES  |     | NULL    |       |
| id    | int         | NO   | PRI | NULL    |       |
+-------+-------------+------+-----+---------+-------+

还可以通过下面的操作达到同样的效果:

alter table student modify id int primary key;

有一点需要注意的是:主键设置了并不会在表中有所显示,除非用desc。按理说应该是会放在最前面的一列才更直观。

删除所有的primary key:

alter table student drop primary key;

(12)create table student(name varchar(10), unique(name));

创建唯一的primary key。

(13)alter table student drop index name;

删除唯一的primary key。

(14)create table student(name varchar(10) not null);

非空约束。

(15)alter table student modify name varchar(10);

删除非空约束, 通过modify间接去掉了原来的约束。

(16)create table student(name varchar(10) default '张三', id int);

默认约束,但这个默认值有用吗?好像有没有都没啥影响吧。

(17)alter table student modify name varchar(10);

删除默认约束。

(18)create table class(name varchar(10), id int primary key);create table student(name varchar(10), id int primary key, class_id int, foreign key(class_id) references class(id));

主表是:class
副表是:student
当主表有数据被副表利用时,主表不能被删除
主表中没有的数值,在副表中不能使用

(19)1NF(范式)

(20)2NF

(21)3NF

(2)3NF

5.实际项目

这个项目目的在于复习。

(1) 建表

# 创建数据库data1
CREATE DATABASE data1;# 使用数据库data1
USE data1;# 创建学生表
CREATE TABLE student (num VARCHAR(20) PRIMARY KEY,name VARCHAR(20) NOT NULL,gender VARCHAR(10) NOT NULL,birthday DATE,class VARCHAR(20)
);# 创建教师表
CREATE TABLE teacher (num VARCHAR(20) PRIMARY KEY,name VARCHAR(20) NOT NULL,gender VARCHAR(10) NOT NULL,birthday DATE,profession VARCHAR(20) NOT NULL,department VARCHAR(20) NOT NULL
);# 创建课程表
CREATE TABLE course (num VARCHAR(20) PRIMARY KEY,name VARCHAR(20) NOT NULL,t_num VARCHAR(20) NOT NULL,FOREIGN KEY(t_num) REFERENCES teacher(num)
);# 成绩表
CREATE TABLE score (s_num VARCHAR(20) NOT NULL,c_num VARCHAR(20) NOT NULL, score DECIMAL,FOREIGN KEY(s_num) REFERENCES student(num),    FOREIGN KEY(c_num) REFERENCES course(num),PRIMARY KEY(s_num, c_num)  # 这里设置联合primary key的原因是学生的num和课程的num只能有一个是相同的,因为同一个学生不会选两个一样的课
);# 查看所有表
SHOW TABLES;# 添加学生表数据
INSERT INTO student VALUES('101', '曾华', '男', '1977-09-01', '95033');
INSERT INTO student VALUES('102', '匡明', '男', '1975-10-02', '95031');
INSERT INTO student VALUES('103', '王丽', '女', '1976-01-23', '95033');
INSERT INTO student VALUES('104', '李军', '男', '1976-02-20', '95033');
INSERT INTO student VALUES('105', '王芳', '女', '1975-02-10', '95031');# 添加教师表数据
INSERT INTO teacher VALUES('804', '李诚', '男', '1958-12-02', '副教授', '计算机系');
INSERT INTO teacher VALUES('856', '张旭', '男', '1969-03-12', '讲师', '电子工程系');
INSERT INTO teacher VALUES('825', '王萍', '女', '1972-05-05', '助教', '计算机系');# 添加课程表数据
INSERT INTO course VALUES('3-105', '计算机导论', '825');
INSERT INTO course VALUES('3-245', '操作系统', '804');
INSERT INTO course VALUES('6-166', '数字电路', '856');# 添加添加成绩表数据
INSERT INTO score VALUES('103', '3-105', '92');
INSERT INTO score VALUES('103', '3-245', '86');
INSERT INTO score VALUES('103', '6-166', '85');
INSERT INTO score VALUES('105', '3-105', '88');
INSERT INTO score VALUES('105', '3-245', '75');
INSERT INTO score VALUES('105', '6-166', '79');# 查看表结构
SELECT * FROM course;
SELECT * FROM score;
SELECT * FROM student;
SELECT * FROM teacher;

结果:

mysql> SELECT * FROM course;
+-------+------------+-------+
| num   | name       | t_num |
+-------+------------+-------+
| 3-105 | 计算机导论 | 825   |
| 3-245 | 操作系统   | 804   |
| 6-166 | 数字电路   | 856   |
+-------+------------+-------+mysql> SELECT * FROM score;
+-------+-------+-------+
| s_num | c_num | score |
+-------+-------+-------+
| 103   | 3-105 |    92 |
| 103   | 3-245 |    86 |
| 103   | 6-166 |    85 |
| 105   | 3-105 |    88 |
| 105   | 3-245 |    75 |
| 105   | 6-166 |    79 |
+-------+-------+-------+mysql> SELECT * FROM student;
+-----+------+--------+------------+-------+
| num | name | gender | birthday   | class |
+-----+------+--------+------------+-------+
| 101 | 曾华 | 男     | 1977-09-01 | 95033 |
| 102 | 匡明 | 男     | 1975-10-02 | 95031 |
| 103 | 王丽 | 女     | 1976-01-23 | 95033 |
| 104 | 李军 | 男     | 1976-02-20 | 95033 |
| 105 | 王芳 | 女     | 1975-02-10 | 95031 |
+-----+------+--------+------------+-------+mysql> SELECT * FROM teacher;
+-----+------+--------+------------+------------+------------+
| num | name | gender | birthday   | profession | department |
+-----+------+--------+------------+------------+------------+
| 804 | 李诚 | 男     | 1958-12-02 | 副教授     | 计算机系   |
| 825 | 王萍 | 女     | 1972-05-05 | 助教       | 计算机系   |
| 856 | 张旭 | 男     | 1969-03-12 | 讲师       | 电子工程系 |
+-----+------+--------+------------+------------+------------+

(2) 查表操作

mysql> select * from student;
+-----+------+--------+------------+-------+
| num | name | gender | birthday   | class |
+-----+------+--------+------------+-------+
| 101 | 曾华 | 男     | 1977-09-01 | 95033 |
| 102 | 匡明 | 男     | 1975-10-02 | 95031 |
| 103 | 王丽 | 女     | 1976-01-23 | 95033 |
| 104 | 李军 | 男     | 1976-02-20 | 95033 |
| 105 | 王芳 | 女     | 1975-02-10 | 95031 |
+-----+------+--------+------------+-------+# 选取某些字段的行,注意了字段的顺序会决定最后表的呈现顺序
mysql> select name, gender, class from student;
+------+--------+-------+
| name | gender | class |
+------+--------+-------+
| 曾华 | 男     | 95033 |
| 匡明 | 男     | 95031 |
| 王丽 | 女     | 95033 |
| 李军 | 男     | 95033 |
| 王芳 | 女     | 95031 |
+------+--------+-------+mysql> select distinct department from teacher;
+------------+
| department |
+------------+
| 计算机系   |
| 电子工程系 |
+------------+mysql> select * from score where score between 60 and 80;
+-------+-------+-------+
| s_num | c_num | score |
+-------+-------+-------+
| 105   | 3-245 |    75 |
| 105   | 6-166 |    79 |
+-------+-------+-------+# 注意要加括号哦
mysql> select * from score where score in (80, 86, 88);
+-------+-------+-------+
| s_num | c_num | score |
+-------+-------+-------+
| 103   | 3-245 |    86 |
| 105   | 3-105 |    88 |
+-------+-------+-------+mysql> select * from student order by class desc;
+-----+------+--------+------------+-------+
| num | name | gender | birthday   | class |
+-----+------+--------+------------+-------+
| 101 | 曾华 | 男     | 1977-09-01 | 95033 |
| 103 | 王丽 | 女     | 1976-01-23 | 95033 |
| 104 | 李军 | 男     | 1976-02-20 | 95033 |
| 102 | 匡明 | 男     | 1975-10-02 | 95031 |
| 105 | 王芳 | 女     | 1975-02-10 | 95031 |
+-----+------+--------+------------+-------+mysql> select * from score order by c_num asc, score desc;
+-------+-------+-------+
| s_num | c_num | score |
+-------+-------+-------+
| 103   | 3-105 |    92 |
| 105   | 3-105 |    88 |
| 103   | 3-245 |    86 |
| 105   | 3-245 |    75 |
| 103   | 6-166 |    85 |
| 105   | 6-166 |    79 |
+-------+-------+-------+mysql> select count(*) from student where class='95031';
+----------+
| count(*) |
+----------+
|        2 |
+----------+mysql> select s_num, c_num from score where score=(select max(score) from score);
+-------+-------+
| s_num | c_num |
+-------+-------+
| 103   | 3-105 |
+-------+-------+或者:mysql> select s_num, c_num from score order by score desc limit 0,1;
+-------+-------+
| s_num | c_num |
+-------+-------+
| 103   | 3-105 |
+-------+-------+mysql> select c_num, avg(score) from score group by c_num;
+-------+------------+
| c_num | avg(score) |
+-------+------------+
| 3-105 |    90.0000 |
| 3-245 |    80.5000 |
| 6-166 |    82.0000 |
+-------+------------+mysql> select c_num, avg(score), count(*) from score group by c_num having count(c_num)>=2 and c_num like '3%';
+-------+------------+----------+
| c_num | avg(score) | count(*) |
+-------+------------+----------+
| 3-105 |    90.0000 |        2 |
| 3-245 |    80.5000 |        2 |
+-------+------------+----------+mysql> select name, c_num, score from student, score where student.num=score.s_num;
+------+-------+-------+
| name | c_num | score |
+------+-------+-------+
| 王丽 | 3-105 |    92 |
| 王丽 | 3-245 |    86 |
| 王丽 | 6-166 |    85 |
| 王芳 | 3-105 |    88 |
| 王芳 | 3-245 |    75 |
| 王芳 | 6-166 |    79 |
+------+-------+-------+mysql> select c_num, avg(score) from score where s_num in (select num from student where class = '95031')-> group by c_num;
+-------+------------+
| c_num | avg(score) |
+-------+------------+
| 3-105 |    88.0000 |
| 3-245 |    75.0000 |
| 6-166 |    79.0000 |
+-------+------------+mysql> select c_num, avg(score), count(*) from score group by c_num having count(c_num)>=2 and c_num like '3%';
+-------+------------+----------+
| c_num | avg(score) | count(*) |
+-------+------------+----------+
| 3-105 |    90.0000 |        2 |
| 3-245 |    80.5000 |        2 |
+-------+------------+----------+mysql> select name, c_num, score from student, score where student.num=score.s_num;
+------+-------+-------+
| name | c_num | score |
+------+-------+-------+
| 王丽 | 3-105 |    92 |
| 王丽 | 3-245 |    86 |
| 王丽 | 6-166 |    85 |
| 王芳 | 3-105 |    88 |
| 王芳 | 3-245 |    75 |
| 王芳 | 6-166 |    79 |
+------+-------+-------+mysql> select num, name,birthday from student where year(birthday)-> in (select year(birthday) from student where num in  (101, 108));
+-----+------+------------+
| num | name | birthday   |
+-----+------+------------+
| 101 | 曾华 | 1977-09-01 |
+-----+------+------------+

--------------to be continued--------------------2020.5.20

mysql启动和常用语法实战回顾相关推荐

  1. MySQL的DML常用语法格式

    MySQL的DML常用语法格式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我们知道MySQL的查询大致分为单表查询,多表查询以及联合查询.多表查询,顾名思义,就是查询的结果可能 ...

  2. MySQL数据库(常用语法)(详细)

    一.数据库概述 1.为什么软件测试工程师还需要学习数据库以及开发方面的知识? 测试工程师的目的是找出软件的不足,并告诉开发工程师,出现问题的环境,操作步骤和输入输出数据:优秀的测试工程师,需要告诉开发 ...

  3. Mysql常用语法总结

    Mysql常用语法总结如下: #连接mysql数据库(Dos下面) mysql -u root -p 123 #创建数据库 create database myschool; #创建表 drop ta ...

  4. MySQL常用语法记录

    0.说明 记录MySQL使用到的常用语法. 1.MySQLDISTINCT唯一 2.MySQLLIMIT限制 3.MySQL ORDER排序升序 4.MySQL DESC降序 5.MySQL WHER ...

  5. Mysql运维常用命令回顾整理

    一.前景 Mysql作为开源数据库的中坚力量之一,虽然目前已被甲骨文收购,面临闭源风险,但是mysql扔是我们运维工作中最常面对的工作,那如何做好mysql运维工作,甚至做好mysql的基础运维,都是 ...

  6. MySQL视图 视图的作用、视图常用语法

    视图(View)是一种虚拟存在的表.视图中的数据并不在数据库中实际存在,行和列数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的. 通俗的讲,视图只保存了查询的SQL逻辑,不保存查询结果. ...

  7. MySQL和POSTGRESQL的常用语法区别

    不一样的地方用红色标记了出来,供大家参考. 转载于: MySQL和PostgreSQL的常用语法差异

  8. MYSQL常用语法命令,MySQL中delete,drop和alter分别是用来删除什么的?有什么区别?删除了以后可以恢复吗?

    1.Alter.Delete.Drop相关语法 a.Alter 删除,添加或重新定义列 删除列:alter table tablename drop columnname 增加列:alter tabl ...

  9. 5.7版本mysql操做语法_mysql-5.7.25安装及常用语法

    我下的是免安装版的压缩文件包,可以选择下载.msi的程序包,那样就可以通过常见的图形界面来进行安装配置了 参考链接:https://blog.csdn.net/qq_23994787/article/ ...

最新文章

  1. 计算机视觉:Bag of words算法实现过程中出现错误及解决方案
  2. 使用Keras进行迁移学习
  3. firefox浏览器不能使用window.close的解决方案
  4. SpringBoot + Redis 解决海量重复提交问题
  5. Nginx流媒体支持配置
  6. 唐岩自述奋斗史:从娄底青年到陌陌上市
  7. 【深度好文】多线程之WaitHandle--派生EventWaitHandle事件构造-》AutoResetEvent、ManualResetEvent...
  8. [第一财经周刊] 疯狂的团购
  9. oracle sql语句 exists
  10. 37岁程序员失业投500份简历就3次面试猎头:超35岁不要
  11. css hack 笔记 for ie8,ie7
  12. kernel or user oops信息定位步骤
  13. 低频声音功率放大器电子设计报告
  14. 典型相关分析相关资料
  15. 航空机票预定系统软件结构图
  16. iOS 拓展,icon,尺寸 开发系列 吕文翰
  17. 阿里巴巴直播防控中的实人认证技术 1
  18. 郭麒麟任《最强大脑》见证官,住杭州的台湾人清华学霸吴哲维来了
  19. 解决Please make sure you have the correct access rights and the repository exists 问题
  20. 伦茨科技-智能语音遥控器

热门文章

  1. 操作系统 第二章 进程管理
  2. python建立FTP服务器
  3. 机器学习入门笔记(七):聚类
  4. P1181 数列分段Section I
  5. 【已解决】手机“此设备已安装证书授权中心,您的安全网络流量可能被监控”怎么办?
  6. maven webapp栽坑录
  7. 小技巧 ----- Java算法题标准模版
  8. leetcode907.SumofSubarrayMinimums
  9. 【CVTE Web后台开发实习生2019.12.05在线笔试】总结
  10. 6.输入四个数,找出最大值方法二